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