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