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