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