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