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