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