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