]> TLD Linux GIT Repositories - packages/rpm-build-tools.git/blob - builder.sh
- auto tagging is history, support pkgrevs
[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         echo "$result" | grep -q -E "^OK$" && return 0
1512         echo "$result" | grep -q -E "^EXISTS$" && Exit_error err_tag_exists "$_tag"
1513         Exit_error err_pkgrev_set
1514 }
1515
1516 branch_files() {
1517         TAG=$1
1518         echo "Git branch: $TAG"
1519         shift
1520
1521         if [ -n "$DEBUG" ]; then
1522                 set -x
1523                 set -v
1524         fi
1525
1526         local OPTIONS="branch $CVS_FORCE"
1527
1528         cd "$PACKAGE_DIR"
1529         git $OPTIONS $TAG || exit
1530 }
1531
1532
1533 # this function should exit early if package can't be built for this arch
1534 # this avoids unneccessary BR filling.
1535 check_buildarch() {
1536         local out ret
1537         out=$(minirpm --short-circuit -bp --define "'prep exit 0'" --nodeps $SPECFILE 2>&1)
1538         ret=$?
1539         if [ $ret -ne 0 ]; then
1540                 echo >&2 "$out"
1541                 exit $ret
1542         fi
1543 }
1544
1545 # from relup.sh
1546 set_release() {
1547         local specfile="$1"
1548         local rel="$2"
1549         local newrel="$3"
1550         sed -i -e "
1551                 s/^\(%define[ \t]\+_\?rel[ \t]\+\)$rel\$/\1$newrel/
1552                 s/^\(Release:[ \t]\+\)$rel\$/\1$newrel/
1553         " $specfile
1554 }
1555
1556 set_version() {
1557         local specfile="$1"
1558         local ver="$2" subver=$ver
1559         local newver="$3" newsubver=$newver
1560
1561         # try handling subver, everything that's not numeric-dotted in version
1562         if grep -Eq '%define\s+subver' $specfile; then
1563                 subver=$(echo "$ver" | sed -re 's,^[0-9.]+,,')
1564                 ver=${ver%$subver}
1565                 newsubver=$(echo "$newver" | sed -re 's,^[0-9.]+,,')
1566                 newver=${newver%$newsubver}
1567         fi
1568         sed -i -e "
1569                 s/^\(%define[ \t]\+_\?ver[ \t]\+\)$ver\$/\1$newver/
1570                 s/^\(%define[ \t]\+subver[ \t]\+\)$subver\$/\1$newsubver/
1571                 s/^\(Version:[ \t]\+\)$ver\$/\1$newver/
1572         " $specfile
1573 }
1574
1575 try_upgrade() {
1576         if [ -n "$TRY_UPGRADE" ]; then
1577                 local TNOTIFY TNEWVER TOLDVER
1578                 update_shell_title "build_package: try_upgrade"
1579
1580                 cd "$PACKAGE_DIR"
1581                 
1582                 if [ "$UPGRADE_VERSION" ]; then
1583                         TNEWVER=$UPGRADE_VERSION
1584                 else
1585                         if [ -n "$FLOAT_VERSION" ]; then
1586                                 TNOTIFY=$($APPDIR/pldnotify.awk ${BE_VERBOSE:+-vDEBUG=1} $SPECFILE -n) || exit 1
1587                         else
1588                                 TNOTIFY=$($APPDIR/pldnotify.awk ${BE_VERBOSE:+-vDEBUG=1} $SPECFILE) || exit 1
1589                         fi
1590
1591                         # pldnotify.awk does not set exit codes, but it has match for ERROR
1592                         # in output which means so.
1593                         if [[ "$TNOTIFY" = *ERROR* ]]; then
1594                                 echo >&2 "$TNOTIFY"
1595                                 exit 1
1596                         fi
1597
1598                         TNEWVER=$(echo $TNOTIFY | awk '{ match($4,/\[NEW\]/); print $5 }')
1599                 fi
1600
1601                 if [ -n "$TNEWVER" ]; then
1602                         TOLDVER=`echo $TNOTIFY | awk '{ print $3; }'`
1603                         echo "New version found, updating spec file from $TOLDVER to version $TNEWVER"
1604                         if [ "$REVERT_BROKEN_UPGRADE" = "yes" ]; then
1605                                 cp -f $SPECFILE $SPECFILE.bak
1606                         fi
1607                         chmod +w $SPECFILE
1608                         set_release $SPECFILE $PACKAGE_RELEASE 1
1609                         set_version $SPECFILE $PACKAGE_VERSION $TNEWVER
1610                         parse_spec
1611                         if [ "$PACKAGE_VERSION" != "$TNEWVER" ]; then
1612                                 echo >&2 "Upgrading version failed, you need to update spec yourself"
1613                                 exit 1
1614                         fi
1615                         return 1
1616                 fi
1617         fi
1618         return 0
1619 }
1620
1621 build_package() {
1622         update_shell_title "build_package"
1623         if [ -n "$DEBUG" ]; then
1624                 set -x
1625                 set -v
1626         fi
1627
1628         cd "$PACKAGE_DIR"
1629
1630         case "$COMMAND" in
1631                 build )
1632                         BUILD_SWITCH="-ba" ;;
1633                 build-binary )
1634                         BUILD_SWITCH="-bb" ;;
1635                 build-source )
1636                         BUILD_SWITCH="-bs --nodeps" ;;
1637                 build-prep )
1638                         BUILD_SWITCH="-bp --nodeps" ;;
1639                 build-build )
1640                         BUILD_SWITCH="-bc" ;;
1641                 build-install )
1642                         BUILD_SWITCH="-bi" ;;
1643                 build-list )
1644                         BUILD_SWITCH="-bl" ;;
1645
1646         esac
1647
1648         update_shell_title "build_package: $COMMAND"
1649         local logfile retval
1650         if [ -n "$LOGFILE" ]; then
1651                 logfile=`eval echo $LOGFILE`
1652                 if [ -d "$logfile" ]; then
1653                         echo "Log file $logfile is a directory."
1654                         echo "Parse error in the spec?"
1655                         Exit_error err_build_fail
1656                 fi
1657                 if [ -n "$LASTLOG_FILE" ]; then
1658                         echo "LASTLOG=$logfile" > $LASTLOG_FILE
1659                 fi
1660         fi
1661
1662         local specdir=$(insert_gitlog $SPECFILE)
1663         # FIXME: eval here is exactly why?
1664         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
1665         retval=$?
1666         rm -r $specdir
1667
1668         if [ -n "$logfile" ] && [ -n "$LOGDIROK" ] && [ -n "$LOGDIRFAIL" ]; then
1669                 if [ "$retval" -eq "0" ]; then
1670                         mv $logfile $LOGDIROK
1671                 else
1672                         mv $logfile $LOGDIRFAIL
1673                 fi
1674         fi
1675
1676         if [ "$retval" -ne "0" ]; then
1677                 if [ -n "$TRY_UPGRADE" ]; then
1678                         echo "\nUpgrade package to new version failed."
1679                         if [ "$REVERT_BROKEN_UPGRADE" = "yes" ]; then
1680                                 echo "Restoring old spec file."
1681                                 mv -f $SPECFILE.bak $SPECFILE
1682                         fi
1683                         echo ""
1684                 fi
1685                 Exit_error err_build_fail
1686         fi
1687         unset BUILD_SWITCH
1688 }
1689
1690 nourl() {
1691         echo "$@" | sed 's#\<\(ftp\|http\|https\|cvs\|svn\)://[^ ]*/##g'
1692 }
1693
1694 install_required_packages() {
1695         run_poldek -vi $1
1696         return $?
1697 }
1698
1699 find_spec_bcond() { # originally from /usr/lib/rpm/find-spec-bcond
1700         local SPEC="$1"
1701         awk -F"\n" '
1702         /^%changelog/ { exit }
1703         /^%bcond_with/{
1704                 match($0, /bcond_with(out)?[ \t]+[_a-zA-Z0-9]+/);
1705                 bcond = substr($0, RSTART + 6, RLENGTH - 6);
1706                 gsub(/[ \t]+/, "_", bcond);
1707                 print bcond
1708         }' $SPEC | LC_ALL=C sort -u
1709 }
1710
1711 process_bcondrc() {
1712         # expand bconds from ~/.bcondrc
1713         # The file structure is like gentoo's package.use:
1714         # ---
1715         # * -selinux
1716         # samba -mysql -pgsql
1717         # w32codec-installer license_agreement
1718         # php +mysqli
1719         # ---
1720         if [ -f $HOME/.bcondrc ] || ([ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ]); then
1721                 :
1722         else
1723                 return
1724         fi
1725
1726         SN=${SPECFILE%%\.spec}
1727
1728         local bcondrc=$HOME/.bcondrc
1729         [ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ] && bcondrc=$HOME_ETC/.bcondrc
1730
1731         local bcond_avail=$(find_spec_bcond $SPECFILE)
1732
1733         while read pkg flags; do
1734                 # ignore comments
1735                 [[ "$pkg" == \#* ]] && continue
1736
1737                 # any package or current package?
1738                 if [ "$pkg" = "*" ] || [ "$pkg" = "$PACKAGE_NAME" ] || [ "$pkg" = "$SN" ]; then
1739                         for flag in $flags; do
1740                                 local opt=${flag#[+-]}
1741
1742                                 # use only flags which are in this package.
1743                                 if [[ $bcond_avail = *${opt}* ]]; then
1744                                         if [[ $flag = -* ]]; then
1745                                                 if [[ $BCOND != *--with?${opt}* ]]; then
1746                                                         BCOND="$BCOND --without $opt"
1747                                                 fi
1748                                         else
1749                                                 if [[ $BCOND != *--without?${opt}* ]]; then
1750                                                         BCOND="$BCOND --with $opt"
1751                                                 fi
1752                                         fi
1753                                 fi
1754                         done
1755                 fi
1756         done < $bcondrc
1757         update_shell_title "parse ~/.bcondrc: DONE!"
1758 }
1759
1760 set_bconds_values() {
1761         update_shell_title "set bcond values"
1762
1763         AVAIL_BCONDS_WITHOUT=""
1764         AVAIL_BCONDS_WITH=""
1765
1766         if grep -Eq '^# *_with' ${SPECFILE}; then
1767                 echo >&2 "ERROR: This spec has old style bconds."
1768                 exit 1
1769         fi
1770
1771         if ! grep -q '^%bcond' ${SPECFILE}; then
1772                 return
1773         fi
1774
1775         local bcond_avail=$(find_spec_bcond $SPECFILE)
1776         process_bcondrc "$SPECFILE"
1777
1778         update_shell_title "parse bconds"
1779
1780         local opt bcond
1781         for opt in $bcond_avail; do
1782                 case "$opt" in
1783                 without_*)
1784                         bcond=${opt#without_}
1785                         case "$BCOND" in
1786                         *--without?${bcond}*)
1787                                 AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT <$bcond>"
1788                                 ;;
1789                         *)
1790                                 AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT $bcond"
1791                                 ;;
1792                         esac
1793                         ;;
1794                 with_*)
1795                         bcond=${opt#with_}
1796                         case "$BCOND" in
1797                         *--with?${bcond}*)
1798                                 AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH <$bcond>"
1799                                 ;;
1800                         *)
1801                                 AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH $bcond"
1802                                 ;;
1803                         esac
1804                         ;;
1805                 *)
1806                         echo >&2 "ERROR: unexpected '$opt' in set_bconds_values"
1807                         exit 1
1808                         ;;
1809                 esac
1810         done
1811 }
1812
1813 run_sub_builder() {
1814         package_name="${1}"
1815         update_shell_title "run_sub_builder $package_name"
1816         #
1817         # No i tutaj bym chcia³ zrobiæ sztuczn± inteligencjê, która spróbuje tego
1818         # pakieta zbudowaæ. Aktualnie niewiele dziala, bo generalnie nie widze do
1819         # konca algorytmu... Ale damy rade. :) Na razie po prostu sie wyjebie tak samo
1820         # jakby nie bylo tego kawalka kodu.
1821         #
1822         # Update: Poprawi³em parê rzeczy i zaczê³o generowaæ pakiety spoza zadanej listy.
1823         #         Jednym s³owem budowanie niespoldkowanych zale¿no¶ci dzia³a w paru przypadkach.
1824         #
1825         #
1826         # y0shi.
1827         # kurwa. translate that ^^^^
1828
1829         parent_spec_name=''
1830
1831         # Istnieje taki spec? ${package}.spec
1832         if [ -f "${PACKAGE_DIR}/${package}.spec" ]; then
1833                 parent_spec_name=${package}.spec
1834         elif [ -f "${PACKAGE_DIR}/$(echo ${package_name} | sed -e s,-devel.*,,g -e s,-static,,g).spec" ]; then
1835                 parent_spec_name="$(echo ${package_name} | sed -e s,-devel.*,,g -e s,-static,,g).spec"
1836         else
1837                 for provides_line in $(grep -r ^Provides:.*$package ${PACKAGE_DIR}); do
1838                         echo $provides_line
1839                 done
1840         fi
1841
1842         if [ "${parent_spec_name}" != "" ]; then
1843                 spawn_sub_builder $parent_spec_name
1844         fi
1845         NOT_INSTALLED_PACKAGES="$NOT_INSTALLED_PACKAGES $package_name"
1846 }
1847
1848 spawn_sub_builder() {
1849         package_name="${1}"
1850         update_shell_title "spawn_sub_builder $package_name"
1851
1852         sub_builder_opts=''
1853         if [ "${FETCH_BUILD_REQUIRES}" = "yes" ]; then
1854                 sub_builder_opts="${sub_builder_opts} -R"
1855         fi
1856         if [ "${REMOVE_BUILD_REQUIRES}" = "nice" ]; then
1857                 sub_builder_opts="${sub_builder_opts} -RB"
1858         elif [ "${REMOVE_BUILD_REQUIRES}" = "force" ]; then
1859                 sub_builder_opts="${sub_builder_opts} -FRB"
1860         fi
1861         if [ "${UPDATE_POLDEK_INDEXES}" = "yes" ]; then
1862                 sub_builder_opts="${sub_builder_opts} -Upi"
1863         fi
1864
1865         cd "${PACKAGE_DIR}"
1866         ./builder ${sub_builder_opts} "$@"
1867 }
1868
1869 remove_build_requires() {
1870         if [ "$INSTALLED_PACKAGES" != "" ]; then
1871                 case "$REMOVE_BUILD_REQUIRES" in
1872                         "force")
1873                                 run_poldek --noask -ve $INSTALLED_PACKAGES
1874                                 ;;
1875                         "nice")
1876                                 run_poldek --ask -ve $INSTALLED_PACKAGES
1877                                 ;;
1878                         *)
1879                                 echo You may want to manually remove following BuildRequires fetched:
1880                                 echo $INSTALLED_PACKAGES
1881                                 echo "Try poldek -e \`cat $(pwd)/.${SPECFILE}_INSTALLED_PACKAGES\`"
1882                                 ;;
1883                 esac
1884         fi
1885 }
1886
1887 display_bconds() {
1888         if [ "$AVAIL_BCONDS_WITH" -o "$AVAIL_BCONDS_WITHOUT" ]; then
1889                 if [ "$BCOND" ]; then
1890                         echo ""
1891                         echo "Building $SPECFILE with the following conditional flags:"
1892                         echo -n "$BCOND"
1893                 else
1894                         echo ""
1895                         echo "No conditional flags passed"
1896                 fi
1897                 echo ""
1898                 echo "from available:"
1899                 echo "--with   :\t$AVAIL_BCONDS_WITH"
1900                 echo "--without:\t$AVAIL_BCONDS_WITHOUT"
1901                 echo ""
1902         fi
1903 }
1904
1905 display_branches() {
1906         echo -n "Available branches: "
1907         git branch -r 2>/dev/null | grep "^  ${REMOTE_PLD}" | grep -v ${REMOTE_PLD}/HEAD | sed "s#^ *${REMOTE_PLD}/##" | xargs
1908 }
1909
1910 # checks a given list of packages/files/provides agains current rpmdb.
1911 # outputs all dependencies which current rpmdb doesn't satisfy.
1912 # input can be either STDIN or parameters
1913 _rpm_prov_check() {
1914         local DEPS
1915
1916         if [ $# -gt 0 ]; then
1917                 DEPS="$@"
1918         else
1919                 DEPS=$(cat)
1920         fi
1921
1922         DEPS=$(LANG=C rpm -q --whatprovides $DEPS 2>&1 | awk '/^(error:|no package provides)/ { print }')
1923
1924         # packages
1925         echo "$DEPS" | awk '/^no package provides/ { print $NF }'
1926
1927         # other deps (files)
1928         echo "$DEPS" | awk -F: '/^error:.*No such file/{o = $2; gsub("^ file ", "", o); print o}'
1929 }
1930
1931 # checks if given package/files/provides exists in rpmdb.
1932 # input can be either stdin or parameters
1933 # returns packages which are present in the rpmdb
1934 _rpm_cnfl_check() {
1935         local DEPS
1936
1937         if [ $# -gt 0 ]; then
1938                 DEPS="$@"
1939         else
1940                 DEPS=$(cat)
1941         fi
1942
1943         LANG=C rpm -q --whatprovides $DEPS 2>/dev/null | awk '!/no package provides/ { print }'
1944 }
1945
1946 # install deps via information from 'rpm-getdeps' or 'rpm --specsrpm'
1947 install_build_requires_rpmdeps() {
1948         if [ "$FETCH_BUILD_REQUIRES_RPMGETDEPS" = "yes" ]; then
1949                 # TODO: Conflicts list doesn't check versions
1950                 local CNFL=$(rpm-getdeps $BCOND $SPECFILE 2> /dev/null | awk '/^\-/ { print $3 } ' | _rpm_cnfl_check | xargs)
1951                 local DEPS=$(rpm-getdeps $BCOND $SPECFILE 2> /dev/null | awk '/^\+/ { print $3 } ' | _rpm_prov_check | xargs)
1952         fi
1953         if [ "$FETCH_BUILD_REQUIRES_RPMSPECSRPM" = "yes" ]; then
1954                 local CNFL=$(rpm -q --specsrpm --conflicts $BCOND $SPECFILE | awk '{print $1}' | _rpm_cnfl_check | xargs)
1955                 local DEPS=$(rpm -q --specsrpm --requires $BCOND $SPECFILE | awk '{print $1}' | _rpm_prov_check | xargs)
1956         fi
1957
1958         if [ -n "$CNFL" ] || [ -n "$DEPS" ]; then
1959                 echo "fetch BuildRequires: install [$DEPS]; remove [$CNFL]"
1960                 update_shell_title "poldek: install [$DEPS]; remove [$CNFL]"
1961                 $SU_SUDO /usr/bin/poldek -q --update || $SU_SUDO /usr/bin/poldek -q --upa
1962         fi
1963         if [ -n "$CNFL" ]; then
1964                 update_shell_title "uninstall conflicting packages: $CNFL"
1965                 echo "Trying to uninstall conflicting packages ($CNFL):"
1966                 $SU_SUDO /usr/bin/poldek --noask --nofollow -ev $CNFL
1967         fi
1968
1969         while [ "$DEPS" ]; do
1970                         update_shell_title "install deps: $DEPS"
1971                         echo "Trying to install dependencies ($DEPS):"
1972                         local log=.${SPECFILE}_poldek.log
1973                         LANG=C $SU_SUDO /usr/bin/poldek --noask --caplookup -uGqQ $DEPS | tee $log
1974                         failed=$(awk '/^error:/{a=$2; sub(/^error: /, "", a); sub(/:$/, "", a); print a}' $log)
1975                         rm -f $log
1976                         local ok
1977                         if [ -n "$failed" ]; then
1978                                 for package in $failed; do
1979                                         spawn_sub_builder -bb $(depspecname $package) && ok="$ok $package"
1980                                 done
1981                                 DEPS="$ok"
1982                         else
1983                                 DEPS=""
1984                         fi
1985         done
1986 }
1987
1988 fetch_build_requires()
1989 {
1990         if [ "${FETCH_BUILD_REQUIRES}" != "yes" ]; then
1991                 return
1992         fi
1993
1994         update_shell_title "fetch build requires"
1995         if [ "$FETCH_BUILD_REQUIRES_RPMGETDEPS" = "yes" ] || [ "$FETCH_BUILD_REQUIRES_RPMSPECSRPM" = "yes" ]; then
1996                 install_build_requires_rpmdeps
1997                 return
1998         fi
1999
2000                 # XXX is this ugliest code written in human history still needed?
2001                 echo "All packages installed by fetch_build_requires() are written to:"
2002                 echo "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES"
2003                 echo ""
2004                 echo "If anything fails, you may get rid of them by executing:"
2005                 echo "poldek -e \`cat `pwd`/.${SPECFILE}_INSTALLED_PACKAGES\`"
2006                 echo ""
2007                 echo > `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
2008                 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)
2009                 do
2010                         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)
2011                         GO="yes"
2012                         package=$(basename "$package_item"|sed -e "s/}$//g")
2013                         COND_ARCH_TST=$(cat $SPECFILE|grep -B1 BuildRequires|grep -B1 $package|grep ifarch|sed -e "s/^.*ifarch//g")
2014                         mach=$(uname -m)
2015
2016                         COND_TST=`cat $SPECFILE|grep BuildRequires|grep "$package"`
2017                         if `echo $COND_TST|grep -q '^BuildRequires:'`; then
2018                                 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
2019                                         GO="yes"
2020                                 fi
2021                         # bcond:
2022                         else
2023                                 COND_NAME=`echo $COND_TST|sed -e s,:BuildRequires:.*$,,g`
2024                                 GO=""
2025                                 # %{without}
2026                                 if `echo $COND_TST|grep -q 'without_'`; then
2027                                         COND_NAME=`echo $COND_NAME|sed -e s,^.*without_,,g`
2028                                         if `echo $COND_TST|grep -q !`; then
2029                                                 COND_STATE="with"
2030                                         else
2031                                                 COND_STATE="wout"
2032                                         fi
2033                                         COND_WITH=`echo $AVAIL_BCONDS_WITH|grep "<$COND_NAME>"`
2034                                         COND_WITHOUT=`echo $AVAIL_BCONDS_WITHOUT|grep "<$COND_NAME>"`
2035                                         if [ -n "$COND_WITHOUT" ] || [ -z "$COND_WITH" ]; then
2036                                                 COND_ARGV="wout"
2037                                         else
2038                                                 COND_ARGV="with"
2039                                         fi
2040                                 # %{with}
2041                                 elif `echo $COND_TST|grep -q 'with_'`; then
2042                                         COND_NAME=`echo $COND_NAME|sed -e s,^.*with_,,g`
2043                                         if `echo $COND_TST|grep -q !`; then
2044                                                 COND_STATE="wout"
2045                                         else
2046                                                 COND_STATE="with"
2047                                         fi
2048                                         COND_WITH=`echo $AVAIL_BCONDS_WITH|grep "<$COND_NAME>"`
2049                                         COND_WITHOUT=`echo $AVAIL_BCONDS_WITHOUT|grep "<$COND_NAME>"`
2050                                         if [ -n "$COND_WITH" ] || [ -z "$COND_WITHOUT" ]; then
2051                                                 COND_ARGV="with"
2052                                         else
2053                                                 COND_ARGV="wout"
2054                                         fi
2055                                 fi
2056                                 RESULT="${COND_STATE}-${COND_ARGV}"
2057                                 case "$RESULT" in
2058                                         "with-wout" | "wout-with" )
2059                                                 GO=""
2060                                                 ;;
2061                                         "wout-wout" | "with-with" )
2062                                                 GO="yes"
2063                                                 ;;
2064                                         * )
2065                                                 echo "Action '$RESULT' was not defined for package '$package_item'"
2066                                                 GO="yes"
2067                                                 ;;
2068                                 esac
2069                         fi
2070
2071                         if [ "$GO" = "yes" ]; then
2072                                 if [ "`rpm -q $package|sed -e "s/$package.*/$package/g"`" != "$package" ]; then
2073                                         echo "Testing if $package has subrequirements..."
2074                                         run_poldek -t -i $package --dumpn=".$package-req.txt"
2075                                         if [ -f ".$package-req.txt" ]; then
2076                                                 for package_name in `cat ".$package-req.txt"|grep -v ^#`
2077                                                 do
2078                                                         if [ "$package_name" = "$package" ]; then
2079                                                                 echo "Installing BuildRequired package:\t$package_name"
2080                                                                 update_shell_title "Installing BuildRequired package: ${package_name}"
2081                                                                 install_required_packages $package
2082                                                         else
2083                                                                 echo "Installing (sub)Required package:\t$package_name"
2084                                                                 update_shell_title "Installing (sub)Required package: ${package_name}"
2085                                                                 install_required_packages $package_name
2086                                                         fi
2087                                                         case $? in
2088                                                                 0)
2089                                                                         INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
2090                                                                         echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
2091                                                                         ;;
2092                                                                 *)
2093                                                                         echo "Attempting to run spawn sub - builder..."
2094                                                                         echo "Package installation failed:\t$package_name"
2095                                                                         run_sub_builder $package_name
2096                                                                         if [ $? -eq 0 ]; then
2097                                                                                 install_required_packages $package_name
2098                                                                                 case $? in
2099                                                                                         0)
2100                                                                                                 INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
2101                                                                                                 echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
2102                                                                                                 ;;
2103                                                                                         *)
2104                                                                                                 NOT_INSTALLED_PACKAGES="$package_name $NOT_INSTALLED_PACKAGES"
2105                                                                                                 ;;
2106                                                                                 esac
2107                                                                         fi
2108                                                                         ;;
2109                                                         esac
2110                                                 done
2111                                                 rm -f ".$package-req.txt"
2112                                         else
2113                                                 echo "Attempting to run spawn sub - builder..."
2114                                                 echo "Package installation failed:\t$package"
2115                                                 run_sub_builder $package
2116                                                 if [ $? -eq 0 ]; then
2117                                                         install_required_packages $package
2118                                                         case $? in
2119                                                                 0)
2120                                                                         INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
2121                                                                         echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
2122                                                                         ;;
2123                                                                 *)
2124                                                                         NOT_INSTALLED_PACKAGES="$package_name $NOT_INSTALLED_PACKAGES"
2125                                                                         ;;
2126                                                         esac
2127                                                 fi
2128                                         fi
2129                                 else
2130                                         echo "Package $package is already installed. BuildRequirement satisfied."
2131                                 fi
2132                         fi
2133                 done
2134                 if [ "$NOT_INSTALLED_PACKAGES" != "" ]; then
2135                         echo >&2 "Unable to install following packages and their dependencies:"
2136                         for pkg in "$NOT_INSTALLED_PACKAGES"
2137                         do
2138                                 echo $pkg
2139                         done
2140                         remove_build_requires
2141                         exit 8
2142                 fi
2143 }
2144
2145 init_rpm_dir() {
2146         local TOP_DIR=$(eval $RPM $RPMOPTS --eval '%{_topdir}')
2147         local rpmdir=$(eval $RPM $RPMOPTS --eval '%{_rpmdir}')
2148         local buildir=$(eval $RPM $RPMOPTS --eval '%{_builddir}')
2149         local srpmdir=$(eval $RPM $RPMOPTS --eval '%{_srcrpmdir}')
2150         local tmp
2151
2152         echo "Initializing rpm directories to $TOP_DIR from $GIT_SERVER"
2153         mkdir -p $TOP_DIR $rpmdir $buildir $srpmdir
2154
2155         cd "$TOP_DIR"
2156         if [ ! -e ../rpm-build-tools ]; then
2157                 git clone  ${GIT_SERVER}/${PACKAGES_DIR}/rpm-build-tools.git ../rpm-build-tools
2158         fi
2159         for a in adapter builder fetchsrc_request compile repackage; do
2160                 ln -sf ../rpm-build-tools/${a}.sh $a
2161         done
2162         for a in md5; do
2163                 ln -sf ../rpm-build-tools/${a} $a
2164         done
2165         ln -sf ../rpm-build-tools/mirrors mirrors
2166         init_builder
2167 }
2168
2169 mr_proper() {
2170         init_builder
2171         NOCVSSPEC="yes"
2172         DONT_PRINT_REVISION="yes"
2173
2174         # remove spec and sources
2175         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
2176         rm -rf $PACKAGE_DIR/{.git,.gitignore}
2177         rmdir --ignore-fail-on-non-empty $PACKAGE_DIR
2178 }
2179
2180 #---------------------------------------------
2181 # main()
2182
2183 if [ $# = 0 ]; then
2184         usage
2185         exit 1
2186 fi
2187
2188 while [ $# -gt 0 ]; do
2189         case "${1}" in
2190                 -4|-6)
2191                         # NOTE: we should be fetcher specific, like fille WGET_OPTS, but
2192                         # unfortunately $GETURI is already formed
2193                         GETURI="$GETURI $1"
2194                         shift
2195                         ;;
2196                 -5 | --update-md5)
2197                         COMMAND="update_md5"
2198                         NODIST="yes"
2199                         NOCVSSPEC="yes"
2200                         shift ;;
2201                 -a5 | --add-md5 )
2202                         COMMAND="update_md5"
2203                         NODIST="yes"
2204                         NOCVSSPEC="yes"
2205                         ADD5="yes"
2206                         shift ;;
2207                 -n5 | --no-md5 )
2208                         NO5="yes"
2209                         shift ;;
2210                 -D | --debug )
2211                         DEBUG="yes"; shift ;;
2212                 -V | --version )
2213                         COMMAND="version"; shift ;;
2214                 --short-version )
2215                         COMMAND="short-version"; shift ;;
2216                 -a | --add_cvs)
2217                         COMMAND="add_cvs";
2218                         shift ;;
2219                 --all-branches )
2220                         ALL_BRANCHES="yes"
2221                         shift ;;
2222                 -b | -ba | --build )
2223                         COMMAND="build"; shift ;;
2224                 -bb | --build-binary )
2225                         COMMAND="build-binary"; shift ;;
2226                 -bc )
2227                         COMMAND="build-build"; shift ;;
2228                 -bi )
2229                         COMMAND="build-install"; shift ;;
2230                 -bl )
2231                         COMMAND="build-list"; shift ;;
2232                 -bp | --build-prep )
2233                         COMMAND="build-prep"; shift ;;
2234                 -bs | --build-source )
2235                         COMMAND="build-source"; shift ;;
2236                 -B | --branch )
2237                         COMMAND="branch"; shift; TAG="${1}"; shift;;
2238                 -c | --clean )
2239                         CLEAN="--clean"; shift ;;
2240                 -cf | --cvs-force )
2241                         CVS_FORCE="-f"; shift;;
2242                 --depth )
2243                         DEPTH="--depth=$2"
2244                         shift 2
2245                         ;;
2246                 -g | --get )
2247                         COMMAND="get"; shift ;;
2248                 -h | --help )
2249                         COMMAND="usage"; shift ;;
2250                 --ftp )
2251                         PROTOCOL="ftp"; shift ;;
2252                 --http )
2253                         PROTOCOL="http"; shift ;;
2254                 -j)
2255                         RPMOPTS="${RPMOPTS} --define \"_smp_mflags -j$2\""
2256                         shift 2
2257                         ;;
2258                 -j[0-9]*)
2259                         RPMOPTS="${RPMOPTS} --define \"_smp_mflags $1\""
2260                         shift
2261                         ;;
2262                 -p)
2263                         PARALLEL_DOWNLOADS=$2
2264                         shift 2
2265                         ;;
2266                 -p[0-9])
2267                         PARALLEL_DOWNLOADS=${1#-p}
2268                         shift
2269                         ;;
2270                 -l | --logtofile )
2271                         shift; LOGFILE="${1}"; shift ;;
2272                 -ni| --nice )
2273                         shift; DEF_NICE_LEVEL=${1}; shift ;;
2274                 -ske | --skip-existing-files)
2275                         SKIP_EXISTING_FILES="yes"; shift ;;
2276                 -m | --mr-proper )
2277                         COMMAND="mr-proper"; shift ;;
2278                 -ncs | --no-cvs-specs )
2279                         NOCVSSPEC="yes"; shift ;;
2280                 -nd | --no-distfiles )
2281                         NODIST="yes"; shift ;;
2282                 -nm | --no-mirrors )
2283                         NOMIRRORS="yes"; shift ;;
2284                 -nu | --no-urls )
2285                         NOURLS="yes"; shift ;;
2286                 -ns | --no-srcs )
2287                         NOSRCS="yes"; shift ;;
2288                 -ns0 | --no-source0 )
2289                         NOSOURCE0="yes"; shift ;;
2290                 -nn | --no-net )
2291                         NOCVSSPEC="yes"
2292                         NODIST="yes"
2293                         NOMIRRORS="yes"
2294                         NOURLS="yes"
2295                         NOSRCS="yes"
2296                         ALWAYS_CVSUP="no"
2297                         shift;;
2298                 -pm | --prefer-mirrors )
2299                         PREFMIRRORS="yes"
2300                         shift;;
2301                 --noinit | --no-init )
2302                         NOINIT="yes"
2303                         shift;;
2304                 --opts )
2305                         shift; RPMOPTS="${RPMOPTS} ${1}"; shift ;;
2306                 --nopatch | -np )
2307                         shift; RPMOPTS="${RPMOPTS} --define \"patch${1} : ignoring patch${1}; exit 1; \""; shift ;;
2308                 --skip-patch | -sp )
2309                         shift; RPMOPTS="${RPMOPTS} --define \"patch${1} : skiping patch${1}\""; shift ;;
2310                 --topdir)
2311                         RPMOPTS="${RPMOPTS} --define \"_topdir $2\""
2312                         shift 2
2313                         ;;
2314                 --with | --without )
2315                         case $GROUP_BCONDS in
2316                                 "yes")
2317                                         COND=${1}
2318                                         shift
2319                                         # XXX: broken: ./builder -bb ucspi-tcp.spec --without mysql
2320                                         while ! `echo ${1}|grep -qE '(^-|spec)'`
2321                                         do
2322                                                 BCOND="$BCOND $COND $1"
2323                                                 shift
2324                                         done;;
2325                                 "no")
2326                                         if [[ "$2" = *,* ]]; then
2327                                                 for a in $(echo "$2" | tr , ' '); do
2328                                                         BCOND="$BCOND $1 $a"
2329                                                 done
2330                                         else
2331                                                 BCOND="$BCOND $1 $2"
2332                                         fi
2333                                         shift 2 ;;
2334                         esac
2335                         ;;
2336                 --target )
2337                         shift; TARGET="${1}"; shift ;;
2338                 --target=* )
2339                         TARGET=$(echo "${1}" | sed 's/^--target=//'); shift ;;
2340                 -q | --quiet )
2341                         QUIET="--quiet"; shift ;;
2342                 --date )
2343                         CVSDATE="${2}"; shift 2
2344                         date -d "$CVSDATE" > /dev/null 2>&1 || { echo >&2 "No valid date specified"; exit 3; }
2345                         ;;
2346                 -r | --cvstag )
2347                         CVSTAG="$2"
2348                         shift 2
2349                         ;;
2350                 -A)
2351                         shift
2352                         CVSTAG="master"
2353                         ;;
2354                 -R | --fetch-build-requires)
2355                         FETCH_BUILD_REQUIRES="yes"
2356                         NOT_INSTALLED_PACKAGES=
2357                         shift ;;
2358                 -RB | --remove-build-requires)
2359                         REMOVE_BUILD_REQUIRES="nice"
2360                         shift ;;
2361                 -FRB | --force-remove-build-requires)
2362                         REMOVE_BUILD_REQUIRES="force"
2363                         shift ;;
2364                 -sc | --source-cvs)
2365                         COMMAND="list-sources-cvs"
2366                         shift ;;
2367                 -sd | --source-distfiles)
2368                         COMMAND="list-sources-distfiles"
2369                         shift ;;
2370                 -sdp | --source-distfiles-paths)
2371                         COMMAND="list-sources-distfiles-paths"
2372                         shift ;;
2373                 -sf | --source-files)
2374                         COMMAND="list-sources-files"
2375                         shift ;;
2376                 -lsp | --source-paths)
2377                         COMMAND="list-sources-local-paths"
2378                         shift ;;
2379                 -su | --source-urls)
2380                         COMMAND="list-sources-urls"
2381                         shift ;;
2382                 -Tvs | --tag-version-stable )
2383                         COMMAND="tag"
2384                         TAG="STABLE"
2385                         TAG_VERSION="yes"
2386                         shift;;
2387                 -Ts | --tag-stable )
2388                         COMMAND="tag"
2389                         TAG="STABLE"
2390                         TAG_VERSION="no"
2391                         shift;;
2392                 -Tv | --tag-version )
2393                         COMMAND="tag"
2394                         TAG=""
2395                         TAG_VERSION="yes"
2396                         shift;;
2397                 -Tp | --tag-prefix )
2398                         TAG_PREFIX="$2"
2399                         shift 2;;
2400                 -tt | --test-tag )
2401                         TEST_TAG="yes"
2402                         shift;;
2403                 -T | --tag )
2404                         COMMAND="tag"
2405                         shift
2406                         TAG="$1"
2407                         TAG_VERSION="no"
2408                         shift;;
2409                 -ir | --integer-release-only )
2410                         INTEGER_RELEASE="yes"
2411                         shift;;
2412                 -U | --update )
2413                         COMMAND="update_md5"
2414                         UPDATE="yes"
2415                         NOCVSSPEC="yes"
2416                         NODIST="yes"
2417                         shift ;;
2418                 -Upi | --update-poldek-indexes )
2419                         UPDATE_POLDEK_INDEXES="yes"
2420                         shift ;;
2421                 --init-rpm-dir)
2422                         COMMAND="init_rpm_dir"
2423                         shift ;;
2424                 -u | --try-upgrade )
2425                         TRY_UPGRADE="1"; shift ;;
2426                 --upgrade-version )
2427                         shift; UPGRADE_VERSION="$1"; shift;;
2428                 -un | --try-upgrade-with-float-version )
2429                         TRY_UPGRADE="1"; FLOAT_VERSION="1"; shift ;;
2430                 -v | --verbose )
2431                         BE_VERBOSE="1"; shift ;;
2432                 --define)
2433                         shift
2434                         MACRO="${1}"
2435                         shift
2436                         if echo "${MACRO}" | grep -q '\W'; then
2437                                 RPMOPTS="${RPMOPTS} --define \"${MACRO}\""
2438                         else
2439                                 VALUE="${1}"
2440                                 shift
2441                                 RPMOPTS="${RPMOPTS} --define \"${MACRO} ${VALUE}\""
2442                         fi
2443                         ;;
2444                 --alt_kernel)
2445                         shift
2446                         RPMOPTS="${RPMOPTS} --define \"alt_kernel $1\""
2447                         shift
2448                         ;;
2449                 --short-circuit)
2450                         RPMBUILDOPTS="${RPMBUILDOPTS} --short-circuit"
2451                         shift
2452                         ;;
2453                 --show-bconds | -show-bconds | -print-bconds | --print-bconds | -display-bconds | --display-bconds )
2454                         COMMAND="show_bconds"
2455                         shift
2456                         ;;
2457                 --show-bcond-args)
2458                         COMMAND="show_bcond_args"
2459                         shift
2460                         ;;
2461                 --show-avail-bconds)
2462                         COMMAND="show_avail_bconds"
2463                         shift
2464                         ;;
2465                 --nodeps)
2466                         shift
2467                         RPMOPTS="${RPMOPTS} --nodeps"
2468                         ;;
2469                 -debug)
2470                         RPMBUILDOPTS="${RPMBUILDOPTS} -debug"; shift
2471                         ;;
2472                 --git-pld)
2473                         shift
2474                         GIT_SERVER=${PLD_GIT_SERVER}
2475                         GIT_PUSH=${PLD_GIT_PUSH}
2476                         PACKAGES_DIR=${PLD_PACKAGES_DIR}
2477                         DISTFILES_SERVER=${PLD_DISTFILES_SERVER}
2478                         ATTICDISTFILES_SERVER=${PLD_ATTICDISTFILES_SERVER}
2479                         ;;
2480                 --git-tld)
2481                         shift
2482                         GIT_SERVER=${TLD_GIT_SERVER}
2483                         GIT_PUSH=${TLD_GIT_PUSH}
2484                         PACKAGES_DIR=${TLD_PACKAGES_DIR}
2485                         DISTFILES_SERVER=${TLD_DISTFILES_SERVER}
2486                         ATTICDISTFILES_SERVER=${TLD_ATTICDISTFILES_SERVER}
2487                         ;;
2488                 --pkgrev)
2489                         COMMAND="set_pkgrev"
2490                         shift;;
2491                 -*)
2492                         Exit_error err_invalid_cmdline "$1"
2493                         ;;
2494                 *)
2495                         SPECFILE=${1%/}; shift
2496                         # check if specname was passed as specname:cvstag
2497                         if [ "${SPECFILE##*:}" != "${SPECFILE}" ]; then
2498                                 CVSTAG="${SPECFILE##*:}"
2499                                 SPECFILE="${SPECFILE%%:*}"
2500                         fi
2501                         # always have SPECFILE ending with .spec extension
2502                         SPECFILE=${SPECFILE%%.spec}.spec
2503                         ASSUMED_NAME=$(basename ${SPECFILE%%.spec})
2504         esac
2505 done
2506
2507 # Check if given package exists in TLD git
2508 if ! git ls-remote --heads ${GIT_SERVER}/${PACKAGES_DIR}/${ASSUMED_NAME} 1>/dev/null 2>&1; then
2509         # Nope, we don't have it in TLD, switch to PLD repositories
2510         GIT_SERVER=${PLD_GIT_SERVER}
2511         GIT_PUSH=${PLD_GIT_PUSH}
2512         PACKAGES_DIR=${PLD_PACKAGES_DIR}
2513         DISTFILES_SERVER=${PLD_DISTFILES_SERVER}
2514         ATTICDISTFILES_SERVER=${PLD_ATTICDISTFILES_SERVER}
2515 fi
2516
2517 if [ "$CVSTAG" ]; then
2518         # pass $CVSTAG used by builder to rpmbuild too, so specs could use it
2519         RPMOPTS="$RPMOPTS --define \"_cvstag $CVSTAG\""
2520 fi
2521
2522 if [ -n "$ALL_BRANCHES" -a -z "$DEPTH" ]; then
2523         echo >&2 "--all branches requires --depth <number>"
2524         Exit_error err_invalid_cmdline
2525 fi
2526
2527 if [ -n "$DEBUG" ]; then
2528         set -x
2529         set -v
2530 fi
2531
2532 if [ -n "$TARGET" ]; then
2533         case "$RPMBUILD" in
2534                 "rpmbuild")
2535                         TARGET_SWITCH="--target $TARGET" ;;
2536                 "rpm")
2537                         TARGET_SWITCH="--target=$TARGET" ;;
2538         esac
2539 fi
2540
2541 if [ "$SCHEDTOOL" != "no" ]; then
2542         NICE_COMMAND="$SCHEDTOOL"
2543 else
2544         NICE_COMMAND="nice -n ${DEF_NICE_LEVEL}"
2545 fi
2546
2547 update_shell_title "$COMMAND"
2548 case "$COMMAND" in
2549         "show_bconds")
2550                 init_builder
2551                 if [ -z "$SPECFILE" ]; then
2552                         Exit_error err_no_spec_in_cmdl
2553                 fi
2554                 get_spec > /dev/null
2555                 parse_spec
2556                 set_bconds_values
2557                 display_bconds
2558                 ;;
2559         "show_bcond_args")
2560                 init_builder
2561                 if [ -z "$SPECFILE" ]; then
2562                         Exit_error err_no_spec_in_cmdl
2563                 fi
2564                 get_spec > /dev/null
2565                 parse_spec
2566                 set_bconds_values
2567                 echo "$BCOND"
2568                 ;;
2569         "show_avail_bconds")
2570                 init_builder
2571                 if [ -z "$SPECFILE" ]; then
2572                         Exit_error err_no_spec_in_cmdl
2573                 fi
2574
2575                 get_spec > /dev/null
2576                 parse_spec
2577                 local bcond_avail=$(find_spec_bcond $SPECFILE)
2578                 local opt bcond bconds
2579                 for opt in $bcond_avail; do
2580                         case "$opt" in
2581                         without_*)
2582                                 bcond=${opt#without_}
2583                                 bconds="$bconds $bcond"
2584                                 ;;
2585                         with_*)
2586                                 bcond=${opt#with_}
2587                                 bconds="$bconds $bcond"
2588                                 ;;
2589                         *)
2590                                 echo >&2 "ERROR: unexpected '$opt' in show_avail_bconds"
2591                                 exit 1
2592                                 ;;
2593                         esac
2594                 done
2595                 echo $bconds
2596
2597                 ;;
2598         "build" | "build-binary" | "build-source" | "build-prep" | "build-build" | "build-install" | "build-list")
2599                 init_builder
2600                 if [ -z "$SPECFILE" ]; then
2601                         Exit_error err_no_spec_in_cmdl
2602                 fi
2603
2604                 # display SMP make flags if set
2605                 smp_mflags=$(rpm -E %{?_smp_mflags})
2606                 if [ "$smp_mflags" ]; then
2607                         echo "builder: SMP make flags are set to $smp_mflags"
2608                 fi
2609
2610                 get_spec
2611                 parse_spec
2612                 set_bconds_values
2613                 display_bconds
2614                 display_branches
2615                 if [ "$COMMAND" != "build-source" ]; then
2616                         check_buildarch
2617                 fi
2618                 fetch_build_requires
2619                 if [ "$INTEGER_RELEASE" = "yes" ]; then
2620                         echo "Checking release $PACKAGE_RELEASE..."
2621                         if echo $PACKAGE_RELEASE | grep -q '^[^.]*\.[^.]*$' 2>/dev/null ; then
2622                                 Exit_error err_fract_rel "$PACKAGE_RELEASE"
2623                         fi
2624                 fi
2625
2626                 # ./builder -bs test.spec -r AC-branch -Tp auto-ac- -tt
2627                 if [ -n "$TEST_TAG" ]; then
2628                         local TAGVER=`
2629                         make_tagver`
2630                         tag_exist $TAGVER || [ $TAGVER = $CVSTAG ] || Exit_error err_tag_exists $TAGVER
2631                         # check also tags created in CVS
2632                         local TAGVER_CVS=$(echo $TAGVER | tr '[.@]' '[_#]')
2633                         local CVSTAG_CVS=$(echo $CVSTAG | tr '[.@]' '[_#]')
2634                         tag_exist $TAGVER_CVS || [ $TAGVER_CVS = $CVSTAG_CVS ] \
2635                                 || Exit_error err_tag_exists $TAGVER_CVS
2636                         # - do not allow to build from HEAD when XX-branch exists
2637                         TREE_PREFIX=$(echo "$TAG_PREFIX" | sed -e 's#^auto/\([a-zA-Z]\+\)/.*#\1#g')
2638                         if [ "$TAGVER" != "$CVSTAG" -a "$TAGVER_CVS" != "$CVSTAG" -a  "$TREE_PREFIX" != "$TAG_PREFIX" ]; then
2639                                 TAG_BRANCH="${TREE_PREFIX}-branch"
2640                                 if [ -n "$DEPTH" ]; then
2641                                         cmd_branches="git ls-remote --heads"
2642                                         ref_prefix=refs/heads
2643                                 else
2644                                         cmd_branches="git show-ref"
2645                                         ref_prefix=refs/remotes/${REMOTE_PLD}
2646                                 fi
2647                                 TAG_STATUS=$($cmd_branches | grep -i "${ref_prefix}/$TAG_BRANCH$" | cut -c'-40')
2648                                 if [ -n "$TAG_STATUS" -a "$TAG_STATUS" != $(git rev-parse "$CVSTAG") ]; then
2649                                         Exit_error err_branch_exists "$TAG_STATUS"
2650                                 fi
2651                         fi
2652
2653                 fi
2654
2655                 if [ -n "$NOSOURCE0" ] ; then
2656                         SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2657                 fi
2658                 try_upgrade
2659                 case $? in
2660                         0)
2661                                 get_files $SOURCES $PATCHES
2662                                 check_md5 $SOURCES $PATCHES
2663                                 ;;
2664                         *)
2665                                 NODIST="yes" get_files $SOURCES $PATCHES
2666                                 update_md5 $SOURCES $PATCHES
2667                                 ;;
2668                 esac
2669                 build_package
2670                 if [ "$UPDATE_POLDEK_INDEXES" = "yes" ] && [ "$COMMAND" = "build" -o "$COMMAND" = "build-binary" ]; then
2671                         run_poldek --sdir="${POLDEK_INDEX_DIR}" ${UPDATE_POLDEK_INDEXES_OPTS} --mkidxz
2672                 fi
2673                 remove_build_requires
2674                 ;;
2675         "branch" )
2676                 init_builder
2677                 if [ -z "$SPECFILE" ]; then
2678                         Exit_error err_no_spec_in_cmdl
2679                 fi
2680
2681                 get_spec
2682                 parse_spec
2683                 branch_files $TAG
2684                 ;;
2685         "add_cvs" )
2686                 init_builder
2687                 if [ -z "$SPECFILE" ]; then
2688                         Exit_error err_no_spec_in_cmdl
2689                 fi
2690
2691                 create_git_repo
2692                 if [ -n "$NEW_REPO" ]; then
2693                         parse_spec
2694                         local file
2695                         for file in $SOURCES $PATCHES; do
2696                                 if [ -z $(src_md5 "$file") ]; then
2697                                         git add $file || Exit_error err_no_source_in_repo $file
2698                                 else
2699                                         cvsignore_df `nourl $file`
2700                                 fi
2701                         done
2702                         git add $SPECFILE
2703                         echo "When you are ready commit your changes and run git push origin master"
2704                 else
2705                         echo "You had already git repository. Push chosen branches to remote: ${REMOTE_PLD}"
2706                 fi
2707                 ;;
2708         "get" )
2709                 init_builder
2710                 if [ -z "$SPECFILE" ]; then
2711                         Exit_error err_no_spec_in_cmdl
2712                 fi
2713
2714                 get_spec
2715                 parse_spec
2716
2717                 if [ -n "$NOSOURCE0" ] ; then
2718                         SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2719                 fi
2720                 get_files $SOURCES $PATCHES
2721                 check_md5 $SOURCES
2722                 ;;
2723         "update_md5" )
2724                 init_builder
2725                 if [ -z "$SPECFILE" ]; then
2726                         Exit_error err_no_spec_in_cmdl
2727                 fi
2728
2729                 get_spec
2730                 parse_spec
2731
2732                 if [ -n "$NOSOURCE0" ] ; then
2733                         SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2734                 fi
2735                 update_md5 $SOURCES $PATCHES
2736                 ;;
2737         "tag" )
2738                 NOURLS=1
2739                 NODIST="yes"
2740                 init_builder
2741                 if [ -z "$SPECFILE" ]; then
2742                         Exit_error err_no_spec_in_cmdl
2743                 fi
2744
2745                 parse_spec
2746                 if  [ ! -d .git ]; then
2747                         echo "No git reposiotory" >&2
2748                         exit 101
2749                 fi
2750                 tag_files
2751                 ;;
2752         "mr-proper" )
2753                 mr_proper
2754                 ;;
2755         "list-sources-files" )
2756                 init_builder
2757                 NOCVSSPEC="yes"
2758                 DONT_PRINT_REVISION="yes"
2759                 get_spec
2760                 parse_spec
2761                 for SAP in $SOURCES $PATCHES; do
2762                         echo $SAP | awk '{gsub(/.*\//,"") ; print}'
2763                 done
2764                 ;;
2765         "list-sources-urls" )
2766                 init_builder >&2
2767                 NOCVSSPEC="yes"
2768                 DONT_PRINT_REVISION="yes"
2769                 get_spec >&2
2770                 parse_spec >&2
2771                 SAPS="$SOURCES $PATCHES"
2772                 for SAP in $SAPS; do
2773                         echo $SAP
2774                 done
2775                 ;;
2776         "list-sources-local-paths" )
2777                 init_builder
2778                 NOCVSSPEC="yes"
2779                 DONT_PRINT_REVISION="yes"
2780                 get_spec
2781                 parse_spec
2782                 for SAP in $SOURCES $PATCHES; do
2783                         echo $PACKAGE_DIR/$(echo $SAP | awk '{gsub(/.*\//,"") ; print }')
2784                 done
2785                 ;;
2786         "list-sources-distfiles-paths" )
2787                 init_builder
2788                 NOCVSSPEC="yes"
2789                 DONT_PRINT_REVISION="yes"
2790                 get_spec
2791                 parse_spec
2792                 for SAP in $SOURCES $PATCHES; do
2793                         if [ -n "$(src_md5 "$SAP")" ]; then
2794                                 distfiles_path "$SAP"
2795                         fi
2796                 done
2797                 ;;
2798         "list-sources-distfiles" )
2799                 init_builder
2800                 NOCVSSPEC="yes"
2801                 DONT_PRINT_REVISION="yes"
2802                 get_spec
2803                 parse_spec
2804                 for SAP in $SOURCES $PATCHES; do
2805                         if [ -n "$(src_md5 "$SAP")" ]; then
2806                                 distfiles_url "$SAP"
2807                         fi
2808                 done
2809                 ;;
2810         "list-sources-cvs" )
2811                 init_builder
2812 #               NOCVSSPEC="yes"
2813                 DONT_PRINT_REVISION="yes"
2814                 get_spec
2815                 parse_spec
2816                 for SAP in $SOURCES $PATCHES; do
2817                         if [ -z "$(src_md5 "$SAP")" ]; then
2818                                 echo $SAP | awk '{gsub(/.*\//,"") ; print}'
2819                         fi
2820                 done
2821                 ;;
2822         "init_rpm_dir")
2823                 init_rpm_dir
2824                 ;;
2825         "usage" )
2826                 usage
2827                 ;;
2828         "short-version" )
2829                 echo "$VERSION"
2830                 ;;
2831         "version" )
2832                 echo "$VERSIONSTRING"
2833                 ;;
2834         "set_pkgrev" )
2835                 init_builder
2836                 if [ -z "$SPECFILE" ]; then
2837                         Exit_error err_no_spec_in_cmdl
2838                 fi
2839                 get_spec > /dev/null
2840                 parse_spec
2841                 set_bconds_values
2842                 display_bconds
2843                 set_pkgrev
2844                 ;;
2845 esac
2846 if [ -f "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES" -a "$REMOVE_BUILD_REQUIRES" != "" ]; then
2847         rm "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES"
2848 fi
2849 cd "$__PWD"
2850
2851 # vi:syntax=sh:ts=4:sw=4:noet