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