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