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