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