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