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