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