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