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