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