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