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