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