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