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