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