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