]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - builder.sh
913f14b37af466727fa9e982994b15acad27f676
[packages/rpm-build-tools.git] / builder.sh
1 #!/bin/ksh
2 # -----------
3 # $Id$
4 # Exit codes:
5 #         0 - succesful
6 #         1 - help displayed
7 #         2 - no spec file name in cmdl parameters
8 #         3 - spec file not stored in repo
9 #         4 - some source, patch or icon files not stored in repo
10 #         5 - package build failed
11 #         6 - spec file with errors
12 #         7 - wrong source in /etc/poldek.conf
13 #         8 - Failed installing buildrequirements and subrequirements
14 #         9 - Requested tag already exist
15 #        10 - Refused to build fractional release
16 #       100 - Unknown error (should not happen)
17
18 # Notes (todo):
19 #       - builder -u fetches current version first (well that's okay, how you compare versions if you have no old spec?)
20 #       - when Icon: field is present, -5 and -a5 doesn't work
21 #       - builder -R skips installing BR if spec is not present before builder invocation (need to run builder twice)
22
23 VERSION="\
24 Build package utility from PLD Linux CVS repository
25 v0.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
899                                         # prefer mirror over distfiles if there's mirror
900                                         # TODO: build url list and then try each url from the list
901                                         if [ -z "$NOMIRRORS" ] && im=$(find_mirror "$i") && [ "$im" != "$i" ]; then
902                                                 url="$im"
903                                         else
904                                                 url=$(distfiles_url "$i")
905                                         fi
906
907                                         url_attic=$(distfiles_attic_url "$i")
908                                         FROM_DISTFILES=1
909                                         # is $url local file?
910                                         if [[ "$url" = [./]* ]]; then
911                                                 update_shell_title "${GETLOCAL%% *}: $url"
912                                                 ${GETLOCAL} $url $target
913                                         else
914                                                 if [ -z "$NOMIRRORS" ]; then
915                                                         url="`find_mirror "$url"`"
916                                                 fi
917                                                 update_shell_title "${GETURI%% *}: $url"
918                                                 ${GETURI} ${OUTFILEOPT} "$target" "$url" || \
919                                                 if [ "`echo $url | grep -E 'ftp://'`" ]; then
920                                                         update_shell_title "${GETURI2%% *}: $url"
921                                                         ${GETURI2} ${OUTFILEOPT} "$target" "$url"
922                                                 fi
923                                         fi
924
925                                         # is it empty file?
926                                         if [ ! -s "$target" ]; then
927                                                 rm -f "$target"
928                                                 if [ `echo $url_attic | grep -E '^(\.|/)'` ]; then
929                                                         update_shell_title "${GETLOCAL%% *}: $url_attic"
930                                                         ${GETLOCAL} $url_attic $target
931                                                 else
932                                                         if [ -z "$NOMIRRORS" ]; then
933                                                                 url_attic="`find_mirror "$url_attic"`"
934                                                         fi
935                                                         update_shell_title "${GETURI%% *}: $url_attic"
936                                                         ${GETURI} ${OUTFILEOPT} "$target" "$url_attic" || \
937                                                         if [ "`echo $url_attic | grep -E 'ftp://'`" ]; then
938                                                                 update_shell_title "${GETURI2%% *}: $url_attic"
939                                                                 ${GETURI2} ${OUTFILEOPT} "$target" "$url_attic"
940                                                         fi
941                                                 fi
942                                         fi
943
944                                         if [ -s "$target" ]; then
945                                                 cvsignore_df $target
946                                         else
947                                                 rm -f "$target"
948                                                 FROM_DISTFILES=0
949                                         fi
950                                 elif [ "$NOCVS" != "yes" -a -z "$srcmd5" ]; then
951                                         if [ $# -gt 1 ]; then
952                                                 get_files_cvs="$get_files_cvs $fp"
953                                                 update_shell_title "$fp (will cvs up later)"
954                                                 cvsup=1
955                                         else
956                                                 cvsup $fp
957                                         fi
958                                 fi
959
960                                 if [ -z "$NOURLS" ] && [ ! -f "$fp" -o -n "$UPDATE" ] && [ "`echo $i | grep -E 'ftp://|http://|https://'`" ]; then
961                                         if [ -z "$NOMIRRORS" ]; then
962                                                 im="`find_mirror "$i"`"
963                                         else
964                                                 im="$i"
965                                         fi
966                                         update_shell_title "${GETURI%% *}: $im"
967                                         ${GETURI} "$im" || \
968                                         if [ "`echo $im | grep -E 'ftp://'`" ]; then
969                                                 update_shell_title "${GETURI2%% *}: $im"
970                                                 ${GETURI2} "$im"
971                                         fi
972                                 fi
973
974                                 if [ "$cvsup" = 1 ]; then
975                                         continue
976                                 fi
977
978                         fi
979
980                         # the md5 check must be moved elsewhere as if we've called from update_md5 the md5 is wrong.
981                         if [ ! -f "$fp" -a "$FAIL_IF_NO_SOURCES" != "no" ]; then
982                                 Exit_error err_no_source_in_repo $i
983                         fi
984
985                         # we check md5 here just only to refetch immediately
986                         if good_md5 "$i" && good_size "$i"; then
987                                 :
988                         elif [ "$FROM_DISTFILES" = 1 ]; then
989                                 # wrong md5 from distfiles: remove the file and try again
990                                 # but only once ...
991                                 echo "MD5 sum mismatch. Trying full fetch."
992                                 FROM_DISTFILES=2
993                                 rm -f $target
994                                 update_shell_title "${GETURI%% *}: $url"
995                                 ${GETURI} ${OUTFILEOPT} "$target" "$url" || \
996                                 if [ "`echo $url | grep -E 'ftp://'`" ]; then
997                                         update_shell_title "${GETURI2%% *}: $url"
998                                         ${GETURI2} ${OUTFILEOPT} "$target" "$url"
999                                 fi
1000                                 if [ ! -s "$target" ]; then
1001                                         rm -f "$target"
1002                                         update_shell_title "${GETURI%% *}: $url_attic"
1003                                         ${GETURI} ${OUTFILEOPT} "$target" "$url_attic" || \
1004                                         if [ "`echo $url_attic | grep -E 'ftp://'`" ]; then
1005                                                 update_shell_title "${GETURI2%% *}: $url_attic"
1006                                                 ${GETURI2} ${OUTFILEOPT} "$target" "$url_attic"
1007                                         fi
1008                                 fi
1009                                 test -s "$target" || rm -f "$target"
1010                         fi
1011                 done
1012                 SHELL_TITLE_PREFIX=""
1013
1014                 if [ "$get_files_cvs" ]; then
1015                         cvsup $get_files_cvs
1016                 fi
1017
1018                 if [ "$CHMOD" = "yes" ]; then
1019                         CHMOD_FILES="`nourl "$@"`"
1020                         if [ -n "$CHMOD_FILES" ]; then
1021                                 chmod $CHMOD_MODE $CHMOD_FILES
1022                         fi
1023                 fi
1024         fi
1025 }
1026
1027 make_tagver() {
1028         if [ -n "$DEBUG" ]; then
1029                 set -x
1030                 set -v
1031         fi
1032
1033         # Check whether first character of PACKAGE_NAME is legal for tag name
1034         if [ -z "${PACKAGE_NAME##[_0-9]*}" -a -z "$TAG_PREFIX" ]; then
1035                 TAG_PREFIX=tag_
1036         fi
1037         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"`
1038         # Remove #kernel.version_release from TAGVER because tagging sources
1039         # could occur with different kernel-headers than kernel-headers used at build time.
1040         TAGVER=$(echo "$TAGVER" | sed -e 's/#.*//g')
1041         echo -n "$TAGVER"
1042 }
1043
1044 tag_files()
1045 {
1046         TAG_FILES="$@"
1047
1048         if [ -n "$DEBUG" ]; then
1049                 set -x
1050                 set -v
1051         fi
1052
1053         echo "Version: $PACKAGE_VERSION"
1054         echo "Release: $PACKAGE_RELEASE"
1055
1056         TAGVER=`make_tagver`
1057
1058         if [ "$TAG_VERSION" = "yes" ]; then
1059                 echo "CVS tag: $TAGVER"
1060         fi
1061         if [ -n "$TAG" ]; then
1062                 echo "CVS tag: $TAG"
1063         fi
1064
1065         local OPTIONS="tag $CVS_FORCE"
1066         if [ -n "$CVSROOT" ]; then
1067                 OPTIONS="-d $CVSROOT $OPTIONS"
1068         fi
1069
1070         cd "$SOURCE_DIR"
1071         local tag_files
1072         for i in $TAG_FILES; do
1073                 # don't tag files stored on distfiles
1074                 [ -n "`src_md5 $i`" ] && continue
1075                 local fp=`nourl "$i"`
1076                 if [ -f "$fp" ]; then
1077                         tag_files="$tag_files $fp"
1078                 else
1079                         Exit_error err_no_source_in_repo $i
1080                 fi
1081         done
1082
1083         if [ "$tag_files" ]; then
1084                 if [ "$TAG_VERSION" = "yes" ]; then
1085                         update_shell_title "tag sources: $TAGVER"
1086                         cvs $OPTIONS $TAGVER $tag_files
1087                 fi
1088                 if [ -n "$TAG" ]; then
1089                         update_shell_title "tag sources: $TAG"
1090                         cvs $OPTIONS $TAG $tag_files
1091                 fi
1092         fi
1093
1094         cd "$SPECS_DIR"
1095         if [ "$TAG_VERSION" = "yes" ]; then
1096                 update_shell_title "tag spec: $TAGVER"
1097                 cvs $OPTIONS $TAGVER $SPECFILE
1098         fi
1099         if [ -n "$TAG" ]; then
1100                 update_shell_title "tag spec: $TAG"
1101                 cvs $OPTIONS $TAG $SPECFILE
1102         fi
1103 }
1104
1105 branch_files()
1106 {
1107         TAG=$1
1108         echo "CVS branch tag: $TAG"
1109         shift
1110
1111         TAG_FILES="$@"
1112
1113         if [ -n "$DEBUG" ]; then
1114                 set -x
1115                 set -v
1116         fi
1117
1118         local OPTIONS="tag $CVS_FORCE -b"
1119         if [ -n "$CVSROOT" ]; then
1120                 OPTIONS="-d $CVSROOT $OPTIONS"
1121         fi
1122         cd "$SOURCE_DIR"
1123         local tag_files
1124         for i in $TAG_FILES; do
1125                 local fp=`nourl "$i"`
1126                 if [ -f "$fp" ]; then
1127                         tag_files="$tag_files $fp"
1128                 else
1129                         Exit_error err_no_source_in_repo $i
1130                 fi
1131         done
1132         if [ "$tag_files" ]; then
1133                 cvs $OPTIONS $TAG $tag_files
1134         fi
1135
1136         cd "$SPECS_DIR"
1137         cvs $OPTIONS $TAG $SPECFILE
1138 }
1139
1140
1141
1142 build_package()
1143 {
1144         update_shell_title "build_package"
1145         if [ -n "$DEBUG" ]; then
1146                 set -x
1147                 set -v
1148         fi
1149
1150         cd "$SPECS_DIR"
1151
1152         if [ -n "$TRY_UPGRADE" ]; then
1153                 update_shell_title "build_package: try_upgrade"
1154                 if [ -n "$FLOAT_VERSION" ]; then
1155                         TNOTIFY=`./pldnotify.awk $SPECFILE -n` || exit 1
1156                 else
1157                         TNOTIFY=`./pldnotify.awk $SPECFILE` || exit 1
1158                 fi
1159
1160                 TNEWVER=`echo $TNOTIFY | awk '{ match($4,/\[NEW\]/); print $5 }'`
1161
1162                 if [ -n "$TNEWVER" ]; then
1163                         TOLDVER=`echo $TNOTIFY | awk '{ print $3; }'`
1164                         echo "New version found, updating spec file to version " $TNEWVER
1165                         if [ "$REVERT_BROKEN_UPGRADE" = "yes" ]; then
1166                                 cp -f $SPECFILE $SPECFILE.bak
1167                         fi
1168                         chmod +w $SPECFILE
1169                         eval "perl -pi -e 's/Version:\t"$TOLDVER"/Version:\t"$TNEWVER"/gs' $SPECFILE"
1170                         eval "perl -pi -e 's/Release:\t[1-9]{0,4}/Release:\t0.1/' $SPECFILE"
1171                         parse_spec
1172                         NODIST="yes" get_files $SOURCES $PATCHES
1173                         update_md5 $SOURCES
1174
1175                         unset TOLDVER TNEWVER TNOTIFY
1176                 fi
1177         fi
1178         cd "$SPECS_DIR"
1179
1180         case "$COMMAND" in
1181                 build )
1182                         BUILD_SWITCH="-ba" ;;
1183                 build-binary )
1184                         BUILD_SWITCH="-bb" ;;
1185                 build-source )
1186                         BUILD_SWITCH="-bs --nodeps" ;;
1187                 build-prep )
1188                         BUILD_SWITCH="-bp --nodeps" ;;
1189         esac
1190
1191         update_shell_title "build_package: $COMMAND"
1192         if [ -n "$LOGFILE" ]; then
1193                 LOG=`eval echo $LOGFILE`
1194                 if [ -d "$LOG" ]; then
1195                         echo "Log file $LOG is a directory."
1196                         echo "Parse error in the spec?"
1197                         Exit_error err_build_fail
1198                 fi
1199                 if [ -n "$LASTLOG_FILE" ]; then
1200                         echo "LASTLOG=$LOG" > $LASTLOG_FILE
1201                 fi
1202                 RES_FILE=~/tmp/$RPMBUILD-exit-status.$RANDOM
1203                 (time eval ${NICE_COMMAND} $RPMBUILD $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $RPMBUILDOPTS $BCOND $TARGET_SWITCH $SPECFILE; echo $? > $RES_FILE) 2>&1 |tee $LOG
1204                 RETVAL=`cat $RES_FILE`
1205                 rm $RES_FILE
1206                 if [ -n "$LOGDIROK" ] && [ -n "$LOGDIRFAIL" ]; then
1207                         if [ "$RETVAL" -eq "0" ]; then
1208                                 mv $LOG $LOGDIROK
1209                         else
1210                                 mv $LOG $LOGDIRFAIL
1211                         fi
1212                 fi
1213         else
1214                 eval ${NICE_COMMAND} $RPMBUILD $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $RPMBUILDOPTS $BCOND $TARGET_SWITCH $SPECFILE
1215                 RETVAL=$?
1216         fi
1217         if [ "$RETVAL" -ne "0" ]; then
1218                 if [ -n "$TRY_UPGRADE" ]; then
1219                         echo "\n!!! Package with new version cannot be built automagically\n"
1220                         if [ "$REVERT_BROKEN_UPGRADE" = "yes" ]; then
1221                                 mv -f $SPECFILE.bak $SPECFILE
1222                         fi
1223                 fi
1224                 Exit_error err_build_fail
1225         fi
1226         unset BUILD_SWITCH
1227 }
1228
1229 nourl()
1230 {
1231         echo "$@" | sed 's#\<\(ftp\|http\|https\|cvs\|svn\)://[^ ]*/##g'
1232 }
1233
1234 install_required_packages()
1235 {
1236         run_poldek -vi $1
1237         return $?
1238 }
1239
1240 find_spec_bcond() {
1241         # taken from find-spec-bcond, but with just getting the list
1242         local SPEC="$1"
1243         # quick revert hint: '$RPMBUILD --bcond $SPEC'
1244         awk -F"\n" '
1245         /^%changelog/ { exit }
1246         /_with(out)?_[_a-zA-Z0-9]+/{
1247                 match($0, /_with(out)?_[_a-zA-Z0-9]+/);
1248                 print substr($0, RSTART, RLENGTH);
1249         }
1250         /^%bcond_with/{
1251                 match($0, /bcond_with(out)?[ \t]+[_a-zA-Z0-9]+/);
1252                 bcond = substr($0, RSTART +5 , RLENGTH -5);
1253                 gsub(/[ \t]+/,"_",bcond);
1254                 print bcond
1255         }' $SPEC | LC_ALL=C sort -u
1256 }
1257
1258 process_bcondrc() {
1259         # expand bconds from ~/.bcondrc
1260         # The file structure is like gentoo's package.use:
1261         # ---
1262         # * -selinux
1263         # samba -mysql -pgsql
1264         # w32codec-installer license_agreement
1265         # php +mysqli
1266         # ---
1267         if ([ -f $HOME/.bcondrc ] || ([ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ])); then
1268                 :
1269         else
1270                 return
1271         fi
1272
1273         SN=${SPECFILE%%\.spec}
1274
1275         local bcondrc=$HOME/.bcondrc
1276         [ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ] && bcondrc=$HOME_ETC/.bcondrc
1277
1278         local bcond_avail=$(find_spec_bcond $SPECFILE)
1279
1280         while read pkg flags; do
1281                 # ignore comments
1282                 [[ "$pkg" == \#* ]] && continue
1283
1284                 # any package or current package?
1285                 if [ "$pkg" = "*" ] || [ "$pkg" = "$PACKAGE_NAME" ] || [ "$pkg" = "$SN" ]; then
1286                         for flag in $flags; do
1287                                 local opt=${flag#[+-]}
1288
1289                                 # use only flags which are in this package.
1290                                 if [[ $bcond_avail = *${opt}* ]]; then
1291                                         if [[ $flag = -* ]]; then
1292                                                 if [[ $BCOND != *--with?${opt}* ]]; then
1293                                                         BCOND="$BCOND --without $opt"
1294                                                 fi
1295                                         else
1296                                                 if [[ $BCOND != *--without?${opt}* ]]; then
1297                                                         BCOND="$BCOND --with $opt"
1298                                                 fi
1299                                         fi
1300                                 fi
1301                         done
1302                 fi
1303         done < $bcondrc
1304         update_shell_title "parse ~/.bcondrc: DONE!"
1305 }
1306
1307 set_bconds_values()
1308 {
1309         update_shell_title "set bcond values"
1310
1311         AVAIL_BCONDS_WITHOUT=""
1312         AVAIL_BCONDS_WITH=""
1313         if `grep -q ^%bcond ${SPECFILE}`; then
1314                 BCOND_VERSION="NEW"
1315         elif `egrep -q ^#\ *_with ${SPECFILE}`; then
1316                 BCOND_VERSION="OLD"
1317         else
1318                 return
1319         fi
1320
1321         local bcond_avail=$(find_spec_bcond $SPECFILE)
1322         process_bcondrc "$SPECFILE"
1323
1324         update_shell_title "parse bconds"
1325         case "${BCOND_VERSION}" in
1326                 NONE)
1327                         :
1328                         ;;
1329                 OLD)
1330                         echo "Warning: This spec has old style bconds. Fix it || die."
1331                         for opt in `echo "$bcond_avail" | grep ^_without_`
1332                         do
1333                                 AVAIL_BCOND_WITHOUT=${opt#_without_}
1334                                 if [[ "$BCOND" = *--without?${AVAIL_BCOND_WITHOUT}* ]]; then
1335                                         AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT <$AVAIL_BCOND_WITHOUT>"
1336                                 else
1337                                         AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT $AVAIL_BCOND_WITHOUT"
1338                                 fi
1339                         done
1340
1341                         for opt in `echo "$bcond_avail" | grep ^_with_`
1342                         do
1343                                 AVAIL_BCOND_WITH=${opt#_with_}
1344                                 if [[ "$BCOND" = *--with?${AVAIL_BCOND_WITH}* ]]; then
1345                                         AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH <$AVAIL_BCOND_WITH>"
1346                                 else
1347                                         AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH $AVAIL_BCOND_WITH"
1348                                 fi
1349                         done
1350                         ;;
1351                 NEW)
1352                         local cond_type="" # with || without
1353                         for opt in $bcond_avail; do
1354                                 case "$opt" in
1355                                         _without)
1356                                                 cond_type="without"
1357                                                 ;;
1358                                         _with)
1359                                                 cond_type="with"
1360                                                 ;;
1361                                         _without_*)
1362                                                 AVAIL_BCOND_WITHOUT=${opt#_without_}
1363                                                 if [[ "$BCOND" = *--without?${AVAIL_BCOND_WITHOUT}* ]]; then
1364                                                         AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT <$AVAIL_BCOND_WITHOUT>"
1365                                                 else
1366                                                         AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT $AVAIL_BCOND_WITHOUT"
1367                                                 fi
1368                                                 ;;
1369                                         _with_*)
1370                                                 AVAIL_BCOND_WITH=${opt#_with_}
1371                                                 if [[ "$BCOND" = *--with?${AVAIL_BCOND_WITH}* ]]; then
1372                                                         AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH <$AVAIL_BCOND_WITH>"
1373                                                 else
1374                                                         AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH $AVAIL_BCOND_WITH"
1375                                                 fi
1376                                                 ;;
1377                                         *)
1378                                                 case "$cond_type" in
1379                                                         with)
1380                                                                 cond_type=''
1381                                                                 AVAIL_BCOND_WITH="$opt"
1382                                                                 if [[ "$BCOND" = *--with?${AVAIL_BCOND_WITH}* ]]; then
1383                                                                         AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH <$AVAIL_BCOND_WITH>"
1384                                                                 else
1385                                                                         AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH $AVAIL_BCOND_WITH"
1386                                                                 fi
1387                                                                 ;;
1388                                                         without)
1389                                                                 cond_type=''
1390                                                                 AVAIL_BCOND_WITHOUT="$opt"
1391                                                                 if [[ "$BCOND" = *--without?${AVAIL_BCOND_WITHOUT}* ]]; then
1392                                                                         AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT <$AVAIL_BCOND_WITHOUT>"
1393                                                                 else
1394                                                                         AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT $AVAIL_BCOND_WITHOUT"
1395                                                                 fi
1396                                                                 ;;
1397                                                 esac
1398                                                 ;;
1399                                 esac
1400                         done
1401                         ;;
1402         esac
1403 }
1404
1405 run_sub_builder()
1406 {
1407         package_name="${1}"
1408         update_shell_title "run_sub_builder $package_name"
1409         #
1410         # No i tutaj bym chcia³ zrobiæ sztuczn± inteligencjê, która spróbuje tego
1411         # pakieta zbudowaæ. Aktualnie niewiele dziala, bo generalnie nie widze do
1412         # konca algorytmu... Ale damy rade. :) Na razie po prostu sie wyjebie tak samo
1413         # jakby nie bylo tego kawalka kodu.
1414         #
1415         # Update: Poprawi³em parê rzeczy i zaczê³o generowaæ pakiety spoza zadanej listy.
1416         #         Jednym s³owem budowanie niespoldkowanych zale¿no¶ci dzia³a w paru przypadkach.
1417         #
1418         #
1419         # y0shi.
1420
1421         parent_spec_name=''
1422
1423         # Istnieje taki spec? ${package}.spec
1424         if [ -f "${SPECS_DIR}/${package}.spec" ]; then
1425                 parent_spec_name=${package}.spec
1426         elif [ -f "${SPECS_DIR}/`echo ${package_name} | sed -e s,-devel.*,,g -e s,-static,,g`.spec" ]; then
1427                 parent_spec_name="`echo ${package_name} | sed -e s,-devel.*,,g -e s,-static,,g`.spec"
1428         else
1429                 for provides_line in `grep ^Provides:.*$package  ${SPECS_DIR} -R`
1430                 do
1431                         echo $provides_line
1432                 done
1433         fi
1434
1435         if [ "${parent_spec_name}" != "" ]; then
1436                 spawn_sub_builder $parent_spec_name
1437         fi
1438         NOT_INSTALLED_PACKAGES="$NOT_INSTALLED_PACKAGES $package_name"
1439 }
1440
1441 spawn_sub_builder()
1442 {
1443         package_name="${1}"
1444         update_shell_title "spawn_sub_builder $package_name"
1445
1446         sub_builder_opts=''
1447         if [ "${FETCH_BUILD_REQUIRES}" == "yes" ]; then
1448                 sub_builder_opts="${sub_builder_opts} -R"
1449         fi
1450         if [ "${REMOVE_BUILD_REQUIRES}" == "nice" ]; then
1451                 sub_builder_opts="${sub_builder_opts} -RB"
1452         elif [ "${REMOVE_BUILD_REQUIRES}" == "force" ]; then
1453                 sub_builder_opts="${sub_builder_opts} -FRB"
1454         fi
1455         if [ "${UPDATE_POLDEK_INDEXES}" == "yes" ]; then
1456                 sub_builder_opts="${sub_builder_opts} -Upi"
1457         fi
1458
1459         cd "${SPECS_DIR}"
1460         ./builder ${sub_builder_opts} "$@"
1461 }
1462
1463 remove_build_requires()
1464 {
1465         if [ "$INSTALLED_PACKAGES" != "" ]; then
1466                 case "$REMOVE_BUILD_REQUIRES" in
1467                         "force")
1468                                 run_poldek --noask -ve $INSTALLED_PACKAGES
1469                                 ;;
1470                         "nice")
1471                                 run_poldek --ask -ve $INSTALLED_PACKAGES
1472                                 ;;
1473                         *)
1474                                 echo You may want to manually remove following BuildRequires fetched:
1475                                 echo $INSTALLED_PACKAGES
1476                                 echo Try poldek -e \`cat `pwd`/.${SPECFILE}_INSTALLED_PACKAGES\`
1477                                 ;;
1478                 esac
1479         fi
1480 }
1481
1482 display_bconds()
1483 {
1484         if [ "$AVAIL_BCONDS_WITH" != "" ] || [ "$AVAIL_BCONDS_WITHOUT" != "" ]; then
1485                 if [ "$BCOND" != "" ]; then
1486                         echo -ne "\nBuilding $SPECFILE with the following conditional flags:\n"
1487                         echo -ne "$BCOND"
1488                 else
1489                         echo -ne "\nNo conditional flags passed"
1490                 fi
1491                 echo -ne "\n\nfrom available:\n"
1492                 echo -ne "--with   :\t$AVAIL_BCONDS_WITH\n--without:\t$AVAIL_BCONDS_WITHOUT\n\n"
1493         fi
1494 }
1495
1496 display_branches()
1497 {
1498         if [ "$NOCVSSPEC" != "yes" ]; then
1499                 echo -ne "Available branches: "
1500                 cvs status -v "${SPECFILE}" | awk '!/Sticky Tag:/ && /\(branch:/ { print $1 } ' | xargs
1501         fi
1502 }
1503
1504 # checks a given list of packages/files/provides agains current rpmdb.
1505 # outputs all dependencies whcih current rpmdb doesn't satisfy.
1506 # input can be either STDIN or parameters
1507 _rpm_prov_check()
1508 {
1509         local DEPS
1510
1511         if [ "$#" -gt 0 ]; then
1512                 DEPS="$@"
1513         else
1514                 DEPS=$(cat)
1515         fi
1516
1517         DEPS=$(rpm -q --whatprovides $DEPS 2>&1 | awk '/^(error:|no package provides)/ { print }')
1518
1519         # packages
1520         echo "$DEPS" | awk '/^no package provides/ { print $NF }'
1521
1522         # other deps (files)
1523         echo "$DEPS" | awk -F: '/^error:.*No such file/{o = $2; gsub("^ file ", "", o); print o}'
1524 }
1525
1526 # checks if given package/files/provides exists in rpmdb.
1527 # inout can be either stdin or parameters
1528 # returns packages wchi hare present in the rpmdb
1529 _rpm_cnfl_check()
1530 {
1531         local DEPS
1532
1533         if [ "$#" -gt 0 ]; then
1534                 DEPS="$@"
1535         else
1536                 DEPS=$(cat)
1537         fi
1538
1539         rpm -q --whatprovides $DEPS 2>/dev/null | awk '!/no package provides/ { print }'
1540 }
1541
1542 fetch_build_requires()
1543 {
1544         if [ "${FETCH_BUILD_REQUIRES}" = "yes" ]; then
1545                 update_shell_title "fetch build requires"
1546                 if [ "$FETCH_BUILD_REQUIRES_RPMGETDEPS" = "yes" ]; then
1547                         local CONF=$(rpm-getdeps $BCOND $SPECFILE 2> /dev/null | awk '/^\-/ { print $3 } ' | _rpm_cnfl_check | xargs)
1548                         local DEPS=$(rpm-getdeps $BCOND $SPECFILE 2> /dev/null | awk '/^\+/ { print $3 } ' | _rpm_prov_check | xargs)
1549
1550                         update_shell_title "poldek: update indexes"
1551                         if [ -n "$CONF" ] || [ -n "$DEPS" ]; then
1552                                 $SU_SUDO /usr/bin/poldek -q --update || $SU_SUDO /usr/bin/poldek -q --upa
1553                         fi
1554                         if [ -n "$CONF" ]; then
1555                                 update_shell_title "uninstall conflicting packages: $CONF"
1556                                 echo "Trying to uninstall conflicting packages ($CONF):"
1557                                 $SU_SUDO /usr/bin/poldek --noask --nofollow -ev $CONF
1558                         fi
1559
1560                 while [ "$DEPS" ]; do
1561                                 update_shell_title "install deps: $DEPS"
1562                                 echo "Trying to install dependencies ($DEPS):"
1563                                 local log=.${SPECFILE}_poldek.log
1564                                 $SU_SUDO /usr/bin/poldek --caplookup -uGq $DEPS | tee $log
1565                                 failed=$(awk -F: '/^error:/{print $2}' $log)
1566                                 rm -f $log
1567                                 local ok
1568                                 if [ -n "$failed" ]; then
1569                                         for package in $failed; do
1570                                                 # FIXME: sanitise, deps could be not .spec files
1571                                                 spawn_sub_builder -bb $package && ok="$ok $package"
1572                                         done
1573                                         DEPS="$ok"
1574                                 else
1575                                         DEPS=""
1576                                 fi
1577                 done
1578                         return
1579                 fi
1580
1581                 echo -ne "\nAll packages installed by fetch_build_requires() are written to:\n"
1582                 echo -ne "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES\n"
1583                 echo -ne "\nIf anything fails, you may get rid of them by executing:\n"
1584                 echo "poldek -e \`cat `pwd`/.${SPECFILE}_INSTALLED_PACKAGES\`\n\n"
1585                 echo > `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1586                 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`
1587                 do
1588                         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`"
1589                         GO="yes"
1590                         package=`basename "$package_item"|sed -e "s/}$//g"`
1591                         COND_ARCH_TST="`cat $SPECFILE|grep -B1 BuildRequires|grep -B1 $package|grep ifarch|sed -e "s/^.*ifarch//g"`"
1592                         mach=`uname -m`
1593
1594                         COND_TST=`cat $SPECFILE|grep BuildRequires|grep "$package"`
1595                         if `echo $COND_TST|grep -q '^BuildRequires:'`; then
1596                                 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
1597                                         GO="yes"
1598                                 fi
1599                         # bcond:
1600                         else
1601                                 COND_NAME=`echo $COND_TST|sed -e s,:BuildRequires:.*$,,g`
1602                                 GO=""
1603                                 # %{without}
1604                                 if `echo $COND_TST|grep -q 'without_'`; then
1605                                         COND_NAME=`echo $COND_NAME|sed -e s,^.*without_,,g`
1606                                         if `echo $COND_TST|grep -q !`; then
1607                                                 COND_STATE="with"
1608                                         else
1609                                                 COND_STATE="wout"
1610                                         fi
1611                                         COND_WITH=`echo $AVAIL_BCONDS_WITH|grep "<$COND_NAME>"`
1612                                         COND_WITHOUT=`echo $AVAIL_BCONDS_WITHOUT|grep "<$COND_NAME>"`
1613                                         if [ -n "$COND_WITHOUT" ] || [ -z "$COND_WITH" ]; then
1614                                                 COND_ARGV="wout"
1615                                         else
1616                                                 COND_ARGV="with"
1617                                         fi
1618                                 # %{with}
1619                                 elif `echo $COND_TST|grep -q 'with_'`; then
1620                                         COND_NAME=`echo $COND_NAME|sed -e s,^.*with_,,g`
1621                                         if `echo $COND_TST|grep -q !`; then
1622                                                 COND_STATE="wout"
1623                                         else
1624                                                 COND_STATE="with"
1625                                         fi
1626                                         COND_WITH=`echo $AVAIL_BCONDS_WITH|grep "<$COND_NAME>"`
1627                                         COND_WITHOUT=`echo $AVAIL_BCONDS_WITHOUT|grep "<$COND_NAME>"`
1628                                         if [ -n "$COND_WITH" ] || [ -z "$COND_WITHOUT" ]; then
1629                                                 COND_ARGV="with"
1630                                         else
1631                                                 COND_ARGV="wout"
1632                                         fi
1633                                 fi
1634                                 RESULT="${COND_STATE}-${COND_ARGV}"
1635                                 case "$RESULT" in
1636                                         "with-wout" | "wout-with" )
1637                                                 GO=""
1638                                                 ;;
1639                                         "wout-wout" | "with-with" )
1640                                                 GO="yes"
1641                                                 ;;
1642                                         * )
1643                                                 echo "Action '$RESULT' was not defined for package '$package_item'"
1644                                                 GO="yes"
1645                                                 ;;
1646                                 esac
1647                         fi
1648
1649                         if [ "$GO" = "yes" ]; then
1650                                 if [ "`rpm -q $package|sed -e "s/$package.*/$package/g"`" != "$package" ]; then
1651                                         echo "Testing if $package has subrequirements..."
1652                                         run_poldek -t -i $package --dumpn=".$package-req.txt"
1653                                         if [ -f ".$package-req.txt" ]; then
1654                                                 for package_name in `cat ".$package-req.txt"|grep -v ^#`
1655                                                 do
1656                                                         if [ "$package_name" = "$package" ]; then
1657                                                                 echo -ne "Installing BuildRequired package:\t$package_name\n"
1658                                                                 update_shell_title "Installing BuildRequired package: ${package_name}"
1659                                                                 install_required_packages $package
1660                                                         else
1661                                                                 echo -ne "Installing (sub)Required package:\t$package_name\n"
1662                                                                 update_shell_title "Installing (sub)Required package: ${package_name}"
1663                                                                 install_required_packages $package_name
1664                                                         fi
1665                                                         case $? in
1666                                                                 0)
1667                                                                         INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
1668                                                                         echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1669                                                                         ;;
1670                                                                 *)
1671                                                                         echo "Attempting to run spawn sub - builder..."
1672                                                                         echo -ne "Package installation failed:\t$package_name\n"
1673                                                                         run_sub_builder $package_name
1674                                                                         if [ $? -eq 0 ]; then
1675                                                                                 install_required_packages $package_name
1676                                                                                 case $? in
1677                                                                                         0)
1678                                                                                                 INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
1679                                                                                                 echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1680                                                                                                 ;;
1681                                                                                         *)
1682                                                                                                 NOT_INSTALLED_PACKAGES="$package_name $NOT_INSTALLED_PACKAGES"
1683                                                                                                 ;;
1684                                                                                 esac
1685                                                                         fi
1686                                                                         ;;
1687                                                         esac
1688                                                 done
1689                                                 rm -f ".$package-req.txt"
1690                                         else
1691                                                 echo "Attempting to run spawn sub - builder..."
1692                                                 echo -ne "Package installation failed:\t$package\n"
1693                                                 run_sub_builder $package
1694                                                 if [ $? -eq 0 ]; then
1695                                                         install_required_packages $package
1696                                                         case $? in
1697                                                                 0)
1698                                                                         INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
1699                                                                         echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1700                                                                         ;;
1701                                                                 *)
1702                                                                         NOT_INSTALLED_PACKAGES="$package_name $NOT_INSTALLED_PACKAGES"
1703                                                                         ;;
1704                                                         esac
1705                                                 fi
1706                                         fi
1707                                 else
1708                                         echo "Package $package is already installed. BuildRequirement satisfied."
1709                                 fi
1710                         fi
1711                 done
1712                 if [ "$NOT_INSTALLED_PACKAGES" != "" ]; then
1713                         echo "Unable to install following packages and their dependencies:"
1714                         for pkg in "$NOT_INSTALLED_PACKAGES"
1715                         do
1716                                 echo $pkg
1717                         done
1718                         remove_build_requires
1719                         exit 8
1720                 fi
1721         fi
1722 }
1723
1724 init_rpm_dir() {
1725
1726         TOP_DIR="`eval $RPM $RPMOPTS --eval '%{_topdir}'`"
1727         CVSROOT=":pserver:cvs@$CVS_SERVER:/cvsroot"
1728
1729         mkdir -p $TOP_DIR/{RPMS,BUILD,SRPMS}
1730         cd $TOP_DIR
1731         cvs -d $CVSROOT co SOURCES/.cvsignore SPECS/{mirrors,adapter{,.awk},fetchsrc_request,builder,{relup,repackage}.sh}
1732
1733         init_builder
1734
1735         echo "To checkout *all* .spec files:"
1736         echo "- remove $SPECS_DIR/CVS/Entries.Static"
1737         echo "- run cvs up in $SPECS_DIR dir"
1738
1739         echo ""
1740         echo "To commit with your developer account:"
1741         echo "- edit $SPECS_DIR/CVS/Root"
1742         echo "- edit $SOURCE_DIR/CVS/Root"
1743 }
1744
1745 #---------------------------------------------
1746 # main()
1747
1748 if [ $# = 0 ]; then
1749         usage
1750         exit 1
1751 fi
1752
1753 while [ $# -gt 0 ]; do
1754         case "${1}" in
1755                 -5 | --update-md5)
1756                         COMMAND="update_md5"
1757                         NODIST="yes"
1758                         NOCVSSPEC="yes"
1759                         shift ;;
1760                 -a5 | --add-md5 )
1761                         COMMAND="update_md5"
1762                         NODIST="yes"
1763                         NOCVS="yes"
1764                         NOCVSSPEC="yes"
1765                         ADD5="yes"
1766                         shift ;;
1767                 -n5 | --no-md5 )
1768                         NO5="yes"
1769                         shift ;;
1770                 -D | --debug )
1771                         DEBUG="yes"; shift ;;
1772                 -V | --version )
1773                         COMMAND="version"; shift ;;
1774                 -a | --as_anon )
1775                         CVSROOT=":pserver:cvs@$CVS_SERVER:/cvsroot"; shift ;;
1776                 -b | -ba | --build )
1777                         COMMAND="build"; shift ;;
1778                 -bb | --build-binary )
1779                         COMMAND="build-binary"; shift ;;
1780                 -bs | --build-source )
1781                         COMMAND="build-source"; shift ;;
1782                 -bp | --build-prep )
1783                         COMMAND="build-prep"; shift ;;
1784                 -B | --branch )
1785                         COMMAND="branch"; shift; TAG="${1}"; shift;;
1786                 -c | --clean )
1787                         CLEAN="--clean --rmspec --rmsource"; shift ;;
1788                 -cf | --cvs-force )
1789                         CVS_FORCE="-F"; shift;;
1790                 -d | --cvsroot )
1791                         shift; CVSROOT="${1}"; shift ;;
1792                 -g | --get )
1793                         COMMAND="get"; shift ;;
1794                 -h | --help )
1795                         COMMAND="usage"; shift ;;
1796                 --http )
1797                         PROTOCOL="http"; shift ;;
1798                 -l | --logtofile )
1799                         shift; LOGFILE="${1}"; shift ;;
1800                 -ni| --nice )
1801                         shift; DEF_NICE_LEVEL=${1}; shift ;;
1802                 -ske | --skip-existing-files)
1803                         SKIP_EXISTING_FILES="yes"; shift ;;
1804                 -m | --mr-proper )
1805                         COMMAND="mr-proper"; shift ;;
1806                 -nc | --no-cvs )
1807                         NOCVS="yes"; shift ;;
1808                 -ncs | --no-cvs-specs )
1809                         NOCVSSPEC="yes"; shift ;;
1810                 -nd | --no-distfiles )
1811                         NODIST="yes"; shift ;;
1812                 -nm | --no-mirrors )
1813                         NOMIRRORS="yes"; shift ;;
1814                 -nu | --no-urls )
1815                         NOURLS="yes"; shift ;;
1816                 -ns | --no-srcs )
1817                         NOSRCS="yes"; shift ;;
1818                 -ns0 | --no-source0 )
1819                         NOSOURCE0="yes"; shift ;;
1820                 -nn | --no-net )
1821                         NOCVS="yes"
1822                         NOCVSSPEC="yes"
1823                         NODIST="yes"
1824                         NOMIRRORS="yes"
1825                         NOURLS="yes"
1826                         NOSRCS="yes"
1827                         ALWAYS_CVSUP="no"
1828                         shift;;
1829                 --no-init )
1830                         NOINIT="yes"
1831                         shift;;
1832                 --opts )
1833                         shift; RPMOPTS="${RPMOPTS} ${1}"; shift ;;
1834                 --nopatch | -np )
1835                         shift; RPMOPTS="${RPMOPTS} --define \"patch${1} : ignoring patch${1}; exit 1; \""; shift ;;
1836                 --with | --without )
1837                         case $GROUP_BCONDS in
1838                                 "yes")
1839                                         COND=${1}
1840                                         shift
1841                                         while ! `echo ${1}|grep -qE '(^-|spec)'`
1842                                         do
1843                                                 BCOND="$BCOND $COND $1"
1844                                                 shift
1845                                         done;;
1846                                 "no")
1847                                         if [[ "$2" = *,* ]]; then
1848                                                 for a in $(echo "$2" | tr , ' '); do
1849                                                         BCOND="$BCOND $1 $a"
1850                                                 done
1851                                         else
1852                                                 BCOND="$BCOND $1 $2"
1853                                         fi
1854                                         shift 2 ;;
1855                         esac
1856                         ;;
1857                 --target )
1858                         shift; TARGET="${1}"; shift ;;
1859                 --target=* )
1860                         TARGET=$(echo "${1}" | sed 's/^--target=//'); shift ;;
1861                 -q | --quiet )
1862                         QUIET="--quiet"; shift ;;
1863                 --date )
1864                         CVSDATE="${2}"; shift 2 ;;
1865                 -r | --cvstag )
1866                         shift; CVSTAG="${1}"; shift ;;
1867                 -A)
1868                         shift; CVSTAG="HEAD"; ;;
1869                 -R | --fetch-build-requires)
1870                         FETCH_BUILD_REQUIRES="yes"
1871                         NOT_INSTALLED_PACKAGES=
1872                         shift ;;
1873                 -RB | --remove-build-requires)
1874                         REMOVE_BUILD_REQUIRES="nice"
1875                         shift ;;
1876                 -FRB | --force-remove-build-requires)
1877                         REMOVE_BUILD_REQUIRES="force"
1878                         shift ;;
1879                 -sd | --sources-distfiles)
1880                         COMMAND="list-sources-distfiles"
1881                         shift ;;
1882                 -sdp | --sources-distfiles-paths)
1883                         COMMAND="list-sources-distfiles-paths"
1884                         shift ;;
1885                 -sf | --sources-files)
1886                         COMMAND="list-sources-files"
1887                         shift ;;
1888                 -sp | --sources-paths)
1889                         COMMAND="list-sources-local-paths"
1890                         shift ;;
1891                 -su | --sources-urls)
1892                         COMMAND="list-sources-urls"
1893                         shift ;;
1894                 -Tvs | --tag-version-stable )
1895                         COMMAND="tag"
1896                         TAG="STABLE"
1897                         TAG_VERSION="yes"
1898                         shift;;
1899                 -Ts | --tag-stable )
1900                         COMMAND="tag"
1901                         TAG="STABLE"
1902                         TAG_VERSION="no"
1903                         shift;;
1904                 -Tv | --tag-version )
1905                         COMMAND="tag"
1906                         TAG=""
1907                         TAG_VERSION="yes"
1908                         shift;;
1909                 -Tp | --tag-prefix )
1910                         TAG_PREFIX="$2"
1911                         shift 2;;
1912                 -tt | --test-tag )
1913                         TEST_TAG="yes"
1914                         shift;;
1915                 -T | --tag )
1916                         COMMAND="tag"
1917                         shift
1918                         TAG="$1"
1919                         TAG_VERSION="no"
1920                         shift;;
1921                 -ir | --integer-release-only )
1922                         INTEGER_RELEASE="yes"
1923                         shift;;
1924                 -U | --update )
1925                         COMMAND="update_md5"
1926                         UPDATE="yes"
1927                         NOCVSSPEC="yes"
1928                         NODIST="yes"
1929                         shift ;;
1930                 -Upi | --update-poldek-indexes )
1931                         UPDATE_POLDEK_INDEXES="yes"
1932                         shift ;;
1933                 --init-rpm-dir)
1934                         COMMAND="init_rpm_dir"
1935                         shift ;;
1936                 -u | --try-upgrade )
1937                         TRY_UPGRADE="1"; shift ;;
1938                 -un | --try-upgrade-with-float-version )
1939                         TRY_UPGRADE="1"; FLOAT_VERSION="1"; shift ;;
1940                 -v | --verbose )
1941                         BE_VERBOSE="1"; shift ;;
1942                 --define)
1943                         shift
1944                         MACRO="${1}"
1945                         shift
1946                         if echo "${MACRO}" | grep -q '\W'; then
1947                                 RPMOPTS="${RPMOPTS} --define \"${MACRO}\""
1948                         else
1949                                 VALUE="${1}"
1950                                 shift
1951                                 RPMOPTS="${RPMOPTS} --define \"${MACRO} ${VALUE}\""
1952                         fi
1953                         ;;
1954                 --show-bconds | -show-bconds | -print-bconds | --print-bconds | -display-bconds | --display-bconds )
1955                         COMMAND="show_bconds"
1956                         shift
1957                         ;;
1958                 --show-bcond-args)
1959                         COMMAND="show_bcond_args"
1960                         shift
1961                         ;;
1962                 --nodeps)
1963                         shift
1964                         RPMOPTS="${RPMOPTS} --nodeps"
1965                         ;;
1966                 -debug)
1967                         RPMBUILDOPTS="${RPMBUILDOPTS} -debug"; shift ;;
1968                 * )
1969                         SPECFILE="${1}"
1970                         # check if specname was passed as specname:cvstag
1971                         if [ "${SPECFILE##*:}" != "${SPECFILE}" ]; then
1972                                 CVSTAG="${SPECFILE##*:}"
1973                                 SPECFILE="${SPECFILE%%:*}"
1974                         fi
1975                         shift
1976         esac
1977 done
1978
1979 if [ -z "$CVSTAG" ]; then
1980         CVSTAG=$(awk -vSPECFILE="${SPECFILE%.spec}.spec" -F/ '$2 == SPECFILE && $6 ~ /^T/{print substr($6, 2)}' CVS/Entries)
1981         if [ "$CVSTAG" ]; then
1982                 echo >&2 "builder: Stick tag $CVSTAG active. Use -r TAGNAME to override."
1983         fi
1984 elif [ "$CVSTAG" = "HEAD" ]; then
1985         # assume -r HEAD is same as -A
1986         CVSTAG=""
1987 fi
1988
1989 if [ -n "$DEBUG" ]; then
1990         set -x
1991         set -v
1992 fi
1993
1994 if [ -n "$TARGET" ]; then
1995         case "$RPMBUILD" in
1996                 "rpmbuild")
1997                         TARGET_SWITCH="--target $TARGET" ;;
1998                 "rpm")
1999                         TARGET_SWITCH="--target=$TARGET" ;;
2000         esac
2001 fi
2002
2003 if [ "$SCHEDTOOL" != "no" ]; then
2004         NICE_COMMAND="$SCHEDTOOL"
2005 else
2006         NICE_COMMAND="nice -n ${DEF_NICE_LEVEL}"
2007 fi
2008
2009 update_shell_title "$COMMAND"
2010 case "$COMMAND" in
2011         "show_bconds")
2012                 init_builder
2013                 if [ -n "$SPECFILE" ]; then
2014                         get_spec > /dev/null
2015                         parse_spec
2016                         set_bconds_values
2017                         display_bconds
2018                 fi
2019                 ;;
2020         "show_bcond_args")
2021                 init_builder
2022                 if [ -n "$SPECFILE" ]; then
2023                         get_spec > /dev/null
2024                         parse_spec
2025                         set_bconds_values
2026                         echo "$BCOND"
2027                 fi
2028                 ;;
2029         "build" | "build-binary" | "build-source" | "build-prep" )
2030                 init_builder
2031                 if [ -n "$SPECFILE" ]; then
2032                         get_spec
2033                         parse_spec
2034                         set_bconds_values
2035                         display_bconds
2036                         display_branches
2037                         fetch_build_requires
2038                         if [ "$INTEGER_RELEASE" = "yes" ]; then
2039                                 echo "Checking release $PACKAGE_RELEASE..."
2040                                 if echo $PACKAGE_RELEASE | grep -q '^[^.]*\.[^.]*$' 2>/dev/null ; then
2041                                         Exit_error err_fract_rel "$PACKAGE_RELEASE"
2042                                 fi
2043                         fi
2044
2045                         if [ -n "$TEST_TAG" ]; then
2046                                 TAGVER=`make_tagver`
2047                                 echo "Searching for tag $TAGVER..."
2048                                 TAGREL=$(cvs status -v $SPECFILE | grep -E "^[[:space:]]*${TAGVER}[[[:space:]]" | sed -e 's#.*(revision: ##g' -e 's#).*##g')
2049                                 if [ -n "$TAGREL" ]; then
2050                                         Exit_error err_tag_exists "$TAGVER" "$TAGREL"
2051                                 fi
2052
2053                                 # - do not allow to build from HEAD when XX-branch exists
2054                                 TREE_PREFIX=$(echo "$TAG_PREFIX" | sed -e 's#^auto-\([a-zA-Z]\+\)-.*#\1#g')
2055                                 if [ "$TREE_PREFIX" != "$TAG_PREFIX" ]; then
2056                                         TAG_BRANCH="${TREE_PREFIX}-branch"
2057                                         TAG_STATUS=$(cvs status -v $SPECFILE | grep -Ei "${TAG_BRANCH}.+(branch: [0-9.]+)")
2058                                         if [ -n "$TAG_STATUS" -a "$CVSTAG" = "HEAD" ]; then
2059                                                 Exit_error err_branch_exists "$TAG_STATUS"
2060                                         fi
2061                                 fi
2062                         fi
2063
2064                         if [ -n "$NOSOURCE0" ] ; then
2065                                 SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2066                         fi
2067                         get_files $SOURCES $PATCHES
2068                         check_md5 $SOURCES
2069                         build_package
2070                         if [ "$UPDATE_POLDEK_INDEXES" = "yes" -a "$COMMAND" != "build-prep" ]; then
2071                                 run_poldek --sdir="${POLDEK_INDEX_DIR}" --mkidxz
2072                         fi
2073                         remove_build_requires
2074                 else
2075                         Exit_error err_no_spec_in_cmdl
2076                 fi
2077                 ;;
2078         "branch" )
2079                 init_builder
2080                 if [ -n "$SPECFILE" ]; then
2081                         get_spec
2082                         parse_spec
2083                         # don't fetch sources from remote locations
2084                         new_SOURCES=""
2085                         for file in $SOURCES; do
2086                                 [ -n "`src_md5 $file`" ] && continue
2087                                 new_SOURCES="$new_SOURCES $file"
2088                         done
2089                         SOURCES="$new_SOURCES"
2090                         get_files $SOURCES $PATCHES
2091                         check_md5 $SOURCES
2092                         branch_files $TAG $SOURCES $PATCHES $ICONS
2093                 else
2094                         Exit_error err_no_spec_in_cmdl
2095                 fi
2096                 ;;
2097         "get" )
2098                 init_builder
2099                 if [ -n "$SPECFILE" ]; then
2100                         get_spec
2101                         parse_spec
2102
2103                         if [ -n "$NOSOURCE0" ] ; then
2104                                 SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2105                         fi
2106                         get_files $SOURCES $PATCHES
2107                         check_md5 $SOURCES
2108                 else
2109                         Exit_error err_no_spec_in_cmdl
2110                 fi
2111                 ;;
2112         "update_md5" )
2113                 init_builder
2114                 if [ -n "$SPECFILE" ]; then
2115                         get_spec
2116                         parse_spec
2117
2118                         if [ -n "$NOSOURCE0" ] ; then
2119                                 SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2120                         fi
2121                         update_md5 $SOURCES
2122                 else
2123                         Exit_error err_no_spec_in_cmdl
2124                 fi
2125                 ;;
2126         "tag" )
2127                 NOURLS=1
2128                 NODIST="yes"
2129                 init_builder
2130                 if [ -n "$SPECFILE" ]; then
2131                         get_spec
2132                         parse_spec
2133
2134                         # don't fetch sources from remote locations
2135                         new_SOURCES=""
2136                         for file in $SOURCES; do
2137                                 [ -n "`src_md5 $file`" ] && continue
2138                                 new_SOURCES="$new_SOURCES $file"
2139                         done
2140                         SOURCES="$new_SOURCES"
2141                         get_files $SOURCES $PATCHES
2142                         check_md5 $SOURCES
2143                         tag_files $SOURCES $PATCHES $ICONS
2144                 else
2145                         Exit_error err_no_spec_in_cmdl
2146                 fi
2147                 ;;
2148         "mr-proper" )
2149                 $RPM --clean --rmsource --rmspec --force --nodeps $SPECFILE
2150                 ;;
2151         "list-sources-files" )
2152                 init_builder
2153                 NOCVSSPEC="yes"
2154                 DONT_PRINT_REVISION="yes"
2155                 get_spec
2156                 parse_spec
2157                 for SAP in $SOURCES $PATCHES; do
2158                         echo $SAP | awk '{gsub(/.*\//,"") ; print}'
2159                 done
2160                 ;;
2161         "list-sources-urls" )
2162                 init_builder
2163                 NOCVSSPEC="yes"
2164                 DONT_PRINT_REVISION="yes"
2165                 get_spec
2166                 parse_spec
2167                 SAPS="$SOURCES $PATCHES"
2168                 for SAP in $SAPS ; do
2169                         echo $SAP
2170                 done
2171                 ;;
2172         "list-sources-local-paths" )
2173                 init_builder
2174                 NOCVSSPEC="yes"
2175                 DONT_PRINT_REVISION="yes"
2176                 get_spec
2177                 parse_spec
2178                 for SAP in $SOURCES $PATCHES; do
2179                         echo $SOURCE_DIR/$(echo $SAP | awk '{gsub(/.*\//,"") ; print }')
2180                 done
2181                 ;;
2182         "list-sources-distfiles-paths" )
2183                 init_builder
2184                 NOCVSSPEC="yes"
2185                 DONT_PRINT_REVISION="yes"
2186                 get_spec
2187                 parse_spec
2188                 for SAP in $SOURCES $PATCHES; do
2189                         if [ -n "$(src_md5 "$SAP")" ]; then
2190                                 distfiles_path "$SAP"
2191                         fi
2192                 done
2193                 ;;
2194         "list-sources-distfiles" )
2195                 init_builder
2196                 NOCVSSPEC="yes"
2197                 DONT_PRINT_REVISION="yes"
2198                 get_spec
2199                 parse_spec
2200                 for SAP in $SOURCES $PATCHES; do
2201                         if [ -n "$(src_md5 "$SAP")" ]; then
2202                                 distfiles_url "$SAP"
2203                         fi
2204                 done
2205                 ;;
2206         "init_rpm_dir")
2207                 init_rpm_dir
2208                 ;;
2209         "usage" )
2210                 usage;;
2211         "version" )
2212                 echo "$VERSION";;
2213 esac
2214 if [ -f "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES" -a "$REMOVE_BUILD_REQUIRES" != "" ]; then
2215         rm "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES"
2216 fi
2217 cd "$__PWD"
2218
2219 # vi:syntax=sh:ts=4:sw=4:noet
This page took 0.173924 seconds and 2 git commands to generate.