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