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