]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - builder.sh
- release 2
[packages/rpm-build-tools.git] / builder.sh
1 #!/bin/ksh
2 #
3 # This program is free software, distributed under the terms of
4 # the GNU General Public License Version 2.
5 #
6 # -----------
7 # Exit codes:
8 #         0 - succesful
9 #         1 - help displayed
10 #         2 - no spec file name in cmdl parameters
11 #         3 - spec file not stored in repo
12 #         4 - some source, patch or icon files not stored in repo
13 #         5 - package build failed
14 #         6 - spec file with errors
15 #         7 - wrong source in /etc/poldek.conf
16 #         8 - Failed installing buildrequirements and subrequirements
17 #         9 - Requested tag already exist
18 #        10 - Refused to build fractional release
19 #       100 - Unknown error (should not happen)
20
21 # Notes (todo/bugs):
22 # - when Icon: field is present, -5 and -a5 doesn't work
23 # - builder -R skips installing BR if spec is not present before builder invocation (need to run builder twice)
24 # - does not respect NoSource: X, and tries to cvs up such files [ example: VirtualBox-bin.spec and its Source0 ]
25 # TODO:
26 # - ability to do ./builder -bb foo.spec foo2.spec foo3.spec
27 # - funny bug, if source-md5 is set then builder will download from distfiles even if there is no url present:
28 #   Source10:   forwardfix.pl
29 #   # Source10-md5:     8bf85f7368933a4e0cb4f875bac28733
30 # - builder --help:
31 #       basename: missing operand
32 #       Try `basename --help' for more information.
33 #       -- and the normal usage info --
34
35 PROGRAM=${0##*/}
36 APPDIR=$(d=$0; [ -L "$d" ] && d=$(readlink -f "$d"); dirname "$d")
37 RCSID='$Id$' r=${RCSID#* * } rev=${r%% *}
38 VERSION="v0.35/$rev"
39 VERSIONSTRING="\
40 Build package utility from PLD Linux 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                                 # skip tools that don't exist in top dir
788                                 [ -f $a ] || continue
789                                 # skip tools that already exist
790                                 [ -f $ASSUMED_NAME/$a ] && continue
791                                 ln -s ../$a $ASSUMED_NAME
792                                 cvsignore_df $a
793                         done
794                 fi
795         fi
796
797         if [ ! -f "$ASSUMED_NAME/$SPECFILE" ]; then
798                 Exit_error err_no_spec_in_repo
799         fi
800
801         if [ "$CHMOD" = "yes" -a -n "$SPECFILE" ]; then
802                 chmod $CHMOD_MODE $ASSUMED_NAME/$SPECFILE
803         fi
804         unset OPTIONS
805         [ -n "$DONT_PRINT_REVISION" ] || grep -E -m 1 "^#.*Revision:.*Date" $ASSUMED_NAME/$SPECFILE
806
807         set_spec_target
808 }
809
810 find_mirror() {
811         cd "$REPO_DIR"
812         local url="$1"
813         if [ ! -f "mirrors" -a "$NOCVSSPEC" != "yes" ] ; then
814                 $CVS_COMMAND update mirrors >&2
815         fi
816
817         IFS="|"
818         local origin mirror name rest ol prefix
819         while read origin mirror name rest; do
820                 # skip comments and empty lines
821                 if [ -z "$origin" ] || [ "${origin#\#}" != "$origin" ]; then
822                         continue
823                 fi
824                 ol=$(echo -n "$origin" | wc -c)
825                 prefix=$(echo -n "$url" | head -c $ol)
826                 if [ "$prefix" = "$origin" ] ; then
827                         suffix=$(echo "$url" | cut -b $((ol+1))-)
828                         echo -n "$mirror$suffix"
829                         return 0
830                 fi
831         done < mirrors
832         echo "$url"
833 }
834
835 # Warning: unpredictable results if same URL used twice
836 src_no() {
837         local file="$1"
838         # escape some regexp characters if part of file name
839         file=$(echo "$file" | sed -e 's#\([\+\*\.\&\#\?]\)#\\\1#g')
840         cd $PACKAGE_DIR
841         rpm_dump | \
842         grep -E "(SOURCE|PATCH)URL[0-9]*[       ]*${file}""[    ]*$" | \
843         sed -e 's/.*\(SOURCE\|PATCH\)URL\([0-9][0-9]*\).*/\1\2/' | \
844         head -n 1 | tr OURCEATH ourceath | xargs
845 }
846
847 src_md5() {
848         [ "$NO5" = "yes" ] && return
849         no=$(src_no "$1")
850         [ -z "$no" ] && return
851         cd $PACKAGE_DIR
852         local md5
853
854         if [ -f additional-md5sums ]; then
855                 local spec_rev=$(grep $SPECFILE CVS/Entries 2>/dev/null | sed -e s:/$SPECFILE/:: -e s:/.*::)
856                 if [ -z "$spec_rev" ]; then
857                         spec_rev=$(head -n 1 $SPECFILE | sed -e 's/.*\$Revision: \([0-9.]*\).*/\1/')
858                 fi
859                 local spec="$SPECFILE[0-9.,]*,$(echo $spec_rev | sed 's/\./\\./g')"
860                 md5=$(grep -s -v '^#' additional-md5sums | \
861                 grep -E "[      ]$(basename "$1")[      ]+${spec}([     ,]|\$)" | \
862                 sed -e 's/^\([0-9a-f]\{32\}\).*/\1/' | \
863                 grep -E '^[0-9a-f]{32}$')
864
865                 if [ "$md5" ]; then
866                         if [ $(echo "$md5" | wc -l) != 1 ] ; then
867                                 echo "$SPECFILE: more then one entry in additional-md5sums for $1" 1>&2
868                         fi
869                         echo "$md5" | tail -n 1
870                         return
871                 fi
872         fi
873
874         source_md5=`grep -i "^#[        ]*$no-md5[      ]*:" $SPECFILE | sed -e 's/.*://'`
875         if [ -n "$source_md5" ]; then
876                 echo $source_md5
877         else
878                 source_md5=`grep -i "BuildRequires:[    ]*digest(%SOURCE$no)[   ]*=" $SPECFILE | sed -e 's/.*=//'`
879                 if [ -n "$source_md5" ]; then
880                         echo $source_md5
881                 else
882                         # we have empty SourceX-md5, but it is still possible
883                         # that we have NoSourceX-md5 AND NoSource: X
884                         nosource_md5=`grep -i "^#[       ]*No$no-md5[    ]*:" $SPECFILE | sed -e 's/.*://'`
885                         if [ -n "$nosource_md5" -a -n "`grep -i "^NoSource:[     ]*$no$" $SPECFILE`" ] ; then
886                                 echo $nosource_md5
887                         fi
888                 fi
889         fi
890 }
891
892 distfiles_path() {
893         echo "by-md5/$(src_md5 "$1" | sed -e 's|^\(.\)\(.\)|\1/\2/&|')/$(basename "$1")"
894 }
895
896 distfiles_url() {
897         echo "$PROTOCOL$DISTFILES_SERVER/distfiles/$(distfiles_path "$1")"
898 }
899
900 distfiles_attic_url() {
901         echo "$PROTOCOL$ATTICDISTFILES_SERVER/distfiles/Attic/$(distfiles_path "$1")"
902 }
903
904 good_md5() {
905         md5=$(src_md5 "$1")
906         [ "$md5" = "" ] || \
907         [ "$md5" = "$(md5sum $(nourl "$1") 2> /dev/null | sed -e 's/ .*//')" ]
908 }
909
910 good_size() {
911         size=$(find $(nourl "$1") -printf "%s" 2>/dev/null)
912         [ -n "$size" -a "$size" -gt 0 ]
913 }
914
915 cvsignore_df() {
916         if [ "$CVSIGNORE_DF" != "yes" ]; then
917                 return
918         fi
919         cvsignore=${PACKAGE_DIR}/.cvsignore
920
921         # add only if not yet there
922         if ! awk -vf="$1" -vc=1 '$0 == f { c = 0 } END { exit c }' $cvsignore 2>/dev/null; then
923                 echo "$1" >> $cvsignore
924         fi
925 }
926
927 cvsup() {
928         update_shell_title "cvsup"
929         local OPTIONS="" ACTION="up"
930
931         # checkout
932         if [ "$1" = "-c" ]; then
933                 ACTION="co"
934                 shift
935         fi
936         # add
937         if [ "$1" = "-a" ]; then
938                 ACTION="add"
939                 shift
940         fi
941
942         OPTIONS="$ACTION "
943
944         if [ -n "$CVSROOT" ]; then
945                 OPTIONS="-d $CVSROOT $OPTIONS"
946         fi
947
948         if [ "$ACTION" != "add" ]; then
949                 if [ -z "$CVSDATE" -a -z "$CVSTAG" ]; then
950                         OPTIONS="$OPTIONS -A"
951                 else
952                         if [ -n "$CVSDATE" ]; then
953                                 OPTIONS="$OPTIONS -D $CVSDATE"
954                         fi
955                         if [ -n "$CVSTAG" ]; then
956                                 # FIXME: cvs add actually works with -r ?
957                                 OPTIONS="$OPTIONS -r $CVSTAG"
958                         fi
959                 fi
960         fi
961
962         local result=1
963         local retries_counter=0
964         if [ $# = 1 ]; then
965                 update_shell_title "cvsup: $*"
966         else
967                 update_shell_title "cvsup: $# files"
968         fi
969         while [ "$result" != "0" -a "$retries_counter" -le "$CVS_RETRIES" ]; do
970                 retries_counter=$(( $retries_counter + 1 ))
971                 output=$(LC_ALL=C $CVS_COMMAND $OPTIONS "$@" 2>&1)
972                 result=$?
973                 [ -n "$output" ] && echo "$output"
974                 if echo "$output" | grep -qE "(Cannot connect to|connect to .* failed|Connection reset by peer|Connection timed out|Unknown host)" \
975                         && [ "$result" -ne "0" -a "$retries_counter" -le "$CVS_RETRIES" ]; then
976                         echo "Trying again [$*]... ($retries_counter)"
977                         update_shell_title "cvsup: retry #$retries_counter"
978                         sleep 2
979                         continue
980                 else
981                         break
982                 fi
983         done
984         update_shell_title "cvsup: done!"
985         return $result
986 }
987
988 # returns true if "$1" is ftp, http or https protocol url
989 is_url() {
990         case "$1" in
991         ftp://*|http://*|https://*)
992                 return 0
993         ;;
994         esac
995         return 1
996 }
997
998 update_md5() {
999         if [ $# -eq 0 ]; then
1000                 return
1001         fi
1002
1003         update_shell_title "update md5"
1004         if [ -n "$DEBUG" ]; then
1005                 set -x
1006                 set -v
1007         fi
1008
1009         cd "$PACKAGE_DIR"
1010
1011         # pass 1: check files to be fetched
1012         local todo
1013         local need_files
1014         for i in "$@"; do
1015                 local fp=$(nourl "$i")
1016                 local srcno=$(src_no "$i")
1017                 if [ -n "$ADD5" ]; then
1018                         [ "$fp" = "$i" ] && continue # FIXME what is this check doing?
1019                         grep -qiE '^#[  ]*'$srcno'-md5[         ]*:' $PACKAGE_DIR/$SPECFILE && continue
1020                         grep -qiE '^BuildRequires:[     ]*digest[(]%SOURCE'$srcno'[)][  ]*=' $PACKAGE_DIR/$SPECFILE && continue
1021                 else
1022                         grep -qiE '^#[  ]*'$srcno'-md5[         ]*:' $PACKAGE_DIR/$SPECFILE || grep -qiE '^BuildRequires:[      ]*digest[(]%SOURCE'$srcno'[)][  ]*=' $PACKAGE_DIR/$SPECFILE || continue
1023                 fi
1024                 if [ ! -f "$fp" ] || [ $ALWAYS_CVSUP = "yes" ]; then
1025                         need_files="$need_files $i"
1026                 fi
1027         done
1028
1029         # pass 1a: get needed files
1030         if [ "$need_files" ]; then
1031                 get_files $need_files
1032         fi
1033
1034         # pass 2: proceed with md5 adding or updating
1035         for i in "$@"; do
1036                 local fp=$(nourl "$i")
1037                 local srcno=$(src_no "$i")
1038                 local md5=$(grep -iE '^#[       ]*(No)?'$srcno'-md5[    ]*:' $PACKAGE_DIR/$SPECFILE )
1039                 if [ -z "$md5" ]; then
1040                         md5=$(grep -iE '^[      ]*BuildRequires:[       ]*digest[(]%SOURCE'$srcno'[)][  ]*=' $PACKAGE_DIR/$SPECFILE )
1041                 fi
1042                 if [ -n "$ADD5" ] && is_url $i || [ -n "$md5" ]; then
1043                         local tag="# $srcno-md5:\t"
1044                         if [[ "$md5" == *NoSource* ]]; then
1045                                 tag="# NoSource$srcno-md5:\t"
1046                         elif [ -n "$USEDIGEST" ]; then
1047                                 tag="BuildRequires:\tdigest(%SOURCE$srcno) = "
1048                         fi
1049                         md5=$(md5sum "$fp" | cut -f1 -d' ')
1050                         echo "Updating $srcno ($md5: $fp)."
1051                         perl -i -ne '
1052                                 print unless (/^\s*#\s*(No)?'$srcno'-md5\s*:/i or /^\s*BuildRequires:\s*digest\(%SOURCE'$srcno'\)/i);
1053                                 print "'"$tag$md5"'\n" if /^'$srcno'\s*:\s+/i;
1054                         ' \
1055                         $PACKAGE_DIR/$SPECFILE
1056                 fi
1057         done
1058 }
1059
1060 check_md5() {
1061         local bad
1062         [ "$NO5" = "yes" ] && return
1063
1064         update_shell_title "check md5"
1065
1066         for i in "$@"; do
1067                 bad=0
1068                 if ! good_md5 "$i"; then
1069                         echo -n "MD5 sum mismatch."
1070                         bad=1
1071                 fi
1072                 if ! good_size "$i"; then
1073                         echo -n "0 sized file."
1074                         bad=1
1075                 fi
1076
1077                 if [ $bad -eq 1 ]; then
1078                         echo " Use -U to refetch sources,"
1079                         echo "or -5 to update md5 sums, if you're sure files are correct."
1080                         Exit_error err_no_source_in_repo $i
1081                 fi
1082         done
1083 }
1084
1085 get_files() {
1086         update_shell_title "get_files"
1087
1088         if [ -n "$DEBUG" ]; then
1089                 set -x
1090                 set -v
1091         fi
1092
1093         if [ $# -gt 0 ]; then
1094                 cd "$PACKAGE_DIR"
1095
1096                 if [ ! -s CVS/Root -a "$NOCVS" != "yes" ]; then
1097                         echo "Warning: No CVS access defined for SOURCES"
1098                         NOCVS="yes"
1099                 fi
1100
1101                 local nc=0
1102                 local get_files_cvs=""
1103                 for i in "$@"; do
1104                         nc=$((nc + 1))
1105                         local cvsup=0
1106                         SHELL_TITLE_PREFIX="get_files[$nc/$#]"
1107                         update_shell_title "$i"
1108                         local fp=`nourl "$i"`
1109                         if [ "$SKIP_EXISTING_FILES" = "yes" ] && [ -f "$fp" ]; then
1110                                 continue
1111                         fi
1112
1113                         FROM_DISTFILES=0
1114                         local srcmd5=$(src_md5 "$i")
1115
1116                         # we know if source/patch is present in cvs/distfiles
1117                         # - has md5 (in distfiles)
1118                         # - in cvs... ideas?
1119
1120                         # CHECK: local file didn't exist or always cvs up (first) requested.
1121                         if [ ! -f "$fp" ] || [ $ALWAYS_CVSUP = "yes" ]; then
1122                                 if echo $i | grep -vE '(http|ftp|https|cvs|svn)://' | grep -qE '\.(gz|bz2)$']; then
1123                                         echo "Warning: no URL given for $i"
1124                                 fi
1125                                 target="$fp"
1126
1127                                 if [ -z "$NODIST" ] && [ -n "$srcmd5" ]; then
1128                                         if good_md5 "$i" && good_size "$i"; then
1129                                                 echo "$fp having proper md5sum already exists"
1130                                                 continue
1131                                         fi
1132
1133                                         # optionally prefer mirror over distfiles if there's mirror
1134                                         # TODO: build url list and then try each url from the list
1135                                         if [ -n "$PREFMIRRORS" ] && [ -z "$NOMIRRORS" ] && im=$(find_mirror "$i") && [ "$im" != "$i" ]; then
1136                                                 url="$im"
1137                                         else
1138                                                 url=$(distfiles_url "$i")
1139                                         fi
1140
1141                                         url_attic=$(distfiles_attic_url "$i")
1142                                         FROM_DISTFILES=1
1143                                         # is $url local file?
1144                                         if [[ "$url" = [./]* ]]; then
1145                                                 update_shell_title "${GETLOCAL%% *}: $url"
1146                                                 ${GETLOCAL} $url $target
1147                                         else
1148                                                 if [ -z "$NOMIRRORS" ]; then
1149                                                         url=$(find_mirror "$url")
1150                                                 fi
1151
1152                                                 local uri=${url}
1153                                                 # make shorter message for distfiles urls
1154                                                 if [[ "$uri" = ${PROTOCOL}${DISTFILES_SERVER}* ]] || [[ "$uri" = ${PROTOCOL}${ATTICDISTFILES_SERVER}* ]]; then
1155                                                         uri=${uri#${PROTOCOL}${DISTFILES_SERVER}/distfiles/by-md5/?/?/*/}
1156                                                         uri=${uri#${PROTOCOL}${ATTICDISTFILES_SERVER}/distfiles/by-md5/?/?/*/}
1157                                                         uri="df: $uri"
1158                                                 fi
1159                                                 update_shell_title "${GETURI%% *}: $uri"
1160                                                 ${GETURI} ${OUTFILEOPT} "$target" "$url" || \
1161                                                 if [ "`echo $url | grep -E 'ftp://'`" ]; then
1162                                                         update_shell_title "${GETURI2%% *}: $url"
1163                                                         ${GETURI2} ${OUTFILEOPT} "$target" "$url"
1164                                                 fi
1165                                         fi
1166
1167                                         # is it empty file?
1168                                         if [ ! -s "$target" ]; then
1169                                                 rm -f "$target"
1170                                                 if [ `echo $url_attic | grep -E '^(\.|/)'` ]; then
1171                                                         update_shell_title "${GETLOCAL%% *}: $url_attic"
1172                                                         ${GETLOCAL} $url_attic $target
1173                                                 else
1174                                                         if [ -z "$NOMIRRORS" ]; then
1175                                                                 url_attic=$(find_mirror "$url_attic")
1176                                                         fi
1177                                                         update_shell_title "${GETURI%% *}: $url_attic"
1178                                                         ${GETURI} ${OUTFILEOPT} "$target" "$url_attic" || \
1179                                                         if [ "`echo $url_attic | grep -E 'ftp://'`" ]; then
1180                                                                 update_shell_title "${GETURI2%% *}: $url_attic"
1181                                                                 ${GETURI2} ${OUTFILEOPT} "$target" "$url_attic"
1182                                                         fi
1183                                                         test -s "$target" || rm -f "$target"
1184                                                 fi
1185                                         fi
1186
1187                                         if [ -s "$target" ]; then
1188                                                 cvsignore_df $target
1189                                         else
1190                                                 rm -f "$target"
1191                                                 FROM_DISTFILES=0
1192                                         fi
1193                                 elif [ "$NOCVS" != "yes" -a -z "$srcmd5" ]; then
1194                                         if [ $# -gt 1 ]; then
1195                                                 get_files_cvs="$get_files_cvs $fp"
1196                                                 update_shell_title "$fp (will cvs up later)"
1197                                                 cvsup=1
1198                                         else
1199                                                 cvsup $fp
1200                                         fi
1201                                 fi
1202
1203                                 if [ -z "$NOURLS" ] && [ ! -f "$fp" -o -n "$UPDATE" ] && [ "`echo $i | grep -E 'ftp://|http://|https://'`" ]; then
1204                                         if [ -z "$NOMIRRORS" ]; then
1205                                                 im=$(find_mirror "$i")
1206                                         else
1207                                                 im="$i"
1208                                         fi
1209                                         update_shell_title "${GETURI%% *}: $im"
1210                                         ${GETURI} "$im" ${OUTFILEOPT} "$target" || \
1211                                         if [ "`echo $im | grep -E 'ftp://'`" ]; then
1212                                                 update_shell_title "${GETURI2%% *}: $im"
1213                                                 ${GETURI2} "$im" ${OUTFILEOPT} "$target"
1214                                         fi
1215                                         test -s "$target" || rm -f "$target"
1216                                 fi
1217
1218                                 if [ "$cvsup" = 1 ]; then
1219                                         continue
1220                                 fi
1221
1222                         fi
1223
1224                         # the md5 check must be moved elsewhere as if we've called from update_md5 the md5 is wrong.
1225                         if [ ! -f "$fp" -a "$FAIL_IF_NO_SOURCES" != "no" ]; then
1226                                 if [ -n "GREEDSRC" ]; then
1227                                         get_greed_sources $i
1228                                 else
1229                                         Exit_error err_no_source_in_repo $i
1230                                 fi
1231                         fi
1232
1233                         # we check md5 here just only to refetch immediately
1234                         if good_md5 "$i" && good_size "$i"; then
1235                                 :
1236                         elif [ "$FROM_DISTFILES" = 1 ]; then
1237                                 # wrong md5 from distfiles: remove the file and try again
1238                                 # but only once ...
1239                                 echo "MD5 sum mismatch. Trying full fetch."
1240                                 FROM_DISTFILES=2
1241                                 rm -f $target
1242                                 update_shell_title "${GETURI%% *}: $url"
1243                                 ${GETURI} ${OUTFILEOPT} "$target" "$url" || \
1244                                 if [ "`echo $url | grep -E 'ftp://'`" ]; then
1245                                         update_shell_title "${GETURI2%% *}: $url"
1246                                         ${GETURI2} ${OUTFILEOPT} "$target" "$url"
1247                                 fi
1248                                 if [ ! -s "$target" ]; then
1249                                         rm -f "$target"
1250                                         update_shell_title "${GETURI%% *}: $url_attic"
1251                                         ${GETURI} ${OUTFILEOPT} "$target" "$url_attic" || \
1252                                         if [ "`echo $url_attic | grep -E 'ftp://'`" ]; then
1253                                                 update_shell_title "${GETURI2%% *}: $url_attic"
1254                                                 ${GETURI2} ${OUTFILEOPT} "$target" "$url_attic"
1255                                         fi
1256                                 fi
1257                                 test -s "$target" || rm -f "$target"
1258                         fi
1259                 done
1260                 SHELL_TITLE_PREFIX=""
1261
1262                 if [ "$get_files_cvs" ]; then
1263                         cvsup $get_files_cvs
1264                 fi
1265
1266                 if [ "$CHMOD" = "yes" ]; then
1267                         CHMOD_FILES=$(nourl "$@")
1268                         if [ -n "$CHMOD_FILES" ]; then
1269                                 chmod $CHMOD_MODE $CHMOD_FILES
1270                         fi
1271                 fi
1272         fi
1273 }
1274
1275 make_tagver() {
1276         if [ -n "$DEBUG" ]; then
1277                 set -x
1278                 set -v
1279         fi
1280
1281         # Check whether first character of PACKAGE_NAME is legal for tag name
1282         if [ -z "${PACKAGE_NAME##[_0-9]*}" -a -z "$TAG_PREFIX" ]; then
1283                 TAG_PREFIX=tag_
1284         fi
1285
1286         # NOTE: CVS tags may must not contain the characters `$,.:;@'
1287         TAGVER=$(echo $TAG_PREFIX$PACKAGE_NAME-$PACKAGE_VERSION-$PACKAGE_RELEASE | tr '[.@]' '[_#]')
1288
1289         # Remove #kernel.version_release from TAGVER because tagging sources
1290         # could occur with different kernel-headers than kernel-headers used at build time.
1291         # besides, %{_kernel_ver_str} is not expanded.
1292
1293         # TAGVER=auto-ac-madwifi-ng-0-0_20070225_1#%{_kernel_ver_str}
1294         # TAGVER=auto-ac-madwifi-ng-0-0_20070225_1
1295
1296         TAGVER=${TAGVER%#*}
1297         echo -n "$TAGVER"
1298 }
1299
1300 # bool is_tag_a_branch(tag)
1301 #
1302 # returns 1 if a tag is a branch set on SPECFILE
1303 is_tag_a_branch() {
1304         if [ -n "$DEBUG" ]; then
1305                 set -x
1306                 set -v
1307         fi
1308
1309         if [ $# -ne 1 ]; then
1310                 return 0;
1311         fi
1312
1313         TAG=$1
1314         # escape some regexp characters if part of TAG
1315         TAG=$(echo "$TAG" | sed -e 's#\([\+\*\.]\)#\\\1#g')
1316
1317
1318         cd "$PACKAGE_DIR"
1319         $CVS_COMMAND status -v $SPECFILE | grep -Eiq "${TAG}.+(branch: [0-9.]+)"
1320         return $?
1321 }
1322
1323 tag_files() {
1324         TAG_FILES="$@"
1325
1326         if [ -n "$DEBUG" ]; then
1327                 set -x
1328                 set -v
1329         fi
1330
1331         echo "Version: $PACKAGE_VERSION"
1332         echo "Release: $PACKAGE_RELEASE"
1333
1334         local TAGVER
1335         if [ "$TAG_VERSION" = "yes" ]; then
1336                 TAGVER=`make_tagver`
1337                 echo "CVS tag: $TAGVER"
1338         fi
1339         if [ -n "$TAG" ]; then
1340                 echo "CVS tag: $TAG"
1341         fi
1342
1343         local OPTIONS="tag $CVS_FORCE"
1344         if [ -n "$CVSROOT" ]; then
1345                 OPTIONS="-d $CVSROOT $OPTIONS"
1346         fi
1347
1348         # if a tagname we are about to set already exists
1349         # and happens to be a branch (common case with AC-branch)
1350         # pass -B (allows -F to disturb branch tag)
1351         local _tag=$TAG
1352         if [ "$TAG_VERSION" = "yes" ]; then
1353                 _tag=$TAGVER
1354         fi;
1355         is_tag_a_branch $_tag
1356         if [ $? -eq 0 -a $CVS_NSERVER -eq 0 ]; then
1357                 OPTIONS="$OPTIONS -B"
1358         fi;
1359
1360         cd "$PACKAGE_DIR"
1361         local tag_files
1362         for i in $TAG_FILES; do
1363                 # don't tag files stored on distfiles
1364                 [ -n "`src_md5 $i`" ] && continue
1365                 local fp=`nourl "$i"`
1366                 if [ -f "$fp" ]; then
1367                         tag_files="$tag_files $fp"
1368                 elif [ -n "GREEDSRC" ]; then
1369                         get_greed_sources $i
1370                 else
1371                         Exit_error err_no_source_in_repo $i
1372                 fi
1373         done
1374
1375         if [ "$tag_files" ]; then
1376                 if [ "$TAG_VERSION" = "yes" ]; then
1377                         update_shell_title "tag sources: $TAGVER"
1378                         printf "Tagging %d files\n" $(echo $tag_files | wc -w)
1379                         $CVS_COMMAND $OPTIONS $TAGVER $tag_files || exit
1380                 fi
1381                 if [ -n "$TAG" ]; then
1382                         update_shell_title "tag sources: $TAG"
1383
1384                         while [ "$tag_files" ]; do
1385                                 local chunk=$(echo $tag_files | tr ' ' '\n' | head -n 100)
1386                                 printf "Tagging %d files\n" $(echo $chunk | wc -w)
1387                                 $CVS_COMMAND $OPTIONS $TAG $chunk || exit
1388                                 tag_files=$(echo $tag_files | tr ' ' '\n' | tail +101)
1389                         done
1390                 fi
1391         fi
1392
1393         cd "$PACKAGE_DIR"
1394         if [ "$TAG_VERSION" = "yes" ]; then
1395                 update_shell_title "tag spec: $TAGVER"
1396                 $CVS_COMMAND $OPTIONS $TAGVER $SPECFILE || exit
1397         fi
1398         if [ -n "$TAG" ]; then
1399                 update_shell_title "tag spec: $TAG"
1400                 $CVS_COMMAND $OPTIONS $TAG $SPECFILE || exit
1401         fi
1402 }
1403
1404 branch_files() {
1405         TAG=$1
1406         echo "CVS branch tag: $TAG"
1407         shift
1408
1409         TAG_FILES="$@"
1410
1411         if [ -n "$DEBUG" ]; then
1412                 set -x
1413                 set -v
1414         fi
1415
1416         local OPTIONS="tag $CVS_FORCE -b"
1417
1418         # branch exists?
1419         is_tag_a_branch $TAG
1420         if [ $? -eq 1 ]; then
1421                 OPTIONS="$OPTIONS -B"
1422         fi
1423
1424         if [ -n "$CVSROOT" ]; then
1425                 OPTIONS="-d $CVSROOT $OPTIONS"
1426         fi
1427         cd "$PACKAGE_DIR"
1428         local tag_files
1429         for i in $TAG_FILES; do
1430                 local fp=`nourl "$i"`
1431                 if [ -f "$fp" ]; then
1432                         tag_files="$tag_files $fp"
1433                 elif [ -n "GREEDSRC" ]; then
1434                         get_greed_sources $i
1435                 else
1436                         Exit_error err_no_source_in_repo $i
1437                 fi
1438         done
1439         if [ "$tag_files" ]; then
1440                 $CVS_COMMAND $OPTIONS $TAG $tag_files || exit
1441         fi
1442
1443         cd "$PACKAGE_DIR"
1444         $CVS_COMMAND $OPTIONS $TAG $SPECFILE || exit
1445 }
1446
1447
1448 # this function should exit early if package can't be built for this arch
1449 # this avoids unneccessary BR filling.
1450 check_buildarch() {
1451         local out ret
1452         out=$(minirpm --short-circuit -bp --define "'prep exit 0'" --nodeps $SPECFILE 2>&1)
1453         ret=$?
1454         if [ $ret -ne 0 ]; then
1455                 echo >&2 "$out"
1456                 exit $ret
1457         fi
1458 }
1459
1460 # from relup.sh
1461 set_release() {
1462         local specfile="$1"
1463         local rel="$2"
1464         local newrel="$3"
1465         sed -i -e "
1466                 s/^\(%define[ \t]\+_\?rel[ \t]\+\)$rel\$/\1$newrel/
1467                 s/^\(Release:[ \t]\+\)$rel\$/\1$newrel/
1468         " $specfile
1469 }
1470
1471 set_version() {
1472         local specfile="$1"
1473         local ver="$2" subver=$ver
1474         local newver="$3" newsubver=$newver
1475
1476         # try handling subver, everything that's not numeric-dotted in version
1477         if grep -Eq '%define\s+subver' $specfile; then
1478                 subver=$(echo "$ver" | sed -re 's,^[0-9.]+,,')
1479                 ver=${ver%$subver}
1480                 newsubver=$(echo "$newver" | sed -re 's,^[0-9.]+,,')
1481                 newver=${newver%$newsubver}
1482         fi
1483         sed -i -e "
1484                 s/^\(%define[ \t]\+_\?ver[ \t]\+\)$ver\$/\1$newver/
1485                 s/^\(%define[ \t]\+subver[ \t]\+\)$subver\$/\1$newsubver/
1486                 s/^\(Version:[ \t]\+\)$ver\$/\1$newver/
1487         " $specfile
1488 }
1489
1490 try_upgrade() {
1491         if [ -n "$TRY_UPGRADE" ]; then
1492                 local TNOTIFY TNEWVER TOLDVER
1493                 update_shell_title "build_package: try_upgrade"
1494
1495                 cd "$PACKAGE_DIR"
1496                 
1497                 if [ "$UPGRADE_VERSION" ]; then
1498                         TNEWVER=$UPGRADE_VERSION
1499                 else
1500                         if [ -n "$FLOAT_VERSION" ]; then
1501                                 TNOTIFY=$($APPDIR/pldnotify.awk ${BE_VERBOSE:+-vDEBUG=1} $SPECFILE -n) || exit 1
1502                         else
1503                                 TNOTIFY=$($APPDIR/pldnotify.awk ${BE_VERBOSE:+-vDEBUG=1} $SPECFILE) || exit 1
1504                         fi
1505
1506                         # pldnotify.awk does not set exit codes, but it has match for ERROR
1507                         # in output which means so.
1508                         if [[ "$TNOTIFY" = *ERROR* ]]; then
1509                                 echo >&2 "$TNOTIFY"
1510                                 exit 1
1511                         fi
1512
1513                         TNEWVER=$(echo $TNOTIFY | awk '{ match($4,/\[NEW\]/); print $5 }')
1514                 fi
1515
1516                 if [ -n "$TNEWVER" ]; then
1517                         TOLDVER=`echo $TNOTIFY | awk '{ print $3; }'`
1518                         echo "New version found, updating spec file from $TOLDVER to version $TNEWVER"
1519                         if [ "$REVERT_BROKEN_UPGRADE" = "yes" ]; then
1520                                 cp -f $SPECFILE $SPECFILE.bak
1521                         fi
1522                         chmod +w $SPECFILE
1523                         set_release $SPECFILE $PACKAGE_RELEASE 1
1524                         set_version $SPECFILE $PACKAGE_VERSION $TNEWVER
1525                         parse_spec
1526                         if [ "$PACKAGE_VERSION" != "$TNEWVER" ]; then
1527                                 echo >&2 "Upgrading version failed, you need to update spec yourself"
1528                                 exit 1
1529                         fi
1530                         return 1
1531                 fi
1532         fi
1533         return 0
1534 }
1535
1536 build_package() {
1537         update_shell_title "build_package"
1538         if [ -n "$DEBUG" ]; then
1539                 set -x
1540                 set -v
1541         fi
1542
1543         cd "$PACKAGE_DIR"
1544
1545         case "$COMMAND" in
1546                 build )
1547                         BUILD_SWITCH="-ba" ;;
1548                 build-binary )
1549                         BUILD_SWITCH="-bb" ;;
1550                 build-source )
1551                         BUILD_SWITCH="-bs --nodeps" ;;
1552                 build-prep )
1553                         BUILD_SWITCH="-bp --nodeps" ;;
1554                 build-build )
1555                         BUILD_SWITCH="-bc" ;;
1556                 build-install )
1557                         BUILD_SWITCH="-bi" ;;
1558                 build-list )
1559                         BUILD_SWITCH="-bl" ;;
1560
1561         esac
1562
1563         update_shell_title "build_package: $COMMAND"
1564         if [ -n "$LOGFILE" ]; then
1565                 LOG=`eval echo $LOGFILE`
1566                 if [ -d "$LOG" ]; then
1567                         echo "Log file $LOG is a directory."
1568                         echo "Parse error in the spec?"
1569                         Exit_error err_build_fail
1570                 fi
1571                 if [ -n "$LASTLOG_FILE" ]; then
1572                         echo "LASTLOG=$LOG" > $LASTLOG_FILE
1573                 fi
1574                 RES_FILE=$(tempfile)
1575
1576                 (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
1577                 RETVAL=`cat $RES_FILE`
1578                 rm $RES_FILE
1579                 if [ -n "$LOGDIROK" ] && [ -n "$LOGDIRFAIL" ]; then
1580                         if [ "$RETVAL" -eq "0" ]; then
1581                                 mv $LOG $LOGDIROK
1582                         else
1583                                 mv $LOG $LOGDIRFAIL
1584                         fi
1585                 fi
1586         else
1587                 eval ${NICE_COMMAND} $RPMBUILD $TARGET_SWITCH $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $RPMBUILDOPTS $BCOND --define \'_specdir $PACKAGE_DIR\' --define \'_sourcedir $PACKAGE_DIR\' $SPECFILE
1588                 RETVAL=$?
1589         fi
1590         if [ "$RETVAL" -ne "0" ]; then
1591                 if [ -n "$TRY_UPGRADE" ]; then
1592                         echo "\nUpgrade package to new version failed."
1593                         if [ "$REVERT_BROKEN_UPGRADE" = "yes" ]; then
1594                                 echo "Restoring old spec file."
1595                                 mv -f $SPECFILE.bak $SPECFILE
1596                         fi
1597                         echo ""
1598                 fi
1599                 Exit_error err_build_fail
1600         fi
1601         unset BUILD_SWITCH
1602 }
1603
1604 nourl() {
1605         echo "$@" | sed 's#\<\(ftp\|http\|https\|cvs\|svn\)://[^ ]*/##g'
1606 }
1607
1608 install_required_packages() {
1609         run_poldek -vi $1
1610         return $?
1611 }
1612
1613 find_spec_bcond() { # originally from /usr/lib/rpm/find-spec-bcond
1614         local SPEC="$1"
1615         awk -F"\n" '
1616         /^%changelog/ { exit }
1617         /^%bcond_with/{
1618                 match($0, /bcond_with(out)?[ \t]+[_a-zA-Z0-9]+/);
1619                 bcond = substr($0, RSTART + 6, RLENGTH - 6);
1620                 gsub(/[ \t]+/, "_", bcond);
1621                 print bcond
1622         }' $SPEC | LC_ALL=C sort -u
1623 }
1624
1625 process_bcondrc() {
1626         # expand bconds from ~/.bcondrc
1627         # The file structure is like gentoo's package.use:
1628         # ---
1629         # * -selinux
1630         # samba -mysql -pgsql
1631         # w32codec-installer license_agreement
1632         # php +mysqli
1633         # ---
1634         if ([ -f $HOME/.bcondrc ] || ([ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ])); then
1635                 :
1636         else
1637                 return
1638         fi
1639
1640         SN=${SPECFILE%%\.spec}
1641
1642         local bcondrc=$HOME/.bcondrc
1643         [ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ] && bcondrc=$HOME_ETC/.bcondrc
1644
1645         local bcond_avail=$(find_spec_bcond $SPECFILE)
1646
1647         while read pkg flags; do
1648                 # ignore comments
1649                 [[ "$pkg" == \#* ]] && continue
1650
1651                 # any package or current package?
1652                 if [ "$pkg" = "*" ] || [ "$pkg" = "$PACKAGE_NAME" ] || [ "$pkg" = "$SN" ]; then
1653                         for flag in $flags; do
1654                                 local opt=${flag#[+-]}
1655
1656                                 # use only flags which are in this package.
1657                                 if [[ $bcond_avail = *${opt}* ]]; then
1658                                         if [[ $flag = -* ]]; then
1659                                                 if [[ $BCOND != *--with?${opt}* ]]; then
1660                                                         BCOND="$BCOND --without $opt"
1661                                                 fi
1662                                         else
1663                                                 if [[ $BCOND != *--without?${opt}* ]]; then
1664                                                         BCOND="$BCOND --with $opt"
1665                                                 fi
1666                                         fi
1667                                 fi
1668                         done
1669                 fi
1670         done < $bcondrc
1671         update_shell_title "parse ~/.bcondrc: DONE!"
1672 }
1673
1674 set_bconds_values() {
1675         update_shell_title "set bcond values"
1676
1677         AVAIL_BCONDS_WITHOUT=""
1678         AVAIL_BCONDS_WITH=""
1679
1680         if grep -Eq '^# *_with' ${SPECFILE}; then
1681                 echo >&2 "ERROR: This spec has old style bconds."
1682                 exit 1
1683         fi
1684
1685         if ! grep -q '^%bcond' ${SPECFILE}; then
1686                 return
1687         fi
1688
1689         local bcond_avail=$(find_spec_bcond $SPECFILE)
1690         process_bcondrc "$SPECFILE"
1691
1692         update_shell_title "parse bconds"
1693
1694         local opt bcond
1695         for opt in $bcond_avail; do
1696                 case "$opt" in
1697                 without_*)
1698                         bcond=${opt#without_}
1699                         case "$BCOND" in
1700                         *--without?${bcond}*)
1701                                 AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT <$bcond>"
1702                                 ;;
1703                         *)
1704                                 AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT $bcond"
1705                                 ;;
1706                         esac
1707                         ;;
1708                 with_*)
1709                         bcond=${opt#with_}
1710                         case "$BCOND" in
1711                         *--with?${bcond}*)
1712                                 AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH <$bcond>"
1713                                 ;;
1714                         *)
1715                                 AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH $bcond"
1716                                 ;;
1717                         esac
1718                         ;;
1719                 *)
1720                         echo >&2 "ERROR: unexpected '$opt' in set_bconds_values"
1721                         exit 1
1722                         ;;
1723                 esac
1724         done
1725 }
1726
1727 run_sub_builder() {
1728         package_name="${1}"
1729         update_shell_title "run_sub_builder $package_name"
1730         #
1731         # No i tutaj bym chcia³ zrobiæ sztuczn± inteligencjê, która spróbuje tego
1732         # pakieta zbudowaæ. Aktualnie niewiele dziala, bo generalnie nie widze do
1733         # konca algorytmu... Ale damy rade. :) Na razie po prostu sie wyjebie tak samo
1734         # jakby nie bylo tego kawalka kodu.
1735         #
1736         # Update: Poprawi³em parê rzeczy i zaczê³o generowaæ pakiety spoza zadanej listy.
1737         #         Jednym s³owem budowanie niespoldkowanych zale¿no¶ci dzia³a w paru przypadkach.
1738         #
1739         #
1740         # y0shi.
1741         # kurwa. translate that ^^^^
1742
1743         parent_spec_name=''
1744
1745         # Istnieje taki spec? ${package}.spec
1746         if [ -f "${PACKAGE_DIR}/${package}.spec" ]; then
1747                 parent_spec_name=${package}.spec
1748         elif [ -f "${PACKAGE_DIR}/$(echo ${package_name} | sed -e s,-devel.*,,g -e s,-static,,g).spec" ]; then
1749                 parent_spec_name="$(echo ${package_name} | sed -e s,-devel.*,,g -e s,-static,,g).spec"
1750         else
1751                 for provides_line in $(grep -r ^Provides:.*$package ${PACKAGE_DIR}); do
1752                         echo $provides_line
1753                 done
1754         fi
1755
1756         if [ "${parent_spec_name}" != "" ]; then
1757                 spawn_sub_builder $parent_spec_name
1758         fi
1759         NOT_INSTALLED_PACKAGES="$NOT_INSTALLED_PACKAGES $package_name"
1760 }
1761
1762 spawn_sub_builder() {
1763         package_name="${1}"
1764         update_shell_title "spawn_sub_builder $package_name"
1765
1766         sub_builder_opts=''
1767         if [ "${FETCH_BUILD_REQUIRES}" = "yes" ]; then
1768                 sub_builder_opts="${sub_builder_opts} -R"
1769         fi
1770         if [ "${REMOVE_BUILD_REQUIRES}" = "nice" ]; then
1771                 sub_builder_opts="${sub_builder_opts} -RB"
1772         elif [ "${REMOVE_BUILD_REQUIRES}" = "force" ]; then
1773                 sub_builder_opts="${sub_builder_opts} -FRB"
1774         fi
1775         if [ "${UPDATE_POLDEK_INDEXES}" = "yes" ]; then
1776                 sub_builder_opts="${sub_builder_opts} -Upi"
1777         fi
1778
1779         cd "${PACKAGE_DIR}"
1780         ./builder ${sub_builder_opts} "$@"
1781 }
1782
1783 remove_build_requires() {
1784         if [ "$INSTALLED_PACKAGES" != "" ]; then
1785                 case "$REMOVE_BUILD_REQUIRES" in
1786                         "force")
1787                                 run_poldek --noask -ve $INSTALLED_PACKAGES
1788                                 ;;
1789                         "nice")
1790                                 run_poldek --ask -ve $INSTALLED_PACKAGES
1791                                 ;;
1792                         *)
1793                                 echo You may want to manually remove following BuildRequires fetched:
1794                                 echo $INSTALLED_PACKAGES
1795                                 echo "Try poldek -e \`cat $(pwd)/.${SPECFILE}_INSTALLED_PACKAGES\`"
1796                                 ;;
1797                 esac
1798         fi
1799 }
1800
1801 display_bconds() {
1802         if [ "$AVAIL_BCONDS_WITH" -o "$AVAIL_BCONDS_WITHOUT" ]; then
1803                 if [ "$BCOND" ]; then
1804                         echo ""
1805                         echo "Building $SPECFILE with the following conditional flags:"
1806                         echo -n "$BCOND"
1807                 else
1808                         echo ""
1809                         echo "No conditional flags passed"
1810                 fi
1811                 echo ""
1812                 echo "from available:"
1813                 echo "--with   :\t$AVAIL_BCONDS_WITH"
1814                 echo "--without:\t$AVAIL_BCONDS_WITHOUT"
1815                 echo ""
1816         fi
1817 }
1818
1819 display_branches() {
1820         if [ "$NOCVSSPEC" != "yes" ]; then
1821                 echo -n "Available branches: "
1822                 $CVS_COMMAND status -v "${SPECFILE}" | awk '!/Sticky Tag:/ && /\(branch:/ { print $1 } ' | xargs
1823         fi
1824 }
1825
1826 # checks a given list of packages/files/provides agains current rpmdb.
1827 # outputs all dependencies which current rpmdb doesn't satisfy.
1828 # input can be either STDIN or parameters
1829 _rpm_prov_check() {
1830         local DEPS
1831
1832         if [ $# -gt 0 ]; then
1833                 DEPS="$@"
1834         else
1835                 DEPS=$(cat)
1836         fi
1837
1838         DEPS=$(LANG=C rpm -q --whatprovides $DEPS 2>&1 | awk '/^(error:|no package provides)/ { print }')
1839
1840         # packages
1841         echo "$DEPS" | awk '/^no package provides/ { print $NF }'
1842
1843         # other deps (files)
1844         echo "$DEPS" | awk -F: '/^error:.*No such file/{o = $2; gsub("^ file ", "", o); print o}'
1845 }
1846
1847 # checks if given package/files/provides exists in rpmdb.
1848 # input can be either stdin or parameters
1849 # returns packages which are present in the rpmdb
1850 _rpm_cnfl_check() {
1851         local DEPS
1852
1853         if [ $# -gt 0 ]; then
1854                 DEPS="$@"
1855         else
1856                 DEPS=$(cat)
1857         fi
1858
1859         LANG=C rpm -q --whatprovides $DEPS 2>/dev/null | awk '!/no package provides/ { print }'
1860 }
1861
1862 # install deps via information from 'rpm-getdeps' or 'rpm --specsrpm'
1863 install_build_requires_rpmdeps() {
1864         if [ "$FETCH_BUILD_REQUIRES_RPMGETDEPS" = "yes" ]; then
1865                 # TODO: Conflicts list doesn't check versions
1866                 local CNFL=$(rpm-getdeps $BCOND $SPECFILE 2> /dev/null | awk '/^\-/ { print $3 } ' | _rpm_cnfl_check | xargs)
1867                 local DEPS=$(rpm-getdeps $BCOND $SPECFILE 2> /dev/null | awk '/^\+/ { print $3 } ' | _rpm_prov_check | xargs)
1868         fi
1869         if [ "$FETCH_BUILD_REQUIRES_RPMSPECSRPM" = "yes" ]; then
1870                 local CNFL=$(rpm -q --specsrpm --conflicts $BCOND $SPECFILE | awk '{print $1}' | _rpm_cnfl_check | xargs)
1871                 local DEPS=$(rpm -q --specsrpm --requires $BCOND $SPECFILE | awk '{print $1}' | _rpm_prov_check | xargs)
1872         fi
1873
1874         if [ -n "$CNFL" ] || [ -n "$DEPS" ]; then
1875                 echo "fetch BuildRequires: install [$DEPS]; remove [$CNFL]"
1876                 update_shell_title "poldek: install [$DEPS]; remove [$CNFL]"
1877                 $SU_SUDO /usr/bin/poldek -q --update || $SU_SUDO /usr/bin/poldek -q --upa
1878         fi
1879         if [ -n "$CNFL" ]; then
1880                 update_shell_title "uninstall conflicting packages: $CNFL"
1881                 echo "Trying to uninstall conflicting packages ($CNFL):"
1882                 $SU_SUDO /usr/bin/poldek --noask --nofollow -ev $CNFL
1883         fi
1884
1885         while [ "$DEPS" ]; do
1886                         update_shell_title "install deps: $DEPS"
1887                         echo "Trying to install dependencies ($DEPS):"
1888                         local log=.${SPECFILE}_poldek.log
1889                         LANG=C $SU_SUDO /usr/bin/poldek --noask --caplookup -uGqQ $DEPS | tee $log
1890                         failed=$(awk '/^error:/{a=$2; sub(/^error: /, "", a); sub(/:$/, "", a); print a}' $log)
1891                         rm -f $log
1892                         local ok
1893                         if [ -n "$failed" ]; then
1894                                 for package in $failed; do
1895                                         spawn_sub_builder -bb $(depspecname $package) && ok="$ok $package"
1896                                 done
1897                                 DEPS="$ok"
1898                         else
1899                                 DEPS=""
1900                         fi
1901         done
1902 }
1903
1904 fetch_build_requires()
1905 {
1906         if [ "${FETCH_BUILD_REQUIRES}" != "yes" ]; then
1907                 return
1908         fi
1909
1910         update_shell_title "fetch build requires"
1911         if [ "$FETCH_BUILD_REQUIRES_RPMGETDEPS" = "yes" ] || [ "$FETCH_BUILD_REQUIRES_RPMSPECSRPM" = "yes" ]; then
1912                 install_build_requires_rpmdeps
1913                 return
1914         fi
1915
1916                 # XXX is this ugliest code written in human history still needed?
1917                 echo "All packages installed by fetch_build_requires() are written to:"
1918                 echo "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES"
1919                 echo ""
1920                 echo "If anything fails, you may get rid of them by executing:"
1921                 echo "poldek -e \`cat `pwd`/.${SPECFILE}_INSTALLED_PACKAGES\`"
1922                 echo ""
1923                 echo > `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1924                 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)
1925                 do
1926                         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)
1927                         GO="yes"
1928                         package=$(basename "$package_item"|sed -e "s/}$//g")
1929                         COND_ARCH_TST=$(cat $SPECFILE|grep -B1 BuildRequires|grep -B1 $package|grep ifarch|sed -e "s/^.*ifarch//g")
1930                         mach=$(uname -m)
1931
1932                         COND_TST=`cat $SPECFILE|grep BuildRequires|grep "$package"`
1933                         if `echo $COND_TST|grep -q '^BuildRequires:'`; then
1934                                 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
1935                                         GO="yes"
1936                                 fi
1937                         # bcond:
1938                         else
1939                                 COND_NAME=`echo $COND_TST|sed -e s,:BuildRequires:.*$,,g`
1940                                 GO=""
1941                                 # %{without}
1942                                 if `echo $COND_TST|grep -q 'without_'`; then
1943                                         COND_NAME=`echo $COND_NAME|sed -e s,^.*without_,,g`
1944                                         if `echo $COND_TST|grep -q !`; then
1945                                                 COND_STATE="with"
1946                                         else
1947                                                 COND_STATE="wout"
1948                                         fi
1949                                         COND_WITH=`echo $AVAIL_BCONDS_WITH|grep "<$COND_NAME>"`
1950                                         COND_WITHOUT=`echo $AVAIL_BCONDS_WITHOUT|grep "<$COND_NAME>"`
1951                                         if [ -n "$COND_WITHOUT" ] || [ -z "$COND_WITH" ]; then
1952                                                 COND_ARGV="wout"
1953                                         else
1954                                                 COND_ARGV="with"
1955                                         fi
1956                                 # %{with}
1957                                 elif `echo $COND_TST|grep -q 'with_'`; then
1958                                         COND_NAME=`echo $COND_NAME|sed -e s,^.*with_,,g`
1959                                         if `echo $COND_TST|grep -q !`; then
1960                                                 COND_STATE="wout"
1961                                         else
1962                                                 COND_STATE="with"
1963                                         fi
1964                                         COND_WITH=`echo $AVAIL_BCONDS_WITH|grep "<$COND_NAME>"`
1965                                         COND_WITHOUT=`echo $AVAIL_BCONDS_WITHOUT|grep "<$COND_NAME>"`
1966                                         if [ -n "$COND_WITH" ] || [ -z "$COND_WITHOUT" ]; then
1967                                                 COND_ARGV="with"
1968                                         else
1969                                                 COND_ARGV="wout"
1970                                         fi
1971                                 fi
1972                                 RESULT="${COND_STATE}-${COND_ARGV}"
1973                                 case "$RESULT" in
1974                                         "with-wout" | "wout-with" )
1975                                                 GO=""
1976                                                 ;;
1977                                         "wout-wout" | "with-with" )
1978                                                 GO="yes"
1979                                                 ;;
1980                                         * )
1981                                                 echo "Action '$RESULT' was not defined for package '$package_item'"
1982                                                 GO="yes"
1983                                                 ;;
1984                                 esac
1985                         fi
1986
1987                         if [ "$GO" = "yes" ]; then
1988                                 if [ "`rpm -q $package|sed -e "s/$package.*/$package/g"`" != "$package" ]; then
1989                                         echo "Testing if $package has subrequirements..."
1990                                         run_poldek -t -i $package --dumpn=".$package-req.txt"
1991                                         if [ -f ".$package-req.txt" ]; then
1992                                                 for package_name in `cat ".$package-req.txt"|grep -v ^#`
1993                                                 do
1994                                                         if [ "$package_name" = "$package" ]; then
1995                                                                 echo "Installing BuildRequired package:\t$package_name"
1996                                                                 update_shell_title "Installing BuildRequired package: ${package_name}"
1997                                                                 install_required_packages $package
1998                                                         else
1999                                                                 echo "Installing (sub)Required package:\t$package_name"
2000                                                                 update_shell_title "Installing (sub)Required package: ${package_name}"
2001                                                                 install_required_packages $package_name
2002                                                         fi
2003                                                         case $? in
2004                                                                 0)
2005                                                                         INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
2006                                                                         echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
2007                                                                         ;;
2008                                                                 *)
2009                                                                         echo "Attempting to run spawn sub - builder..."
2010                                                                         echo "Package installation failed:\t$package_name"
2011                                                                         run_sub_builder $package_name
2012                                                                         if [ $? -eq 0 ]; then
2013                                                                                 install_required_packages $package_name
2014                                                                                 case $? in
2015                                                                                         0)
2016                                                                                                 INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
2017                                                                                                 echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
2018                                                                                                 ;;
2019                                                                                         *)
2020                                                                                                 NOT_INSTALLED_PACKAGES="$package_name $NOT_INSTALLED_PACKAGES"
2021                                                                                                 ;;
2022                                                                                 esac
2023                                                                         fi
2024                                                                         ;;
2025                                                         esac
2026                                                 done
2027                                                 rm -f ".$package-req.txt"
2028                                         else
2029                                                 echo "Attempting to run spawn sub - builder..."
2030                                                 echo "Package installation failed:\t$package"
2031                                                 run_sub_builder $package
2032                                                 if [ $? -eq 0 ]; then
2033                                                         install_required_packages $package
2034                                                         case $? in
2035                                                                 0)
2036                                                                         INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
2037                                                                         echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
2038                                                                         ;;
2039                                                                 *)
2040                                                                         NOT_INSTALLED_PACKAGES="$package_name $NOT_INSTALLED_PACKAGES"
2041                                                                         ;;
2042                                                         esac
2043                                                 fi
2044                                         fi
2045                                 else
2046                                         echo "Package $package is already installed. BuildRequirement satisfied."
2047                                 fi
2048                         fi
2049                 done
2050                 if [ "$NOT_INSTALLED_PACKAGES" != "" ]; then
2051                         echo >&2 "Unable to install following packages and their dependencies:"
2052                         for pkg in "$NOT_INSTALLED_PACKAGES"
2053                         do
2054                                 echo $pkg
2055                         done
2056                         remove_build_requires
2057                         exit 8
2058                 fi
2059 }
2060
2061 init_rpm_dir() {
2062         local CVSROOT=":pserver:cvs@$CVS_SERVER:/cvsroot"
2063         local TOP_DIR=$(eval $RPM $RPMOPTS --eval '%{_topdir}')
2064         local rpmdir=$(eval $RPM $RPMOPTS --eval '%{_rpmdir}')
2065         local buildir=$(eval $RPM $RPMOPTS --eval '%{_builddir}')
2066         local srpmdir=$(eval $RPM $RPMOPTS --eval '%{_srcrpmdir}')
2067         local tmp
2068
2069         echo "Initializing rpm directories to $TOP_DIR from $CVSROOT"
2070         mkdir -p $TOP_DIR $rpmdir $buildir $srpmdir
2071
2072         # need to checkout to tmp dir or we can't name our topdir
2073         tmp=$(TMPDIR= TEMPDIR= mktemp -p $TOP_DIR -d) || exit 1
2074         cd $tmp
2075         $CVS_COMMAND -d $CVSROOT co packages/{.cvsignore,rpm.groups,dropin,mirrors,md5,adapter{,.awk},fetchsrc_request,builder,{relup,compile,repackage}.sh,ci,civim}
2076         cd -
2077         mv $tmp/packages/* $TOP_DIR
2078         rm -rf $tmp
2079
2080         init_builder
2081
2082         echo "To checkout *all* .spec files (read-only):"
2083         echo "- run cvs co SPECS"
2084
2085         echo "To checkout *all* packages:"
2086         echo "- run cvs up -dP in $TOP_DIR dir"
2087
2088         echo ""
2089         echo "To commit with your developer account:"
2090         echo "- edit $TOP_DIR/CVS/Root"
2091 }
2092
2093 get_greed_sources() {
2094         CVSROOT=":pserver:cvs@$CVS_SERVER:/cvsroot"
2095         if [ -n "BE_VERBOSE" ]; then
2096                 echo "Try greed download: $1 from: $CVSROOT"
2097         fi
2098         $CVS_COMMAND -d $CVSROOT get SOURCES/$1
2099         if [ $? != 0 ]; then
2100                 Exit_error err_no_source_in_repo $1
2101         fi
2102
2103 }
2104
2105 # remove entries from CVS/Entries
2106 cvs_entry_remove() {
2107         local cvsdir="$1"; shift
2108         if [ ! -d "$cvsdir" ]; then
2109                 echo >&2 "cvs_entry_remove: $cvsdir is not a directory"
2110                 exit 1
2111         fi
2112
2113         for file in "$@"; do
2114                 rm -f $cvsdir/CVS/Entries.new || return 1
2115                 awk -ve="${file##*/}" -F/ '$2 != e {print}' $cvsdir/CVS/Entries > $cvsdir/CVS/Entries.new || return 1
2116                 mv -f $cvsdir/CVS/Entries.new $cvsdir/CVS/Entries || return 1
2117         done
2118         return 0
2119 }
2120
2121 mr_proper() {
2122         init_builder
2123         NOCVSSPEC="yes"
2124         DONT_PRINT_REVISION="yes"
2125         get_spec
2126         parse_spec
2127
2128         local builddir=$(eval $RPM $RPMOPTS --eval '%{_builddir}')
2129         
2130         # remove from CVS/Entries
2131         cvs_entry_remove $PACKAGE_DIR $SPECFILE $SOURCES $PATCHES
2132
2133         # remove spec and sources
2134         $RPMBUILD --clean --rmsource --rmspec --nodeps --define "_specdir $PACKAGE_DIR" --define "_sourcedir $PACKAGE_DIR" --define "_builddir $builddir" $SPECFILE
2135 }
2136
2137 #---------------------------------------------
2138 # main()
2139
2140 if [ $# = 0 ]; then
2141         usage
2142         exit 1
2143 fi
2144
2145 while [ $# -gt 0 ]; do
2146         case "${1}" in
2147                 -4|-6)
2148                         # NOTE: we should be fetcher specific, like fille WGET_OPTS, but
2149                         # unfortunately $GETURI is already formed
2150                         GETURI="$GETURI $1"
2151                         shift
2152                         ;;
2153                 -5 | --update-md5)
2154                         COMMAND="update_md5"
2155                         NODIST="yes"
2156                         NOCVSSPEC="yes"
2157                         shift ;;
2158                 -a5 | --add-md5 )
2159                         COMMAND="update_md5"
2160                         NODIST="yes"
2161                         NOCVS="yes"
2162                         NOCVSSPEC="yes"
2163                         ADD5="yes"
2164                         shift ;;
2165                 -n5 | --no-md5 )
2166                         NO5="yes"
2167                         shift ;;
2168                 -D | --debug )
2169                         DEBUG="yes"; shift ;;
2170                 -V | --version )
2171                         COMMAND="version"; shift ;;
2172                 --short-version )
2173                         COMMAND="short-version"; shift ;;
2174                 --as_anon )
2175                         CVSROOT=":pserver:cvs@$CVS_SERVER:/cvsroot"; shift ;;
2176                 -a | --add_cvs)
2177                         COMMAND="add_cvs";
2178                         shift ;;
2179                 -b | -ba | --build )
2180                         COMMAND="build"; shift ;;
2181                 -bb | --build-binary )
2182                         COMMAND="build-binary"; shift ;;
2183                 -bc )
2184                         COMMAND="build-build"; shift ;;
2185                 -bi )
2186                         COMMAND="build-install"; shift ;;
2187                 -bl )
2188                         COMMAND="build-list"; shift ;;
2189                 -bp | --build-prep )
2190                         COMMAND="build-prep"; shift ;;
2191                 -bs | --build-source )
2192                         COMMAND="build-source"; shift ;;
2193                 -B | --branch )
2194                         COMMAND="branch"; shift; TAG="${1}"; shift;;
2195                 -c | --clean )
2196                         CLEAN="--clean"; shift ;;
2197                 -cf | --cvs-force )
2198                         CVS_FORCE="-F"; shift;;
2199                 -d | --cvsroot )
2200                         shift; CVSROOT="${1}"; shift ;;
2201                 -g | --get )
2202                         COMMAND="get"; shift ;;
2203                 -h | --help )
2204                         COMMAND="usage"; shift ;;
2205                 --ftp )
2206                         PROTOCOL="ftp"; shift ;;
2207                 --http )
2208                         PROTOCOL="http"; shift ;;
2209                 -j)
2210                         RPMOPTS="${RPMOPTS} --define \"_smp_mflags -j$2\""
2211                         shift 2
2212                         ;;
2213                 -j[0-9]*)
2214                         RPMOPTS="${RPMOPTS} --define \"_smp_mflags $1\""
2215                         shift
2216                         ;;
2217                 -l | --logtofile )
2218                         shift; LOGFILE="${1}"; shift ;;
2219                 -ni| --nice )
2220                         shift; DEF_NICE_LEVEL=${1}; shift ;;
2221                 -ske | --skip-existing-files)
2222                         SKIP_EXISTING_FILES="yes"; shift ;;
2223                 -m | --mr-proper )
2224                         COMMAND="mr-proper"; shift ;;
2225                 -nc | --no-cvs )
2226                         NOCVS="yes"; shift ;;
2227                 -ncs | --no-cvs-specs )
2228                         NOCVSSPEC="yes"; shift ;;
2229                 -nd | --no-distfiles )
2230                         NODIST="yes"; shift ;;
2231                 -nm | --no-mirrors )
2232                         NOMIRRORS="yes"; shift ;;
2233                 -nu | --no-urls )
2234                         NOURLS="yes"; shift ;;
2235                 -ns | --no-srcs )
2236                         NOSRCS="yes"; shift ;;
2237                 -ns0 | --no-source0 )
2238                         NOSOURCE0="yes"; shift ;;
2239                 -nn | --no-net )
2240                         NOCVS="yes"
2241                         NOCVSSPEC="yes"
2242                         NODIST="yes"
2243                         NOMIRRORS="yes"
2244                         NOURLS="yes"
2245                         NOSRCS="yes"
2246                         ALWAYS_CVSUP="no"
2247                         shift;;
2248                 -pm | --prefer-mirrors )
2249                         PREFMIRRORS="yes"
2250                         shift;;
2251                 --no-init )
2252                         NOINIT="yes"
2253                         shift;;
2254                 --opts )
2255                         shift; RPMOPTS="${RPMOPTS} ${1}"; shift ;;
2256                 --nopatch | -np )
2257                         shift; RPMOPTS="${RPMOPTS} --define \"patch${1} : ignoring patch${1}; exit 1; \""; shift ;;
2258                 --skip-patch | -sp )
2259                         shift; RPMOPTS="${RPMOPTS} --define \"patch${1} : skiping patch${1}\""; shift ;;
2260                 --topdir)
2261                         RPMOPTS="${RPMOPTS} --define \"_topdir $2\""
2262                         shift 2
2263                         ;;
2264                 --with | --without )
2265                         case $GROUP_BCONDS in
2266                                 "yes")
2267                                         COND=${1}
2268                                         shift
2269                                         # XXX: broken: ./builder -bb ucspi-tcp.spec --without mysql
2270                                         while ! `echo ${1}|grep -qE '(^-|spec)'`
2271                                         do
2272                                                 BCOND="$BCOND $COND $1"
2273                                                 shift
2274                                         done;;
2275                                 "no")
2276                                         if [[ "$2" = *,* ]]; then
2277                                                 for a in $(echo "$2" | tr , ' '); do
2278                                                         BCOND="$BCOND $1 $a"
2279                                                 done
2280                                         else
2281                                                 BCOND="$BCOND $1 $2"
2282                                         fi
2283                                         shift 2 ;;
2284                         esac
2285                         ;;
2286                 --target )
2287                         shift; TARGET="${1}"; shift ;;
2288                 --target=* )
2289                         TARGET=$(echo "${1}" | sed 's/^--target=//'); shift ;;
2290                 -q | --quiet )
2291                         QUIET="--quiet"; shift ;;
2292                 --date )
2293                         CVSDATE="${2}"; shift 2 ;;
2294                 -r | --cvstag )
2295                         CVSTAG="$2"
2296                         shift 2
2297                         ;;
2298                 -A)
2299                         shift
2300                         CVSTAG="HEAD"
2301                         ;;
2302                 -R | --fetch-build-requires)
2303                         FETCH_BUILD_REQUIRES="yes"
2304                         NOT_INSTALLED_PACKAGES=
2305                         shift ;;
2306                 -RB | --remove-build-requires)
2307                         REMOVE_BUILD_REQUIRES="nice"
2308                         shift ;;
2309                 -FRB | --force-remove-build-requires)
2310                         REMOVE_BUILD_REQUIRES="force"
2311                         shift ;;
2312                 -sc | --sources-cvs)
2313                         COMMAND="list-sources-cvs"
2314                         shift ;;
2315                 -sd | --source-distfiles)
2316                         COMMAND="list-sources-distfiles"
2317                         shift ;;
2318                 -sdp | --source-distfiles-paths)
2319                         COMMAND="list-sources-distfiles-paths"
2320                         shift ;;
2321                 -sf | --source-files)
2322                         COMMAND="list-sources-files"
2323                         shift ;;
2324                 -lsp | --source-paths)
2325                         COMMAND="list-sources-local-paths"
2326                         shift ;;
2327                 -su | --source-urls)
2328                         COMMAND="list-sources-urls"
2329                         shift ;;
2330                 -Tvs | --tag-version-stable )
2331                         COMMAND="tag"
2332                         TAG="STABLE"
2333                         TAG_VERSION="yes"
2334                         shift;;
2335                 -Ts | --tag-stable )
2336                         COMMAND="tag"
2337                         TAG="STABLE"
2338                         TAG_VERSION="no"
2339                         shift;;
2340                 -Tv | --tag-version )
2341                         COMMAND="tag"
2342                         TAG=""
2343                         TAG_VERSION="yes"
2344                         shift;;
2345                 -Tp | --tag-prefix )
2346                         TAG_PREFIX="$2"
2347                         shift 2;;
2348                 -tt | --test-tag )
2349                         TEST_TAG="yes"
2350                         shift;;
2351                 -T | --tag )
2352                         COMMAND="tag"
2353                         shift
2354                         TAG="$1"
2355                         TAG_VERSION="no"
2356                         shift;;
2357                 -ir | --integer-release-only )
2358                         INTEGER_RELEASE="yes"
2359                         shift;;
2360                 -U | --update )
2361                         COMMAND="update_md5"
2362                         UPDATE="yes"
2363                         NOCVSSPEC="yes"
2364                         NODIST="yes"
2365                         shift ;;
2366                 -Upi | --update-poldek-indexes )
2367                         UPDATE_POLDEK_INDEXES="yes"
2368                         shift ;;
2369                 --init-rpm-dir)
2370                         COMMAND="init_rpm_dir"
2371                         shift ;;
2372                 --use-greed-sources )
2373                         GREEDSRC="1"
2374                         shift;;
2375                 -u | --try-upgrade )
2376                         TRY_UPGRADE="1"; shift ;;
2377                 --upgrade-version )
2378                         shift; UPGRADE_VERSION="$1"; shift;;
2379                 -un | --try-upgrade-with-float-version )
2380                         TRY_UPGRADE="1"; FLOAT_VERSION="1"; shift ;;
2381                 -v | --verbose )
2382                         BE_VERBOSE="1"; shift ;;
2383                 --define)
2384                         shift
2385                         MACRO="${1}"
2386                         shift
2387                         if echo "${MACRO}" | grep -q '\W'; then
2388                                 RPMOPTS="${RPMOPTS} --define \"${MACRO}\""
2389                         else
2390                                 VALUE="${1}"
2391                                 shift
2392                                 RPMOPTS="${RPMOPTS} --define \"${MACRO} ${VALUE}\""
2393                         fi
2394                         ;;
2395                 --alt_kernel)
2396                         shift
2397                         RPMOPTS="${RPMOPTS} --define \"alt_kernel $1\""
2398                         shift
2399                         ;;
2400                 --short-circuit)
2401                         RPMBUILDOPTS="${RPMBUILDOPTS} --short-circuit"
2402                         shift
2403                         ;;
2404                 --show-bconds | -show-bconds | -print-bconds | --print-bconds | -display-bconds | --display-bconds )
2405                         COMMAND="show_bconds"
2406                         shift
2407                         ;;
2408                 --show-bcond-args)
2409                         COMMAND="show_bcond_args"
2410                         shift
2411                         ;;
2412                 --show-avail-bconds)
2413                         COMMAND="show_avail_bconds"
2414                         shift
2415                         ;;
2416                 --nodeps)
2417                         shift
2418                         RPMOPTS="${RPMOPTS} --nodeps"
2419                         ;;
2420                 -debug)
2421                         RPMBUILDOPTS="${RPMBUILDOPTS} -debug"; shift
2422                         ;;
2423                 -*)
2424                         Exit_error err_invalid_cmdline "$1"
2425                         ;;
2426                 *)
2427                         SPECFILE=${1%/}; shift
2428                         # check if specname was passed as specname:cvstag
2429                         if [ "${SPECFILE##*:}" != "${SPECFILE}" ]; then
2430                                 CVSTAG="${SPECFILE##*:}"
2431                                 SPECFILE="${SPECFILE%%:*}"
2432                         fi
2433                         # always have SPECFILE ending with .spec extension
2434                         SPECFILE=${SPECFILE%%.spec}.spec
2435                         ASSUMED_NAME=$(basename ${SPECFILE%%.spec})
2436         esac
2437 done
2438
2439 [ -d "$ASSUMED_NAME" ] && CVS_ENTRIES="$ASSUMED_NAME/CVS/Entries" || CVS_ENTRIES="CVS/Entries"
2440 if [ -f "$CVS_ENTRIES" ] && [ -z "$CVSTAG" ]; then
2441         CVSTAG=$(awk -vSPECFILE=$(basename $SPECFILE) -F/ '$2 == SPECFILE && $6 ~ /^T/{print substr($6, 2)}' ${CVS_ENTRIES})
2442         if [ "$CVSTAG" ]; then
2443                 echo >&2 "builder: Sticky tag $CVSTAG active. Use -r TAGNAME to override."
2444         fi
2445 elif [ "$CVSTAG" = "HEAD" ]; then
2446         # assume -r HEAD is same as -A
2447         CVSTAG=""
2448 fi
2449
2450 if [ "$CVSTAG" ]; then
2451         # pass $CVSTAG used by builder to rpmbuild too, so specs could use it
2452         RPMOPTS="$RPMOPTS --define \"_cvstag $CVSTAG\""
2453 fi
2454
2455 if [ -n "$DEBUG" ]; then
2456         set -x
2457         set -v
2458 fi
2459
2460 if [ -n "$TARGET" ]; then
2461         case "$RPMBUILD" in
2462                 "rpmbuild")
2463                         TARGET_SWITCH="--target $TARGET" ;;
2464                 "rpm")
2465                         TARGET_SWITCH="--target=$TARGET" ;;
2466         esac
2467 fi
2468
2469 if [ "$SCHEDTOOL" != "no" ]; then
2470         NICE_COMMAND="$SCHEDTOOL"
2471 else
2472         NICE_COMMAND="nice -n ${DEF_NICE_LEVEL}"
2473 fi
2474
2475 update_shell_title "$COMMAND"
2476 case "$COMMAND" in
2477         "show_bconds")
2478                 init_builder
2479                 if [ -z "$SPECFILE" ]; then
2480                         Exit_error err_no_spec_in_cmdl
2481                 fi
2482                 get_spec > /dev/null
2483                 parse_spec
2484                 set_bconds_values
2485                 display_bconds
2486                 ;;
2487         "show_bcond_args")
2488                 init_builder
2489                 if [ -z "$SPECFILE" ]; then
2490                         Exit_error err_no_spec_in_cmdl
2491                 fi
2492                 get_spec > /dev/null
2493                 parse_spec
2494                 set_bconds_values
2495                 echo "$BCOND"
2496                 ;;
2497         "show_avail_bconds")
2498                 init_builder
2499                 if [ -z "$SPECFILE" ]; then
2500                         Exit_error err_no_spec_in_cmdl
2501                 fi
2502
2503                 get_spec > /dev/null
2504                 parse_spec
2505                 local bcond_avail=$(find_spec_bcond $SPECFILE)
2506                 local opt bcond bconds
2507                 for opt in $bcond_avail; do
2508                         case "$opt" in
2509                         without_*)
2510                                 bcond=${opt#without_}
2511                                 bconds="$bconds $bcond"
2512                                 ;;
2513                         with_*)
2514                                 bcond=${opt#with_}
2515                                 bconds="$bconds $bcond"
2516                                 ;;
2517                         *)
2518                                 echo >&2 "ERROR: unexpected '$opt' in show_avail_bconds"
2519                                 exit 1
2520                                 ;;
2521                         esac
2522                 done
2523                 echo $bconds
2524
2525                 ;;
2526         "build" | "build-binary" | "build-source" | "build-prep" | "build-build" | "build-install" | "build-list")
2527                 init_builder
2528                 if [ -z "$SPECFILE" ]; then
2529                         Exit_error err_no_spec_in_cmdl
2530                 fi
2531
2532                 # display SMP make flags if set
2533                 smp_mflags=$(rpm -E %{?_smp_mflags})
2534                 if [ "$smp_mflags" ]; then
2535                         echo "builder: SMP make flags are set to $smp_mflags"
2536                 fi
2537
2538                 get_spec
2539                 parse_spec
2540                 set_bconds_values
2541                 display_bconds
2542                 display_branches
2543                 if [ "$COMMAND" != "build-source" ]; then
2544                         check_buildarch
2545                 fi
2546                 fetch_build_requires
2547                 if [ "$INTEGER_RELEASE" = "yes" ]; then
2548                         echo "Checking release $PACKAGE_RELEASE..."
2549                         if echo $PACKAGE_RELEASE | grep -q '^[^.]*\.[^.]*$' 2>/dev/null ; then
2550                                 Exit_error err_fract_rel "$PACKAGE_RELEASE"
2551                         fi
2552                 fi
2553
2554                 # ./builder -bs test.spec -r AC-branch -Tp auto-ac- -tt
2555                 if [ -n "$TEST_TAG" ]; then
2556                         local TAGVER=`make_tagver`
2557                         # escape some regexp characters if part of TAGVER
2558                         TAGVER=$(echo "$TAGVER" | sed -e 's#\([\+\*\.]\)#\\\1#g')
2559                         echo "Searching for tag $TAGVER..."
2560                         TAGREL=$($CVS_COMMAND status -v $SPECFILE | grep -E "^[[:space:]]*${TAGVER}[[[:space:]]" | sed -e 's#.*(revision: ##g' -e 's#).*##g')
2561                         if [ -n "$TAGREL" ]; then
2562                                 Exit_error err_tag_exists "$TAGVER" "$TAGREL"
2563                         fi
2564
2565                         # - do not allow to build from HEAD when XX-branch exists
2566                         TREE_PREFIX=$(echo "$TAG_PREFIX" | sed -e 's#^auto-\([a-zA-Z]\+\)-.*#\1#g')
2567                         if [ "$TREE_PREFIX" != "$TAG_PREFIX" ]; then
2568                                 TAG_BRANCH="${TREE_PREFIX}-branch"
2569                                 TAG_STATUS=$($CVS_COMMAND status -v $SPECFILE | grep -Ei "${TAG_BRANCH}.+(branch: [0-9.]+)")
2570                                 if [ -n "$TAG_STATUS" -a "$CVSTAG" = "HEAD" ]; then
2571                                         Exit_error err_branch_exists "$TAG_STATUS"
2572                                 fi
2573                         fi
2574
2575                 fi
2576
2577                 if [ -n "$NOSOURCE0" ] ; then
2578                         SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2579                 fi
2580                 try_upgrade
2581                 case $? in
2582                         0)
2583                                 get_files $SOURCES $PATCHES
2584                                 check_md5 $SOURCES
2585                                 ;;
2586                         *)
2587                                 NODIST="yes" get_files $SOURCES $PATCHES
2588                                 update_md5 $SOURCES $PATCHES
2589                                 ;;
2590                 esac
2591                 build_package
2592                 if [ "$UPDATE_POLDEK_INDEXES" = "yes" -a "$COMMAND" != "build-prep" ]; then
2593                         run_poldek --sdir="${POLDEK_INDEX_DIR}" --mkidxz
2594                 fi
2595                 remove_build_requires
2596                 ;;
2597         "branch" )
2598                 init_builder
2599                 if [ -z "$SPECFILE" ]; then
2600                         Exit_error err_no_spec_in_cmdl
2601                 fi
2602
2603                 get_spec
2604                 parse_spec
2605                 # don't fetch sources from remote locations
2606                 new_SOURCES=""
2607                 for file in $SOURCES; do
2608                         [ -n "`src_md5 $file`" ] && continue
2609                         new_SOURCES="$new_SOURCES $file"
2610                 done
2611                 SOURCES="$new_SOURCES"
2612                 get_files $SOURCES $PATCHES
2613                 check_md5 $SOURCES
2614                 branch_files $TAG $SOURCES $PATCHES $ICONS
2615                 ;;
2616         "add_cvs" )
2617                 init_builder
2618                 if [ -z "$SPECFILE" ]; then
2619                         Exit_error err_no_spec_in_cmdl
2620                 fi
2621
2622                 ADD_PACKAGE_CVS=yes get_spec
2623                 parse_spec
2624
2625                 if [ -n "$NOSOURCE0" ] ; then
2626                         SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2627                 fi
2628                 get_files $SOURCES $PATCHES
2629                 check_md5 $SOURCES
2630                 ;;
2631         "get" )
2632                 init_builder
2633                 if [ -z "$SPECFILE" ]; then
2634                         Exit_error err_no_spec_in_cmdl
2635                 fi
2636
2637                 get_spec
2638                 parse_spec
2639
2640                 if [ -n "$NOSOURCE0" ] ; then
2641                         SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2642                 fi
2643                 get_files $SOURCES $PATCHES
2644                 check_md5 $SOURCES
2645                 ;;
2646         "update_md5" )
2647                 init_builder
2648                 if [ -z "$SPECFILE" ]; then
2649                         Exit_error err_no_spec_in_cmdl
2650                 fi
2651
2652                 get_spec
2653                 parse_spec
2654
2655                 if [ -n "$NOSOURCE0" ] ; then
2656                         SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2657                 fi
2658                 update_md5 $SOURCES $PATCHES
2659                 ;;
2660         "tag" )
2661                 NOURLS=1
2662                 NODIST="yes"
2663                 init_builder
2664                 if [ -z "$SPECFILE" ]; then
2665                         Exit_error err_no_spec_in_cmdl
2666                 fi
2667
2668                 get_spec
2669                 parse_spec
2670
2671                 # don't fetch sources from remote locations
2672                 new_SOURCES=""
2673                 for file in $SOURCES; do
2674                         [ -n "`src_md5 $file`" ] && continue
2675                         new_SOURCES="$new_SOURCES $file"
2676                 done
2677                 SOURCES="$new_SOURCES"
2678                 get_files $SOURCES $PATCHES
2679                 check_md5 $SOURCES
2680                 tag_files $SOURCES $PATCHES $ICONS
2681                 ;;
2682         "mr-proper" )
2683                 mr_proper
2684                 ;;
2685         "list-sources-files" )
2686                 init_builder
2687                 NOCVSSPEC="yes"
2688                 DONT_PRINT_REVISION="yes"
2689                 get_spec
2690                 parse_spec
2691                 for SAP in $SOURCES $PATCHES; do
2692                         echo $SAP | awk '{gsub(/.*\//,"") ; print}'
2693                 done
2694                 ;;
2695         "list-sources-urls" )
2696                 init_builder
2697                 NOCVSSPEC="yes"
2698                 DONT_PRINT_REVISION="yes"
2699                 get_spec
2700                 parse_spec
2701                 SAPS="$SOURCES $PATCHES"
2702                 for SAP in $SAPS ; do
2703                         echo $SAP
2704                 done
2705                 ;;
2706         "list-sources-local-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                         echo $PACKAGE_DIR/$(echo $SAP | awk '{gsub(/.*\//,"") ; print }')
2714                 done
2715                 ;;
2716         "list-sources-distfiles-paths" )
2717                 init_builder
2718                 NOCVSSPEC="yes"
2719                 DONT_PRINT_REVISION="yes"
2720                 get_spec
2721                 parse_spec
2722                 for SAP in $SOURCES $PATCHES; do
2723                         if [ -n "$(src_md5 "$SAP")" ]; then
2724                                 distfiles_path "$SAP"
2725                         fi
2726                 done
2727                 ;;
2728         "list-sources-distfiles" )
2729                 init_builder
2730                 NOCVSSPEC="yes"
2731                 DONT_PRINT_REVISION="yes"
2732                 get_spec
2733                 parse_spec
2734                 for SAP in $SOURCES $PATCHES; do
2735                         if [ -n "$(src_md5 "$SAP")" ]; then
2736                                 distfiles_url "$SAP"
2737                         fi
2738                 done
2739                 ;;
2740         "list-sources-cvs" )
2741                 init_builder
2742 #               NOCVSSPEC="yes"
2743                 DONT_PRINT_REVISION="yes"
2744                 get_spec
2745                 parse_spec
2746                 for SAP in $SOURCES $PATCHES; do
2747                         if [ -z "$(src_md5 "$SAP")" ]; then
2748                                 echo $SAP | awk '{gsub(/.*\//,"") ; print}'
2749                         fi
2750                 done
2751                 ;;
2752         "init_rpm_dir")
2753                 init_rpm_dir
2754                 ;;
2755         "usage" )
2756                 usage
2757                 ;;
2758         "short-version" )
2759                 echo "$VERSION"
2760                 ;;
2761         "version" )
2762                 echo "$VERSIONSTRING"
2763                 ;;
2764 esac
2765 if [ -f "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES" -a "$REMOVE_BUILD_REQUIRES" != "" ]; then
2766         rm "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES"
2767 fi
2768 cd "$__PWD"
2769
2770 # vi:syntax=sh:ts=4:sw=4:noet
This page took 0.886558 seconds and 4 git commands to generate.