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