]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - builder.sh
- %requires_ge_to, %requires_ge
[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                         # create symlinks for tools
742                         if [ "$SYMLINK_TOOLS" != "no" ]; then
743                                 for a in dropin md5 adapter builder {relup,compile,repackage,rsync,pearize}.sh pldnotify.awk; do
744                                         [ -f $a ] || continue
745                                         ln -s ../$a $ASSUMED_NAME
746                                         cvsignore_df $a
747                                 done
748                         fi
749                 fi
750         fi
751
752         if [ ! -f "$ASSUMED_NAME/$SPECFILE" ]; then
753                 Exit_error err_no_spec_in_repo
754         fi
755
756         if [ "$CHMOD" = "yes" -a -n "$SPECFILE" ]; then
757                 chmod $CHMOD_MODE $ASSUMED_NAME/$SPECFILE
758         fi
759         unset OPTIONS
760         [ -n "$DONT_PRINT_REVISION" ] || grep -E -m 1 "^#.*Revision:.*Date" $ASSUMED_NAME/$SPECFILE
761
762         set_spec_target
763 }
764
765 find_mirror() {
766         cd "$REPO_DIR"
767         local url="$1"
768         if [ ! -f "mirrors" -a "$NOCVSSPEC" != "yes" ] ; then
769                 $CVS_COMMAND update mirrors >&2
770         fi
771
772         IFS="|"
773         local origin mirror name rest ol prefix
774         while read origin mirror name rest; do
775                 # skip comments and empty lines
776                 if [ -z "$origin" ] || [ "${origin#\#}" != "$origin" ]; then
777                         continue
778                 fi
779                 ol=$(echo -n "$origin" | wc -c)
780                 prefix=$(echo -n "$url" | head -c $ol)
781                 if [ "$prefix" = "$origin" ] ; then
782                         suffix=$(echo "$url" | cut -b $((ol+1))-)
783                         echo -n "$mirror$suffix"
784                         return 0
785                 fi
786         done < mirrors
787         echo "$url"
788 }
789
790 # Warning: unpredictable results if same URL used twice
791 src_no() {
792         cd $PACKAGE_DIR
793         rpm_dump | \
794         grep "SOURCEURL[0-9]*[  ]*$1""[         ]*$" | \
795         sed -e 's/.*SOURCEURL\([0-9][0-9]*\).*/\1/' | \
796         head -n 1 | xargs
797 }
798
799 src_md5() {
800         [ "$NO5" = "yes" ] && return
801         no=$(src_no "$1")
802         [ -z "$no" ] && return
803         cd $PACKAGE_DIR
804         local md5
805
806         if [ -f additional-md5sums ]; then
807                 local spec_rev=$(grep $SPECFILE CVS/Entries 2>/dev/null | sed -e s:/$SPECFILE/:: -e s:/.*::)
808                 if [ -z "$spec_rev" ]; then
809                         spec_rev=$(head -n 1 $SPECFILE | sed -e 's/.*\$Revision: \([0-9.]*\).*/\1/')
810                 fi
811                 local spec="$SPECFILE[0-9.,]*,$(echo $spec_rev | sed 's/\./\\./g')"
812                 md5=$(grep -s -v '^#' additional-md5sums | \
813                 grep -E "[      ]$(basename "$1")[      ]+${spec}([     ,]|\$)" | \
814                 sed -e 's/^\([0-9a-f]\{32\}\).*/\1/' | \
815                 grep -E '^[0-9a-f]{32}$')
816
817                 if [ "$md5" ]; then
818                         if [ $(echo "$md5" | wc -l) != 1 ] ; then
819                                 echo "$SPECFILE: more then one entry in additional-md5sums for $1" 1>&2
820                         fi
821                         echo "$md5" | tail -n 1
822                         return
823                 fi
824         fi
825
826         source_md5=`grep -i "^#[        ]*Source$no-md5[        ]*:" $SPECFILE | sed -e 's/.*://'`
827         if [ -n "$source_md5" ]; then
828                 echo $source_md5
829         else
830                 source_md5=`grep -i "BuildRequires:[    ]*digest(%SOURCE$no)[   ]*=" $SPECFILE | sed -e 's/.*=//'`
831                 if [ -n "$source_md5" ]; then
832                         echo $source_md5
833                 else
834                         # we have empty SourceX-md5, but it is still possible
835                         # that we have NoSourceX-md5 AND NoSource: X
836                         nosource_md5=`grep -i "^#[       ]*NoSource$no-md5[      ]*:" $SPECFILE | sed -e 's/.*://'`
837                         if [ -n "$nosource_md5" -a -n "`grep -i "^NoSource:[     ]*$no$" $SPECFILE`" ] ; then
838                                 echo $nosource_md5
839                         fi
840                 fi
841         fi
842 }
843
844 distfiles_path() {
845         echo "by-md5/$(src_md5 "$1" | sed -e 's|^\(.\)\(.\)|\1/\2/&|')/$(basename "$1")"
846 }
847
848 distfiles_url() {
849         echo "$PROTOCOL$DISTFILES_SERVER/distfiles/$(distfiles_path "$1")"
850 }
851
852 distfiles_attic_url() {
853         echo "$PROTOCOL$ATTICDISTFILES_SERVER/distfiles/Attic/$(distfiles_path "$1")"
854 }
855
856 good_md5() {
857         md5=$(src_md5 "$1")
858         [ "$md5" = "" ] || \
859         [ "$md5" = "$(md5sum $(nourl "$1") 2> /dev/null | sed -e 's/ .*//')" ]
860 }
861
862 good_size() {
863         size=$(find $(nourl "$1") -printf "%s" 2>/dev/null)
864         [ -n "$size" -a "$size" -gt 0 ]
865 }
866
867 cvsignore_df() {
868         if [ "$CVSIGNORE_DF" != "yes" ]; then
869                 return
870         fi
871         cvsignore=${PACKAGE_DIR}/.cvsignore
872
873         # add only if not yet there
874         if ! awk -vf="$1" -vc=1 '$0 == f { c = 0 } END { exit c }' $cvsignore 2>/dev/null; then
875                 echo "$1" >> $cvsignore
876         fi
877 }
878
879 cvsup() {
880         update_shell_title "cvsup"
881         local OPTIONS="up "
882
883         if [ "$1" = "-c" ]; then
884                 OPTIONS="co "
885                 shift
886         fi
887         if [ -n "$CVSROOT" ]; then
888                 OPTIONS="-d $CVSROOT $OPTIONS"
889         fi
890
891         if [ -z "$CVSDATE" -a -z "$CVSTAG" ]; then
892                 OPTIONS="$OPTIONS -A"
893         else
894                 if [ -n "$CVSDATE" ]; then
895                         OPTIONS="$OPTIONS -D $CVSDATE"
896                 fi
897                 if [ -n "$CVSTAG" ]; then
898                         OPTIONS="$OPTIONS -r $CVSTAG"
899                 fi
900         fi
901
902         local result=1
903         local retries_counter=0
904         if [ $# = 1 ]; then
905                 update_shell_title "cvsup: $*"
906         else
907                 update_shell_title "cvsup: $# files"
908         fi
909         while [ "$result" != "0" -a "$retries_counter" -le "$CVS_RETRIES" ]; do
910                 retries_counter=$(( $retries_counter + 1 ))
911                 output=$(LC_ALL=C $CVS_COMMAND $OPTIONS "$@" 2>&1)
912                 result=$?
913                 [ -n "$output" ] && echo "$output"
914                 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
915                         echo "Trying again [$*]... ($retries_counter)"
916                         update_shell_title "cvsup: retry #$retries_counter"
917                         sleep 2
918                         continue
919                 else
920                         break
921                 fi
922         done
923         update_shell_title "cvsup: done!"
924         return $result
925 }
926
927 # returns true if "$1" is ftp, http or https protocol url
928 is_url() {
929         case "$1" in
930         ftp://*|http://*|https://*)
931                 return 0
932         ;;
933         esac
934         return 1
935 }
936
937 update_md5() {
938         if [ $# -eq 0 ]; then
939                 return
940         fi
941
942         update_shell_title "update md5"
943         if [ -n "$DEBUG" ]; then
944                 set -x
945                 set -v
946         fi
947
948         cd "$PACKAGE_DIR"
949
950         # pass 1: check files to be fetched
951         local todo
952         local need_files
953         for i in "$@"; do
954                 local fp=$(nourl "$i")
955                 local srcno=$(src_no "$i")
956                 if [ -n "$ADD5" ]; then
957                         [ "$fp" = "$i" ] && continue # FIXME what is this check doing?
958                         grep -qiE '^#[  ]*Source'$srcno'-md5[   ]*:' $PACKAGE_DIR/$SPECFILE && continue
959                         grep -qiE '^BuildRequires:[     ]*digest[(]%SOURCE'$srcno'[)][  ]*=' $PACKAGE_DIR/$SPECFILE && continue
960                 else
961                         grep -qiE '^#[  ]*Source'$srcno'-md5[   ]*:' $PACKAGE_DIR/$SPECFILE || grep -qiE '^BuildRequires:[      ]*digest[(]%SOURCE'$srcno'[)][  ]*=' $PACKAGE_DIR/$SPECFILE || continue
962                 fi
963                 if [ ! -f "$fp" ] || [ $ALWAYS_CVSUP = "yes" ]; then
964                         need_files="$need_files $i"
965                 fi
966         done
967
968         # pass 1a: get needed files
969         if [ "$need_files" ]; then
970                 get_files $need_files
971         fi
972
973         # pass 2: proceed with md5 adding or updating
974         for i in "$@"; do
975                 local fp=$(nourl "$i")
976                 local srcno=$(src_no "$i")
977                 local md5=$(grep -iE '^#[       ]*(No)?Source'$srcno'-md5[      ]*:' $PACKAGE_DIR/$SPECFILE )
978                 if [ -z "$md5" ]; then
979                         md5=$(grep -iE '^[      ]*BuildRequires:[       ]*digest[(]%SOURCE'$srcno'[)][  ]*=' $PACKAGE_DIR/$SPECFILE )
980                 fi
981                 if [ -n "$ADD5" ] && is_url $i || [ -n "$md5" ]; then
982                         local tag="# Source$srcno-md5:\t"
983                         if [[ "$md5" == *NoSource* ]]; then
984                                 tag="# NoSource$srcno-md5:\t"
985                         elif [ -n "$USEDIGEST" ]; then
986                                 tag="BuildRequires:\tdigest(%SOURCE$srcno) = "
987                         fi
988                         md5=$(md5sum "$fp" | cut -f1 -d' ')
989                         echo "Updating Source$srcno ($md5: $fp)."
990                         perl -i -ne '
991                                 print unless (/^\s*#\s*(No)?Source'$srcno'-md5\s*:/i or /^\s*BuildRequires:\s*digest\(%SOURCE'$srcno'\)/i);
992                                 print "'"$tag$md5"'\n" if /^Source'$srcno'\s*:\s+/;
993                         ' \
994                         $PACKAGE_DIR/$SPECFILE
995                 fi
996         done
997 }
998
999 check_md5() {
1000         local bad
1001         [ "$NO5" = "yes" ] && return
1002
1003         update_shell_title "check md5"
1004
1005         for i in "$@"; do
1006                 bad=0
1007                 if ! good_md5 "$i"; then
1008                         echo -n "MD5 sum mismatch."
1009                         bad=1
1010                 fi
1011                 if ! good_size "$i"; then
1012                         echo -n "0 sized file."
1013                         bad=1
1014                 fi
1015
1016                 if [ $bad -eq 1 ]; then
1017                         echo " Use -U to refetch sources,"
1018                         echo "or -5 to update md5 sums, if you're sure files are correct."
1019                         Exit_error err_no_source_in_repo $i
1020                 fi
1021         done
1022 }
1023
1024 get_files() {
1025         update_shell_title "get_files"
1026
1027         if [ -n "$DEBUG" ]; then
1028                 set -x
1029                 set -v
1030         fi
1031
1032         if [ $# -gt 0 ]; then
1033                 cd "$PACKAGE_DIR"
1034
1035                 if [ ! -s CVS/Root -a "$NOCVS" != "yes" ]; then
1036                         echo "Warning: No CVS access defined for SOURCES"
1037                         NOCVS="yes"
1038                 fi
1039
1040                 local nc=0
1041                 local get_files_cvs=""
1042                 for i in "$@"; do
1043                         nc=$((nc + 1))
1044                         local cvsup=0
1045                         SHELL_TITLE_PREFIX="get_files[$nc/$#]"
1046                         update_shell_title "$i"
1047                         local fp=`nourl "$i"`
1048                         if [ "$SKIP_EXISTING_FILES" = "yes" ] && [ -f "$fp" ]; then
1049                                 continue
1050                         fi
1051
1052                         FROM_DISTFILES=0
1053                         local srcmd5=$(src_md5 "$i")
1054
1055                         # we know if source/patch is present in cvs/distfiles
1056                         # - has md5 (in distfiles)
1057                         # - in cvs... ideas?
1058
1059                         # CHECK: local file didn't exist or always cvs up (first) requested.
1060                         if [ ! -f "$fp" ] || [ $ALWAYS_CVSUP = "yes" ]; then
1061                                 if echo $i | grep -vE '(http|ftp|https|cvs|svn)://' | grep -qE '\.(gz|bz2)$']; then
1062                                         echo "Warning: no URL given for $i"
1063                                 fi
1064                                 target="$fp"
1065
1066                                 if [ -z "$NODIST" ] && [ -n "$srcmd5" ]; then
1067                                         if good_md5 "$i" && good_size "$i"; then
1068                                                 echo "$fp having proper md5sum already exists"
1069                                                 continue
1070                                         fi
1071
1072                                         # optionally prefer mirror over distfiles if there's mirror
1073                                         # TODO: build url list and then try each url from the list
1074                                         if [ -n "$PREFMIRRORS" ] && [ -z "$NOMIRRORS" ] && im=$(find_mirror "$i") && [ "$im" != "$i" ]; then
1075                                                 url="$im"
1076                                         else
1077                                                 url=$(distfiles_url "$i")
1078                                         fi
1079
1080                                         url_attic=$(distfiles_attic_url "$i")
1081                                         FROM_DISTFILES=1
1082                                         # is $url local file?
1083                                         if [[ "$url" = [./]* ]]; then
1084                                                 update_shell_title "${GETLOCAL%% *}: $url"
1085                                                 ${GETLOCAL} $url $target
1086                                         else
1087                                                 if [ -z "$NOMIRRORS" ]; then
1088                                                         url=$(find_mirror "$url")
1089                                                 fi
1090
1091                                                 local uri=${url}
1092                                                 # make shorter message for distfiles urls
1093                                                 if [[ "$uri" = ${PROTOCOL}${DISTFILES_SERVER}* ]] || [[ "$uri" = ${PROTOCOL}${ATTICDISTFILES_SERVER}* ]]; then
1094                                                         uri=${uri#${PROTOCOL}${DISTFILES_SERVER}/distfiles/by-md5/?/?/*/}
1095                                                         uri=${uri#${PROTOCOL}${ATTICDISTFILES_SERVER}/distfiles/by-md5/?/?/*/}
1096                                                         uri="df: $uri"
1097                                                 fi
1098                                                 update_shell_title "${GETURI%% *}: $uri"
1099                                                 ${GETURI} ${OUTFILEOPT} "$target" "$url" || \
1100                                                 if [ "`echo $url | grep -E 'ftp://'`" ]; then
1101                                                         update_shell_title "${GETURI2%% *}: $url"
1102                                                         ${GETURI2} ${OUTFILEOPT} "$target" "$url"
1103                                                 fi
1104                                         fi
1105
1106                                         # is it empty file?
1107                                         if [ ! -s "$target" ]; then
1108                                                 rm -f "$target"
1109                                                 if [ `echo $url_attic | grep -E '^(\.|/)'` ]; then
1110                                                         update_shell_title "${GETLOCAL%% *}: $url_attic"
1111                                                         ${GETLOCAL} $url_attic $target
1112                                                 else
1113                                                         if [ -z "$NOMIRRORS" ]; then
1114                                                                 url_attic=$(find_mirror "$url_attic")
1115                                                         fi
1116                                                         update_shell_title "${GETURI%% *}: $url_attic"
1117                                                         ${GETURI} ${OUTFILEOPT} "$target" "$url_attic" || \
1118                                                         if [ "`echo $url_attic | grep -E 'ftp://'`" ]; then
1119                                                                 update_shell_title "${GETURI2%% *}: $url_attic"
1120                                                                 ${GETURI2} ${OUTFILEOPT} "$target" "$url_attic"
1121                                                         fi
1122                                                         test -s "$target" || rm -f "$target"
1123                                                 fi
1124                                         fi
1125
1126                                         if [ -s "$target" ]; then
1127                                                 cvsignore_df $target
1128                                         else
1129                                                 rm -f "$target"
1130                                                 FROM_DISTFILES=0
1131                                         fi
1132                                 elif [ "$NOCVS" != "yes" -a -z "$srcmd5" ]; then
1133                                         if [ $# -gt 1 ]; then
1134                                                 get_files_cvs="$get_files_cvs $fp"
1135                                                 update_shell_title "$fp (will cvs up later)"
1136                                                 cvsup=1
1137                                         else
1138                                                 cvsup $fp
1139                                         fi
1140                                 fi
1141
1142                                 if [ -z "$NOURLS" ] && [ ! -f "$fp" -o -n "$UPDATE" ] && [ "`echo $i | grep -E 'ftp://|http://|https://'`" ]; then
1143                                         if [ -z "$NOMIRRORS" ]; then
1144                                                 im=$(find_mirror "$i")
1145                                         else
1146                                                 im="$i"
1147                                         fi
1148                                         update_shell_title "${GETURI%% *}: $im"
1149                                         ${GETURI} "$im" ${OUTFILEOPT} "$target" || \
1150                                         if [ "`echo $im | grep -E 'ftp://'`" ]; then
1151                                                 update_shell_title "${GETURI2%% *}: $im"
1152                                                 ${GETURI2} "$im" ${OUTFILEOPT} "$target"
1153                                         fi
1154                                         test -s "$target" || rm -f "$target"
1155                                 fi
1156
1157                                 if [ "$cvsup" = 1 ]; then
1158                                         continue
1159                                 fi
1160
1161                         fi
1162
1163                         # the md5 check must be moved elsewhere as if we've called from update_md5 the md5 is wrong.
1164                         if [ ! -f "$fp" -a "$FAIL_IF_NO_SOURCES" != "no" ]; then
1165                                 if [ -n "GREEDSRC" ]; then
1166                                         get_greed_sources $i
1167                                 else
1168                                         Exit_error err_no_source_in_repo $i
1169                                 fi
1170                         fi
1171
1172                         # we check md5 here just only to refetch immediately
1173                         if good_md5 "$i" && good_size "$i"; then
1174                                 :
1175                         elif [ "$FROM_DISTFILES" = 1 ]; then
1176                                 # wrong md5 from distfiles: remove the file and try again
1177                                 # but only once ...
1178                                 echo "MD5 sum mismatch. Trying full fetch."
1179                                 FROM_DISTFILES=2
1180                                 rm -f $target
1181                                 update_shell_title "${GETURI%% *}: $url"
1182                                 ${GETURI} ${OUTFILEOPT} "$target" "$url" || \
1183                                 if [ "`echo $url | grep -E 'ftp://'`" ]; then
1184                                         update_shell_title "${GETURI2%% *}: $url"
1185                                         ${GETURI2} ${OUTFILEOPT} "$target" "$url"
1186                                 fi
1187                                 if [ ! -s "$target" ]; then
1188                                         rm -f "$target"
1189                                         update_shell_title "${GETURI%% *}: $url_attic"
1190                                         ${GETURI} ${OUTFILEOPT} "$target" "$url_attic" || \
1191                                         if [ "`echo $url_attic | grep -E 'ftp://'`" ]; then
1192                                                 update_shell_title "${GETURI2%% *}: $url_attic"
1193                                                 ${GETURI2} ${OUTFILEOPT} "$target" "$url_attic"
1194                                         fi
1195                                 fi
1196                                 test -s "$target" || rm -f "$target"
1197                         fi
1198                 done
1199                 SHELL_TITLE_PREFIX=""
1200
1201                 if [ "$get_files_cvs" ]; then
1202                         cvsup $get_files_cvs
1203                 fi
1204
1205                 if [ "$CHMOD" = "yes" ]; then
1206                         CHMOD_FILES=$(nourl "$@")
1207                         if [ -n "$CHMOD_FILES" ]; then
1208                                 chmod $CHMOD_MODE $CHMOD_FILES
1209                         fi
1210                 fi
1211         fi
1212 }
1213
1214 make_tagver() {
1215         if [ -n "$DEBUG" ]; then
1216                 set -x
1217                 set -v
1218         fi
1219
1220         # Check whether first character of PACKAGE_NAME is legal for tag name
1221         if [ -z "${PACKAGE_NAME##[_0-9]*}" -a -z "$TAG_PREFIX" ]; then
1222                 TAG_PREFIX=tag_
1223         fi
1224
1225         # NOTE: CVS tags may must not contain the characters `$,.:;@'
1226         TAGVER=$(echo $TAG_PREFIX$PACKAGE_NAME-$PACKAGE_VERSION-$PACKAGE_RELEASE | tr '[.@]' '[_#]')
1227
1228         # Remove #kernel.version_release from TAGVER because tagging sources
1229         # could occur with different kernel-headers than kernel-headers used at build time.
1230         # besides, %{_kernel_ver_str} is not expanded.
1231
1232         # TAGVER=auto-ac-madwifi-ng-0-0_20070225_1#%{_kernel_ver_str}
1233         # TAGVER=auto-ac-madwifi-ng-0-0_20070225_1
1234
1235         TAGVER=${TAGVER%#*}
1236         echo -n "$TAGVER"
1237 }
1238
1239 # bool is_tag_a_branch(tag)
1240 #
1241 # returns 1 if a tag is a branch set on SPECFILE
1242 is_tag_a_branch() {
1243         if [ -n "$DEBUG" ]; then
1244                 set -x
1245                 set -v
1246         fi
1247
1248         if [ $# -ne 1 ]; then
1249                 return 0;
1250         fi
1251
1252         TAG=$1
1253
1254         cd "$PACKAGE_DIR"
1255         $CVS_COMMAND status -v $SPECFILE | grep -Eiq "${TAG}.+(branch: [0-9.]+)"
1256         return $?
1257 }
1258
1259 tag_files() {
1260         TAG_FILES="$@"
1261
1262         if [ -n "$DEBUG" ]; then
1263                 set -x
1264                 set -v
1265         fi
1266
1267         echo "Version: $PACKAGE_VERSION"
1268         echo "Release: $PACKAGE_RELEASE"
1269
1270         local TAGVER
1271         if [ "$TAG_VERSION" = "yes" ]; then
1272                 TAGVER=`make_tagver`
1273                 echo "CVS tag: $TAGVER"
1274         fi
1275         if [ -n "$TAG" ]; then
1276                 echo "CVS tag: $TAG"
1277         fi
1278
1279         local OPTIONS="tag $CVS_FORCE"
1280         if [ -n "$CVSROOT" ]; then
1281                 OPTIONS="-d $CVSROOT $OPTIONS"
1282         fi
1283
1284         # if a tagname we are about to set already exists
1285         # and happens to be a branch (common case with AC-branch)
1286         # pass -B (allows -F to disturb branch tag)
1287         local _tag=$TAG
1288         if [ "$TAG_VERSION" = "yes" ]; then
1289                 _tag=$TAGVER
1290         fi;
1291         is_tag_a_branch $_tag
1292         if [ $? -eq 0 -a $CVS_NSERVER -eq 0 ]; then
1293                 OPTIONS="$OPTIONS -B"
1294         fi;
1295
1296         cd "$PACKAGE_DIR"
1297         local tag_files
1298         for i in $TAG_FILES; do
1299                 # don't tag files stored on distfiles
1300                 [ -n "`src_md5 $i`" ] && continue
1301                 local fp=`nourl "$i"`
1302                 if [ -f "$fp" ]; then
1303                         tag_files="$tag_files $fp"
1304                 elif [ -n "GREEDSRC" ]; then
1305                         get_greed_sources $i
1306                 else
1307                         Exit_error err_no_source_in_repo $i
1308                 fi
1309         done
1310
1311         if [ "$tag_files" ]; then
1312                 if [ "$TAG_VERSION" = "yes" ]; then
1313                         update_shell_title "tag sources: $TAGVER"
1314                         printf "Tagging %d files\n" $(echo $tag_files | wc -w)
1315                         $CVS_COMMAND $OPTIONS $TAGVER $tag_files || exit
1316                 fi
1317                 if [ -n "$TAG" ]; then
1318                         update_shell_title "tag sources: $TAG"
1319
1320                         while [ "$tag_files" ]; do
1321                                 local chunk=$(echo $tag_files | tr ' ' '\n' | head -n 100)
1322                                 printf "Tagging %d files\n" $(echo $chunk | wc -w)
1323                                 $CVS_COMMAND $OPTIONS $TAG $chunk || exit
1324                                 tag_files=$(echo $tag_files | tr ' ' '\n' | tail +101)
1325                         done
1326                 fi
1327         fi
1328
1329         cd "$PACKAGE_DIR"
1330         if [ "$TAG_VERSION" = "yes" ]; then
1331                 update_shell_title "tag spec: $TAGVER"
1332                 $CVS_COMMAND $OPTIONS $TAGVER $SPECFILE || exit
1333         fi
1334         if [ -n "$TAG" ]; then
1335                 update_shell_title "tag spec: $TAG"
1336                 $CVS_COMMAND $OPTIONS $TAG $SPECFILE || exit
1337         fi
1338 }
1339
1340 branch_files() {
1341         TAG=$1
1342         echo "CVS branch tag: $TAG"
1343         shift
1344
1345         TAG_FILES="$@"
1346
1347         if [ -n "$DEBUG" ]; then
1348                 set -x
1349                 set -v
1350         fi
1351
1352         local OPTIONS="tag $CVS_FORCE -b"
1353
1354         # branch exists?
1355         is_tag_a_branch $TAG
1356         if [ $? -eq 1 ]; then
1357                 OPTIONS="$OPTIONS -B"
1358         fi
1359
1360         if [ -n "$CVSROOT" ]; then
1361                 OPTIONS="-d $CVSROOT $OPTIONS"
1362         fi
1363         cd "$PACKAGE_DIR"
1364         local tag_files
1365         for i in $TAG_FILES; do
1366                 local fp=`nourl "$i"`
1367                 if [ -f "$fp" ]; then
1368                         tag_files="$tag_files $fp"
1369                 elif [ -n "GREEDSRC" ]; then
1370                         get_greed_sources $i
1371                 else
1372                         Exit_error err_no_source_in_repo $i
1373                 fi
1374         done
1375         if [ "$tag_files" ]; then
1376                 $CVS_COMMAND $OPTIONS $TAG $tag_files || exit
1377         fi
1378
1379         cd "$PACKAGE_DIR"
1380         $CVS_COMMAND $OPTIONS $TAG $SPECFILE || exit
1381 }
1382
1383
1384 # this function should exit early if package can't be built for this arch
1385 # this avoids unneccessary BR filling.
1386 check_buildarch() {
1387         local out ret
1388         out=$(minirpm --short-circuit -bp --define "'prep exit 0'" --nodeps $SPECFILE 2>&1)
1389         ret=$?
1390         if [ $ret -ne 0 ]; then
1391                 echo >&2 "$out"
1392                 exit $ret
1393         fi
1394 }
1395
1396 # from relup.sh
1397 set_release() {
1398         local specfile="$1"
1399         local rel="$2"
1400         local newrel="$3"
1401         sed -i -e "
1402                 s/^\(%define[ \t]\+_\?rel[ \t]\+\)$rel\$/\1$newrel/
1403                 s/^\(Release:[ \t]\+\)$rel\$/\1$newrel/
1404         " $specfile
1405 }
1406
1407 set_version() {
1408         local specfile="$1"
1409         local ver="$2"
1410         local newver="$3"
1411         sed -i -e "
1412                 s/^\(%define[ \t]\+_\?ver[ \t]\+\)$ver\$/\1$newver/
1413                 s/^\(Version:[ \t]\+\)$ver\$/\1$newver/
1414         " $specfile
1415 }
1416
1417 try_upgrade() {
1418         if [ -n "$TRY_UPGRADE" ]; then
1419                 local TNOTIFY TNEWVER TOLDVER
1420                 update_shell_title "build_package: try_upgrade"
1421
1422                 cd "$PACKAGE_DIR"
1423                 
1424                 if [ -n "$FLOAT_VERSION" ]; then
1425                         TNOTIFY=$($APPDIR/pldnotify.awk ${BE_VERBOSE:+-vDEBUG=1} $SPECFILE -n) || exit 1
1426                 else
1427                         TNOTIFY=$($APPDIR/pldnotify.awk ${BE_VERBOSE:+-vDEBUG=1} $SPECFILE) || exit 1
1428                 fi
1429
1430                 TNEWVER=$(echo $TNOTIFY | awk '{ match($4,/\[NEW\]/); print $5 }')
1431
1432                 if [ -n "$TNEWVER" ]; then
1433                         TOLDVER=`echo $TNOTIFY | awk '{ print $3; }'`
1434                         echo "New version found, updating spec file from $TOLDVER to version $TNEWVER"
1435                         if [ "$REVERT_BROKEN_UPGRADE" = "yes" ]; then
1436                                 cp -f $SPECFILE $SPECFILE.bak
1437                         fi
1438                         chmod +w $SPECFILE
1439                         set_release $SPECFILE $PACKAGE_RELEASE 1
1440                         set_version $SPECFILE $PACKAGE_VERSION $TNEWVER
1441                         parse_spec
1442                         return 1
1443                 fi
1444         fi
1445         return 0
1446 }
1447
1448 build_package() {
1449         update_shell_title "build_package"
1450         if [ -n "$DEBUG" ]; then
1451                 set -x
1452                 set -v
1453         fi
1454
1455         cd "$PACKAGE_DIR"
1456
1457         case "$COMMAND" in
1458                 build )
1459                         BUILD_SWITCH="-ba" ;;
1460                 build-binary )
1461                         BUILD_SWITCH="-bb" ;;
1462                 build-source )
1463                         BUILD_SWITCH="-bs --nodeps" ;;
1464                 build-prep )
1465                         BUILD_SWITCH="-bp --nodeps" ;;
1466                 build-build )
1467                         BUILD_SWITCH="-bc" ;;
1468                 build-install )
1469                         BUILD_SWITCH="-bi" ;;
1470                 build-list )
1471                         BUILD_SWITCH="-bl" ;;
1472
1473         esac
1474
1475         update_shell_title "build_package: $COMMAND"
1476         if [ -n "$LOGFILE" ]; then
1477                 LOG=`eval echo $LOGFILE`
1478                 if [ -d "$LOG" ]; then
1479                         echo "Log file $LOG is a directory."
1480                         echo "Parse error in the spec?"
1481                         Exit_error err_build_fail
1482                 fi
1483                 if [ -n "$LASTLOG_FILE" ]; then
1484                         echo "LASTLOG=$LOG" > $LASTLOG_FILE
1485                 fi
1486                 RES_FILE=$(mktemp -t builder.XXXXXX || ${TMPDIR:-/tmp}/builder.$RANDOM)
1487
1488                 (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
1489                 RETVAL=`cat $RES_FILE`
1490                 rm $RES_FILE
1491                 if [ -n "$LOGDIROK" ] && [ -n "$LOGDIRFAIL" ]; then
1492                         if [ "$RETVAL" -eq "0" ]; then
1493                                 mv $LOG $LOGDIROK
1494                         else
1495                                 mv $LOG $LOGDIRFAIL
1496                         fi
1497                 fi
1498         else
1499                 eval ${NICE_COMMAND} $RPMBUILD $TARGET_SWITCH $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $RPMBUILDOPTS $BCOND --define \'_specdir $PACKAGE_DIR\' --define \'_sourcedir $PACKAGE_DIR\' $SPECFILE
1500                 RETVAL=$?
1501         fi
1502         if [ "$RETVAL" -ne "0" ]; then
1503                 if [ -n "$TRY_UPGRADE" ]; then
1504                         echo "\nUpgrade package to new version failed."
1505                         if [ "$REVERT_BROKEN_UPGRADE" = "yes" ]; then
1506                                 echo "Restoring old spec file."
1507                                 mv -f $SPECFILE.bak $SPECFILE
1508                         fi
1509                         echo ""
1510                 fi
1511                 Exit_error err_build_fail
1512         fi
1513         unset BUILD_SWITCH
1514 }
1515
1516 nourl() {
1517         echo "$@" | sed 's#\<\(ftp\|http\|https\|cvs\|svn\)://[^ ]*/##g'
1518 }
1519
1520 install_required_packages() {
1521         run_poldek -vi $1
1522         return $?
1523 }
1524
1525 find_spec_bcond() { # originally from /usr/lib/rpm/find-spec-bcond
1526         local SPEC="$1"
1527         awk -F"\n" '
1528         /^%changelog/ { exit }
1529         /^%bcond_with/{
1530                 match($0, /bcond_with(out)?[ \t]+[_a-zA-Z0-9]+/);
1531                 bcond = substr($0, RSTART + 6, RLENGTH - 6);
1532                 gsub(/[ \t]+/, "_", bcond);
1533                 print bcond
1534         }' $SPEC | LC_ALL=C sort -u
1535 }
1536
1537 process_bcondrc() {
1538         # expand bconds from ~/.bcondrc
1539         # The file structure is like gentoo's package.use:
1540         # ---
1541         # * -selinux
1542         # samba -mysql -pgsql
1543         # w32codec-installer license_agreement
1544         # php +mysqli
1545         # ---
1546         if ([ -f $HOME/.bcondrc ] || ([ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ])); then
1547                 :
1548         else
1549                 return
1550         fi
1551
1552         SN=${SPECFILE%%\.spec}
1553
1554         local bcondrc=$HOME/.bcondrc
1555         [ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ] && bcondrc=$HOME_ETC/.bcondrc
1556
1557         local bcond_avail=$(find_spec_bcond $SPECFILE)
1558
1559         while read pkg flags; do
1560                 # ignore comments
1561                 [[ "$pkg" == \#* ]] && continue
1562
1563                 # any package or current package?
1564                 if [ "$pkg" = "*" ] || [ "$pkg" = "$PACKAGE_NAME" ] || [ "$pkg" = "$SN" ]; then
1565                         for flag in $flags; do
1566                                 local opt=${flag#[+-]}
1567
1568                                 # use only flags which are in this package.
1569                                 if [[ $bcond_avail = *${opt}* ]]; then
1570                                         if [[ $flag = -* ]]; then
1571                                                 if [[ $BCOND != *--with?${opt}* ]]; then
1572                                                         BCOND="$BCOND --without $opt"
1573                                                 fi
1574                                         else
1575                                                 if [[ $BCOND != *--without?${opt}* ]]; then
1576                                                         BCOND="$BCOND --with $opt"
1577                                                 fi
1578                                         fi
1579                                 fi
1580                         done
1581                 fi
1582         done < $bcondrc
1583         update_shell_title "parse ~/.bcondrc: DONE!"
1584 }
1585
1586 set_bconds_values() {
1587         update_shell_title "set bcond values"
1588
1589         AVAIL_BCONDS_WITHOUT=""
1590         AVAIL_BCONDS_WITH=""
1591
1592         if egrep -q '^# *_with' ${SPECFILE}; then
1593                 echo >&2 "ERROR: This spec has old style bconds."
1594                 exit 1
1595         fi
1596
1597         if ! grep -q '^%bcond' ${SPECFILE}; then
1598                 return
1599         fi
1600
1601         local bcond_avail=$(find_spec_bcond $SPECFILE)
1602         process_bcondrc "$SPECFILE"
1603
1604         update_shell_title "parse bconds"
1605
1606         local opt bcond
1607         for opt in $bcond_avail; do
1608                 case "$opt" in
1609                 without_*)
1610                         bcond=${opt#without_}
1611                         case "$BCOND" in
1612                         *--without?${bcond}*)
1613                                 AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT <$bcond>"
1614                                 ;;
1615                         *)
1616                                 AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT $bcond"
1617                                 ;;
1618                         esac
1619                         ;;
1620                 with_*)
1621                         bcond=${opt#with_}
1622                         case "$BCOND" in
1623                         *--with?${bcond}*)
1624                                 AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH <$bcond>"
1625                                 ;;
1626                         *)
1627                                 AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH $bcond"
1628                                 ;;
1629                         esac
1630                         ;;
1631                 *)
1632                         echo >&2 "ERROR: unexpected '$opt' in set_bconds_values"
1633                         exit 1
1634                         ;;
1635                 esac
1636         done
1637 }
1638
1639 run_sub_builder() {
1640         package_name="${1}"
1641         update_shell_title "run_sub_builder $package_name"
1642         #
1643         # No i tutaj bym chcia³ zrobiæ sztuczn± inteligencjê, która spróbuje tego
1644         # pakieta zbudowaæ. Aktualnie niewiele dziala, bo generalnie nie widze do
1645         # konca algorytmu... Ale damy rade. :) Na razie po prostu sie wyjebie tak samo
1646         # jakby nie bylo tego kawalka kodu.
1647         #
1648         # Update: Poprawi³em parê rzeczy i zaczê³o generowaæ pakiety spoza zadanej listy.
1649         #         Jednym s³owem budowanie niespoldkowanych zale¿no¶ci dzia³a w paru przypadkach.
1650         #
1651         #
1652         # y0shi.
1653         # kurwa. translate that ^^^^
1654
1655         parent_spec_name=''
1656
1657         # Istnieje taki spec? ${package}.spec
1658         if [ -f "${PACKAGE_DIR}/${package}.spec" ]; then
1659                 parent_spec_name=${package}.spec
1660         elif [ -f "${PACKAGE_DIR}/$(echo ${package_name} | sed -e s,-devel.*,,g -e s,-static,,g).spec" ]; then
1661                 parent_spec_name="$(echo ${package_name} | sed -e s,-devel.*,,g -e s,-static,,g).spec"
1662         else
1663                 for provides_line in $(grep -r ^Provides:.*$package ${PACKAGE_DIR}); do
1664                         echo $provides_line
1665                 done
1666         fi
1667
1668         if [ "${parent_spec_name}" != "" ]; then
1669                 spawn_sub_builder $parent_spec_name
1670         fi
1671         NOT_INSTALLED_PACKAGES="$NOT_INSTALLED_PACKAGES $package_name"
1672 }
1673
1674 spawn_sub_builder() {
1675         package_name="${1}"
1676         update_shell_title "spawn_sub_builder $package_name"
1677
1678         sub_builder_opts=''
1679         if [ "${FETCH_BUILD_REQUIRES}" == "yes" ]; then
1680                 sub_builder_opts="${sub_builder_opts} -R"
1681         fi
1682         if [ "${REMOVE_BUILD_REQUIRES}" == "nice" ]; then
1683                 sub_builder_opts="${sub_builder_opts} -RB"
1684         elif [ "${REMOVE_BUILD_REQUIRES}" == "force" ]; then
1685                 sub_builder_opts="${sub_builder_opts} -FRB"
1686         fi
1687         if [ "${UPDATE_POLDEK_INDEXES}" == "yes" ]; then
1688                 sub_builder_opts="${sub_builder_opts} -Upi"
1689         fi
1690
1691         cd "${PACKAGE_DIR}"
1692         ./builder ${sub_builder_opts} "$@"
1693 }
1694
1695 remove_build_requires() {
1696         if [ "$INSTALLED_PACKAGES" != "" ]; then
1697                 case "$REMOVE_BUILD_REQUIRES" in
1698                         "force")
1699                                 run_poldek --noask -ve $INSTALLED_PACKAGES
1700                                 ;;
1701                         "nice")
1702                                 run_poldek --ask -ve $INSTALLED_PACKAGES
1703                                 ;;
1704                         *)
1705                                 echo You may want to manually remove following BuildRequires fetched:
1706                                 echo $INSTALLED_PACKAGES
1707                                 echo "Try poldek -e \`cat $(pwd)/.${SPECFILE}_INSTALLED_PACKAGES\`"
1708                                 ;;
1709                 esac
1710         fi
1711 }
1712
1713 display_bconds() {
1714         if [ "$AVAIL_BCONDS_WITH" -o "$AVAIL_BCONDS_WITHOUT" ]; then
1715                 if [ "$BCOND" ]; then
1716                         echo ""
1717                         echo "Building $SPECFILE with the following conditional flags:"
1718                         echo -n "$BCOND"
1719                 else
1720                         echo ""
1721                         echo "No conditional flags passed"
1722                 fi
1723                 echo ""
1724                 echo "from available:"
1725                 echo "--with   :\t$AVAIL_BCONDS_WITH"
1726                 echo "--without:\t$AVAIL_BCONDS_WITHOUT"
1727                 echo ""
1728         fi
1729 }
1730
1731 display_branches() {
1732         if [ "$NOCVSSPEC" != "yes" ]; then
1733                 echo -n "Available branches: "
1734                 $CVS_COMMAND status -v "${SPECFILE}" | awk '!/Sticky Tag:/ && /\(branch:/ { print $1 } ' | xargs
1735         fi
1736 }
1737
1738 # checks a given list of packages/files/provides agains current rpmdb.
1739 # outputs all dependencies which current rpmdb doesn't satisfy.
1740 # input can be either STDIN or parameters
1741 _rpm_prov_check() {
1742         local DEPS
1743
1744         if [ $# -gt 0 ]; then
1745                 DEPS="$@"
1746         else
1747                 DEPS=$(cat)
1748         fi
1749
1750         DEPS=$(LANG=C rpm -q --whatprovides $DEPS 2>&1 | awk '/^(error:|no package provides)/ { print }')
1751
1752         # packages
1753         echo "$DEPS" | awk '/^no package provides/ { print $NF }'
1754
1755         # other deps (files)
1756         echo "$DEPS" | awk -F: '/^error:.*No such file/{o = $2; gsub("^ file ", "", o); print o}'
1757 }
1758
1759 # checks if given package/files/provides exists in rpmdb.
1760 # input can be either stdin or parameters
1761 # returns packages which are present in the rpmdb
1762 _rpm_cnfl_check() {
1763         local DEPS
1764
1765         if [ $# -gt 0 ]; then
1766                 DEPS="$@"
1767         else
1768                 DEPS=$(cat)
1769         fi
1770
1771         LANG=C rpm -q --whatprovides $DEPS 2>/dev/null | awk '!/no package provides/ { print }'
1772 }
1773
1774 # install deps via information from 'rpm-getdeps' or 'rpm --specsrpm'
1775 install_build_requires_rpmdeps() {
1776         if [ "$FETCH_BUILD_REQUIRES_RPMGETDEPS" = "yes" ]; then
1777                 # TODO: Conflicts list doesn't check versions
1778                 local CNFL=$(rpm-getdeps $BCOND $SPECFILE 2> /dev/null | awk '/^\-/ { print $3 } ' | _rpm_cnfl_check | xargs)
1779                 local DEPS=$(rpm-getdeps $BCOND $SPECFILE 2> /dev/null | awk '/^\+/ { print $3 } ' | _rpm_prov_check | xargs)
1780         fi
1781         if [ "$FETCH_BUILD_REQUIRES_RPMSPECSRPM" = "yes" ]; then
1782                 local CNFL=$(rpm -q --specsrpm --conflicts $BCOND $SPECFILE | awk '{print $1}' | _rpm_cnfl_check | xargs)
1783                 local DEPS=$(rpm -q --specsrpm --requires $BCOND $SPECFILE | awk '{print $1}' | _rpm_prov_check | xargs)
1784         fi
1785
1786         if [ -n "$CNFL" ] || [ -n "$DEPS" ]; then
1787                 echo "fetch BuildRequires: install [$DEPS]; remove [$CNFL]"
1788                 update_shell_title "poldek: install [$DEPS]; remove [$CNFL]"
1789                 $SU_SUDO /usr/bin/poldek -q --update || $SU_SUDO /usr/bin/poldek -q --upa
1790         fi
1791         if [ -n "$CNFL" ]; then
1792                 update_shell_title "uninstall conflicting packages: $CNFL"
1793                 echo "Trying to uninstall conflicting packages ($CNFL):"
1794                 $SU_SUDO /usr/bin/poldek --noask --nofollow -ev $CNFL
1795         fi
1796
1797         while [ "$DEPS" ]; do
1798                         update_shell_title "install deps: $DEPS"
1799                         echo "Trying to install dependencies ($DEPS):"
1800                         local log=.${SPECFILE}_poldek.log
1801                         LANG=C $SU_SUDO /usr/bin/poldek --noask --caplookup -uGqQ $DEPS | tee $log
1802                         failed=$(awk '/^error:/{a=$2; sub(/^error: /, "", a); sub(/:$/, "", a); print a}' $log)
1803                         rm -f $log
1804                         local ok
1805                         if [ -n "$failed" ]; then
1806                                 for package in $failed; do
1807                                         spawn_sub_builder -bb $(depspecname $package) && ok="$ok $package"
1808                                 done
1809                                 DEPS="$ok"
1810                         else
1811                                 DEPS=""
1812                         fi
1813         done
1814 }
1815
1816 fetch_build_requires()
1817 {
1818         if [ "${FETCH_BUILD_REQUIRES}" != "yes" ]; then
1819                 return
1820         fi
1821
1822         update_shell_title "fetch build requires"
1823         if [ "$FETCH_BUILD_REQUIRES_RPMGETDEPS" = "yes" ] || [ "$FETCH_BUILD_REQUIRES_RPMSPECSRPM" = "yes" ]; then
1824                 install_build_requires_rpmdeps
1825                 return
1826         fi
1827
1828                 # XXX is this ugliest code written in human history still needed?
1829                 echo "All packages installed by fetch_build_requires() are written to:"
1830                 echo "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES"
1831                 echo ""
1832                 echo "If anything fails, you may get rid of them by executing:"
1833                 echo "poldek -e \`cat `pwd`/.${SPECFILE}_INSTALLED_PACKAGES\`"
1834                 echo ""
1835                 echo > `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1836                 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)
1837                 do
1838                         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)
1839                         GO="yes"
1840                         package=$(basename "$package_item"|sed -e "s/}$//g")
1841                         COND_ARCH_TST=$(cat $SPECFILE|grep -B1 BuildRequires|grep -B1 $package|grep ifarch|sed -e "s/^.*ifarch//g")
1842                         mach=$(uname -m)
1843
1844                         COND_TST=`cat $SPECFILE|grep BuildRequires|grep "$package"`
1845                         if `echo $COND_TST|grep -q '^BuildRequires:'`; then
1846                                 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
1847                                         GO="yes"
1848                                 fi
1849                         # bcond:
1850                         else
1851                                 COND_NAME=`echo $COND_TST|sed -e s,:BuildRequires:.*$,,g`
1852                                 GO=""
1853                                 # %{without}
1854                                 if `echo $COND_TST|grep -q 'without_'`; then
1855                                         COND_NAME=`echo $COND_NAME|sed -e s,^.*without_,,g`
1856                                         if `echo $COND_TST|grep -q !`; then
1857                                                 COND_STATE="with"
1858                                         else
1859                                                 COND_STATE="wout"
1860                                         fi
1861                                         COND_WITH=`echo $AVAIL_BCONDS_WITH|grep "<$COND_NAME>"`
1862                                         COND_WITHOUT=`echo $AVAIL_BCONDS_WITHOUT|grep "<$COND_NAME>"`
1863                                         if [ -n "$COND_WITHOUT" ] || [ -z "$COND_WITH" ]; then
1864                                                 COND_ARGV="wout"
1865                                         else
1866                                                 COND_ARGV="with"
1867                                         fi
1868                                 # %{with}
1869                                 elif `echo $COND_TST|grep -q 'with_'`; then
1870                                         COND_NAME=`echo $COND_NAME|sed -e s,^.*with_,,g`
1871                                         if `echo $COND_TST|grep -q !`; then
1872                                                 COND_STATE="wout"
1873                                         else
1874                                                 COND_STATE="with"
1875                                         fi
1876                                         COND_WITH=`echo $AVAIL_BCONDS_WITH|grep "<$COND_NAME>"`
1877                                         COND_WITHOUT=`echo $AVAIL_BCONDS_WITHOUT|grep "<$COND_NAME>"`
1878                                         if [ -n "$COND_WITH" ] || [ -z "$COND_WITHOUT" ]; then
1879                                                 COND_ARGV="with"
1880                                         else
1881                                                 COND_ARGV="wout"
1882                                         fi
1883                                 fi
1884                                 RESULT="${COND_STATE}-${COND_ARGV}"
1885                                 case "$RESULT" in
1886                                         "with-wout" | "wout-with" )
1887                                                 GO=""
1888                                                 ;;
1889                                         "wout-wout" | "with-with" )
1890                                                 GO="yes"
1891                                                 ;;
1892                                         * )
1893                                                 echo "Action '$RESULT' was not defined for package '$package_item'"
1894                                                 GO="yes"
1895                                                 ;;
1896                                 esac
1897                         fi
1898
1899                         if [ "$GO" = "yes" ]; then
1900                                 if [ "`rpm -q $package|sed -e "s/$package.*/$package/g"`" != "$package" ]; then
1901                                         echo "Testing if $package has subrequirements..."
1902                                         run_poldek -t -i $package --dumpn=".$package-req.txt"
1903                                         if [ -f ".$package-req.txt" ]; then
1904                                                 for package_name in `cat ".$package-req.txt"|grep -v ^#`
1905                                                 do
1906                                                         if [ "$package_name" = "$package" ]; then
1907                                                                 echo "Installing BuildRequired package:\t$package_name"
1908                                                                 update_shell_title "Installing BuildRequired package: ${package_name}"
1909                                                                 install_required_packages $package
1910                                                         else
1911                                                                 echo "Installing (sub)Required package:\t$package_name"
1912                                                                 update_shell_title "Installing (sub)Required package: ${package_name}"
1913                                                                 install_required_packages $package_name
1914                                                         fi
1915                                                         case $? in
1916                                                                 0)
1917                                                                         INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
1918                                                                         echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1919                                                                         ;;
1920                                                                 *)
1921                                                                         echo "Attempting to run spawn sub - builder..."
1922                                                                         echo "Package installation failed:\t$package_name"
1923                                                                         run_sub_builder $package_name
1924                                                                         if [ $? -eq 0 ]; then
1925                                                                                 install_required_packages $package_name
1926                                                                                 case $? in
1927                                                                                         0)
1928                                                                                                 INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
1929                                                                                                 echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1930                                                                                                 ;;
1931                                                                                         *)
1932                                                                                                 NOT_INSTALLED_PACKAGES="$package_name $NOT_INSTALLED_PACKAGES"
1933                                                                                                 ;;
1934                                                                                 esac
1935                                                                         fi
1936                                                                         ;;
1937                                                         esac
1938                                                 done
1939                                                 rm -f ".$package-req.txt"
1940                                         else
1941                                                 echo "Attempting to run spawn sub - builder..."
1942                                                 echo "Package installation failed:\t$package"
1943                                                 run_sub_builder $package
1944                                                 if [ $? -eq 0 ]; then
1945                                                         install_required_packages $package
1946                                                         case $? in
1947                                                                 0)
1948                                                                         INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
1949                                                                         echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1950                                                                         ;;
1951                                                                 *)
1952                                                                         NOT_INSTALLED_PACKAGES="$package_name $NOT_INSTALLED_PACKAGES"
1953                                                                         ;;
1954                                                         esac
1955                                                 fi
1956                                         fi
1957                                 else
1958                                         echo "Package $package is already installed. BuildRequirement satisfied."
1959                                 fi
1960                         fi
1961                 done
1962                 if [ "$NOT_INSTALLED_PACKAGES" != "" ]; then
1963                         echo >&2 "Unable to install following packages and their dependencies:"
1964                         for pkg in "$NOT_INSTALLED_PACKAGES"
1965                         do
1966                                 echo $pkg
1967                         done
1968                         remove_build_requires
1969                         exit 8
1970                 fi
1971 }
1972
1973 init_rpm_dir() {
1974         TOP_DIR=$(eval $RPM $RPMOPTS --eval '%{_topdir}')
1975         CVSROOT=":pserver:cvs@$CVS_SERVER:/cvsroot"
1976
1977         echo "Initializing rpm directories to $TOP_DIR from $CVSROOT"
1978         mkdir -p $TOP_DIR/{RPMS,BUILD,SRPMS}
1979         cd $TOP_DIR
1980         $CVS_COMMAND -d $CVSROOT co packages/{.cvsignore,rpm.groups,dropin,mirrors,md5,adapter{,.awk},fetchsrc_request,builder,{relup,compile,repackage}.sh}
1981
1982         init_builder
1983
1984         echo "To checkout *all* .spec files (read-only):"
1985         echo "- run cvs co SPECS"
1986
1987         echo "To checkout *all* packages:"
1988         echo "- run cvs up -dP in $TOP_DIR/packages dir"
1989
1990         echo ""
1991         echo "To commit with your developer account:"
1992         echo "- edit $TOP_DIR/packages/CVS/Root"
1993 }
1994
1995 get_greed_sources() {
1996         CVSROOT=":pserver:cvs@$CVS_SERVER:/cvsroot"
1997         if [ -n "BE_VERBOSE" ]; then
1998                 echo "Try greed download: $1 from: $CVSROOT"
1999         fi
2000         $CVS_COMMAND -d $CVSROOT get SOURCES/$1
2001         if [ $? != 0 ]; then
2002                 Exit_error err_no_source_in_repo $1
2003         fi
2004
2005 }
2006
2007 # remove entries from CVS/Entries
2008 cvs_entry_remove() {
2009         local cvsdir="$1"; shift
2010         if [ ! -d "$cvsdir" ]; then
2011                 echo >&2 "cvs_entry_remove: $cvsdir is not a directory"
2012                 exit 1
2013         fi
2014
2015         for file in "$@"; do
2016                 rm -f $cvsdir/CVS/Entries.new || return 1
2017                 awk -ve="${file##*/}" -F/ '$2 != e {print}' $cvsdir/CVS/Entries > $cvsdir/CVS/Entries.new || return 1
2018                 mv -f $cvsdir/CVS/Entries.new $cvsdir/CVS/Entries || return 1
2019         done
2020         return 0
2021 }
2022
2023 mr_proper() {
2024         init_builder
2025         NOCVSSPEC="yes"
2026         DONT_PRINT_REVISION="yes"
2027         get_spec
2028         parse_spec
2029
2030         # remove from CVS/Entries
2031         cvs_entry_remove $PACKAGE_DIR $SPECFILE $SOURCES $PATCHES
2032
2033         # remove spec and sources
2034         $RPMBUILD --clean --rmsource --rmspec --nodeps --define \'_specdir $PACKAGE_DIR\' --define \'_sourcedir $PACKAGE_DIR\' $SPECFILE
2035 }
2036
2037 #---------------------------------------------
2038 # main()
2039
2040 if [ $# = 0 ]; then
2041         usage
2042         exit 1
2043 fi
2044
2045 while [ $# -gt 0 ]; do
2046         case "${1}" in
2047                 -5 | --update-md5)
2048                         COMMAND="update_md5"
2049                         NODIST="yes"
2050                         NOCVSSPEC="yes"
2051                         shift ;;
2052                 -a5 | --add-md5 )
2053                         COMMAND="update_md5"
2054                         NODIST="yes"
2055                         NOCVS="yes"
2056                         NOCVSSPEC="yes"
2057                         ADD5="yes"
2058                         shift ;;
2059                 -n5 | --no-md5 )
2060                         NO5="yes"
2061                         shift ;;
2062                 -D | --debug )
2063                         DEBUG="yes"; shift ;;
2064                 -V | --version )
2065                         COMMAND="version"; shift ;;
2066                 --short-version )
2067                         COMMAND="short-version"; shift ;;
2068                 -a | --as_anon )
2069                         CVSROOT=":pserver:cvs@$CVS_SERVER:/cvsroot"; shift ;;
2070                 -b | -ba | --build )
2071                         COMMAND="build"; shift ;;
2072                 -bb | --build-binary )
2073                         COMMAND="build-binary"; shift ;;
2074                 -bc )
2075                         COMMAND="build-build"; shift ;;
2076                 -bi )
2077                         COMMAND="build-install"; shift ;;
2078                 -bl )
2079                         COMMAND="build-list"; shift ;;
2080                 -bp | --build-prep )
2081                         COMMAND="build-prep"; shift ;;
2082                 -bs | --build-source )
2083                         COMMAND="build-source"; shift ;;
2084                 -B | --branch )
2085                         COMMAND="branch"; shift; TAG="${1}"; shift;;
2086                 -c | --clean )
2087                         CLEAN="--clean"; shift ;;
2088                 -cf | --cvs-force )
2089                         CVS_FORCE="-F"; shift;;
2090                 -d | --cvsroot )
2091                         shift; CVSROOT="${1}"; shift ;;
2092                 -g | --get )
2093                         COMMAND="get"; shift ;;
2094                 -h | --help )
2095                         COMMAND="usage"; shift ;;
2096                 --http )
2097                         PROTOCOL="http"; shift ;;
2098                 -l | --logtofile )
2099                         shift; LOGFILE="${1}"; shift ;;
2100                 -ni| --nice )
2101                         shift; DEF_NICE_LEVEL=${1}; shift ;;
2102                 -ske | --skip-existing-files)
2103                         SKIP_EXISTING_FILES="yes"; shift ;;
2104                 -m | --mr-proper )
2105                         COMMAND="mr-proper"; shift ;;
2106                 -nc | --no-cvs )
2107                         NOCVS="yes"; shift ;;
2108                 -ncs | --no-cvs-specs )
2109                         NOCVSSPEC="yes"; shift ;;
2110                 -nd | --no-distfiles )
2111                         NODIST="yes"; shift ;;
2112                 -nm | --no-mirrors )
2113                         NOMIRRORS="yes"; shift ;;
2114                 -nu | --no-urls )
2115                         NOURLS="yes"; shift ;;
2116                 -ns | --no-srcs )
2117                         NOSRCS="yes"; shift ;;
2118                 -ns0 | --no-source0 )
2119                         NOSOURCE0="yes"; shift ;;
2120                 -nn | --no-net )
2121                         NOCVS="yes"
2122                         NOCVSSPEC="yes"
2123                         NODIST="yes"
2124                         NOMIRRORS="yes"
2125                         NOURLS="yes"
2126                         NOSRCS="yes"
2127                         ALWAYS_CVSUP="no"
2128                         shift;;
2129                 -pm | --prefer-mirrors )
2130                         PREFMIRRORS="yes"
2131                         shift;;
2132                 --no-init )
2133                         NOINIT="yes"
2134                         shift;;
2135                 --opts )
2136                         shift; RPMOPTS="${RPMOPTS} ${1}"; shift ;;
2137                 --nopatch | -np )
2138                         shift; RPMOPTS="${RPMOPTS} --define \"patch${1} : ignoring patch${1}; exit 1; \""; shift ;;
2139                 --topdir)
2140                         RPMOPTS="${RPMOPTS} --define \"_topdir $2\""
2141                         shift 2
2142                         ;;
2143                 --with | --without )
2144                         case $GROUP_BCONDS in
2145                                 "yes")
2146                                         COND=${1}
2147                                         shift
2148                                         # XXX: broken: ./builder -bb ucspi-tcp.spec --without mysql
2149                                         while ! `echo ${1}|grep -qE '(^-|spec)'`
2150                                         do
2151                                                 BCOND="$BCOND $COND $1"
2152                                                 shift
2153                                         done;;
2154                                 "no")
2155                                         if [[ "$2" = *,* ]]; then
2156                                                 for a in $(echo "$2" | tr , ' '); do
2157                                                         BCOND="$BCOND $1 $a"
2158                                                 done
2159                                         else
2160                                                 BCOND="$BCOND $1 $2"
2161                                         fi
2162                                         shift 2 ;;
2163                         esac
2164                         ;;
2165                 --target )
2166                         shift; TARGET="${1}"; shift ;;
2167                 --target=* )
2168                         TARGET=$(echo "${1}" | sed 's/^--target=//'); shift ;;
2169                 -q | --quiet )
2170                         QUIET="--quiet"; shift ;;
2171                 --date )
2172                         CVSDATE="${2}"; shift 2 ;;
2173                 -r | --cvstag )
2174                         CVSTAG="$2"
2175                         shift 2
2176                         ;;
2177                 -A)
2178                         shift
2179                         CVSTAG="HEAD"
2180                         ;;
2181                 -R | --fetch-build-requires)
2182                         FETCH_BUILD_REQUIRES="yes"
2183                         NOT_INSTALLED_PACKAGES=
2184                         shift ;;
2185                 -RB | --remove-build-requires)
2186                         REMOVE_BUILD_REQUIRES="nice"
2187                         shift ;;
2188                 -FRB | --force-remove-build-requires)
2189                         REMOVE_BUILD_REQUIRES="force"
2190                         shift ;;
2191                 -sc | --sources-cvs)
2192                         COMMAND="list-sources-cvs"
2193                         shift ;;
2194                 -sd | --sources-distfiles)
2195                         COMMAND="list-sources-distfiles"
2196                         shift ;;
2197                 -sdp | --sources-distfiles-paths)
2198                         COMMAND="list-sources-distfiles-paths"
2199                         shift ;;
2200                 -sf | --sources-files)
2201                         COMMAND="list-sources-files"
2202                         shift ;;
2203                 -sp | --sources-paths)
2204                         COMMAND="list-sources-local-paths"
2205                         shift ;;
2206                 -su | --sources-urls)
2207                         COMMAND="list-sources-urls"
2208                         shift ;;
2209                 -Tvs | --tag-version-stable )
2210                         COMMAND="tag"
2211                         TAG="STABLE"
2212                         TAG_VERSION="yes"
2213                         shift;;
2214                 -Ts | --tag-stable )
2215                         COMMAND="tag"
2216                         TAG="STABLE"
2217                         TAG_VERSION="no"
2218                         shift;;
2219                 -Tv | --tag-version )
2220                         COMMAND="tag"
2221                         TAG=""
2222                         TAG_VERSION="yes"
2223                         shift;;
2224                 -Tp | --tag-prefix )
2225                         TAG_PREFIX="$2"
2226                         shift 2;;
2227                 -tt | --test-tag )
2228                         TEST_TAG="yes"
2229                         shift;;
2230                 -T | --tag )
2231                         COMMAND="tag"
2232                         shift
2233                         TAG="$1"
2234                         TAG_VERSION="no"
2235                         shift;;
2236                 -ir | --integer-release-only )
2237                         INTEGER_RELEASE="yes"
2238                         shift;;
2239                 -U | --update )
2240                         COMMAND="update_md5"
2241                         UPDATE="yes"
2242                         NOCVSSPEC="yes"
2243                         NODIST="yes"
2244                         shift ;;
2245                 -Upi | --update-poldek-indexes )
2246                         UPDATE_POLDEK_INDEXES="yes"
2247                         shift ;;
2248                 --init-rpm-dir)
2249                         COMMAND="init_rpm_dir"
2250                         shift ;;
2251                 --use-greed-sources )
2252                         GREEDSRC="1"
2253                         shift;;
2254                 -u | --try-upgrade )
2255                         TRY_UPGRADE="1"; shift ;;
2256                 -un | --try-upgrade-with-float-version )
2257                         TRY_UPGRADE="1"; FLOAT_VERSION="1"; shift ;;
2258                 -v | --verbose )
2259                         BE_VERBOSE="1"; shift ;;
2260                 --define)
2261                         shift
2262                         MACRO="${1}"
2263                         shift
2264                         if echo "${MACRO}" | grep -q '\W'; then
2265                                 RPMOPTS="${RPMOPTS} --define \"${MACRO}\""
2266                         else
2267                                 VALUE="${1}"
2268                                 shift
2269                                 RPMOPTS="${RPMOPTS} --define \"${MACRO} ${VALUE}\""
2270                         fi
2271                         ;;
2272                 --alt_kernel)
2273                         shift
2274                         RPMOPTS="${RPMOPTS} --define \"alt_kernel $1\""
2275                         shift
2276                         ;;
2277                 --short-circuit)
2278                         RPMBUILDOPTS="${RPMBUILDOPTS} --short-circuit"
2279                         shift
2280                         ;;
2281                 --show-bconds | -show-bconds | -print-bconds | --print-bconds | -display-bconds | --display-bconds )
2282                         COMMAND="show_bconds"
2283                         shift
2284                         ;;
2285                 --show-bcond-args)
2286                         COMMAND="show_bcond_args"
2287                         shift
2288                         ;;
2289                 --show-avail-bconds)
2290                         COMMAND="show_avail_bconds"
2291                         shift
2292                         ;;
2293                 --nodeps)
2294                         shift
2295                         RPMOPTS="${RPMOPTS} --nodeps"
2296                         ;;
2297                 -debug)
2298                         RPMBUILDOPTS="${RPMBUILDOPTS} -debug"; shift
2299                         ;;
2300                 -*)
2301                         Exit_error err_invalid_cmdline "$1"
2302                         ;;
2303                 *)
2304                         SPECFILE=$1; shift
2305                         # check if specname was passed as specname:cvstag
2306                         if [ "${SPECFILE##*:}" != "${SPECFILE}" ]; then
2307                                 CVSTAG="${SPECFILE##*:}"
2308                                 SPECFILE="${SPECFILE%%:*}"
2309                         fi
2310                         # always have SPECFILE ending with .spec extension
2311                         SPECFILE=${SPECFILE%%.spec}.spec
2312                         ASSUMED_NAME=$(basename ${SPECFILE%%.spec})
2313         esac
2314 done
2315
2316 [ -d "$ASSUMED_NAME" ] && CVS_ENTRIES="$ASSUMED_NAME/CVS/Entries" || CVS_ENTRIES="CVS/Entries"
2317 if [ -f "$CVS_ENTRIES" ] && [ -z "$CVSTAG" ]; then
2318         CVSTAG=$(awk -vSPECFILE=$(basename $SPECFILE) -F/ '$2 == SPECFILE && $6 ~ /^T/{print substr($6, 2)}' ${CVS_ENTRIES})
2319         if [ "$CVSTAG" ]; then
2320                 echo >&2 "builder: Sticky tag $CVSTAG active. Use -r TAGNAME to override."
2321         fi
2322 elif [ "$CVSTAG" = "HEAD" ]; then
2323         # assume -r HEAD is same as -A
2324         CVSTAG=""
2325 fi
2326
2327 if [ "$CVSTAG" ]; then
2328         # pass $CVSTAG used by builder to rpmbuild too, so specs could use it
2329         RPMOPTS="$RPMOPTS --define \"_cvstag $CVSTAG\""
2330 fi
2331
2332 if [ -n "$DEBUG" ]; then
2333         set -x
2334         set -v
2335 fi
2336
2337 if [ -n "$TARGET" ]; then
2338         case "$RPMBUILD" in
2339                 "rpmbuild")
2340                         TARGET_SWITCH="--target $TARGET" ;;
2341                 "rpm")
2342                         TARGET_SWITCH="--target=$TARGET" ;;
2343         esac
2344 fi
2345
2346 if [ "$SCHEDTOOL" != "no" ]; then
2347         NICE_COMMAND="$SCHEDTOOL"
2348 else
2349         NICE_COMMAND="nice -n ${DEF_NICE_LEVEL}"
2350 fi
2351
2352 update_shell_title "$COMMAND"
2353 case "$COMMAND" in
2354         "show_bconds")
2355                 init_builder
2356                 if [ -n "$SPECFILE" ]; then
2357                         get_spec > /dev/null
2358                         parse_spec
2359                         set_bconds_values
2360                         display_bconds
2361                 fi
2362                 ;;
2363         "show_bcond_args")
2364                 init_builder
2365                 if [ -n "$SPECFILE" ]; then
2366                         get_spec > /dev/null
2367                         parse_spec
2368                         set_bconds_values
2369                         echo "$BCOND"
2370                 fi
2371                 ;;
2372         "show_avail_bconds")
2373                 init_builder
2374                 if [ -n "$SPECFILE" ]; then
2375                         get_spec > /dev/null
2376                         parse_spec
2377                         local bcond_avail=$(find_spec_bcond $SPECFILE)
2378                         local opt bcond bconds
2379                         for opt in $bcond_avail; do
2380                                 case "$opt" in
2381                                 without_*)
2382                                         bcond=${opt#without_}
2383                                         bconds="$bconds $bcond"
2384                                         ;;
2385                                 with_*)
2386                                         bcond=${opt#with_}
2387                                         bconds="$bconds $bcond"
2388                                         ;;
2389                                 *)
2390                                         echo >&2 "ERROR: unexpected '$opt' in show_avail_bconds"
2391                                         exit 1
2392                                         ;;
2393                                 esac
2394                         done
2395                         echo $bconds
2396                 fi
2397
2398                 ;;
2399         "build" | "build-binary" | "build-source" | "build-prep" | "build-build" | "build-install" | "build-list")
2400                 init_builder
2401                 if [ -n "$SPECFILE" ]; then
2402                         # display SMP make flags if set
2403                         smp_mflags=$(rpm -E %{?_smp_mflags})
2404                         if [ "$smp_mflags" ]; then
2405                                 echo "builder: SMP make flags are set to $smp_mflags"
2406                         fi
2407
2408                         get_spec
2409                         parse_spec
2410                         set_bconds_values
2411                         display_bconds
2412                         display_branches
2413                         if [ "$COMMAND" != "build-source" ]; then
2414                                 check_buildarch
2415                         fi
2416                         fetch_build_requires
2417                         if [ "$INTEGER_RELEASE" = "yes" ]; then
2418                                 echo "Checking release $PACKAGE_RELEASE..."
2419                                 if echo $PACKAGE_RELEASE | grep -q '^[^.]*\.[^.]*$' 2>/dev/null ; then
2420                                         Exit_error err_fract_rel "$PACKAGE_RELEASE"
2421                                 fi
2422                         fi
2423
2424                         # ./builder -bs test.spec -r AC-branch -Tp auto-ac- -tt
2425                         if [ -n "$TEST_TAG" ]; then
2426                                 local TAGVER=`make_tagver`
2427                                 echo "Searching for tag $TAGVER..."
2428                                 TAGREL=$($CVS_COMMAND status -v $SPECFILE | grep -E "^[[:space:]]*${TAGVER}[[[:space:]]" | sed -e 's#.*(revision: ##g' -e 's#).*##g')
2429                                 if [ -n "$TAGREL" ]; then
2430                                         Exit_error err_tag_exists "$TAGVER" "$TAGREL"
2431                                 fi
2432
2433                                 # - do not allow to build from HEAD when XX-branch exists
2434                                 TREE_PREFIX=$(echo "$TAG_PREFIX" | sed -e 's#^auto-\([a-zA-Z]\+\)-.*#\1#g')
2435                                 if [ "$TREE_PREFIX" != "$TAG_PREFIX" ]; then
2436                                         TAG_BRANCH="${TREE_PREFIX}-branch"
2437                                         TAG_STATUS=$($CVS_COMMAND status -v $SPECFILE | grep -Ei "${TAG_BRANCH}.+(branch: [0-9.]+)")
2438                                         if [ -n "$TAG_STATUS" -a "$CVSTAG" = "HEAD" ]; then
2439                                                 Exit_error err_branch_exists "$TAG_STATUS"
2440                                         fi
2441                                 fi
2442
2443                         fi
2444
2445                         if [ -n "$NOSOURCE0" ] ; then
2446                                 SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2447                         fi
2448                         try_upgrade
2449                         case $? in
2450                                 0)
2451                                         get_files $SOURCES $PATCHES
2452                                         check_md5 $SOURCES
2453                                         ;;
2454                                 *)
2455                                         NODIST="yes" get_files $SOURCES $PATCHES
2456                                         update_md5 $SOURCES
2457                                         ;;
2458                         esac
2459                         build_package
2460                         if [ "$UPDATE_POLDEK_INDEXES" = "yes" -a "$COMMAND" != "build-prep" ]; then
2461                                 run_poldek --sdir="${POLDEK_INDEX_DIR}" --mkidxz
2462                         fi
2463                         remove_build_requires
2464                 else
2465                         Exit_error err_no_spec_in_cmdl
2466                 fi
2467                 ;;
2468         "branch" )
2469                 init_builder
2470                 if [ -n "$SPECFILE" ]; then
2471                         get_spec
2472                         parse_spec
2473                         # don't fetch sources from remote locations
2474                         new_SOURCES=""
2475                         for file in $SOURCES; do
2476                                 [ -n "`src_md5 $file`" ] && continue
2477                                 new_SOURCES="$new_SOURCES $file"
2478                         done
2479                         SOURCES="$new_SOURCES"
2480                         get_files $SOURCES $PATCHES
2481                         check_md5 $SOURCES
2482                         branch_files $TAG $SOURCES $PATCHES $ICONS
2483                 else
2484                         Exit_error err_no_spec_in_cmdl
2485                 fi
2486                 ;;
2487         "get" )
2488                 init_builder
2489                 if [ -n "$SPECFILE" ]; then
2490                         get_spec
2491                         parse_spec
2492
2493                         if [ -n "$NOSOURCE0" ] ; then
2494                                 SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2495                         fi
2496                         get_files $SOURCES $PATCHES
2497                         check_md5 $SOURCES
2498                 else
2499                         Exit_error err_no_spec_in_cmdl
2500                 fi
2501                 ;;
2502         "update_md5" )
2503                 init_builder
2504                 if [ -n "$SPECFILE" ]; then
2505                         get_spec
2506                         parse_spec
2507
2508                         if [ -n "$NOSOURCE0" ] ; then
2509                                 SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2510                         fi
2511                         update_md5 $SOURCES
2512                 else
2513                         Exit_error err_no_spec_in_cmdl
2514                 fi
2515                 ;;
2516         "tag" )
2517                 NOURLS=1
2518                 NODIST="yes"
2519                 init_builder
2520                 if [ -n "$SPECFILE" ]; then
2521                         get_spec
2522                         parse_spec
2523
2524                         # don't fetch sources from remote locations
2525                         new_SOURCES=""
2526                         for file in $SOURCES; do
2527                                 [ -n "`src_md5 $file`" ] && continue
2528                                 new_SOURCES="$new_SOURCES $file"
2529                         done
2530                         SOURCES="$new_SOURCES"
2531                         get_files $SOURCES $PATCHES
2532                         check_md5 $SOURCES
2533                         tag_files $SOURCES $PATCHES $ICONS
2534                 else
2535                         Exit_error err_no_spec_in_cmdl
2536                 fi
2537                 ;;
2538         "mr-proper" )
2539                 mr_proper
2540                 ;;
2541         "list-sources-files" )
2542                 init_builder
2543                 NOCVSSPEC="yes"
2544                 DONT_PRINT_REVISION="yes"
2545                 get_spec
2546                 parse_spec
2547                 for SAP in $SOURCES $PATCHES; do
2548                         echo $SAP | awk '{gsub(/.*\//,"") ; print}'
2549                 done
2550                 ;;
2551         "list-sources-urls" )
2552                 init_builder
2553                 NOCVSSPEC="yes"
2554                 DONT_PRINT_REVISION="yes"
2555                 get_spec
2556                 parse_spec
2557                 SAPS="$SOURCES $PATCHES"
2558                 for SAP in $SAPS ; do
2559                         echo $SAP
2560                 done
2561                 ;;
2562         "list-sources-local-paths" )
2563                 init_builder
2564                 NOCVSSPEC="yes"
2565                 DONT_PRINT_REVISION="yes"
2566                 get_spec
2567                 parse_spec
2568                 for SAP in $SOURCES $PATCHES; do
2569                         echo $PACKAGE_DIR/$(echo $SAP | awk '{gsub(/.*\//,"") ; print }')
2570                 done
2571                 ;;
2572         "list-sources-distfiles-paths" )
2573                 init_builder
2574                 NOCVSSPEC="yes"
2575                 DONT_PRINT_REVISION="yes"
2576                 get_spec
2577                 parse_spec
2578                 for SAP in $SOURCES $PATCHES; do
2579                         if [ -n "$(src_md5 "$SAP")" ]; then
2580                                 distfiles_path "$SAP"
2581                         fi
2582                 done
2583                 ;;
2584         "list-sources-distfiles" )
2585                 init_builder
2586                 NOCVSSPEC="yes"
2587                 DONT_PRINT_REVISION="yes"
2588                 get_spec
2589                 parse_spec
2590                 for SAP in $SOURCES $PATCHES; do
2591                         if [ -n "$(src_md5 "$SAP")" ]; then
2592                                 distfiles_url "$SAP"
2593                         fi
2594                 done
2595                 ;;
2596         "list-sources-cvs" )
2597                 init_builder
2598 #               NOCVSSPEC="yes"
2599                 DONT_PRINT_REVISION="yes"
2600                 get_spec
2601                 parse_spec
2602                 for SAP in $SOURCES $PATCHES; do
2603                         if [ -z "$(src_md5 "$SAP")" ]; then
2604                                 echo $SAP | awk '{gsub(/.*\//,"") ; print}'
2605                         fi
2606                 done
2607                 ;;
2608         "init_rpm_dir")
2609                 init_rpm_dir
2610                 ;;
2611         "usage" )
2612                 usage
2613                 ;;
2614         "short-version" )
2615                 echo "$VERSION"
2616                 ;;
2617         "version" )
2618                 echo "$VERSIONSTRING"
2619                 ;;
2620 esac
2621 if [ -f "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES" -a "$REMOVE_BUILD_REQUIRES" != "" ]; then
2622         rm "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES"
2623 fi
2624 cd "$__PWD"
2625
2626 # vi:syntax=sh:ts=4:sw=4:noet
This page took 0.683728 seconds and 3 git commands to generate.