]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - builder.sh
- add %pear_package_print_optionalpackages
[packages/rpm-build-tools.git] / builder.sh
1 #!/bin/ksh
2 #
3 # This program is free software, distributed under the terms of
4 # the GNU General Public License Version 2.
5 #
6 # -----------
7 # Exit codes:
8 #         0 - succesful
9 #         1 - help displayed
10 #         2 - no spec file name in cmdl parameters
11 #         3 - spec file not stored in repo
12 #         4 - some source, patch or icon files not stored in repo
13 #         5 - package build failed
14 #         6 - spec file with errors
15 #         7 - wrong source in /etc/poldek.conf
16 #         8 - Failed installing buildrequirements and subrequirements
17 #         9 - Requested tag already exist
18 #        10 - Refused to build fractional release
19 #       100 - Unknown error (should not happen)
20
21 # Notes (todo/bugs):
22 # - when Icon: field is present, -5 and -a5 doesn't work
23 # - builder -R skips installing BR if spec is not present before builder invocation (need to run builder twice)
24 # - does not respect NoSource: X, and tries to cvs up such files [ example: VirtualBox-bin.spec and its Source0 ]
25 # TODO:
26 # - ability to do ./builder -bb foo.spec foo2.spec foo3.spec
27 # - funny bug, if source-md5 is set then builder will download from distfiles even if there is no url present:
28 #   Source10:   forwardfix.pl
29 #   # Source10-md5:     8bf85f7368933a4e0cb4f875bac28733
30 # - builder --help:
31 #       basename: missing operand
32 #       Try `basename --help' for more information.
33 #       -- and the normal usage info --
34
35 PROGRAM=${0##*/}
36 APPDIR=$(d=$0; [ -L "$d" ] && d=$(readlink -f "$d"); dirname "$d")
37 RCSID='$Id$' r=${RCSID#* * } rev=${r%% *}
38 VERSION="v0.35/$rev"
39 VERSIONSTRING="\
40 Build package utility from PLD Linux CVS repository
41 $VERSION (C) 1999-2010 Free Penguins".
42
43 PATH="/bin:/usr/bin:/usr/sbin:/sbin:/usr/X11R6/bin"
44
45 # required rpm-build-macros
46 RPM_MACROS_VER=1.534
47
48 COMMAND="build"
49 TARGET=""
50
51 SPECFILE=""
52 BE_VERBOSE=""
53 QUIET=""
54 CLEAN=""
55 DEBUG=""
56 NOURLS=""
57 NOCVS=""
58 NOCVSSPEC=""
59 NODIST=""
60 NOINIT=""
61 PREFMIRRORS=""
62 UPDATE=""
63 ADD5=""
64 NO5=""
65 ALWAYS_CVSUP=${ALWAYS_CVSUP:-"yes"}
66 CVSROOT=""
67 GREEDSRC=""
68
69 # use rpm 4.4.6+ digest format instead of comments if non-zero
70 USEDIGEST=
71
72 # user agent when fetching files
73 USER_AGENT="PLD/Builder($VERSION)"
74
75 # It can be used i.e. in log file naming.
76 # See LOGFILE example.
77 DATE=`date +%Y-%m-%d_%H-%M-%S`
78
79 # Example: LOGFILE='../log.$PACKAGE_NAME'
80 # Example: LOGFILE='../LOGS/log.$PACKAGE_NAME.$DATE'
81 # Example: LOGFILE='$PACKAGE_NAME/$PACKAGE_NAME.$DATE.log'
82 # Example: LOGFILE='$PACKAGE_NAME.$DATE.log'
83 # Yes, you can use variable name! Note _single_ quotes!
84 LOGFILE=''
85
86 LOGDIR=""
87 LOGDIROK=""
88 LOGDIRFAIL=""
89 LASTLOG_FILE=""
90
91 CHMOD="no"
92 CHMOD_MODE="0644"
93 RPMOPTS=""
94 RPMBUILDOPTS=""
95 BCOND=""
96 GROUP_BCONDS="no"
97
98 # create symlinks for tools in PACKAGE_DIR, see get_spec()
99 SYMLINK_TOOLS="no"
100
101 PATCHES=""
102 SOURCES=""
103 ICONS=""
104 PACKAGE_RELEASE=""
105 PACKAGE_VERSION=""
106 PACKAGE_NAME=""
107 ASSUMED_NAME=""
108 PROTOCOL="ftp"
109 WGET_RETRIES=${MAX_WGET_RETRIES:-0}
110
111 CVS_COMMAND=${CVS_COMMAND:-cvs}
112 CVS_FORCE=""
113 CVSIGNORE_DF="yes"
114 CVS_RETRIES=${MAX_CVS_RETRIES:-1000}
115 CVS_SERVER="cvs.pld-linux.org"
116 CVSTAG=""
117
118 RES_FILE=""
119
120 DISTFILES_SERVER="://distfiles.pld-linux.org"
121 ATTICDISTFILES_SERVER="://attic-distfiles.pld-linux.org"
122
123 DEF_NICE_LEVEL=19
124 SCHEDTOOL="auto"
125
126 FAIL_IF_NO_SOURCES="yes"
127
128 # let get_files skip over files which are present to get those damn files fetched
129 SKIP_EXISTING_FILES="no"
130
131 TRY_UPGRADE=""
132 # should the specfile be restored if upgrade failed?
133 REVERT_BROKEN_UPGRADE="yes"
134
135 if rpm --specsrpm 2>/dev/null; then
136         FETCH_BUILD_REQUIRES_RPMSPECSRPM="yes"
137         FETCH_BUILD_REQUIRES_RPMGETDEPS="no"
138 else
139         FETCH_BUILD_REQUIRES_RPMSPECSRPM="no"
140         if [ -x /usr/bin/rpm-getdeps ]; then
141                 FETCH_BUILD_REQUIRES_RPMGETDEPS="yes"
142         else
143                 FETCH_BUILD_REQUIRES_RPMGETDEPS="no"
144         fi
145 fi
146
147
148 # Here we load saved user environment used to
149 # predefine options set above, or passed to builder
150 # in command line.
151 # This one reads global system environment settings:
152 if [ -f ~/etc/builderrc ]; then
153         . ~/etc/builderrc
154 fi
155 # And this one cascades settings using user personal
156 # builder settings.
157 # Example of ~/.builderrc:
158 #
159 #UPDATE_POLDEK_INDEXES="yes"
160 #FETCH_BUILD_REQUIRES="yes"
161 #REMOVE_BUILD_REQUIRES="force"
162 #GROUP_BCONDS="yes"
163 #LOGFILE='../LOGS/log.$PACKAGE_NAME.$DATE'
164 #TITLECHANGE=no
165 #
166 SU_SUDO=""
167 if [ -n "$HOME_ETC" ]; then
168         USER_CFG="$HOME_ETC/.builderrc"
169         BUILDER_MACROS="$HOME_ETC/.builder-rpmmacros"
170 else
171         USER_CFG=~/.builderrc
172         BUILDER_MACROS=~/.builder-rpmmacros
173 fi
174
175 [ -f "$USER_CFG" ] && . "$USER_CFG"
176
177 if [ "$SCHEDTOOL" = "auto" ]; then
178         if [ -x /usr/bin/schedtool ] && schedtool -B -e echo >/dev/null; then
179                 SCHEDTOOL="schedtool -B -e"
180         else
181                 SCHEDTOOL="no"
182         fi
183 fi
184
185 if [ -n "$USE_PROZILLA" ]; then
186         GETURI="proz --no-getch -r -P ./ -t$WGET_RETRIES $PROZILLA_OPTS"
187         GETURI2="$GETURI"
188         OUTFILEOPT="-O"
189 elif [ -n "$USE_AXEL" ]; then
190         GETURI="axel -a $AXEL_OPTS"
191         GETURI2="$GETURI"
192         OUTFILEOPT="-o"
193 else
194         wget --help 2>&1 | grep -q -- ' --no-check-certificate ' && WGET_OPTS="$WGET_OPTS --no-check-certificate"
195         wget --help 2>&1 | grep -q -- ' --inet ' && WGET_OPTS="$WGET_OPTS --inet"
196         wget --help 2>&1 | grep -q -- ' --retry-connrefused ' && WGET_OPTS="$WGET_OPTS --retry-connrefused"
197         WGET_OPTS="$WGET_OPTS --user-agent=$USER_AGENT"
198
199         GETURI="wget --passive-ftp -c -nd -t$WGET_RETRIES $WGET_OPTS"
200         GETURI2="wget -c -nd -t$WGET_RETRIES $WGET_OPTS"
201         OUTFILEOPT="-O"
202 fi
203
204 GETLOCAL="cp -a"
205
206 if (rpm --version 2>&1 | grep -q '4.0.[0-2]'); then
207         RPM="rpm"
208         RPMBUILD="rpm"
209 else
210         RPM="rpm"
211         RPMBUILD="rpmbuild"
212 fi
213
214 #
215 # sanity checks
216 #
217 if [ -d $HOME/rpm/SOURCES ]; then
218         echo "ERROR: ~/rpm/{SPECS,SOURCES} structure is obsolete" >&2
219         echo "ERROR: get rid of your ~/rpm/SOURCES" >&2
220         exit 1
221 fi
222
223 #
224 # are we using cvs-nserver ?
225 #
226 CVS_NSERVER=0
227 $CVS_COMMAND --version 2>&1 | grep -q 'CVS-nserver'
228 [ $? -eq 0 ] && CVS_NSERVER=1
229
230 POLDEK_INDEX_DIR="$($RPM --eval %_rpmdir)/"
231 POLDEK_CMD="$SU_SUDO /usr/bin/poldek --noask"
232
233 run_poldek() {
234         RES_FILE=$(mktemp -t builder.XXXXXX || ${TMPDIR:-/tmp}/builder.$RANDOM)
235         if [ -n "$LOGFILE" ]; then
236                 LOG=`eval echo $LOGFILE`
237                 if [ -n "$LASTLOG_FILE" ]; then
238                         echo "LASTLOG=$LOG" > $LASTLOG_FILE
239                 fi
240                 (${NICE_COMMAND} ${POLDEK_CMD} `while test $# -gt 0; do echo "$1 ";shift;done` ; echo $? > ${RES_FILE})|tee -a $LOG
241                 return $exit_pldk
242         else
243                 (${NICE_COMMAND} ${POLDEK_CMD} `while test $# -gt 0; do echo "$1 ";shift;done` ; echo $? > ${RES_FILE}) 1>&2 >/dev/null
244                 return `cat ${RES_FILE}`
245                 rm -rf ${RES_FILE}
246         fi
247 }
248
249 #---------------------------------------------
250 # functions
251
252 usage() {
253         if [ -n "$DEBUG" ]; then set -xv; fi
254         echo "\
255 Usage: builder [-D|--debug] [-V|--version] [--short-version] [-a|--as_anon] [-b|-ba|--build]
256 [-bb|--build-binary] [-bs|--build-source] [-bc] [-bi] [-bl] [-u|--try-upgrade]
257 [{-cf|--cvs-force}] [{-B|--branch} <branch>] [{-d|--cvsroot} <cvsroot>]
258 [-g|--get] [-h|--help] [--http] [{-l|--logtofile} <logfile>] [-m|--mr-proper]
259 [-q|--quiet] [--date <yyyy-mm-dd> [-r <cvstag>] [{-T|--tag <cvstag>]
260 [-Tvs|--tag-version-stable] [-Ts|--tag-stable] [-Tv|--tag-version]
261 [{-Tp|--tag-prefix} <prefix>] [{-tt|--test-tag}] [--use-greed-sources]
262 [-nu|--no-urls] [-v|--verbose] [--opts <rpm opts>] [--short-circuit]
263 [--show-bconds] [--with/--without <feature>] [--define <macro> <value>]
264 <package>[.spec][:cvstag]
265
266 -5, --update-md5    - update md5 comments in spec, implies -nd -ncs
267 -a5, --add-md5      - add md5 comments to URL sources, implies -nc -nd -ncs
268 -n5, --no-md5       - ignore md5 comments in spec
269 -D, --debug         - enable builder script debugging mode,
270 -debug              - produce rpm debug package (same as --opts -debug)
271 -V, --version       - output builder version string
272 --short-version     - output builder short version
273 -a, --as_anon       - get files via pserver as cvs@$CVS_SERVER,
274 -b, -ba, --build    - get all files from CVS repo or HTTP/FTP and build package
275                       from <package>.spec,
276 -bb, --build-binary - get all files from CVS repo or HTTP/FTP and build binary
277                       only package from <package>.spec,
278 -bp, --build-prep   - execute the %prep phase of <package>.spec,
279 -bc                 - execute the %build phase of <package>.spec,
280 -bi                 - execute the %install phase of <package>.spec
281 -bl                                     - execute the %files phase of <package>.spec
282 -bs, --build-source - get all files from CVS repo or HTTP/FTP and only pack
283                       them into src.rpm,
284 --short-circuit     - short-circuit build
285 -B, --branch        - add branch
286 -c, --clean         - clean all temporarily created files (in BUILD\$RPM_BUILD_ROOT) after rpmbuild commands.
287                       may be used with building process.
288 -m, --mr-proper     - clean all temporarily created files (in BUILD, SOURCES,
289                                           SPECS and \$RPM_BUILD_ROOT and CVS/Entries). Doesn't run
290                                           any rpm building.
291 -cf, --cvs-force        - use -F when tagging (useful when moving branches)
292 -d <cvsroot>, --cvsroot <cvsroot>
293                     - setup \$CVSROOT,
294 --define <macro> <value>
295                     - define a macro <macro> with value <value>,
296 --alt_kernel <kernel>
297                     - same as --define 'alt_kernel <kernel>'
298 --nodeps            - rpm won't check any dependences
299 -g, --get           - get <package>.spec and all related files from CVS repo
300                       or HTTP/FTP,
301 -h, --help          - this message,
302 --http              - use http instead of ftp,
303 -l <logfile>, --logtofile <logfile>
304                     - log all to file,
305 -nc, --no-cvs       - don't download sources from CVS, if source URL is given,
306 -ncs, --no-cvs-specs
307                     - don't check specs in CVS
308 -nd, --no-distfiles - don't download from distfiles
309 -nm, --no-mirrors   - don't download from mirror, if source URL is given,
310 -nu, --no-urls      - don't try to download from FTP/HTTP location,
311 -ns, --no-srcs      - don't download Sources/Patches
312 -ns0, --no-source0  - don't download Source0
313 -nn, --no-net       - don't download anything from the net
314 -pm, --prefer-mirrors - prefer mirrors (if any) over distfiles for SOURCES
315 --no-init           - don't initialize builder paths (SPECS and SOURCES)
316 -ske,
317 --skip-existing-files - skip existing files in get_files
318 --opts <rpm opts>   - additional options for rpm
319 -q, --quiet         - be quiet,
320 --date yyyy-mm-dd   - build package using resources from specified CVS date,
321 -r <cvstag>, --cvstag <cvstag>
322                     - build package using resources from specified CVS tag,
323 -A                  - build package using CVS resources as any sticky tags/date/kopts being reset.
324 -R, --fetch-build-requires
325                     - fetch what is BuildRequired,
326 -RB, --remove-build-requires
327                     - remove all you fetched with -R or --fetch-build-requires
328                       remember, this option requires confirmation,
329 -FRB, --force-remove-build-requires
330                     - remove all you fetched with -R or --fetch-build-requires
331                       remember, this option works without confirmation,
332 -sd, --source-distfiles - list sources available from distfiles (intended for offline
333                       operations; does not work when Icon field is present
334                       but icon file is absent),
335 -sc, --source-cvs - list sources available from CVS
336 -sdp, --source-distfiles-paths - list sources available from distfiles -
337                       paths relative to distfiles directory (intended for offline
338                       operations; does not work when Icon field is present
339                       but icon file is absent),
340 -sf, --source-files - list sources - bare filenames (intended for offline
341                       operations; does not work when Icon field is present
342                       but icon file is absent),
343 -sp, --source-paths - list sources - filenames with full local paths (intended for
344                       offline operations; does not work when Icon field is present
345                       but icon file is absent),
346 -su, --source-urls  - list urls - urls to sources and patches
347                       intended for copying urls with spec with lots of macros in urls
348 -T <cvstag> , --tag <cvstag>
349                     - add cvs tag <cvstag> for files,
350 -Tvs, --tag-version-stable
351                     - add cvs tags STABLE and NAME-VERSION-RELEASE for files,
352 -Ts, --tag-stable
353                     - add cvs tag STABLE for files,
354 -Tv, --tag-version
355                     - add cvs tag NAME-VERSION-RELEASE for files,
356 -Tp, --tag-prefix <prefix>
357                     - add <prefix> to NAME-VERSION-RELEASE tags,
358 -tt, --test-tag <prefix>
359                     - fail if tag is already present,
360 -ir, --integer-release-only
361                     - allow only integer and snapshot releases
362 -v, --verbose       - be verbose,
363 -u, --try-upgrade   - check version, and try to upgrade package
364 -un, --try-upgrade-with-float-version
365                     - as above, but allow float version
366                                         php-pear-Services_Digg/
367 --upgrade-version   - upgrade to specified version in try-upgrade
368 --use-greed-sources
369                     - try download source from tag head if don't find it in
370                       current tag
371 -U, --update        - refetch sources, don't use distfiles, and update md5 comments
372 -Upi, --update-poldek-indexes
373                     - refresh or make poldek package index files.
374 -np, --nopatch <patchnumber>
375                     - don't apply <patchnumber>
376 --show-bconds       - show available conditional builds, which can be used
377                     - with --with and/or --without switches.
378 --show-bcond-args   - show active bconds, from ~/.bcondrc. this is used by
379                       ./repackage.sh script. in other words, the output is
380                       parseable by scripts.
381 --show-avail-bconds - show available bconds
382 --with/--without <feature>
383                     - conditional build package depending on %_with_<feature>/
384                       %_without_<feature> macro switch.  You may now use
385                       --with feat1 feat2 feat3 --without feat4 feat5 --with feat6
386                       constructions. Set GROUP_BCONDS to yes to make use of it.
387 --target <platform>, --target=<platform>
388                      - build for platform <platform>.
389 --init-rpm-dir       - initialize ~/rpm directory structure
390 "
391 }
392
393 # change dependency to specname
394 # common changes:
395 # - perl(Package::Name) -> perl-Package-Name
396 depspecname() {
397         local package="$1"
398
399         package=$(echo "$package" | sed -e '/perl(.*)/{s,perl(\(.*\)),perl-\1,;s,::,-,g};' -e 's/-\(devel\|static\)$//' )
400         echo "$package"
401 }
402
403 update_shell_title() {
404         [ -t 1 ] || return
405         local len=${COLUMNS:-80}
406         local msg="$(echo "$*" | cut -c-$len)"
407
408         if [ -n "$BE_VERBOSE" ]; then
409                 echo >&2 "$(date +%s.%N) $*"
410         fi
411
412         if [ "x$TITLECHANGE" = "xyes" -o "x$TITLECHANGE" = "x" ]; then
413                 local pkg
414                 if [ -n "$PACKAGE_NAME" ]; then
415                         pkg=${PACKAGE_NAME}-${PACKAGE_VERSION}-${PACKAGE_RELEASE}
416                 else
417                         pkg=${SPECFILE}
418                 fi
419
420                 msg="$pkg: ${SHELL_TITLE_PREFIX:+$SHELL_TITLE_PREFIX }$msg"
421                 msg=$(echo $msg | tr -d '\n\r')
422                 case "$TERM" in
423                         cygwin|xterm*)
424                         echo >&2 -ne "\033]1;$msg\007\033]2;$msg\007"
425                 ;;
426                         screen*)
427                         echo >&2 -ne "\033]0;$msg\007"
428                 ;;
429                 esac
430         fi
431 }
432
433 # set TARGET from BuildArch: from SPECFILE
434 set_spec_target() {
435         if [ -n "$SPECFILE" ] && [ -z "$TARGET" ]; then
436                 tmp=$(awk '/^BuildArch:/ { print $NF; exit }' $ASSUMED_NAME/$SPECFILE)
437                 if [ "$tmp" ]; then
438                                 target_platform=$(rpm -E '%{_target_vendor}-%{_target_os}%{?_gnu}')
439                                 TARGET="$tmp"
440                                 case "$RPMBUILD" in
441                                 "rpmbuild")
442                                         TARGET_SWITCH="--target ${TARGET}-${target_platform}" ;;
443                                 "rpm")
444                                         TARGET_SWITCH="--target=$TARGET" ;;
445                                 esac
446                 fi
447         fi
448 }
449
450 # runs rpm with minimal macroset
451 minirpm() {
452         # we reset macros not to contain macros.build as all the %() macros are
453         # executed here, while none of them are actually needed.
454         # at the time of this writing macros.build + macros contained 70 "%(...)" macros.
455         safe_macrofiles=$(rpm $TARGET_SWITCH --showrc | awk -F: '/^macrofiles/ { gsub(/^macrofiles[ \t]+:/, "", $0); gsub(/:.*macros.build:/, ":", $0); print $0 } ')
456
457         # TODO: move these to /usr/lib/rpm/macros
458         cat > $BUILDER_MACROS <<'EOF'
459 %x8664 x86_64 amd64 ia32e
460 %alt_kernel %{nil}
461 %_alt_kernel %{nil}
462 %bootstrap_release() %{1}
463 %requires_releq_kernel_up(s:n:) %{nil}
464 %requires_releq_kernel_smp(s:n:) %{nil}
465 %requires_releq_kernel(s:n:) %{nil}
466 %requires_releq() %{nil}
467 %pyrequires_eq() %{nil}
468 %requires_eq() %{nil}
469 %requires_eq_to() %{nil}
470 %requires_ge() %{nil}
471 %requires_ge_to() %{nil}
472 %requires_ge() %{nil}
473 %releq_kernel_up(n:) ERROR
474 %releq_kernel_smp(n:) ERROR
475 %releq_kernel(n:) ERROR
476 %kgcc_package ERROR
477 %_fontsdir ERROR
478 %ruby_version ERROR
479 %ruby_ver_requires_eq() %{nil}
480 %ruby_mod_ver_requires_eq() %{nil}
481 %__php_api_requires() %{nil}
482 %php_major_version ERROR
483 %php_api_version ERROR
484 %requires_xorg_xserver_extension %{nil}
485 %requires_xorg_xserver_xinput %{nil}
486 %requires_xorg_xserver_font %{nil}
487 %requires_xorg_xserver_videodrv %{nil}
488 %py_ver ERROR
489 %perl_vendorarch ERROR
490 %perl_vendorlib ERROR
491 # damn. need it here! - copied from /usr/lib/rpm/macros.build
492 %tmpdir         %(echo "${TMPDIR:-/tmp}")
493 %patchset_source(f:b:) %(
494         base=%{-b*}%{!-b*:10000};
495         start=$(expr $base + %1);
496         end=$(expr $base + %{?2}%{!?2:%{1}});
497         # we need to call seq twice as it doesn't allow two formats
498         seq -f 'Patch%g:' $start $end > %{tmpdir}/__ps1;
499         seq -f '%{-f*}' %1 %{?2}%{!?2:%{1}} > %{tmpdir}/__ps2;
500         paste %{tmpdir}/__ps{1,2};
501         rm -f %{tmpdir}/__ps{1,2};
502 ) \
503 %{nil}
504 %add_etc_shells(p) %{p:<lua>}
505 %remove_etc_shells(p) %{p:<lua>}
506 %lua_add_etc_shells()  %{nil}
507 %lua_remove_etc_shells() %{nil}
508 %required_jdk %{nil}
509 %buildrequires_jdk %{nil}
510 %pear_package_print_optionalpackages %{nil}
511 EOF
512         if [ "$NOINIT" = "yes" ] ; then
513                 cat >> $BUILDER_MACROS <<'EOF'
514 %_specdir ./
515 %_sourcedir ./
516 EOF
517         fi
518         eval $RPMBUILD $TARGET_SWITCH --macros "$safe_macrofiles:$BUILDER_MACROS" $QUIET $RPMOPTS $RPMBUILDOPTS $BCOND $* 2>&1
519 }
520
521 cache_rpm_dump() {
522         if [ -n "$DEBUG" ]; then
523                 set -x
524                 set -v
525         fi
526
527         if [ -x /usr/bin/rpm-specdump ]; then
528                 update_shell_title "cache_rpm_dump using rpm-specdump command"
529                 rpm_dump_cache=$(rpm-specdump $TARGET_SWITCH $BCOND $SPECFILE)
530         else
531                 update_shell_title "cache_rpm_dump using rpmbuild command"
532                 local rpm_dump
533                 rpm_dump=`
534                         # what we need from dump is NAME, VERSION, RELEASE and PATCHES/SOURCES.
535                         dump='%{echo:dummy: PACKAGE_NAME %{name} }%dump'
536                         case "$RPMBUILD" in
537                         rpm)
538                                 ARGS='-bp'
539                                 ;;
540                         rpmbuild)
541                                 ARGS='--nodigest --nosignature --nobuild'
542                                 ;;
543                         esac
544                         minirpm $ARGS --define "'prep $dump'" --nodeps $SPECFILE
545                 `
546                 if [ $? -gt 0 ]; then
547                         error=$(echo "$rpm_dump" | sed -ne '/^error:/,$p')
548                         echo "$error" >&2
549                         Exit_error err_build_fail
550                 fi
551
552                 # make small dump cache
553                 rpm_dump_cache=`echo "$rpm_dump" | awk '
554                         $2 ~ /^SOURCEURL/ {print}
555                         $2 ~ /^PATCHURL/  {print}
556                         $2 ~ /^nosource/ {print}
557                         $2 ~ /^PACKAGE_/ {print}
558                 '`
559         fi
560
561         update_shell_title "cache_rpm_dump: OK!"
562 }
563
564 rpm_dump() {
565         if [ -z "$rpm_dump_cache" ] ; then
566                 echo >&2 "internal error: cache_rpm_dump not called! (missing %prep?)"
567         fi
568         echo "$rpm_dump_cache"
569 }
570
571 get_icons() {
572         update_shell_title "get icons"
573         ICONS=$(awk '/^Icon:/ {print $2}' $PACKAGE_DIR/${SPECFILE})
574         if [ -z "$ICONS" ]; then
575                 return
576         fi
577
578         rpm_dump_cache="kalasaba" NODIST="yes" get_files $ICONS
579 }
580
581 parse_spec() {
582         update_shell_title "parsing specfile"
583         if [ -n "$DEBUG" ]; then
584                 set -x
585                 set -v
586         fi
587
588         # icons are needed for successful spec parse
589         get_icons
590
591         cd $PACKAGE_DIR
592         cache_rpm_dump
593
594         if (rpm_dump | grep -qEi ":.*nosource.*1"); then
595                 FAIL_IF_NO_SOURCES="no"
596         fi
597
598         if [ "$NOSRCS" != "yes" ]; then
599                 SOURCES=$(rpm_dump | awk '$2 ~ /^SOURCEURL[0-9]+/ {print substr($2, length("SOURCEURL") + 1), $3}' | LC_ALL=C sort -n | awk '{print $2}')
600                 PATCHES=$(rpm_dump | awk '$2 ~ /^PATCHURL[0-9]+/ {print substr($2, length("PATCHURL") + 1), $3}' | LC_ALL=C sort -n | awk '{print $2}')
601                 ICONS=$(awk '/^Icon:/ {print $2}' ${SPECFILE})
602         fi
603
604         PACKAGE_NAME=$(rpm_dump | awk '$2 == "PACKAGE_NAME" { print $3; exit}')
605         PACKAGE_VERSION=$(rpm_dump | awk '$2 == "PACKAGE_VERSION" { print $3; exit}')
606         PACKAGE_RELEASE=$(rpm_dump | awk '$2 == "PACKAGE_RELEASE" { print $3; exit}')
607
608         if [ "$PACKAGE_NAME" != "$ASSUMED_NAME" ]; then
609                 echo >&2 "WARNING! Spec name ($ASSUMED_NAME) does not agree with package name ($PACKAGE_NAME)"
610         fi
611
612         if [ -n "$BE_VERBOSE" ]; then
613                 echo "- Sources :  `nourl $SOURCES`"
614                 if [ -n "$PATCHES" ]; then
615                         echo "- Patches :  `nourl $PATCHES`"
616                 else
617                         echo "- Patches :  *no patches needed*"
618                 fi
619                 if [ -n "$ICONS" ]; then
620                         echo "- Icon    :  `nourl $ICONS`"
621                 else
622                         echo "- Icon    :  *no package icon*"
623                 fi
624                 echo "- Name    : $PACKAGE_NAME"
625                 echo "- Version : $PACKAGE_VERSION"
626                 echo "- Release : $PACKAGE_RELEASE"
627         fi
628
629         update_shell_title "parse_spec: OK!"
630 }
631
632 Exit_error() {
633         if [ -n "$DEBUG" ]; then
634                 set -x
635                 set -v
636         fi
637
638         cd "$__PWD"
639
640         case "$1" in
641                 "err_no_spec_in_cmdl" )
642                         remove_build_requires
643                         echo >&2 "ERROR: spec file name not specified."
644                         exit 2 ;;
645                 "err_invalid_cmdline" )
646                         echo >&2 "ERROR: invalid command line arg ($2)."
647                         exit 2 ;;
648                 "err_no_spec_in_repo" )
649                         remove_build_requires
650                         echo >&2 "Error: spec file not stored in CVS repo."
651                         exit 3 ;;
652                 "err_no_source_in_repo" )
653                         remove_build_requires
654                         echo >&2 "Error: some source, patch or icon files not stored in CVS repo. ($2)"
655                         exit 4 ;;
656                 "err_build_fail" )
657                         remove_build_requires
658                         echo >&2 "Error: package build failed. (${2:-no more info})"
659                         exit 5 ;;
660                 "err_no_package_data" )
661                         remove_build_requires
662                         echo >&2 "Error: couldn't get out package name/version/release from spec file."
663                         exit 6 ;;
664                 "err_tag_exists" )
665                         remove_build_requires
666                         echo >&2 "Tag ${2} already exists (spec release: ${3})."
667                         exit 9 ;;
668                 "err_fract_rel" )
669                         remove_build_requires
670                         echo >&2 "Release ${2} not integer and not a snapshot."
671                         exit 10 ;;
672                 "err_branch_exists" )
673                         remove_build_requires
674                         echo >&2 "Tree branch already exists (${2})."
675                         exit 11 ;;
676                 "err_acl_deny" )
677                         remove_build_requires
678                         echo >&2 "Error: conditions reject building this spec (${2})."
679                         exit 12 ;;
680         esac
681         echo >&2 "Unknown error."
682         exit 100
683 }
684
685 init_builder() {
686         if [ -n "$DEBUG" ]; then
687                 set -x
688                 set -v
689         fi
690
691         if [ "$NOINIT" != "yes" ] ; then
692                 TOP_DIR=$(eval $RPM $RPMOPTS --eval '%{_topdir}')
693
694                 local macros_ver=$(rpm -E %?rpm_build_macros)
695                 if [ -z "$macros_ver" ]; then
696                         REPO_DIR=$TOP_DIR/packages
697                         PACKAGE_DIR=$TOP_DIR/packages/$ASSUMED_NAME
698                 else
699                         if awk "BEGIN{exit($macros_ver>=$RPM_MACROS_VER)}"; then
700                                 echo >&2 "builder requires rpm-build-macros >= $RPM_MACROS_VER"
701                                 exit 1
702                         fi
703                         REPO_DIR=$TOP_DIR
704                         PACKAGE_DIR=$REPO_DIR/$ASSUMED_NAME
705                 fi
706         else
707                 REPO_DIR="."
708                 PACKAGE_DIR="."
709         fi
710
711         __PWD=$(pwd)
712 }
713
714 get_spec() {
715
716         update_shell_title "get_spec"
717
718         if [ -n "$DEBUG" ]; then
719                 set -x
720                 set -v
721         fi
722
723         cd "$REPO_DIR"
724         if [ ! -f "$ASSUMED_NAME/$SPECFILE" ]; then
725                 # XXX: still needed?
726                 SPECFILE=$(basename $SPECFILE)
727         fi
728         if [ "$NOCVSSPEC" != "yes" ]; then
729
730                 if [ ! -s CVS/Root -a "$NOCVSSPEC" != "yes" ]; then
731                         echo "Warning: No CVS access defined - using local .spec file"
732                         NOCVSSPEC="yes"
733                 fi
734
735                 if [ -d "$ASSUMED_NAME" ]; then
736                         cvsup "$ASSUMED_NAME/$SPECFILE" || Exit_error err_no_spec_in_repo
737                 else
738                         cvsup -c -d $ASSUMED_NAME "packages/$ASSUMED_NAME/$SPECFILE" || Exit_error err_no_spec_in_repo
739
740                         # remove Entries.Static -- so 'cvs up' would update all files in a repo
741                         rm "$ASSUMED_NAME/CVS/Entries.Static"
742                         cvsignore_df .cvsignore
743
744                         # add default log format to .cvsignore if it is relative
745                         if [ "$LOGFILE" == $(basename "$LOGFILE") ]; then
746                                 # substitute known "macros" to glob
747                                 local logfile=$(echo "$LOGFILE" | sed -e 's,\$\(PACKAGE_NAME\|DATE\),*,g')
748                                 if [ "$logfile" ]; then
749                                         cvsignore_df "$logfile"
750                                 fi
751                         fi
752
753                         # create symlinks for tools
754                         if [ "$SYMLINK_TOOLS" != "no" ]; then
755                                 for a in dropin md5 adapter builder {relup,compile,repackage,rsync,pearize}.sh pldnotify.awk; do
756                                         [ -f $a ] || continue
757                                         ln -s ../$a $ASSUMED_NAME
758                                         cvsignore_df $a
759                                 done
760                         fi
761                 fi
762         fi
763
764         if [ ! -f "$ASSUMED_NAME/$SPECFILE" ]; then
765                 Exit_error err_no_spec_in_repo
766         fi
767
768         if [ "$CHMOD" = "yes" -a -n "$SPECFILE" ]; then
769                 chmod $CHMOD_MODE $ASSUMED_NAME/$SPECFILE
770         fi
771         unset OPTIONS
772         [ -n "$DONT_PRINT_REVISION" ] || grep -E -m 1 "^#.*Revision:.*Date" $ASSUMED_NAME/$SPECFILE
773
774         set_spec_target
775 }
776
777 find_mirror() {
778         cd "$REPO_DIR"
779         local url="$1"
780         if [ ! -f "mirrors" -a "$NOCVSSPEC" != "yes" ] ; then
781                 $CVS_COMMAND update mirrors >&2
782         fi
783
784         IFS="|"
785         local origin mirror name rest ol prefix
786         while read origin mirror name rest; do
787                 # skip comments and empty lines
788                 if [ -z "$origin" ] || [ "${origin#\#}" != "$origin" ]; then
789                         continue
790                 fi
791                 ol=$(echo -n "$origin" | wc -c)
792                 prefix=$(echo -n "$url" | head -c $ol)
793                 if [ "$prefix" = "$origin" ] ; then
794                         suffix=$(echo "$url" | cut -b $((ol+1))-)
795                         echo -n "$mirror$suffix"
796                         return 0
797                 fi
798         done < mirrors
799         echo "$url"
800 }
801
802 # Warning: unpredictable results if same URL used twice
803 src_no() {
804         cd $PACKAGE_DIR
805         rpm_dump | \
806         grep "SOURCEURL[0-9]*[  ]*$1""[         ]*$" | \
807         sed -e 's/.*SOURCEURL\([0-9][0-9]*\).*/\1/' | \
808         head -n 1 | xargs
809 }
810
811 src_md5() {
812         [ "$NO5" = "yes" ] && return
813         no=$(src_no "$1")
814         [ -z "$no" ] && return
815         cd $PACKAGE_DIR
816         local md5
817
818         if [ -f additional-md5sums ]; then
819                 local spec_rev=$(grep $SPECFILE CVS/Entries 2>/dev/null | sed -e s:/$SPECFILE/:: -e s:/.*::)
820                 if [ -z "$spec_rev" ]; then
821                         spec_rev=$(head -n 1 $SPECFILE | sed -e 's/.*\$Revision: \([0-9.]*\).*/\1/')
822                 fi
823                 local spec="$SPECFILE[0-9.,]*,$(echo $spec_rev | sed 's/\./\\./g')"
824                 md5=$(grep -s -v '^#' additional-md5sums | \
825                 grep -E "[      ]$(basename "$1")[      ]+${spec}([     ,]|\$)" | \
826                 sed -e 's/^\([0-9a-f]\{32\}\).*/\1/' | \
827                 grep -E '^[0-9a-f]{32}$')
828
829                 if [ "$md5" ]; then
830                         if [ $(echo "$md5" | wc -l) != 1 ] ; then
831                                 echo "$SPECFILE: more then one entry in additional-md5sums for $1" 1>&2
832                         fi
833                         echo "$md5" | tail -n 1
834                         return
835                 fi
836         fi
837
838         source_md5=`grep -i "^#[        ]*Source$no-md5[        ]*:" $SPECFILE | sed -e 's/.*://'`
839         if [ -n "$source_md5" ]; then
840                 echo $source_md5
841         else
842                 source_md5=`grep -i "BuildRequires:[    ]*digest(%SOURCE$no)[   ]*=" $SPECFILE | sed -e 's/.*=//'`
843                 if [ -n "$source_md5" ]; then
844                         echo $source_md5
845                 else
846                         # we have empty SourceX-md5, but it is still possible
847                         # that we have NoSourceX-md5 AND NoSource: X
848                         nosource_md5=`grep -i "^#[       ]*NoSource$no-md5[      ]*:" $SPECFILE | sed -e 's/.*://'`
849                         if [ -n "$nosource_md5" -a -n "`grep -i "^NoSource:[     ]*$no$" $SPECFILE`" ] ; then
850                                 echo $nosource_md5
851                         fi
852                 fi
853         fi
854 }
855
856 distfiles_path() {
857         echo "by-md5/$(src_md5 "$1" | sed -e 's|^\(.\)\(.\)|\1/\2/&|')/$(basename "$1")"
858 }
859
860 distfiles_url() {
861         echo "$PROTOCOL$DISTFILES_SERVER/distfiles/$(distfiles_path "$1")"
862 }
863
864 distfiles_attic_url() {
865         echo "$PROTOCOL$ATTICDISTFILES_SERVER/distfiles/Attic/$(distfiles_path "$1")"
866 }
867
868 good_md5() {
869         md5=$(src_md5 "$1")
870         [ "$md5" = "" ] || \
871         [ "$md5" = "$(md5sum $(nourl "$1") 2> /dev/null | sed -e 's/ .*//')" ]
872 }
873
874 good_size() {
875         size=$(find $(nourl "$1") -printf "%s" 2>/dev/null)
876         [ -n "$size" -a "$size" -gt 0 ]
877 }
878
879 cvsignore_df() {
880         if [ "$CVSIGNORE_DF" != "yes" ]; then
881                 return
882         fi
883         cvsignore=${PACKAGE_DIR}/.cvsignore
884
885         # add only if not yet there
886         if ! awk -vf="$1" -vc=1 '$0 == f { c = 0 } END { exit c }' $cvsignore 2>/dev/null; then
887                 echo "$1" >> $cvsignore
888         fi
889 }
890
891 cvsup() {
892         update_shell_title "cvsup"
893         local OPTIONS="up "
894
895         if [ "$1" = "-c" ]; then
896                 OPTIONS="co "
897                 shift
898         fi
899         if [ -n "$CVSROOT" ]; then
900                 OPTIONS="-d $CVSROOT $OPTIONS"
901         fi
902
903         if [ -z "$CVSDATE" -a -z "$CVSTAG" ]; then
904                 OPTIONS="$OPTIONS -A"
905         else
906                 if [ -n "$CVSDATE" ]; then
907                         OPTIONS="$OPTIONS -D $CVSDATE"
908                 fi
909                 if [ -n "$CVSTAG" ]; then
910                         OPTIONS="$OPTIONS -r $CVSTAG"
911                 fi
912         fi
913
914         local result=1
915         local retries_counter=0
916         if [ $# = 1 ]; then
917                 update_shell_title "cvsup: $*"
918         else
919                 update_shell_title "cvsup: $# files"
920         fi
921         while [ "$result" != "0" -a "$retries_counter" -le "$CVS_RETRIES" ]; do
922                 retries_counter=$(( $retries_counter + 1 ))
923                 output=$(LC_ALL=C $CVS_COMMAND $OPTIONS "$@" 2>&1)
924                 result=$?
925                 [ -n "$output" ] && echo "$output"
926                 if (echo "$output" | grep -qE "(Cannot connect to|connect to .* failed|Connection reset by peer|Connection timed out|Unknown host)") && [ "$result" -ne "0" -a "$retries_counter" -le "$CVS_RETRIES" ]; then
927                         echo "Trying again [$*]... ($retries_counter)"
928                         update_shell_title "cvsup: retry #$retries_counter"
929                         sleep 2
930                         continue
931                 else
932                         break
933                 fi
934         done
935         update_shell_title "cvsup: done!"
936         return $result
937 }
938
939 # returns true if "$1" is ftp, http or https protocol url
940 is_url() {
941         case "$1" in
942         ftp://*|http://*|https://*)
943                 return 0
944         ;;
945         esac
946         return 1
947 }
948
949 update_md5() {
950         if [ $# -eq 0 ]; then
951                 return
952         fi
953
954         update_shell_title "update md5"
955         if [ -n "$DEBUG" ]; then
956                 set -x
957                 set -v
958         fi
959
960         cd "$PACKAGE_DIR"
961
962         # pass 1: check files to be fetched
963         local todo
964         local need_files
965         for i in "$@"; do
966                 local fp=$(nourl "$i")
967                 local srcno=$(src_no "$i")
968                 if [ -n "$ADD5" ]; then
969                         [ "$fp" = "$i" ] && continue # FIXME what is this check doing?
970                         grep -qiE '^#[  ]*Source'$srcno'-md5[   ]*:' $PACKAGE_DIR/$SPECFILE && continue
971                         grep -qiE '^BuildRequires:[     ]*digest[(]%SOURCE'$srcno'[)][  ]*=' $PACKAGE_DIR/$SPECFILE && continue
972                 else
973                         grep -qiE '^#[  ]*Source'$srcno'-md5[   ]*:' $PACKAGE_DIR/$SPECFILE || grep -qiE '^BuildRequires:[      ]*digest[(]%SOURCE'$srcno'[)][  ]*=' $PACKAGE_DIR/$SPECFILE || continue
974                 fi
975                 if [ ! -f "$fp" ] || [ $ALWAYS_CVSUP = "yes" ]; then
976                         need_files="$need_files $i"
977                 fi
978         done
979
980         # pass 1a: get needed files
981         if [ "$need_files" ]; then
982                 get_files $need_files
983         fi
984
985         # pass 2: proceed with md5 adding or updating
986         for i in "$@"; do
987                 local fp=$(nourl "$i")
988                 local srcno=$(src_no "$i")
989                 local md5=$(grep -iE '^#[       ]*(No)?Source'$srcno'-md5[      ]*:' $PACKAGE_DIR/$SPECFILE )
990                 if [ -z "$md5" ]; then
991                         md5=$(grep -iE '^[      ]*BuildRequires:[       ]*digest[(]%SOURCE'$srcno'[)][  ]*=' $PACKAGE_DIR/$SPECFILE )
992                 fi
993                 if [ -n "$ADD5" ] && is_url $i || [ -n "$md5" ]; then
994                         local tag="# Source$srcno-md5:\t"
995                         if [[ "$md5" == *NoSource* ]]; then
996                                 tag="# NoSource$srcno-md5:\t"
997                         elif [ -n "$USEDIGEST" ]; then
998                                 tag="BuildRequires:\tdigest(%SOURCE$srcno) = "
999                         fi
1000                         md5=$(md5sum "$fp" | cut -f1 -d' ')
1001                         echo "Updating Source$srcno ($md5: $fp)."
1002                         perl -i -ne '
1003                                 print unless (/^\s*#\s*(No)?Source'$srcno'-md5\s*:/i or /^\s*BuildRequires:\s*digest\(%SOURCE'$srcno'\)/i);
1004                                 print "'"$tag$md5"'\n" if /^Source'$srcno'\s*:\s+/;
1005                         ' \
1006                         $PACKAGE_DIR/$SPECFILE
1007                 fi
1008         done
1009 }
1010
1011 check_md5() {
1012         local bad
1013         [ "$NO5" = "yes" ] && return
1014
1015         update_shell_title "check md5"
1016
1017         for i in "$@"; do
1018                 bad=0
1019                 if ! good_md5 "$i"; then
1020                         echo -n "MD5 sum mismatch."
1021                         bad=1
1022                 fi
1023                 if ! good_size "$i"; then
1024                         echo -n "0 sized file."
1025                         bad=1
1026                 fi
1027
1028                 if [ $bad -eq 1 ]; then
1029                         echo " Use -U to refetch sources,"
1030                         echo "or -5 to update md5 sums, if you're sure files are correct."
1031                         Exit_error err_no_source_in_repo $i
1032                 fi
1033         done
1034 }
1035
1036 get_files() {
1037         update_shell_title "get_files"
1038
1039         if [ -n "$DEBUG" ]; then
1040                 set -x
1041                 set -v
1042         fi
1043
1044         if [ $# -gt 0 ]; then
1045                 cd "$PACKAGE_DIR"
1046
1047                 if [ ! -s CVS/Root -a "$NOCVS" != "yes" ]; then
1048                         echo "Warning: No CVS access defined for SOURCES"
1049                         NOCVS="yes"
1050                 fi
1051
1052                 local nc=0
1053                 local get_files_cvs=""
1054                 for i in "$@"; do
1055                         nc=$((nc + 1))
1056                         local cvsup=0
1057                         SHELL_TITLE_PREFIX="get_files[$nc/$#]"
1058                         update_shell_title "$i"
1059                         local fp=`nourl "$i"`
1060                         if [ "$SKIP_EXISTING_FILES" = "yes" ] && [ -f "$fp" ]; then
1061                                 continue
1062                         fi
1063
1064                         FROM_DISTFILES=0
1065                         local srcmd5=$(src_md5 "$i")
1066
1067                         # we know if source/patch is present in cvs/distfiles
1068                         # - has md5 (in distfiles)
1069                         # - in cvs... ideas?
1070
1071                         # CHECK: local file didn't exist or always cvs up (first) requested.
1072                         if [ ! -f "$fp" ] || [ $ALWAYS_CVSUP = "yes" ]; then
1073                                 if echo $i | grep -vE '(http|ftp|https|cvs|svn)://' | grep -qE '\.(gz|bz2)$']; then
1074                                         echo "Warning: no URL given for $i"
1075                                 fi
1076                                 target="$fp"
1077
1078                                 if [ -z "$NODIST" ] && [ -n "$srcmd5" ]; then
1079                                         if good_md5 "$i" && good_size "$i"; then
1080                                                 echo "$fp having proper md5sum already exists"
1081                                                 continue
1082                                         fi
1083
1084                                         # optionally prefer mirror over distfiles if there's mirror
1085                                         # TODO: build url list and then try each url from the list
1086                                         if [ -n "$PREFMIRRORS" ] && [ -z "$NOMIRRORS" ] && im=$(find_mirror "$i") && [ "$im" != "$i" ]; then
1087                                                 url="$im"
1088                                         else
1089                                                 url=$(distfiles_url "$i")
1090                                         fi
1091
1092                                         url_attic=$(distfiles_attic_url "$i")
1093                                         FROM_DISTFILES=1
1094                                         # is $url local file?
1095                                         if [[ "$url" = [./]* ]]; then
1096                                                 update_shell_title "${GETLOCAL%% *}: $url"
1097                                                 ${GETLOCAL} $url $target
1098                                         else
1099                                                 if [ -z "$NOMIRRORS" ]; then
1100                                                         url=$(find_mirror "$url")
1101                                                 fi
1102
1103                                                 local uri=${url}
1104                                                 # make shorter message for distfiles urls
1105                                                 if [[ "$uri" = ${PROTOCOL}${DISTFILES_SERVER}* ]] || [[ "$uri" = ${PROTOCOL}${ATTICDISTFILES_SERVER}* ]]; then
1106                                                         uri=${uri#${PROTOCOL}${DISTFILES_SERVER}/distfiles/by-md5/?/?/*/}
1107                                                         uri=${uri#${PROTOCOL}${ATTICDISTFILES_SERVER}/distfiles/by-md5/?/?/*/}
1108                                                         uri="df: $uri"
1109                                                 fi
1110                                                 update_shell_title "${GETURI%% *}: $uri"
1111                                                 ${GETURI} ${OUTFILEOPT} "$target" "$url" || \
1112                                                 if [ "`echo $url | grep -E 'ftp://'`" ]; then
1113                                                         update_shell_title "${GETURI2%% *}: $url"
1114                                                         ${GETURI2} ${OUTFILEOPT} "$target" "$url"
1115                                                 fi
1116                                         fi
1117
1118                                         # is it empty file?
1119                                         if [ ! -s "$target" ]; then
1120                                                 rm -f "$target"
1121                                                 if [ `echo $url_attic | grep -E '^(\.|/)'` ]; then
1122                                                         update_shell_title "${GETLOCAL%% *}: $url_attic"
1123                                                         ${GETLOCAL} $url_attic $target
1124                                                 else
1125                                                         if [ -z "$NOMIRRORS" ]; then
1126                                                                 url_attic=$(find_mirror "$url_attic")
1127                                                         fi
1128                                                         update_shell_title "${GETURI%% *}: $url_attic"
1129                                                         ${GETURI} ${OUTFILEOPT} "$target" "$url_attic" || \
1130                                                         if [ "`echo $url_attic | grep -E 'ftp://'`" ]; then
1131                                                                 update_shell_title "${GETURI2%% *}: $url_attic"
1132                                                                 ${GETURI2} ${OUTFILEOPT} "$target" "$url_attic"
1133                                                         fi
1134                                                         test -s "$target" || rm -f "$target"
1135                                                 fi
1136                                         fi
1137
1138                                         if [ -s "$target" ]; then
1139                                                 cvsignore_df $target
1140                                         else
1141                                                 rm -f "$target"
1142                                                 FROM_DISTFILES=0
1143                                         fi
1144                                 elif [ "$NOCVS" != "yes" -a -z "$srcmd5" ]; then
1145                                         if [ $# -gt 1 ]; then
1146                                                 get_files_cvs="$get_files_cvs $fp"
1147                                                 update_shell_title "$fp (will cvs up later)"
1148                                                 cvsup=1
1149                                         else
1150                                                 cvsup $fp
1151                                         fi
1152                                 fi
1153
1154                                 if [ -z "$NOURLS" ] && [ ! -f "$fp" -o -n "$UPDATE" ] && [ "`echo $i | grep -E 'ftp://|http://|https://'`" ]; then
1155                                         if [ -z "$NOMIRRORS" ]; then
1156                                                 im=$(find_mirror "$i")
1157                                         else
1158                                                 im="$i"
1159                                         fi
1160                                         update_shell_title "${GETURI%% *}: $im"
1161                                         ${GETURI} "$im" ${OUTFILEOPT} "$target" || \
1162                                         if [ "`echo $im | grep -E 'ftp://'`" ]; then
1163                                                 update_shell_title "${GETURI2%% *}: $im"
1164                                                 ${GETURI2} "$im" ${OUTFILEOPT} "$target"
1165                                         fi
1166                                         test -s "$target" || rm -f "$target"
1167                                 fi
1168
1169                                 if [ "$cvsup" = 1 ]; then
1170                                         continue
1171                                 fi
1172
1173                         fi
1174
1175                         # the md5 check must be moved elsewhere as if we've called from update_md5 the md5 is wrong.
1176                         if [ ! -f "$fp" -a "$FAIL_IF_NO_SOURCES" != "no" ]; then
1177                                 if [ -n "GREEDSRC" ]; then
1178                                         get_greed_sources $i
1179                                 else
1180                                         Exit_error err_no_source_in_repo $i
1181                                 fi
1182                         fi
1183
1184                         # we check md5 here just only to refetch immediately
1185                         if good_md5 "$i" && good_size "$i"; then
1186                                 :
1187                         elif [ "$FROM_DISTFILES" = 1 ]; then
1188                                 # wrong md5 from distfiles: remove the file and try again
1189                                 # but only once ...
1190                                 echo "MD5 sum mismatch. Trying full fetch."
1191                                 FROM_DISTFILES=2
1192                                 rm -f $target
1193                                 update_shell_title "${GETURI%% *}: $url"
1194                                 ${GETURI} ${OUTFILEOPT} "$target" "$url" || \
1195                                 if [ "`echo $url | grep -E 'ftp://'`" ]; then
1196                                         update_shell_title "${GETURI2%% *}: $url"
1197                                         ${GETURI2} ${OUTFILEOPT} "$target" "$url"
1198                                 fi
1199                                 if [ ! -s "$target" ]; then
1200                                         rm -f "$target"
1201                                         update_shell_title "${GETURI%% *}: $url_attic"
1202                                         ${GETURI} ${OUTFILEOPT} "$target" "$url_attic" || \
1203                                         if [ "`echo $url_attic | grep -E 'ftp://'`" ]; then
1204                                                 update_shell_title "${GETURI2%% *}: $url_attic"
1205                                                 ${GETURI2} ${OUTFILEOPT} "$target" "$url_attic"
1206                                         fi
1207                                 fi
1208                                 test -s "$target" || rm -f "$target"
1209                         fi
1210                 done
1211                 SHELL_TITLE_PREFIX=""
1212
1213                 if [ "$get_files_cvs" ]; then
1214                         cvsup $get_files_cvs
1215                 fi
1216
1217                 if [ "$CHMOD" = "yes" ]; then
1218                         CHMOD_FILES=$(nourl "$@")
1219                         if [ -n "$CHMOD_FILES" ]; then
1220                                 chmod $CHMOD_MODE $CHMOD_FILES
1221                         fi
1222                 fi
1223         fi
1224 }
1225
1226 make_tagver() {
1227         if [ -n "$DEBUG" ]; then
1228                 set -x
1229                 set -v
1230         fi
1231
1232         # Check whether first character of PACKAGE_NAME is legal for tag name
1233         if [ -z "${PACKAGE_NAME##[_0-9]*}" -a -z "$TAG_PREFIX" ]; then
1234                 TAG_PREFIX=tag_
1235         fi
1236
1237         # NOTE: CVS tags may must not contain the characters `$,.:;@'
1238         TAGVER=$(echo $TAG_PREFIX$PACKAGE_NAME-$PACKAGE_VERSION-$PACKAGE_RELEASE | tr '[.@]' '[_#]')
1239
1240         # Remove #kernel.version_release from TAGVER because tagging sources
1241         # could occur with different kernel-headers than kernel-headers used at build time.
1242         # besides, %{_kernel_ver_str} is not expanded.
1243
1244         # TAGVER=auto-ac-madwifi-ng-0-0_20070225_1#%{_kernel_ver_str}
1245         # TAGVER=auto-ac-madwifi-ng-0-0_20070225_1
1246
1247         TAGVER=${TAGVER%#*}
1248         echo -n "$TAGVER"
1249 }
1250
1251 # bool is_tag_a_branch(tag)
1252 #
1253 # returns 1 if a tag is a branch set on SPECFILE
1254 is_tag_a_branch() {
1255         if [ -n "$DEBUG" ]; then
1256                 set -x
1257                 set -v
1258         fi
1259
1260         if [ $# -ne 1 ]; then
1261                 return 0;
1262         fi
1263
1264         TAG=$1
1265
1266         cd "$PACKAGE_DIR"
1267         $CVS_COMMAND status -v $SPECFILE | grep -Eiq "${TAG}.+(branch: [0-9.]+)"
1268         return $?
1269 }
1270
1271 tag_files() {
1272         TAG_FILES="$@"
1273
1274         if [ -n "$DEBUG" ]; then
1275                 set -x
1276                 set -v
1277         fi
1278
1279         echo "Version: $PACKAGE_VERSION"
1280         echo "Release: $PACKAGE_RELEASE"
1281
1282         local TAGVER
1283         if [ "$TAG_VERSION" = "yes" ]; then
1284                 TAGVER=`make_tagver`
1285                 echo "CVS tag: $TAGVER"
1286         fi
1287         if [ -n "$TAG" ]; then
1288                 echo "CVS tag: $TAG"
1289         fi
1290
1291         local OPTIONS="tag $CVS_FORCE"
1292         if [ -n "$CVSROOT" ]; then
1293                 OPTIONS="-d $CVSROOT $OPTIONS"
1294         fi
1295
1296         # if a tagname we are about to set already exists
1297         # and happens to be a branch (common case with AC-branch)
1298         # pass -B (allows -F to disturb branch tag)
1299         local _tag=$TAG
1300         if [ "$TAG_VERSION" = "yes" ]; then
1301                 _tag=$TAGVER
1302         fi;
1303         is_tag_a_branch $_tag
1304         if [ $? -eq 0 -a $CVS_NSERVER -eq 0 ]; then
1305                 OPTIONS="$OPTIONS -B"
1306         fi;
1307
1308         cd "$PACKAGE_DIR"
1309         local tag_files
1310         for i in $TAG_FILES; do
1311                 # don't tag files stored on distfiles
1312                 [ -n "`src_md5 $i`" ] && continue
1313                 local fp=`nourl "$i"`
1314                 if [ -f "$fp" ]; then
1315                         tag_files="$tag_files $fp"
1316                 elif [ -n "GREEDSRC" ]; then
1317                         get_greed_sources $i
1318                 else
1319                         Exit_error err_no_source_in_repo $i
1320                 fi
1321         done
1322
1323         if [ "$tag_files" ]; then
1324                 if [ "$TAG_VERSION" = "yes" ]; then
1325                         update_shell_title "tag sources: $TAGVER"
1326                         printf "Tagging %d files\n" $(echo $tag_files | wc -w)
1327                         $CVS_COMMAND $OPTIONS $TAGVER $tag_files || exit
1328                 fi
1329                 if [ -n "$TAG" ]; then
1330                         update_shell_title "tag sources: $TAG"
1331
1332                         while [ "$tag_files" ]; do
1333                                 local chunk=$(echo $tag_files | tr ' ' '\n' | head -n 100)
1334                                 printf "Tagging %d files\n" $(echo $chunk | wc -w)
1335                                 $CVS_COMMAND $OPTIONS $TAG $chunk || exit
1336                                 tag_files=$(echo $tag_files | tr ' ' '\n' | tail +101)
1337                         done
1338                 fi
1339         fi
1340
1341         cd "$PACKAGE_DIR"
1342         if [ "$TAG_VERSION" = "yes" ]; then
1343                 update_shell_title "tag spec: $TAGVER"
1344                 $CVS_COMMAND $OPTIONS $TAGVER $SPECFILE || exit
1345         fi
1346         if [ -n "$TAG" ]; then
1347                 update_shell_title "tag spec: $TAG"
1348                 $CVS_COMMAND $OPTIONS $TAG $SPECFILE || exit
1349         fi
1350 }
1351
1352 branch_files() {
1353         TAG=$1
1354         echo "CVS branch tag: $TAG"
1355         shift
1356
1357         TAG_FILES="$@"
1358
1359         if [ -n "$DEBUG" ]; then
1360                 set -x
1361                 set -v
1362         fi
1363
1364         local OPTIONS="tag $CVS_FORCE -b"
1365
1366         # branch exists?
1367         is_tag_a_branch $TAG
1368         if [ $? -eq 1 ]; then
1369                 OPTIONS="$OPTIONS -B"
1370         fi
1371
1372         if [ -n "$CVSROOT" ]; then
1373                 OPTIONS="-d $CVSROOT $OPTIONS"
1374         fi
1375         cd "$PACKAGE_DIR"
1376         local tag_files
1377         for i in $TAG_FILES; do
1378                 local fp=`nourl "$i"`
1379                 if [ -f "$fp" ]; then
1380                         tag_files="$tag_files $fp"
1381                 elif [ -n "GREEDSRC" ]; then
1382                         get_greed_sources $i
1383                 else
1384                         Exit_error err_no_source_in_repo $i
1385                 fi
1386         done
1387         if [ "$tag_files" ]; then
1388                 $CVS_COMMAND $OPTIONS $TAG $tag_files || exit
1389         fi
1390
1391         cd "$PACKAGE_DIR"
1392         $CVS_COMMAND $OPTIONS $TAG $SPECFILE || exit
1393 }
1394
1395
1396 # this function should exit early if package can't be built for this arch
1397 # this avoids unneccessary BR filling.
1398 check_buildarch() {
1399         local out ret
1400         out=$(minirpm --short-circuit -bp --define "'prep exit 0'" --nodeps $SPECFILE 2>&1)
1401         ret=$?
1402         if [ $ret -ne 0 ]; then
1403                 echo >&2 "$out"
1404                 exit $ret
1405         fi
1406 }
1407
1408 # from relup.sh
1409 set_release() {
1410         local specfile="$1"
1411         local rel="$2"
1412         local newrel="$3"
1413         sed -i -e "
1414                 s/^\(%define[ \t]\+_\?rel[ \t]\+\)$rel\$/\1$newrel/
1415                 s/^\(Release:[ \t]\+\)$rel\$/\1$newrel/
1416         " $specfile
1417 }
1418
1419 set_version() {
1420         local specfile="$1"
1421         local ver="$2" subver=$ver
1422         local newver="$3" newsubver=$newver
1423
1424         # try handling subver, everything that's not numeric-dotted in version
1425         if grep -Eq '%define\s+subver' $specfile; then
1426                 subver=$(echo "$ver" | sed -re 's,^[0-9.]+,,')
1427                 ver=${ver%$subver}
1428                 newsubver=$(echo "$newver" | sed -re 's,^[0-9.]+,,')
1429                 newver=${newver%$newsubver}
1430         fi
1431         sed -i -e "
1432                 s/^\(%define[ \t]\+_\?ver[ \t]\+\)$ver\$/\1$newver/
1433                 s/^\(%define[ \t]\+subver[ \t]\+\)$subver\$/\1$newsubver/
1434                 s/^\(Version:[ \t]\+\)$ver\$/\1$newver/
1435         " $specfile
1436 }
1437
1438 try_upgrade() {
1439         if [ -n "$TRY_UPGRADE" ]; then
1440                 local TNOTIFY TNEWVER TOLDVER
1441                 update_shell_title "build_package: try_upgrade"
1442
1443                 cd "$PACKAGE_DIR"
1444                 
1445                 if [ "$UPGRADE_VERSION" ]; then
1446                         TNEWVER=$UPGRADE_VERSION
1447                 else
1448                         if [ -n "$FLOAT_VERSION" ]; then
1449                                 TNOTIFY=$($APPDIR/pldnotify.awk ${BE_VERBOSE:+-vDEBUG=1} $SPECFILE -n) || exit 1
1450                         else
1451                                 TNOTIFY=$($APPDIR/pldnotify.awk ${BE_VERBOSE:+-vDEBUG=1} $SPECFILE) || exit 1
1452                         fi
1453
1454                         TNEWVER=$(echo $TNOTIFY | awk '{ match($4,/\[NEW\]/); print $5 }')
1455                 fi
1456
1457                 if [ -n "$TNEWVER" ]; then
1458                         TOLDVER=`echo $TNOTIFY | awk '{ print $3; }'`
1459                         echo "New version found, updating spec file from $TOLDVER to version $TNEWVER"
1460                         if [ "$REVERT_BROKEN_UPGRADE" = "yes" ]; then
1461                                 cp -f $SPECFILE $SPECFILE.bak
1462                         fi
1463                         chmod +w $SPECFILE
1464                         set_release $SPECFILE $PACKAGE_RELEASE 1
1465                         set_version $SPECFILE $PACKAGE_VERSION $TNEWVER
1466                         parse_spec
1467                         return 1
1468                 fi
1469         fi
1470         return 0
1471 }
1472
1473 build_package() {
1474         update_shell_title "build_package"
1475         if [ -n "$DEBUG" ]; then
1476                 set -x
1477                 set -v
1478         fi
1479
1480         cd "$PACKAGE_DIR"
1481
1482         case "$COMMAND" in
1483                 build )
1484                         BUILD_SWITCH="-ba" ;;
1485                 build-binary )
1486                         BUILD_SWITCH="-bb" ;;
1487                 build-source )
1488                         BUILD_SWITCH="-bs --nodeps" ;;
1489                 build-prep )
1490                         BUILD_SWITCH="-bp --nodeps" ;;
1491                 build-build )
1492                         BUILD_SWITCH="-bc" ;;
1493                 build-install )
1494                         BUILD_SWITCH="-bi" ;;
1495                 build-list )
1496                         BUILD_SWITCH="-bl" ;;
1497
1498         esac
1499
1500         update_shell_title "build_package: $COMMAND"
1501         if [ -n "$LOGFILE" ]; then
1502                 LOG=`eval echo $LOGFILE`
1503                 if [ -d "$LOG" ]; then
1504                         echo "Log file $LOG is a directory."
1505                         echo "Parse error in the spec?"
1506                         Exit_error err_build_fail
1507                 fi
1508                 if [ -n "$LASTLOG_FILE" ]; then
1509                         echo "LASTLOG=$LOG" > $LASTLOG_FILE
1510                 fi
1511                 RES_FILE=$(mktemp -t builder.XXXXXX || ${TMPDIR:-/tmp}/builder.$RANDOM)
1512
1513                 (time eval ${NICE_COMMAND} $RPMBUILD $TARGET_SWITCH $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $RPMBUILDOPTS $BCOND --define \'_specdir $PACKAGE_DIR\' --define \'_sourcedir $PACKAGE_DIR\' $SPECFILE; echo $? > $RES_FILE) 2>&1 |tee $LOG
1514                 RETVAL=`cat $RES_FILE`
1515                 rm $RES_FILE
1516                 if [ -n "$LOGDIROK" ] && [ -n "$LOGDIRFAIL" ]; then
1517                         if [ "$RETVAL" -eq "0" ]; then
1518                                 mv $LOG $LOGDIROK
1519                         else
1520                                 mv $LOG $LOGDIRFAIL
1521                         fi
1522                 fi
1523         else
1524                 eval ${NICE_COMMAND} $RPMBUILD $TARGET_SWITCH $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $RPMBUILDOPTS $BCOND --define \'_specdir $PACKAGE_DIR\' --define \'_sourcedir $PACKAGE_DIR\' $SPECFILE
1525                 RETVAL=$?
1526         fi
1527         if [ "$RETVAL" -ne "0" ]; then
1528                 if [ -n "$TRY_UPGRADE" ]; then
1529                         echo "\nUpgrade package to new version failed."
1530                         if [ "$REVERT_BROKEN_UPGRADE" = "yes" ]; then
1531                                 echo "Restoring old spec file."
1532                                 mv -f $SPECFILE.bak $SPECFILE
1533                         fi
1534                         echo ""
1535                 fi
1536                 Exit_error err_build_fail
1537         fi
1538         unset BUILD_SWITCH
1539 }
1540
1541 nourl() {
1542         echo "$@" | sed 's#\<\(ftp\|http\|https\|cvs\|svn\)://[^ ]*/##g'
1543 }
1544
1545 install_required_packages() {
1546         run_poldek -vi $1
1547         return $?
1548 }
1549
1550 find_spec_bcond() { # originally from /usr/lib/rpm/find-spec-bcond
1551         local SPEC="$1"
1552         awk -F"\n" '
1553         /^%changelog/ { exit }
1554         /^%bcond_with/{
1555                 match($0, /bcond_with(out)?[ \t]+[_a-zA-Z0-9]+/);
1556                 bcond = substr($0, RSTART + 6, RLENGTH - 6);
1557                 gsub(/[ \t]+/, "_", bcond);
1558                 print bcond
1559         }' $SPEC | LC_ALL=C sort -u
1560 }
1561
1562 process_bcondrc() {
1563         # expand bconds from ~/.bcondrc
1564         # The file structure is like gentoo's package.use:
1565         # ---
1566         # * -selinux
1567         # samba -mysql -pgsql
1568         # w32codec-installer license_agreement
1569         # php +mysqli
1570         # ---
1571         if ([ -f $HOME/.bcondrc ] || ([ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ])); then
1572                 :
1573         else
1574                 return
1575         fi
1576
1577         SN=${SPECFILE%%\.spec}
1578
1579         local bcondrc=$HOME/.bcondrc
1580         [ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ] && bcondrc=$HOME_ETC/.bcondrc
1581
1582         local bcond_avail=$(find_spec_bcond $SPECFILE)
1583
1584         while read pkg flags; do
1585                 # ignore comments
1586                 [[ "$pkg" == \#* ]] && continue
1587
1588                 # any package or current package?
1589                 if [ "$pkg" = "*" ] || [ "$pkg" = "$PACKAGE_NAME" ] || [ "$pkg" = "$SN" ]; then
1590                         for flag in $flags; do
1591                                 local opt=${flag#[+-]}
1592
1593                                 # use only flags which are in this package.
1594                                 if [[ $bcond_avail = *${opt}* ]]; then
1595                                         if [[ $flag = -* ]]; then
1596                                                 if [[ $BCOND != *--with?${opt}* ]]; then
1597                                                         BCOND="$BCOND --without $opt"
1598                                                 fi
1599                                         else
1600                                                 if [[ $BCOND != *--without?${opt}* ]]; then
1601                                                         BCOND="$BCOND --with $opt"
1602                                                 fi
1603                                         fi
1604                                 fi
1605                         done
1606                 fi
1607         done < $bcondrc
1608         update_shell_title "parse ~/.bcondrc: DONE!"
1609 }
1610
1611 set_bconds_values() {
1612         update_shell_title "set bcond values"
1613
1614         AVAIL_BCONDS_WITHOUT=""
1615         AVAIL_BCONDS_WITH=""
1616
1617         if egrep -q '^# *_with' ${SPECFILE}; then
1618                 echo >&2 "ERROR: This spec has old style bconds."
1619                 exit 1
1620         fi
1621
1622         if ! grep -q '^%bcond' ${SPECFILE}; then
1623                 return
1624         fi
1625
1626         local bcond_avail=$(find_spec_bcond $SPECFILE)
1627         process_bcondrc "$SPECFILE"
1628
1629         update_shell_title "parse bconds"
1630
1631         local opt bcond
1632         for opt in $bcond_avail; do
1633                 case "$opt" in
1634                 without_*)
1635                         bcond=${opt#without_}
1636                         case "$BCOND" in
1637                         *--without?${bcond}*)
1638                                 AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT <$bcond>"
1639                                 ;;
1640                         *)
1641                                 AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT $bcond"
1642                                 ;;
1643                         esac
1644                         ;;
1645                 with_*)
1646                         bcond=${opt#with_}
1647                         case "$BCOND" in
1648                         *--with?${bcond}*)
1649                                 AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH <$bcond>"
1650                                 ;;
1651                         *)
1652                                 AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH $bcond"
1653                                 ;;
1654                         esac
1655                         ;;
1656                 *)
1657                         echo >&2 "ERROR: unexpected '$opt' in set_bconds_values"
1658                         exit 1
1659                         ;;
1660                 esac
1661         done
1662 }
1663
1664 run_sub_builder() {
1665         package_name="${1}"
1666         update_shell_title "run_sub_builder $package_name"
1667         #
1668         # No i tutaj bym chcia³ zrobiæ sztuczn± inteligencjê, która spróbuje tego
1669         # pakieta zbudowaæ. Aktualnie niewiele dziala, bo generalnie nie widze do
1670         # konca algorytmu... Ale damy rade. :) Na razie po prostu sie wyjebie tak samo
1671         # jakby nie bylo tego kawalka kodu.
1672         #
1673         # Update: Poprawi³em parê rzeczy i zaczê³o generowaæ pakiety spoza zadanej listy.
1674         #         Jednym s³owem budowanie niespoldkowanych zale¿no¶ci dzia³a w paru przypadkach.
1675         #
1676         #
1677         # y0shi.
1678         # kurwa. translate that ^^^^
1679
1680         parent_spec_name=''
1681
1682         # Istnieje taki spec? ${package}.spec
1683         if [ -f "${PACKAGE_DIR}/${package}.spec" ]; then
1684                 parent_spec_name=${package}.spec
1685         elif [ -f "${PACKAGE_DIR}/$(echo ${package_name} | sed -e s,-devel.*,,g -e s,-static,,g).spec" ]; then
1686                 parent_spec_name="$(echo ${package_name} | sed -e s,-devel.*,,g -e s,-static,,g).spec"
1687         else
1688                 for provides_line in $(grep -r ^Provides:.*$package ${PACKAGE_DIR}); do
1689                         echo $provides_line
1690                 done
1691         fi
1692
1693         if [ "${parent_spec_name}" != "" ]; then
1694                 spawn_sub_builder $parent_spec_name
1695         fi
1696         NOT_INSTALLED_PACKAGES="$NOT_INSTALLED_PACKAGES $package_name"
1697 }
1698
1699 spawn_sub_builder() {
1700         package_name="${1}"
1701         update_shell_title "spawn_sub_builder $package_name"
1702
1703         sub_builder_opts=''
1704         if [ "${FETCH_BUILD_REQUIRES}" == "yes" ]; then
1705                 sub_builder_opts="${sub_builder_opts} -R"
1706         fi
1707         if [ "${REMOVE_BUILD_REQUIRES}" == "nice" ]; then
1708                 sub_builder_opts="${sub_builder_opts} -RB"
1709         elif [ "${REMOVE_BUILD_REQUIRES}" == "force" ]; then
1710                 sub_builder_opts="${sub_builder_opts} -FRB"
1711         fi
1712         if [ "${UPDATE_POLDEK_INDEXES}" == "yes" ]; then
1713                 sub_builder_opts="${sub_builder_opts} -Upi"
1714         fi
1715
1716         cd "${PACKAGE_DIR}"
1717         ./builder ${sub_builder_opts} "$@"
1718 }
1719
1720 remove_build_requires() {
1721         if [ "$INSTALLED_PACKAGES" != "" ]; then
1722                 case "$REMOVE_BUILD_REQUIRES" in
1723                         "force")
1724                                 run_poldek --noask -ve $INSTALLED_PACKAGES
1725                                 ;;
1726                         "nice")
1727                                 run_poldek --ask -ve $INSTALLED_PACKAGES
1728                                 ;;
1729                         *)
1730                                 echo You may want to manually remove following BuildRequires fetched:
1731                                 echo $INSTALLED_PACKAGES
1732                                 echo "Try poldek -e \`cat $(pwd)/.${SPECFILE}_INSTALLED_PACKAGES\`"
1733                                 ;;
1734                 esac
1735         fi
1736 }
1737
1738 display_bconds() {
1739         if [ "$AVAIL_BCONDS_WITH" -o "$AVAIL_BCONDS_WITHOUT" ]; then
1740                 if [ "$BCOND" ]; then
1741                         echo ""
1742                         echo "Building $SPECFILE with the following conditional flags:"
1743                         echo -n "$BCOND"
1744                 else
1745                         echo ""
1746                         echo "No conditional flags passed"
1747                 fi
1748                 echo ""
1749                 echo "from available:"
1750                 echo "--with   :\t$AVAIL_BCONDS_WITH"
1751                 echo "--without:\t$AVAIL_BCONDS_WITHOUT"
1752                 echo ""
1753         fi
1754 }
1755
1756 display_branches() {
1757         if [ "$NOCVSSPEC" != "yes" ]; then
1758                 echo -n "Available branches: "
1759                 $CVS_COMMAND status -v "${SPECFILE}" | awk '!/Sticky Tag:/ && /\(branch:/ { print $1 } ' | xargs
1760         fi
1761 }
1762
1763 # checks a given list of packages/files/provides agains current rpmdb.
1764 # outputs all dependencies which current rpmdb doesn't satisfy.
1765 # input can be either STDIN or parameters
1766 _rpm_prov_check() {
1767         local DEPS
1768
1769         if [ $# -gt 0 ]; then
1770                 DEPS="$@"
1771         else
1772                 DEPS=$(cat)
1773         fi
1774
1775         DEPS=$(LANG=C rpm -q --whatprovides $DEPS 2>&1 | awk '/^(error:|no package provides)/ { print }')
1776
1777         # packages
1778         echo "$DEPS" | awk '/^no package provides/ { print $NF }'
1779
1780         # other deps (files)
1781         echo "$DEPS" | awk -F: '/^error:.*No such file/{o = $2; gsub("^ file ", "", o); print o}'
1782 }
1783
1784 # checks if given package/files/provides exists in rpmdb.
1785 # input can be either stdin or parameters
1786 # returns packages which are present in the rpmdb
1787 _rpm_cnfl_check() {
1788         local DEPS
1789
1790         if [ $# -gt 0 ]; then
1791                 DEPS="$@"
1792         else
1793                 DEPS=$(cat)
1794         fi
1795
1796         LANG=C rpm -q --whatprovides $DEPS 2>/dev/null | awk '!/no package provides/ { print }'
1797 }
1798
1799 # install deps via information from 'rpm-getdeps' or 'rpm --specsrpm'
1800 install_build_requires_rpmdeps() {
1801         if [ "$FETCH_BUILD_REQUIRES_RPMGETDEPS" = "yes" ]; then
1802                 # TODO: Conflicts list doesn't check versions
1803                 local CNFL=$(rpm-getdeps $BCOND $SPECFILE 2> /dev/null | awk '/^\-/ { print $3 } ' | _rpm_cnfl_check | xargs)
1804                 local DEPS=$(rpm-getdeps $BCOND $SPECFILE 2> /dev/null | awk '/^\+/ { print $3 } ' | _rpm_prov_check | xargs)
1805         fi
1806         if [ "$FETCH_BUILD_REQUIRES_RPMSPECSRPM" = "yes" ]; then
1807                 local CNFL=$(rpm -q --specsrpm --conflicts $BCOND $SPECFILE | awk '{print $1}' | _rpm_cnfl_check | xargs)
1808                 local DEPS=$(rpm -q --specsrpm --requires $BCOND $SPECFILE | awk '{print $1}' | _rpm_prov_check | xargs)
1809         fi
1810
1811         if [ -n "$CNFL" ] || [ -n "$DEPS" ]; then
1812                 echo "fetch BuildRequires: install [$DEPS]; remove [$CNFL]"
1813                 update_shell_title "poldek: install [$DEPS]; remove [$CNFL]"
1814                 $SU_SUDO /usr/bin/poldek -q --update || $SU_SUDO /usr/bin/poldek -q --upa
1815         fi
1816         if [ -n "$CNFL" ]; then
1817                 update_shell_title "uninstall conflicting packages: $CNFL"
1818                 echo "Trying to uninstall conflicting packages ($CNFL):"
1819                 $SU_SUDO /usr/bin/poldek --noask --nofollow -ev $CNFL
1820         fi
1821
1822         while [ "$DEPS" ]; do
1823                         update_shell_title "install deps: $DEPS"
1824                         echo "Trying to install dependencies ($DEPS):"
1825                         local log=.${SPECFILE}_poldek.log
1826                         LANG=C $SU_SUDO /usr/bin/poldek --noask --caplookup -uGqQ $DEPS | tee $log
1827                         failed=$(awk '/^error:/{a=$2; sub(/^error: /, "", a); sub(/:$/, "", a); print a}' $log)
1828                         rm -f $log
1829                         local ok
1830                         if [ -n "$failed" ]; then
1831                                 for package in $failed; do
1832                                         spawn_sub_builder -bb $(depspecname $package) && ok="$ok $package"
1833                                 done
1834                                 DEPS="$ok"
1835                         else
1836                                 DEPS=""
1837                         fi
1838         done
1839 }
1840
1841 fetch_build_requires()
1842 {
1843         if [ "${FETCH_BUILD_REQUIRES}" != "yes" ]; then
1844                 return
1845         fi
1846
1847         update_shell_title "fetch build requires"
1848         if [ "$FETCH_BUILD_REQUIRES_RPMGETDEPS" = "yes" ] || [ "$FETCH_BUILD_REQUIRES_RPMSPECSRPM" = "yes" ]; then
1849                 install_build_requires_rpmdeps
1850                 return
1851         fi
1852
1853                 # XXX is this ugliest code written in human history still needed?
1854                 echo "All packages installed by fetch_build_requires() are written to:"
1855                 echo "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES"
1856                 echo ""
1857                 echo "If anything fails, you may get rid of them by executing:"
1858                 echo "poldek -e \`cat `pwd`/.${SPECFILE}_INSTALLED_PACKAGES\`"
1859                 echo ""
1860                 echo > `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1861                 for package_item in $(cat $SPECFILE | grep -B100000 ^%changelog|grep -v ^#|grep BuildRequires|grep -v ^-|sed -e "s/^.*BuildRequires://g"|awk '{print $1}'|sed -e s,perl\(,perl-,g -e s,::,-,g -e s,\(.*\),,g -e s,%{,,g -e s,},,g|grep -v OpenGL-devel|sed -e s,sh-utils,coreutils,g -e s,fileutils,coreutils,g -e s,textutils,coreutils,g -e s,kgcc_package,gcc,g -e s,\),,g)
1862                 do
1863                         package_item=$(echo $package_item|sed -e s,rpmbuild,rpm-build,g |sed -e s,__perl,perl,g |sed -e s,gasp,binutils-gasp,g -e s,binutils-binutils,binutils,g -e s,apxs,apache,g|sed -e s,apache\(EAPI\)-devel,apache-devel,g -e s,kernel-headers\(netfilter\),kernel-headers,g -e s,awk,mawk,g -e s,mmawk,mawk,g -e s,motif,openmotif,g -e s,openopenmotif,openmotif,g)
1864                         GO="yes"
1865                         package=$(basename "$package_item"|sed -e "s/}$//g")
1866                         COND_ARCH_TST=$(cat $SPECFILE|grep -B1 BuildRequires|grep -B1 $package|grep ifarch|sed -e "s/^.*ifarch//g")
1867                         mach=$(uname -m)
1868
1869                         COND_TST=`cat $SPECFILE|grep BuildRequires|grep "$package"`
1870                         if `echo $COND_TST|grep -q '^BuildRequires:'`; then
1871                                 if [ "$COND_ARCH_TST" != "" ] && [ "`echo $COND_ARCH_TST|sed -e "s/i.86/ix86/g"`" != "`echo $mach|sed -e "s/i.86/ix86/g"`" ]; then
1872                                         GO="yes"
1873                                 fi
1874                         # bcond:
1875                         else
1876                                 COND_NAME=`echo $COND_TST|sed -e s,:BuildRequires:.*$,,g`
1877                                 GO=""
1878                                 # %{without}
1879                                 if `echo $COND_TST|grep -q 'without_'`; then
1880                                         COND_NAME=`echo $COND_NAME|sed -e s,^.*without_,,g`
1881                                         if `echo $COND_TST|grep -q !`; then
1882                                                 COND_STATE="with"
1883                                         else
1884                                                 COND_STATE="wout"
1885                                         fi
1886                                         COND_WITH=`echo $AVAIL_BCONDS_WITH|grep "<$COND_NAME>"`
1887                                         COND_WITHOUT=`echo $AVAIL_BCONDS_WITHOUT|grep "<$COND_NAME>"`
1888                                         if [ -n "$COND_WITHOUT" ] || [ -z "$COND_WITH" ]; then
1889                                                 COND_ARGV="wout"
1890                                         else
1891                                                 COND_ARGV="with"
1892                                         fi
1893                                 # %{with}
1894                                 elif `echo $COND_TST|grep -q 'with_'`; then
1895                                         COND_NAME=`echo $COND_NAME|sed -e s,^.*with_,,g`
1896                                         if `echo $COND_TST|grep -q !`; then
1897                                                 COND_STATE="wout"
1898                                         else
1899                                                 COND_STATE="with"
1900                                         fi
1901                                         COND_WITH=`echo $AVAIL_BCONDS_WITH|grep "<$COND_NAME>"`
1902                                         COND_WITHOUT=`echo $AVAIL_BCONDS_WITHOUT|grep "<$COND_NAME>"`
1903                                         if [ -n "$COND_WITH" ] || [ -z "$COND_WITHOUT" ]; then
1904                                                 COND_ARGV="with"
1905                                         else
1906                                                 COND_ARGV="wout"
1907                                         fi
1908                                 fi
1909                                 RESULT="${COND_STATE}-${COND_ARGV}"
1910                                 case "$RESULT" in
1911                                         "with-wout" | "wout-with" )
1912                                                 GO=""
1913                                                 ;;
1914                                         "wout-wout" | "with-with" )
1915                                                 GO="yes"
1916                                                 ;;
1917                                         * )
1918                                                 echo "Action '$RESULT' was not defined for package '$package_item'"
1919                                                 GO="yes"
1920                                                 ;;
1921                                 esac
1922                         fi
1923
1924                         if [ "$GO" = "yes" ]; then
1925                                 if [ "`rpm -q $package|sed -e "s/$package.*/$package/g"`" != "$package" ]; then
1926                                         echo "Testing if $package has subrequirements..."
1927                                         run_poldek -t -i $package --dumpn=".$package-req.txt"
1928                                         if [ -f ".$package-req.txt" ]; then
1929                                                 for package_name in `cat ".$package-req.txt"|grep -v ^#`
1930                                                 do
1931                                                         if [ "$package_name" = "$package" ]; then
1932                                                                 echo "Installing BuildRequired package:\t$package_name"
1933                                                                 update_shell_title "Installing BuildRequired package: ${package_name}"
1934                                                                 install_required_packages $package
1935                                                         else
1936                                                                 echo "Installing (sub)Required package:\t$package_name"
1937                                                                 update_shell_title "Installing (sub)Required package: ${package_name}"
1938                                                                 install_required_packages $package_name
1939                                                         fi
1940                                                         case $? in
1941                                                                 0)
1942                                                                         INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
1943                                                                         echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1944                                                                         ;;
1945                                                                 *)
1946                                                                         echo "Attempting to run spawn sub - builder..."
1947                                                                         echo "Package installation failed:\t$package_name"
1948                                                                         run_sub_builder $package_name
1949                                                                         if [ $? -eq 0 ]; then
1950                                                                                 install_required_packages $package_name
1951                                                                                 case $? in
1952                                                                                         0)
1953                                                                                                 INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
1954                                                                                                 echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1955                                                                                                 ;;
1956                                                                                         *)
1957                                                                                                 NOT_INSTALLED_PACKAGES="$package_name $NOT_INSTALLED_PACKAGES"
1958                                                                                                 ;;
1959                                                                                 esac
1960                                                                         fi
1961                                                                         ;;
1962                                                         esac
1963                                                 done
1964                                                 rm -f ".$package-req.txt"
1965                                         else
1966                                                 echo "Attempting to run spawn sub - builder..."
1967                                                 echo "Package installation failed:\t$package"
1968                                                 run_sub_builder $package
1969                                                 if [ $? -eq 0 ]; then
1970                                                         install_required_packages $package
1971                                                         case $? in
1972                                                                 0)
1973                                                                         INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
1974                                                                         echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1975                                                                         ;;
1976                                                                 *)
1977                                                                         NOT_INSTALLED_PACKAGES="$package_name $NOT_INSTALLED_PACKAGES"
1978                                                                         ;;
1979                                                         esac
1980                                                 fi
1981                                         fi
1982                                 else
1983                                         echo "Package $package is already installed. BuildRequirement satisfied."
1984                                 fi
1985                         fi
1986                 done
1987                 if [ "$NOT_INSTALLED_PACKAGES" != "" ]; then
1988                         echo >&2 "Unable to install following packages and their dependencies:"
1989                         for pkg in "$NOT_INSTALLED_PACKAGES"
1990                         do
1991                                 echo $pkg
1992                         done
1993                         remove_build_requires
1994                         exit 8
1995                 fi
1996 }
1997
1998 init_rpm_dir() {
1999         TOP_DIR=$(eval $RPM $RPMOPTS --eval '%{_topdir}')
2000         CVSROOT=":pserver:cvs@$CVS_SERVER:/cvsroot"
2001
2002         echo "Initializing rpm directories to $TOP_DIR from $CVSROOT"
2003         mkdir -p $TOP_DIR/{RPMS,BUILD,SRPMS}
2004         cd $TOP_DIR
2005         $CVS_COMMAND -d $CVSROOT co packages/{.cvsignore,rpm.groups,dropin,mirrors,md5,adapter{,.awk},fetchsrc_request,builder,{relup,compile,repackage}.sh}
2006
2007         init_builder
2008
2009         echo "To checkout *all* .spec files (read-only):"
2010         echo "- run cvs co SPECS"
2011
2012         echo "To checkout *all* packages:"
2013         echo "- run cvs up -dP in $TOP_DIR/packages dir"
2014
2015         echo ""
2016         echo "To commit with your developer account:"
2017         echo "- edit $TOP_DIR/packages/CVS/Root"
2018 }
2019
2020 get_greed_sources() {
2021         CVSROOT=":pserver:cvs@$CVS_SERVER:/cvsroot"
2022         if [ -n "BE_VERBOSE" ]; then
2023                 echo "Try greed download: $1 from: $CVSROOT"
2024         fi
2025         $CVS_COMMAND -d $CVSROOT get SOURCES/$1
2026         if [ $? != 0 ]; then
2027                 Exit_error err_no_source_in_repo $1
2028         fi
2029
2030 }
2031
2032 # remove entries from CVS/Entries
2033 cvs_entry_remove() {
2034         local cvsdir="$1"; shift
2035         if [ ! -d "$cvsdir" ]; then
2036                 echo >&2 "cvs_entry_remove: $cvsdir is not a directory"
2037                 exit 1
2038         fi
2039
2040         for file in "$@"; do
2041                 rm -f $cvsdir/CVS/Entries.new || return 1
2042                 awk -ve="${file##*/}" -F/ '$2 != e {print}' $cvsdir/CVS/Entries > $cvsdir/CVS/Entries.new || return 1
2043                 mv -f $cvsdir/CVS/Entries.new $cvsdir/CVS/Entries || return 1
2044         done
2045         return 0
2046 }
2047
2048 mr_proper() {
2049         init_builder
2050         NOCVSSPEC="yes"
2051         DONT_PRINT_REVISION="yes"
2052         get_spec
2053         parse_spec
2054
2055         # remove from CVS/Entries
2056         cvs_entry_remove $PACKAGE_DIR $SPECFILE $SOURCES $PATCHES
2057
2058         # remove spec and sources
2059         $RPMBUILD --clean --rmsource --rmspec --nodeps --define \'_specdir $PACKAGE_DIR\' --define \'_sourcedir $PACKAGE_DIR\' $SPECFILE
2060 }
2061
2062 #---------------------------------------------
2063 # main()
2064
2065 if [ $# = 0 ]; then
2066         usage
2067         exit 1
2068 fi
2069
2070 while [ $# -gt 0 ]; do
2071         case "${1}" in
2072                 -5 | --update-md5)
2073                         COMMAND="update_md5"
2074                         NODIST="yes"
2075                         NOCVSSPEC="yes"
2076                         shift ;;
2077                 -a5 | --add-md5 )
2078                         COMMAND="update_md5"
2079                         NODIST="yes"
2080                         NOCVS="yes"
2081                         NOCVSSPEC="yes"
2082                         ADD5="yes"
2083                         shift ;;
2084                 -n5 | --no-md5 )
2085                         NO5="yes"
2086                         shift ;;
2087                 -D | --debug )
2088                         DEBUG="yes"; shift ;;
2089                 -V | --version )
2090                         COMMAND="version"; shift ;;
2091                 --short-version )
2092                         COMMAND="short-version"; shift ;;
2093                 -a | --as_anon )
2094                         CVSROOT=":pserver:cvs@$CVS_SERVER:/cvsroot"; shift ;;
2095                 -b | -ba | --build )
2096                         COMMAND="build"; shift ;;
2097                 -bb | --build-binary )
2098                         COMMAND="build-binary"; shift ;;
2099                 -bc )
2100                         COMMAND="build-build"; shift ;;
2101                 -bi )
2102                         COMMAND="build-install"; shift ;;
2103                 -bl )
2104                         COMMAND="build-list"; shift ;;
2105                 -bp | --build-prep )
2106                         COMMAND="build-prep"; shift ;;
2107                 -bs | --build-source )
2108                         COMMAND="build-source"; shift ;;
2109                 -B | --branch )
2110                         COMMAND="branch"; shift; TAG="${1}"; shift;;
2111                 -c | --clean )
2112                         CLEAN="--clean"; shift ;;
2113                 -cf | --cvs-force )
2114                         CVS_FORCE="-F"; shift;;
2115                 -d | --cvsroot )
2116                         shift; CVSROOT="${1}"; shift ;;
2117                 -g | --get )
2118                         COMMAND="get"; shift ;;
2119                 -h | --help )
2120                         COMMAND="usage"; shift ;;
2121                 --http )
2122                         PROTOCOL="http"; shift ;;
2123                 -l | --logtofile )
2124                         shift; LOGFILE="${1}"; shift ;;
2125                 -ni| --nice )
2126                         shift; DEF_NICE_LEVEL=${1}; shift ;;
2127                 -ske | --skip-existing-files)
2128                         SKIP_EXISTING_FILES="yes"; shift ;;
2129                 -m | --mr-proper )
2130                         COMMAND="mr-proper"; shift ;;
2131                 -nc | --no-cvs )
2132                         NOCVS="yes"; shift ;;
2133                 -ncs | --no-cvs-specs )
2134                         NOCVSSPEC="yes"; shift ;;
2135                 -nd | --no-distfiles )
2136                         NODIST="yes"; shift ;;
2137                 -nm | --no-mirrors )
2138                         NOMIRRORS="yes"; shift ;;
2139                 -nu | --no-urls )
2140                         NOURLS="yes"; shift ;;
2141                 -ns | --no-srcs )
2142                         NOSRCS="yes"; shift ;;
2143                 -ns0 | --no-source0 )
2144                         NOSOURCE0="yes"; shift ;;
2145                 -nn | --no-net )
2146                         NOCVS="yes"
2147                         NOCVSSPEC="yes"
2148                         NODIST="yes"
2149                         NOMIRRORS="yes"
2150                         NOURLS="yes"
2151                         NOSRCS="yes"
2152                         ALWAYS_CVSUP="no"
2153                         shift;;
2154                 -pm | --prefer-mirrors )
2155                         PREFMIRRORS="yes"
2156                         shift;;
2157                 --no-init )
2158                         NOINIT="yes"
2159                         shift;;
2160                 --opts )
2161                         shift; RPMOPTS="${RPMOPTS} ${1}"; shift ;;
2162                 --nopatch | -np )
2163                         shift; RPMOPTS="${RPMOPTS} --define \"patch${1} : ignoring patch${1}; exit 1; \""; shift ;;
2164                 --topdir)
2165                         RPMOPTS="${RPMOPTS} --define \"_topdir $2\""
2166                         shift 2
2167                         ;;
2168                 --with | --without )
2169                         case $GROUP_BCONDS in
2170                                 "yes")
2171                                         COND=${1}
2172                                         shift
2173                                         # XXX: broken: ./builder -bb ucspi-tcp.spec --without mysql
2174                                         while ! `echo ${1}|grep -qE '(^-|spec)'`
2175                                         do
2176                                                 BCOND="$BCOND $COND $1"
2177                                                 shift
2178                                         done;;
2179                                 "no")
2180                                         if [[ "$2" = *,* ]]; then
2181                                                 for a in $(echo "$2" | tr , ' '); do
2182                                                         BCOND="$BCOND $1 $a"
2183                                                 done
2184                                         else
2185                                                 BCOND="$BCOND $1 $2"
2186                                         fi
2187                                         shift 2 ;;
2188                         esac
2189                         ;;
2190                 --target )
2191                         shift; TARGET="${1}"; shift ;;
2192                 --target=* )
2193                         TARGET=$(echo "${1}" | sed 's/^--target=//'); shift ;;
2194                 -q | --quiet )
2195                         QUIET="--quiet"; shift ;;
2196                 --date )
2197                         CVSDATE="${2}"; shift 2 ;;
2198                 -r | --cvstag )
2199                         CVSTAG="$2"
2200                         shift 2
2201                         ;;
2202                 -A)
2203                         shift
2204                         CVSTAG="HEAD"
2205                         ;;
2206                 -R | --fetch-build-requires)
2207                         FETCH_BUILD_REQUIRES="yes"
2208                         NOT_INSTALLED_PACKAGES=
2209                         shift ;;
2210                 -RB | --remove-build-requires)
2211                         REMOVE_BUILD_REQUIRES="nice"
2212                         shift ;;
2213                 -FRB | --force-remove-build-requires)
2214                         REMOVE_BUILD_REQUIRES="force"
2215                         shift ;;
2216                 -sc | --sources-cvs)
2217                         COMMAND="list-sources-cvs"
2218                         shift ;;
2219                 -sd | --sources-distfiles)
2220                         COMMAND="list-sources-distfiles"
2221                         shift ;;
2222                 -sdp | --sources-distfiles-paths)
2223                         COMMAND="list-sources-distfiles-paths"
2224                         shift ;;
2225                 -sf | --sources-files)
2226                         COMMAND="list-sources-files"
2227                         shift ;;
2228                 -sp | --sources-paths)
2229                         COMMAND="list-sources-local-paths"
2230                         shift ;;
2231                 -su | --sources-urls)
2232                         COMMAND="list-sources-urls"
2233                         shift ;;
2234                 -Tvs | --tag-version-stable )
2235                         COMMAND="tag"
2236                         TAG="STABLE"
2237                         TAG_VERSION="yes"
2238                         shift;;
2239                 -Ts | --tag-stable )
2240                         COMMAND="tag"
2241                         TAG="STABLE"
2242                         TAG_VERSION="no"
2243                         shift;;
2244                 -Tv | --tag-version )
2245                         COMMAND="tag"
2246                         TAG=""
2247                         TAG_VERSION="yes"
2248                         shift;;
2249                 -Tp | --tag-prefix )
2250                         TAG_PREFIX="$2"
2251                         shift 2;;
2252                 -tt | --test-tag )
2253                         TEST_TAG="yes"
2254                         shift;;
2255                 -T | --tag )
2256                         COMMAND="tag"
2257                         shift
2258                         TAG="$1"
2259                         TAG_VERSION="no"
2260                         shift;;
2261                 -ir | --integer-release-only )
2262                         INTEGER_RELEASE="yes"
2263                         shift;;
2264                 -U | --update )
2265                         COMMAND="update_md5"
2266                         UPDATE="yes"
2267                         NOCVSSPEC="yes"
2268                         NODIST="yes"
2269                         shift ;;
2270                 -Upi | --update-poldek-indexes )
2271                         UPDATE_POLDEK_INDEXES="yes"
2272                         shift ;;
2273                 --init-rpm-dir)
2274                         COMMAND="init_rpm_dir"
2275                         shift ;;
2276                 --use-greed-sources )
2277                         GREEDSRC="1"
2278                         shift;;
2279                 -u | --try-upgrade )
2280                         TRY_UPGRADE="1"; shift ;;
2281                 --upgrade-version )
2282                         shift; UPGRADE_VERSION="$1"; shift;;
2283                 -un | --try-upgrade-with-float-version )
2284                         TRY_UPGRADE="1"; FLOAT_VERSION="1"; shift ;;
2285                 -v | --verbose )
2286                         BE_VERBOSE="1"; shift ;;
2287                 --define)
2288                         shift
2289                         MACRO="${1}"
2290                         shift
2291                         if echo "${MACRO}" | grep -q '\W'; then
2292                                 RPMOPTS="${RPMOPTS} --define \"${MACRO}\""
2293                         else
2294                                 VALUE="${1}"
2295                                 shift
2296                                 RPMOPTS="${RPMOPTS} --define \"${MACRO} ${VALUE}\""
2297                         fi
2298                         ;;
2299                 --alt_kernel)
2300                         shift
2301                         RPMOPTS="${RPMOPTS} --define \"alt_kernel $1\""
2302                         shift
2303                         ;;
2304                 --short-circuit)
2305                         RPMBUILDOPTS="${RPMBUILDOPTS} --short-circuit"
2306                         shift
2307                         ;;
2308                 --show-bconds | -show-bconds | -print-bconds | --print-bconds | -display-bconds | --display-bconds )
2309                         COMMAND="show_bconds"
2310                         shift
2311                         ;;
2312                 --show-bcond-args)
2313                         COMMAND="show_bcond_args"
2314                         shift
2315                         ;;
2316                 --show-avail-bconds)
2317                         COMMAND="show_avail_bconds"
2318                         shift
2319                         ;;
2320                 --nodeps)
2321                         shift
2322                         RPMOPTS="${RPMOPTS} --nodeps"
2323                         ;;
2324                 -debug)
2325                         RPMBUILDOPTS="${RPMBUILDOPTS} -debug"; shift
2326                         ;;
2327                 -*)
2328                         Exit_error err_invalid_cmdline "$1"
2329                         ;;
2330                 *)
2331                         SPECFILE=${1%/}; shift
2332                         # check if specname was passed as specname:cvstag
2333                         if [ "${SPECFILE##*:}" != "${SPECFILE}" ]; then
2334                                 CVSTAG="${SPECFILE##*:}"
2335                                 SPECFILE="${SPECFILE%%:*}"
2336                         fi
2337                         # always have SPECFILE ending with .spec extension
2338                         SPECFILE=${SPECFILE%%.spec}.spec
2339                         ASSUMED_NAME=$(basename ${SPECFILE%%.spec})
2340         esac
2341 done
2342
2343 [ -d "$ASSUMED_NAME" ] && CVS_ENTRIES="$ASSUMED_NAME/CVS/Entries" || CVS_ENTRIES="CVS/Entries"
2344 if [ -f "$CVS_ENTRIES" ] && [ -z "$CVSTAG" ]; then
2345         CVSTAG=$(awk -vSPECFILE=$(basename $SPECFILE) -F/ '$2 == SPECFILE && $6 ~ /^T/{print substr($6, 2)}' ${CVS_ENTRIES})
2346         if [ "$CVSTAG" ]; then
2347                 echo >&2 "builder: Sticky tag $CVSTAG active. Use -r TAGNAME to override."
2348         fi
2349 elif [ "$CVSTAG" = "HEAD" ]; then
2350         # assume -r HEAD is same as -A
2351         CVSTAG=""
2352 fi
2353
2354 if [ "$CVSTAG" ]; then
2355         # pass $CVSTAG used by builder to rpmbuild too, so specs could use it
2356         RPMOPTS="$RPMOPTS --define \"_cvstag $CVSTAG\""
2357 fi
2358
2359 if [ -n "$DEBUG" ]; then
2360         set -x
2361         set -v
2362 fi
2363
2364 if [ -n "$TARGET" ]; then
2365         case "$RPMBUILD" in
2366                 "rpmbuild")
2367                         TARGET_SWITCH="--target $TARGET" ;;
2368                 "rpm")
2369                         TARGET_SWITCH="--target=$TARGET" ;;
2370         esac
2371 fi
2372
2373 if [ "$SCHEDTOOL" != "no" ]; then
2374         NICE_COMMAND="$SCHEDTOOL"
2375 else
2376         NICE_COMMAND="nice -n ${DEF_NICE_LEVEL}"
2377 fi
2378
2379 update_shell_title "$COMMAND"
2380 case "$COMMAND" in
2381         "show_bconds")
2382                 init_builder
2383                 if [ -n "$SPECFILE" ]; then
2384                         get_spec > /dev/null
2385                         parse_spec
2386                         set_bconds_values
2387                         display_bconds
2388                 fi
2389                 ;;
2390         "show_bcond_args")
2391                 init_builder
2392                 if [ -n "$SPECFILE" ]; then
2393                         get_spec > /dev/null
2394                         parse_spec
2395                         set_bconds_values
2396                         echo "$BCOND"
2397                 fi
2398                 ;;
2399         "show_avail_bconds")
2400                 init_builder
2401                 if [ -n "$SPECFILE" ]; then
2402                         get_spec > /dev/null
2403                         parse_spec
2404                         local bcond_avail=$(find_spec_bcond $SPECFILE)
2405                         local opt bcond bconds
2406                         for opt in $bcond_avail; do
2407                                 case "$opt" in
2408                                 without_*)
2409                                         bcond=${opt#without_}
2410                                         bconds="$bconds $bcond"
2411                                         ;;
2412                                 with_*)
2413                                         bcond=${opt#with_}
2414                                         bconds="$bconds $bcond"
2415                                         ;;
2416                                 *)
2417                                         echo >&2 "ERROR: unexpected '$opt' in show_avail_bconds"
2418                                         exit 1
2419                                         ;;
2420                                 esac
2421                         done
2422                         echo $bconds
2423                 fi
2424
2425                 ;;
2426         "build" | "build-binary" | "build-source" | "build-prep" | "build-build" | "build-install" | "build-list")
2427                 init_builder
2428                 if [ -n "$SPECFILE" ]; then
2429                         # display SMP make flags if set
2430                         smp_mflags=$(rpm -E %{?_smp_mflags})
2431                         if [ "$smp_mflags" ]; then
2432                                 echo "builder: SMP make flags are set to $smp_mflags"
2433                         fi
2434
2435                         get_spec
2436                         parse_spec
2437                         set_bconds_values
2438                         display_bconds
2439                         display_branches
2440                         if [ "$COMMAND" != "build-source" ]; then
2441                                 check_buildarch
2442                         fi
2443                         fetch_build_requires
2444                         if [ "$INTEGER_RELEASE" = "yes" ]; then
2445                                 echo "Checking release $PACKAGE_RELEASE..."
2446                                 if echo $PACKAGE_RELEASE | grep -q '^[^.]*\.[^.]*$' 2>/dev/null ; then
2447                                         Exit_error err_fract_rel "$PACKAGE_RELEASE"
2448                                 fi
2449                         fi
2450
2451                         # ./builder -bs test.spec -r AC-branch -Tp auto-ac- -tt
2452                         if [ -n "$TEST_TAG" ]; then
2453                                 local TAGVER=`make_tagver`
2454                                 echo "Searching for tag $TAGVER..."
2455                                 TAGREL=$($CVS_COMMAND status -v $SPECFILE | grep -E "^[[:space:]]*${TAGVER}[[[:space:]]" | sed -e 's#.*(revision: ##g' -e 's#).*##g')
2456                                 if [ -n "$TAGREL" ]; then
2457                                         Exit_error err_tag_exists "$TAGVER" "$TAGREL"
2458                                 fi
2459
2460                                 # - do not allow to build from HEAD when XX-branch exists
2461                                 TREE_PREFIX=$(echo "$TAG_PREFIX" | sed -e 's#^auto-\([a-zA-Z]\+\)-.*#\1#g')
2462                                 if [ "$TREE_PREFIX" != "$TAG_PREFIX" ]; then
2463                                         TAG_BRANCH="${TREE_PREFIX}-branch"
2464                                         TAG_STATUS=$($CVS_COMMAND status -v $SPECFILE | grep -Ei "${TAG_BRANCH}.+(branch: [0-9.]+)")
2465                                         if [ -n "$TAG_STATUS" -a "$CVSTAG" = "HEAD" ]; then
2466                                                 Exit_error err_branch_exists "$TAG_STATUS"
2467                                         fi
2468                                 fi
2469
2470                         fi
2471
2472                         if [ -n "$NOSOURCE0" ] ; then
2473                                 SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2474                         fi
2475                         try_upgrade
2476                         case $? in
2477                                 0)
2478                                         get_files $SOURCES $PATCHES
2479                                         check_md5 $SOURCES
2480                                         ;;
2481                                 *)
2482                                         NODIST="yes" get_files $SOURCES $PATCHES
2483                                         update_md5 $SOURCES
2484                                         ;;
2485                         esac
2486                         build_package
2487                         if [ "$UPDATE_POLDEK_INDEXES" = "yes" -a "$COMMAND" != "build-prep" ]; then
2488                                 run_poldek --sdir="${POLDEK_INDEX_DIR}" --mkidxz
2489                         fi
2490                         remove_build_requires
2491                 else
2492                         Exit_error err_no_spec_in_cmdl
2493                 fi
2494                 ;;
2495         "branch" )
2496                 init_builder
2497                 if [ -n "$SPECFILE" ]; then
2498                         get_spec
2499                         parse_spec
2500                         # don't fetch sources from remote locations
2501                         new_SOURCES=""
2502                         for file in $SOURCES; do
2503                                 [ -n "`src_md5 $file`" ] && continue
2504                                 new_SOURCES="$new_SOURCES $file"
2505                         done
2506                         SOURCES="$new_SOURCES"
2507                         get_files $SOURCES $PATCHES
2508                         check_md5 $SOURCES
2509                         branch_files $TAG $SOURCES $PATCHES $ICONS
2510                 else
2511                         Exit_error err_no_spec_in_cmdl
2512                 fi
2513                 ;;
2514         "get" )
2515                 init_builder
2516                 if [ -n "$SPECFILE" ]; then
2517                         get_spec
2518                         parse_spec
2519
2520                         if [ -n "$NOSOURCE0" ] ; then
2521                                 SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2522                         fi
2523                         get_files $SOURCES $PATCHES
2524                         check_md5 $SOURCES
2525                 else
2526                         Exit_error err_no_spec_in_cmdl
2527                 fi
2528                 ;;
2529         "update_md5" )
2530                 init_builder
2531                 if [ -n "$SPECFILE" ]; then
2532                         get_spec
2533                         parse_spec
2534
2535                         if [ -n "$NOSOURCE0" ] ; then
2536                                 SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2537                         fi
2538                         update_md5 $SOURCES
2539                 else
2540                         Exit_error err_no_spec_in_cmdl
2541                 fi
2542                 ;;
2543         "tag" )
2544                 NOURLS=1
2545                 NODIST="yes"
2546                 init_builder
2547                 if [ -n "$SPECFILE" ]; then
2548                         get_spec
2549                         parse_spec
2550
2551                         # don't fetch sources from remote locations
2552                         new_SOURCES=""
2553                         for file in $SOURCES; do
2554                                 [ -n "`src_md5 $file`" ] && continue
2555                                 new_SOURCES="$new_SOURCES $file"
2556                         done
2557                         SOURCES="$new_SOURCES"
2558                         get_files $SOURCES $PATCHES
2559                         check_md5 $SOURCES
2560                         tag_files $SOURCES $PATCHES $ICONS
2561                 else
2562                         Exit_error err_no_spec_in_cmdl
2563                 fi
2564                 ;;
2565         "mr-proper" )
2566                 mr_proper
2567                 ;;
2568         "list-sources-files" )
2569                 init_builder
2570                 NOCVSSPEC="yes"
2571                 DONT_PRINT_REVISION="yes"
2572                 get_spec
2573                 parse_spec
2574                 for SAP in $SOURCES $PATCHES; do
2575                         echo $SAP | awk '{gsub(/.*\//,"") ; print}'
2576                 done
2577                 ;;
2578         "list-sources-urls" )
2579                 init_builder
2580                 NOCVSSPEC="yes"
2581                 DONT_PRINT_REVISION="yes"
2582                 get_spec
2583                 parse_spec
2584                 SAPS="$SOURCES $PATCHES"
2585                 for SAP in $SAPS ; do
2586                         echo $SAP
2587                 done
2588                 ;;
2589         "list-sources-local-paths" )
2590                 init_builder
2591                 NOCVSSPEC="yes"
2592                 DONT_PRINT_REVISION="yes"
2593                 get_spec
2594                 parse_spec
2595                 for SAP in $SOURCES $PATCHES; do
2596                         echo $PACKAGE_DIR/$(echo $SAP | awk '{gsub(/.*\//,"") ; print }')
2597                 done
2598                 ;;
2599         "list-sources-distfiles-paths" )
2600                 init_builder
2601                 NOCVSSPEC="yes"
2602                 DONT_PRINT_REVISION="yes"
2603                 get_spec
2604                 parse_spec
2605                 for SAP in $SOURCES $PATCHES; do
2606                         if [ -n "$(src_md5 "$SAP")" ]; then
2607                                 distfiles_path "$SAP"
2608                         fi
2609                 done
2610                 ;;
2611         "list-sources-distfiles" )
2612                 init_builder
2613                 NOCVSSPEC="yes"
2614                 DONT_PRINT_REVISION="yes"
2615                 get_spec
2616                 parse_spec
2617                 for SAP in $SOURCES $PATCHES; do
2618                         if [ -n "$(src_md5 "$SAP")" ]; then
2619                                 distfiles_url "$SAP"
2620                         fi
2621                 done
2622                 ;;
2623         "list-sources-cvs" )
2624                 init_builder
2625 #               NOCVSSPEC="yes"
2626                 DONT_PRINT_REVISION="yes"
2627                 get_spec
2628                 parse_spec
2629                 for SAP in $SOURCES $PATCHES; do
2630                         if [ -z "$(src_md5 "$SAP")" ]; then
2631                                 echo $SAP | awk '{gsub(/.*\//,"") ; print}'
2632                         fi
2633                 done
2634                 ;;
2635         "init_rpm_dir")
2636                 init_rpm_dir
2637                 ;;
2638         "usage" )
2639                 usage
2640                 ;;
2641         "short-version" )
2642                 echo "$VERSION"
2643                 ;;
2644         "version" )
2645                 echo "$VERSIONSTRING"
2646                 ;;
2647 esac
2648 if [ -f "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES" -a "$REMOVE_BUILD_REQUIRES" != "" ]; then
2649         rm "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES"
2650 fi
2651 cd "$__PWD"
2652
2653 # vi:syntax=sh:ts=4:sw=4:noet
This page took 0.285511 seconds and 4 git commands to generate.