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