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