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