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