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