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