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