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