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