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