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