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