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