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