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