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