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