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