]> git.pld-linux.org Git - packages/rpm-build-tools.git/blame - builder.sh
- move tmpfile handling outside of lftp
[packages/rpm-build-tools.git] / builder.sh
CommitLineData
10d92f71 1#!/bin/ksh
44058243 2#
dece74ed
AM
3# This program is free software, distributed under the terms of
4# the GNU General Public License Version 2.
5#
cd445739 6# -----------
cd445739 7# Exit codes:
2a5f078d 8# 0 - succesful
9# 1 - help displayed
10# 2 - no spec file name in cmdl parameters
11# 3 - spec file not stored in repo
12# 4 - some source, patch or icon files not stored in repo
13# 5 - package build failed
14# 6 - spec file with errors
15# 7 - wrong source in /etc/poldek.conf
16# 8 - Failed installing buildrequirements and subrequirements
17# 9 - Requested tag already exist
18# 10 - Refused to build fractional release
19# 100 - Unknown error (should not happen)
cd445739 20
f8e50be5 21# Notes (todo/bugs):
f8e50be5
ER
22# - when Icon: field is present, -5 and -a5 doesn't work
23# - builder -R skips installing BR if spec is not present before builder invocation (need to run builder twice)
19aa2571 24# - does not respect NoSource: X, and tries to cvs up such files [ example: VirtualBox-bin.spec and its Source0 ]
f8e50be5
ER
25# TODO:
26# - ability to do ./builder -bb foo.spec foo2.spec foo3.spec
1ffcdaff
ER
27# - funny bug, if source-md5 is set then builder will download from distfiles even if there is no url present:
28# Source10: forwardfix.pl
29# # Source10-md5: 8bf85f7368933a4e0cb4f875bac28733
6ca6f876
ZU
30# - builder --help:
31# basename: missing operand
32# Try `basename --help' for more information.
33# -- and the normal usage info --
34
9d99a240
ER
35PROGRAM=${0##*/}
36APPDIR=$(d=$0; [ -L "$d" ] && d=$(readlink -f "$d"); dirname "$d")
37RCSID='$Id$' r=${RCSID#* * } rev=${r%% *}
b0f031e7 38VERSION="v0.35/$rev"
d43959a2 39VERSIONSTRING="\
b71f5024
ER
40Build package utility from PLD Linux Packages repository
41$VERSION (C) 1999-2011 Free Penguins".
d43959a2 42
d6a77ddb 43PATH="/bin:/usr/bin:/usr/sbin:/sbin:/usr/X11R6/bin"
b03f053b 44
b7cc0c94
ER
45# required rpm-build-macros
46RPM_MACROS_VER=1.534
47
cd445739 48COMMAND="build"
64ea5308 49TARGET=""
cd445739 50
bde1c404 51SPECFILE=""
d287305c 52BE_VERBOSE=""
4003ad8c 53QUIET=""
cd445739
AM
54CLEAN=""
55DEBUG=""
56NOURLS=""
57NOCVS=""
58NOCVSSPEC=""
59NODIST=""
47761337 60NOINIT=""
bc10f694 61PREFMIRRORS=""
cd445739 62UPDATE=""
cd445739
AM
63ADD5=""
64NO5=""
65ALWAYS_CVSUP=${ALWAYS_CVSUP:-"yes"}
c8f50498 66CVSROOT=""
cdbd30e8 67GREEDSRC=""
cd445739 68
6c1b2e7d
PZ
69# use rpm 4.4.6+ digest format instead of comments if non-zero
70USEDIGEST=
71
e3aa6f5e
ER
72# user agent when fetching files
73USER_AGENT="PLD/Builder($VERSION)"
74
cd445739
AM
75# It can be used i.e. in log file naming.
76# See LOGFILE example.
77DATE=`date +%Y-%m-%d_%H-%M-%S`
78
79# Example: LOGFILE='../log.$PACKAGE_NAME'
80# Example: LOGFILE='../LOGS/log.$PACKAGE_NAME.$DATE'
d280fd97 81# Example: LOGFILE='$PACKAGE_NAME/$PACKAGE_NAME.$DATE.log'
f5687002 82# Example: LOGFILE='$PACKAGE_NAME.$DATE.log'
cd445739
AM
83# Yes, you can use variable name! Note _single_ quotes!
84LOGFILE=''
85
86LOGDIR=""
87LOGDIROK=""
88LOGDIRFAIL=""
89LASTLOG_FILE=""
90
91CHMOD="no"
1da227e8 92CHMOD_MODE="0644"
cd445739 93RPMOPTS=""
d1c5a9a7 94RPMBUILDOPTS=""
cd445739
AM
95BCOND=""
96GROUP_BCONDS="no"
ccffa567
ER
97
98# create symlinks for tools in PACKAGE_DIR, see get_spec()
2dd0af86 99SYMLINK_TOOLS="no"
d287305c 100
101PATCHES=""
102SOURCES=""
cd445739 103ICONS=""
d287305c 104PACKAGE_RELEASE=""
105PACKAGE_VERSION=""
106PACKAGE_NAME=""
14bead22 107ASSUMED_NAME=""
fd71d65c 108PROTOCOL="http"
cd445739 109WGET_RETRIES=${MAX_WGET_RETRIES:-0}
cd445739 110
db5831da 111CVS_COMMAND=${CVS_COMMAND:-cvs}
939f2c12 112CVS_FORCE=""
f123bcb5 113CVSIGNORE_DF="yes"
939f2c12
ER
114CVS_RETRIES=${MAX_CVS_RETRIES:-1000}
115CVS_SERVER="cvs.pld-linux.org"
cd445739 116CVSTAG=""
939f2c12 117
cd445739
AM
118RES_FILE=""
119
cd445739 120DISTFILES_SERVER="://distfiles.pld-linux.org"
d4730dae 121ATTICDISTFILES_SERVER="://attic-distfiles.pld-linux.org"
cd445739 122
36d03934 123DEF_NICE_LEVEL=19
86aafdbe 124SCHEDTOOL="auto"
cd445739
AM
125
126FAIL_IF_NO_SOURCES="yes"
127
18e5347d
ER
128# let get_files skip over files which are present to get those damn files fetched
129SKIP_EXISTING_FILES="no"
130
975d7b23 131TRY_UPGRADE=""
257af81b
ER
132# should the specfile be restored if upgrade failed?
133REVERT_BROKEN_UPGRADE="yes"
134
90baf90b
ER
135if rpm --specsrpm 2>/dev/null; then
136 FETCH_BUILD_REQUIRES_RPMSPECSRPM="yes"
ce40491a 137 FETCH_BUILD_REQUIRES_RPMGETDEPS="no"
90baf90b
ER
138else
139 FETCH_BUILD_REQUIRES_RPMSPECSRPM="no"
140 if [ -x /usr/bin/rpm-getdeps ]; then
141 FETCH_BUILD_REQUIRES_RPMGETDEPS="yes"
142 else
143 FETCH_BUILD_REQUIRES_RPMGETDEPS="no"
144 fi
ab4a2b6e
ER
145fi
146
90baf90b 147
cd445739
AM
148# Here we load saved user environment used to
149# predefine options set above, or passed to builder
150# in command line.
151# This one reads global system environment settings:
152if [ -f ~/etc/builderrc ]; then
153 . ~/etc/builderrc
154fi
155# And this one cascades settings using user personal
156# builder settings.
157# Example of ~/.builderrc:
158#
159#UPDATE_POLDEK_INDEXES="yes"
160#FETCH_BUILD_REQUIRES="yes"
161#REMOVE_BUILD_REQUIRES="force"
162#GROUP_BCONDS="yes"
163#LOGFILE='../LOGS/log.$PACKAGE_NAME.$DATE'
982e358c 164#TITLECHANGE=no
cd445739 165#
a83e5405 166SU_SUDO=""
cd445739
AM
167if [ -n "$HOME_ETC" ]; then
168 USER_CFG="$HOME_ETC/.builderrc"
20117088 169 BUILDER_MACROS="$HOME_ETC/.builder-rpmmacros"
cd445739
AM
170else
171 USER_CFG=~/.builderrc
20117088 172 BUILDER_MACROS=~/.builder-rpmmacros
cd445739
AM
173fi
174
175[ -f "$USER_CFG" ] && . "$USER_CFG"
176
86aafdbe
ER
177if [ "$SCHEDTOOL" = "auto" ]; then
178 if [ -x /usr/bin/schedtool ] && schedtool -B -e echo >/dev/null; then
179 SCHEDTOOL="schedtool -B -e"
180 else
181 SCHEDTOOL="no"
182 fi
183fi
184
ca2e6c31 185if [ -n "$USE_PROZILLA" ]; then
7e365483
KK
186 GETURI="proz --no-getch -r -P ./ -t$WGET_RETRIES $PROZILLA_OPTS"
187 GETURI2="$GETURI"
188 OUTFILEOPT="-O"
ca2e6c31 189elif [ -n "$USE_AXEL" ]; then
cede92b3 190 GETURI="axel -a $AXEL_OPTS"
7e365483
KK
191 GETURI2="$GETURI"
192 OUTFILEOPT="-o"
9639be96
AM
193elif [ -n "$USE_LFTP" ]; then
194download_lftp() {
2830c943 195 local url outfile retval
9639be96
AM
196 url="$1"
197 outfile="$2"
2830c943
AM
198 lftp -c "set net:max-retries $WGET_RETRIES; set http:user-agent \"$USER_AGENT\"; pget -n 10 -c \"$url\" -o \"$outfile.tmp\""
199 if [ $? -eq 0 ]; then
200 mv -f "$outfile.tmp" "$outfile"
201 else
202 rm -f "$outfile.tmp"
203 fi
9639be96
AM
204}
205 GETURI=download_lftp
206 GETURI2=$GETURI
207 OUTFILEOPT=""
ca2e6c31 208else
5700ee32
ER
209 wget --help 2>&1 | grep -q -- ' --no-check-certificate ' && WGET_OPTS="$WGET_OPTS --no-check-certificate"
210 wget --help 2>&1 | grep -q -- ' --inet ' && WGET_OPTS="$WGET_OPTS --inet"
211 wget --help 2>&1 | grep -q -- ' --retry-connrefused ' && WGET_OPTS="$WGET_OPTS --retry-connrefused"
e3aa6f5e 212 WGET_OPTS="$WGET_OPTS --user-agent=$USER_AGENT"
6f9a5d76 213
7e365483
KK
214 GETURI="wget --passive-ftp -c -nd -t$WGET_RETRIES $WGET_OPTS"
215 GETURI2="wget -c -nd -t$WGET_RETRIES $WGET_OPTS"
216 OUTFILEOPT="-O"
2bc5451f 217fi
11468b23 218
11468b23
AM
219GETLOCAL="cp -a"
220
221if (rpm --version 2>&1 | grep -q '4.0.[0-2]'); then
222 RPM="rpm"
223 RPMBUILD="rpm"
224else
225 RPM="rpm"
226 RPMBUILD="rpmbuild"
227fi
228
c86939e2 229#
230# sanity checks
231#
232if [ -d $HOME/rpm/SOURCES ]; then
233 echo "ERROR: ~/rpm/{SPECS,SOURCES} structure is obsolete" >&2
234 echo "ERROR: get rid of your ~/rpm/SOURCES" >&2
235 exit 1
236fi
237
28c23a72
AG
238#
239# are we using cvs-nserver ?
240#
e21039da 241CVS_NSERVER=0
939f2c12 242$CVS_COMMAND --version 2>&1 | grep -q 'CVS-nserver'
e21039da 243[ $? -eq 0 ] && CVS_NSERVER=1
28c23a72 244
a8c9a155 245POLDEK_INDEX_DIR="$($RPM --eval %_rpmdir)/"
a83e5405 246POLDEK_CMD="$SU_SUDO /usr/bin/poldek --noask"
11468b23 247
f6711e2a 248run_poldek() {
390d081d 249 RES_FILE=$(tempfile)
cd445739
AM
250 if [ -n "$LOGFILE" ]; then
251 LOG=`eval echo $LOGFILE`
252 if [ -n "$LASTLOG_FILE" ]; then
253 echo "LASTLOG=$LOG" > $LASTLOG_FILE
254 fi
86aafdbe 255 (${NICE_COMMAND} ${POLDEK_CMD} `while test $# -gt 0; do echo "$1 ";shift;done` ; echo $? > ${RES_FILE})|tee -a $LOG
cd445739
AM
256 return $exit_pldk
257 else
86aafdbe 258 (${NICE_COMMAND} ${POLDEK_CMD} `while test $# -gt 0; do echo "$1 ";shift;done` ; echo $? > ${RES_FILE}) 1>&2 >/dev/null
cd445739
AM
259 return `cat ${RES_FILE}`
260 rm -rf ${RES_FILE}
261 fi
262}
263
cd445739
AM
264#---------------------------------------------
265# functions
da946cd6 266
f6711e2a 267usage() {
cd445739
AM
268 if [ -n "$DEBUG" ]; then set -xv; fi
269 echo "\
db9fdf51 270Usage: builder [-D|--debug] [-V|--version] [--short-version] [--as_anon] [-a|--add_cvs] [-b|-ba|--build]
6594293d 271[-bb|--build-binary] [-bs|--build-source] [-bc] [-bi] [-bl] [-u|--try-upgrade]
4efa0b98 272[{-cf|--cvs-force}] [{-B|--branch} <branch>] [{-d|--cvsroot} <cvsroot>]
fd71d65c 273[-g|--get] [-h|--help] [--ftp] [--http] [{-l|--logtofile} <logfile>] [-m|--mr-proper]
19dc5591 274[-q|--quiet] [--date <yyyy-mm-dd> [-r <cvstag>] [{-T|--tag <cvstag>]
a7b2951c 275[-Tvs|--tag-version-stable] [-Ts|--tag-stable] [-Tv|--tag-version]
f8dba812 276[{-Tp|--tag-prefix} <prefix>] [{-tt|--test-tag}] [--use-greed-sources]
6594293d 277[-nu|--no-urls] [-v|--verbose] [--opts <rpm opts>] [--short-circuit]
3009b80d 278[--show-bconds] [--with/--without <feature>] [--define <macro> <value>]
6594293d 279<package>[.spec][:cvstag]
2a5f078d 280
5efcb865 281-4 - force ipv4 when transferring files
cd445739 282-5, --update-md5 - update md5 comments in spec, implies -nd -ncs
5efcb865 283-6 - force ipv6 when transferring files
cd445739
AM
284-a5, --add-md5 - add md5 comments to URL sources, implies -nc -nd -ncs
285-n5, --no-md5 - ignore md5 comments in spec
9243e80b
ER
286-D, --debug - enable builder script debugging mode,
287-debug - produce rpm debug package (same as --opts -debug)
d43959a2
ER
288-V, --version - output builder version string
289--short-version - output builder short version
db9fdf51
ER
290--as_anon - get files via pserver as cvs@$CVS_SERVER,
291-a, --add_cvs - try add new package to CVS.
cd445739
AM
292-b, -ba, --build - get all files from CVS repo or HTTP/FTP and build package
293 from <package>.spec,
294-bb, --build-binary - get all files from CVS repo or HTTP/FTP and build binary
295 only package from <package>.spec,
c56807e0 296-bp, --build-prep - execute the %prep phase of <package>.spec,
6594293d
AG
297-bc - execute the %build phase of <package>.spec,
298-bi - execute the %install phase of <package>.spec
b05205af 299-bl - execute the %files phase of <package>.spec
cd445739
AM
300-bs, --build-source - get all files from CVS repo or HTTP/FTP and only pack
301 them into src.rpm,
6594293d 302--short-circuit - short-circuit build
cd445739 303-B, --branch - add branch
387eaf99
ER
304-c, --clean - clean all temporarily created files (in BUILD\$RPM_BUILD_ROOT) after rpmbuild commands.
305 may be used with building process.
b05205af
BZ
306-m, --mr-proper - clean all temporarily created files (in BUILD, SOURCES, SPECS and \$RPM_BUILD_ROOT
307 and CVS/Entries). Doesn't run any rpm building.
308-cf, --cvs-force - use -F when tagging (useful when moving branches)
2a5f078d 309-d <cvsroot>, --cvsroot <cvsroot>
cd445739 310 - setup \$CVSROOT,
b05205af
BZ
311--define <macro> <value> - define a macro <macro> with value <value>,
312--alt_kernel <kernel> - same as --define 'alt_kernel <kernel>'
cd445739 313--nodeps - rpm won't check any dependences
b05205af
BZ
314-g, --get - get <package>.spec and all related files from CVS repo or HTTP/FTP
315-h, --help - this message
6c5362e5 316-jN, -j N - set %_smp_mflags to propagate concurrent jobs
fd71d65c 317--ftp, --http - use ftp or http protocol to access distfiles server
cd445739
AM
318-l <logfile>, --logtofile <logfile>
319 - log all to file,
cd445739 320-nc, --no-cvs - don't download sources from CVS, if source URL is given,
b05205af 321-ncs, --no-cvs-specs - don't check specs in CVS
cd445739
AM
322-nd, --no-distfiles - don't download from distfiles
323-nm, --no-mirrors - don't download from mirror, if source URL is given,
324-nu, --no-urls - don't try to download from FTP/HTTP location,
7cbc2d88 325-ns, --no-srcs - don't download Sources/Patches
cd445739
AM
326-ns0, --no-source0 - don't download Source0
327-nn, --no-net - don't download anything from the net
a93e8cdf 328-pm, --prefer-mirrors - prefer mirrors (if any) over distfiles for SOURCES
b572253e 329--no-init - don't initialize builder paths (SPECS and SOURCES)
18e5347d
ER
330-ske,
331--skip-existing-files - skip existing files in get_files
cd445739
AM
332--opts <rpm opts> - additional options for rpm
333-q, --quiet - be quiet,
334--date yyyy-mm-dd - build package using resources from specified CVS date,
335-r <cvstag>, --cvstag <cvstag>
336 - build package using resources from specified CVS tag,
5323fffd 337-A - build package using CVS resources as any sticky tags/date/kopts being reset.
cd445739
AM
338-R, --fetch-build-requires
339 - fetch what is BuildRequired,
340-RB, --remove-build-requires
341 - remove all you fetched with -R or --fetch-build-requires
342 remember, this option requires confirmation,
343-FRB, --force-remove-build-requires
344 - remove all you fetched with -R or --fetch-build-requires
345 remember, this option works without confirmation,
06541433
JR
346-sd, --source-distfiles - list sources available from distfiles (intended for offline
347 operations; does not work when Icon field is present
348 but icon file is absent),
a6494cd4 349-sc, --source-cvs - list sources available from CVS
06541433
JR
350-sdp, --source-distfiles-paths - list sources available from distfiles -
351 paths relative to distfiles directory (intended for offline
352 operations; does not work when Icon field is present
353 but icon file is absent),
a7eefc54 354-sf, --source-files - list sources - bare filenames (intended for offline
355 operations; does not work when Icon field is present
3621e588 356 but icon file is absent),
8bd5e66d 357-lsp, --source-paths - list sources - filenames with full local paths (intended for
a7eefc54 358 offline operations; does not work when Icon field is present
3621e588 359 but icon file is absent),
2dddf439
ER
360-su, --source-urls - list urls - urls to sources and patches
361 intended for copying urls with spec with lots of macros in urls
cd445739
AM
362-T <cvstag> , --tag <cvstag>
363 - add cvs tag <cvstag> for files,
364-Tvs, --tag-version-stable
094bd95b 365 - add cvs tags STABLE and NAME-VERSION-RELEASE for files,
b05205af
BZ
366-Ts, --tag-stable - add cvs tag STABLE for files,
367-Tv, --tag-version - add cvs tag NAME-VERSION-RELEASE for files,
cd445739
AM
368-Tp, --tag-prefix <prefix>
369 - add <prefix> to NAME-VERSION-RELEASE tags,
370-tt, --test-tag <prefix>
371 - fail if tag is already present,
50321881
JK
372-ir, --integer-release-only
373 - allow only integer and snapshot releases
cd445739
AM
374-v, --verbose - be verbose,
375-u, --try-upgrade - check version, and try to upgrade package
376-un, --try-upgrade-with-float-version
377 - as above, but allow float version
a6494cd4 378 php-pear-Services_Digg/
0907f1eb 379--upgrade-version - upgrade to specified version in try-upgrade
f8dba812 380--use-greed-sources
381 - try download source from tag head if don't find it in
f5141bbc 382 current tag
deccc50e 383-U, --update - refetch sources, don't use distfiles, and update md5 comments
cd445739
AM
384-Upi, --update-poldek-indexes
385 - refresh or make poldek package index files.
8bd5e66d
ER
386-sp, --skip-patch <patchnumber>
387 - don't apply <patchnumber>. may be repeated.
c890b916 388-np, --nopatch <patchnumber>
8bd5e66d 389 - abort instead of applying patch <patchnumber>
dfe2cb9a
MK
390--show-bconds - show available conditional builds, which can be used
391 - with --with and/or --without switches.
b05205af
BZ
392--show-bcond-args - show active bconds, from ~/.bcondrc. this is used by ./repackage.sh script.
393 In other words, the output is parseable by scripts.
24a97174 394--show-avail-bconds - show available bconds
cd445739
AM
395--with/--without <feature>
396 - conditional build package depending on %_with_<feature>/
397 %_without_<feature> macro switch. You may now use
398 --with feat1 feat2 feat3 --without feat4 feat5 --with feat6
399 constructions. Set GROUP_BCONDS to yes to make use of it.
6dd19291 400--target <platform>, --target=<platform>
a6494cd4
ER
401 - build for platform <platform>.
402--init-rpm-dir - initialize ~/rpm directory structure
5a491465 403"
404}
405
390d081d
ER
406# create tempfile. as secure as possible
407tempfile() {
408 mktemp -t builder.XXXXXX || ${TMPDIR:-/tmp}/builder.$RANDOM.$$
409}
410
31d2bd0b
ER
411# change dependency to specname
412# common changes:
413# - perl(Package::Name) -> perl-Package-Name
414depspecname() {
415 local package="$1"
416
560c4b1a 417 package=$(echo "$package" | sed -e '/perl(.*)/{s,perl(\(.*\)),perl-\1,;s,::,-,g};' -e 's/-\(devel\|static\)$//' )
31d2bd0b
ER
418 echo "$package"
419}
420
c890b916 421update_shell_title() {
a0d6396b 422 [ -t 1 ] || return
0b65d15e 423 local len=${COLUMNS:-80}
5c795f16 424 local msg="$(echo "$*" | cut -c-$len)"
0b65d15e 425
e225de91
ER
426 if [ -n "$BE_VERBOSE" ]; then
427 echo >&2 "$(date +%s.%N) $*"
428 fi
429
5c795f16 430 if [ "x$TITLECHANGE" = "xyes" -o "x$TITLECHANGE" = "x" ]; then
138bbc98
ER
431 local pkg
432 if [ -n "$PACKAGE_NAME" ]; then
433 pkg=${PACKAGE_NAME}-${PACKAGE_VERSION}-${PACKAGE_RELEASE}
434 else
435 pkg=${SPECFILE}
436 fi
437
438 msg="$pkg: ${SHELL_TITLE_PREFIX:+$SHELL_TITLE_PREFIX }$msg"
a8c9a155 439 msg=$(echo $msg | tr -d '\n\r')
982e358c
MP
440 case "$TERM" in
441 cygwin|xterm*)
442 echo >&2 -ne "\033]1;$msg\007\033]2;$msg\007"
443 ;;
444 screen*)
445 echo >&2 -ne "\033]0;$msg\007"
446 ;;
447 esac
448 fi
c890b916
ER
449}
450
7e40520f
ER
451# set TARGET from BuildArch: from SPECFILE
452set_spec_target() {
ce40491a 453 if [ -n "$SPECFILE" ] && [ -z "$TARGET" ]; then
b45a3b2a 454 tmp=$(awk '/^BuildArch:/ { print $NF; exit }' $ASSUMED_NAME/$SPECFILE)
ce40491a 455 if [ "$tmp" ]; then
f9109a96 456 target_platform=$(rpm -E '%{_target_vendor}-%{_target_os}%{?_gnu}')
7e40520f
ER
457 TARGET="$tmp"
458 case "$RPMBUILD" in
459 "rpmbuild")
f9109a96 460 TARGET_SWITCH="--target ${TARGET}-${target_platform}" ;;
7e40520f 461 "rpm")
ce40491a 462 TARGET_SWITCH="--target=$TARGET" ;;
7e40520f 463 esac
ce40491a
ER
464 fi
465 fi
7e40520f
ER
466}
467
ace3fd80
ER
468# runs rpm with minimal macroset
469minirpm() {
fa4bf1df 470 # we reset macros not to contain macros.build as all the %() macros are
559d511f 471 # executed here, while none of them are actually needed.
559d511f 472 # at the time of this writing macros.build + macros contained 70 "%(...)" macros.
525bf240 473 safe_macrofiles=$(rpm $TARGET_SWITCH --showrc | awk -F: '/^macrofiles/ { gsub(/^macrofiles[ \t]+:/, "", $0); gsub(/:.*macros.build:/, ":", $0); print $0 } ')
ace3fd80
ER
474
475 # TODO: move these to /usr/lib/rpm/macros
20117088 476 cat > $BUILDER_MACROS <<'EOF'
5b5e5f7f 477%x8664 x86_64 amd64 ia32e
f78a0bb7 478%alt_kernel %{nil}
479%_alt_kernel %{nil}
5ff15c97 480%bootstrap_release() %{1}
407b204b
ER
481%requires_releq_kernel_up(s:n:) %{nil}
482%requires_releq_kernel_smp(s:n:) %{nil}
483%requires_releq_kernel(s:n:) %{nil}
6b8134e3 484%requires_releq() %{nil}
bb01dee9 485%pyrequires_eq() %{nil}
559d511f 486%requires_eq() %{nil}
c13be3d1 487%requires_eq_to() %{nil}
99c05e13 488%requires_ge() %{nil}
500b0eb8
ER
489%requires_ge_to() %{nil}
490%requires_ge() %{nil}
407b204b
ER
491%releq_kernel_up(n:) ERROR
492%releq_kernel_smp(n:) ERROR
493%releq_kernel(n:) ERROR
74d9fd14 494%kgcc_package ERROR
237bd3f1 495%_fontsdir ERROR
7141278d 496%ruby_version ERROR
497%ruby_ver_requires_eq() %{nil}
498%ruby_mod_ver_requires_eq() %{nil}
c13be3d1 499%__php_api_requires() %{nil}
7141278d 500%php_major_version ERROR
501%php_api_version ERROR
99f819cf
AM
502%requires_xorg_xserver_extension %{nil}
503%requires_xorg_xserver_xinput %{nil}
504%requires_xorg_xserver_font %{nil}
505%requires_xorg_xserver_videodrv %{nil}
7141278d 506%py_ver ERROR
41035426
ER
507%perl_vendorarch ERROR
508%perl_vendorlib ERROR
703ae20b
ER
509# damn. need it here! - copied from /usr/lib/rpm/macros.build
510%tmpdir %(echo "${TMPDIR:-/tmp}")
1f28fcb7
ER
511%patchset_source(f:b:) %(
512 base=%{-b*}%{!-b*:10000};
513 start=$(expr $base + %1);
514 end=$(expr $base + %{?2}%{!?2:%{1}});
515 # we need to call seq twice as it doesn't allow two formats
516 seq -f 'Patch%g:' $start $end > %{tmpdir}/__ps1;
517 seq -f '%{-f*}' %1 %{?2}%{!?2:%{1}} > %{tmpdir}/__ps2;
518 paste %{tmpdir}/__ps{1,2};
519 rm -f %{tmpdir}/__ps{1,2};
520) \
521%{nil}
991f09a5
ER
522%add_etc_shells(p) %{p:<lua>}
523%remove_etc_shells(p) %{p:<lua>}
709995c4
ER
524%lua_add_etc_shells() %{nil}
525%lua_remove_etc_shells() %{nil}
0ca65fa4
JK
526%required_jdk %{nil}
527%buildrequires_jdk %{nil}
f9063abe 528%pear_package_print_optionalpackages %{nil}
fb198857 529EOF
3d12d055 530 if [ "$NOINIT" = "yes" ] ; then
20117088 531 cat >> $BUILDER_MACROS <<'EOF'
3d12d055
JR
532%_specdir ./
533%_sourcedir ./
534EOF
c039643c 535 fi
fff1b991 536 eval $RPMBUILD $TARGET_SWITCH --macros "$safe_macrofiles:$BUILDER_MACROS" $QUIET $RPMOPTS $RPMBUILDOPTS $BCOND $* 2>&1
ace3fd80
ER
537}
538
539cache_rpm_dump() {
540 if [ -n "$DEBUG" ]; then
541 set -x
542 set -v
543 fi
544
d1d24c14 545 if [ -x /usr/bin/rpm-specdump ]; then
525bf240 546 update_shell_title "cache_rpm_dump using rpm-specdump command"
fff1b991 547 rpm_dump_cache=$(rpm-specdump $TARGET_SWITCH $BCOND $SPECFILE)
d1d24c14 548 else
525bf240 549 update_shell_title "cache_rpm_dump using rpmbuild command"
d1d24c14
ER
550 local rpm_dump
551 rpm_dump=`
552 # what we need from dump is NAME, VERSION, RELEASE and PATCHES/SOURCES.
553 dump='%{echo:dummy: PACKAGE_NAME %{name} }%dump'
554 case "$RPMBUILD" in
555 rpm)
556 ARGS='-bp'
557 ;;
558 rpmbuild)
559 ARGS='--nodigest --nosignature --nobuild'
560 ;;
561 esac
562 minirpm $ARGS --define "'prep $dump'" --nodeps $SPECFILE
563 `
564 if [ $? -gt 0 ]; then
565 error=$(echo "$rpm_dump" | sed -ne '/^error:/,$p')
566 echo "$error" >&2
567 Exit_error err_build_fail
568 fi
d05e8080 569
d1d24c14
ER
570 # make small dump cache
571 rpm_dump_cache=`echo "$rpm_dump" | awk '
572 $2 ~ /^SOURCEURL/ {print}
573 $2 ~ /^PATCHURL/ {print}
574 $2 ~ /^nosource/ {print}
575 $2 ~ /^PACKAGE_/ {print}
576 '`
577 fi
d05e8080 578
13974367 579 update_shell_title "cache_rpm_dump: OK!"
cd445739
AM
580}
581
ace3fd80 582rpm_dump() {
cd445739 583 if [ -z "$rpm_dump_cache" ] ; then
45e2a818 584 echo >&2 "internal error: cache_rpm_dump not called! (missing %prep?)"
cd445739
AM
585 fi
586 echo "$rpm_dump_cache"
587}
588
f6711e2a 589get_icons() {
d05e8080 590 update_shell_title "get icons"
9d99a240 591 ICONS=$(awk '/^Icon:/ {print $2}' $PACKAGE_DIR/${SPECFILE})
d05e8080
ER
592 if [ -z "$ICONS" ]; then
593 return
594 fi
595
975d7b23 596 rpm_dump_cache="kalasaba" NODIST="yes" get_files $ICONS
d05e8080
ER
597}
598
f6711e2a 599parse_spec() {
af4d5315 600 update_shell_title "parsing specfile"
cd445739 601 if [ -n "$DEBUG" ]; then
518ff1fb
ER
602 set -x
603 set -v
cd445739 604 fi
b2975fc3 605
d05e8080 606 # icons are needed for successful spec parse
518ff1fb 607 get_icons
d05e8080 608
9c909460 609 cd $PACKAGE_DIR
cd445739 610 cache_rpm_dump
00fcec7e 611
cd445739
AM
612 if (rpm_dump | grep -qEi ":.*nosource.*1"); then
613 FAIL_IF_NO_SOURCES="no"
614 fi
615
7cbc2d88
ER
616 if [ "$NOSRCS" != "yes" ]; then
617 SOURCES=$(rpm_dump | awk '$2 ~ /^SOURCEURL[0-9]+/ {print substr($2, length("SOURCEURL") + 1), $3}' | LC_ALL=C sort -n | awk '{print $2}')
618 PATCHES=$(rpm_dump | awk '$2 ~ /^PATCHURL[0-9]+/ {print substr($2, length("PATCHURL") + 1), $3}' | LC_ALL=C sort -n | awk '{print $2}')
619 ICONS=$(awk '/^Icon:/ {print $2}' ${SPECFILE})
620 fi
621
dfe2b2d9
ER
622 PACKAGE_NAME=$(rpm_dump | awk '$2 == "PACKAGE_NAME" { print $3; exit}')
623 PACKAGE_VERSION=$(rpm_dump | awk '$2 == "PACKAGE_VERSION" { print $3; exit}')
624 PACKAGE_RELEASE=$(rpm_dump | awk '$2 == "PACKAGE_RELEASE" { print $3; exit}')
78eab2a1 625
0b4119a2 626 if [ "$PACKAGE_NAME" != "$ASSUMED_NAME" ]; then
7854d40c 627 echo >&2 "WARNING! Spec name ($ASSUMED_NAME) does not agree with package name ($PACKAGE_NAME)"
0b4119a2
JR
628 fi
629
cd445739
AM
630 if [ -n "$BE_VERBOSE" ]; then
631 echo "- Sources : `nourl $SOURCES`"
632 if [ -n "$PATCHES" ]; then
633 echo "- Patches : `nourl $PATCHES`"
634 else
635 echo "- Patches : *no patches needed*"
636 fi
637 if [ -n "$ICONS" ]; then
638 echo "- Icon : `nourl $ICONS`"
639 else
640 echo "- Icon : *no package icon*"
641 fi
642 echo "- Name : $PACKAGE_NAME"
643 echo "- Version : $PACKAGE_VERSION"
644 echo "- Release : $PACKAGE_RELEASE"
645 fi
13974367
ER
646
647 update_shell_title "parse_spec: OK!"
cd445739
AM
648}
649
f6711e2a 650Exit_error() {
cd445739 651 if [ -n "$DEBUG" ]; then
518ff1fb
ER
652 set -x
653 set -v
cd445739
AM
654 fi
655
656 cd "$__PWD"
657
658 case "$1" in
659 "err_no_spec_in_cmdl" )
660 remove_build_requires
45e2a818 661 echo >&2 "ERROR: spec file name not specified."
cd445739 662 exit 2 ;;
a08d92fc 663 "err_invalid_cmdline" )
45e2a818 664 echo >&2 "ERROR: invalid command line arg ($2)."
a08d92fc 665 exit 2 ;;
cd445739
AM
666 "err_no_spec_in_repo" )
667 remove_build_requires
45e2a818 668 echo >&2 "Error: spec file not stored in CVS repo."
cd445739
AM
669 exit 3 ;;
670 "err_no_source_in_repo" )
671 remove_build_requires
45e2a818 672 echo >&2 "Error: some source, patch or icon files not stored in CVS repo. ($2)"
cd445739 673 exit 4 ;;
703b6deb
ER
674 "err_cvs_add_failed" )
675 echo >&2 "Error: failed to add package to CVS repo."
676 exit 4 ;;
cd445739
AM
677 "err_build_fail" )
678 remove_build_requires
45e2a818 679 echo >&2 "Error: package build failed. (${2:-no more info})"
cd445739 680 exit 5 ;;
78eab2a1
AM
681 "err_no_package_data" )
682 remove_build_requires
45e2a818 683 echo >&2 "Error: couldn't get out package name/version/release from spec file."
78eab2a1 684 exit 6 ;;
ce40491a 685 "err_tag_exists" )
50321881 686 remove_build_requires
45e2a818 687 echo >&2 "Tag ${2} already exists (spec release: ${3})."
50321881 688 exit 9 ;;
ce40491a 689 "err_fract_rel" )
50321881 690 remove_build_requires
45e2a818 691 echo >&2 "Release ${2} not integer and not a snapshot."
50321881 692 exit 10 ;;
fb96e0b5
AM
693 "err_branch_exists" )
694 remove_build_requires
45e2a818 695 echo >&2 "Tree branch already exists (${2})."
fb96e0b5 696 exit 11 ;;
cfc497c0
ER
697 "err_acl_deny" )
698 remove_build_requires
45e2a818 699 echo >&2 "Error: conditions reject building this spec (${2})."
cfc497c0 700 exit 12 ;;
cd445739 701 esac
45e2a818 702 echo >&2 "Unknown error."
73848362 703 exit 100
cd445739
AM
704}
705
f6711e2a 706init_builder() {
cd445739 707 if [ -n "$DEBUG" ]; then
518ff1fb
ER
708 set -x
709 set -v
0dd6320d 710 fi
cd445739 711
4aeb9973 712 if [ "$NOINIT" != "yes" ] ; then
9c909460 713 TOP_DIR=$(eval $RPM $RPMOPTS --eval '%{_topdir}')
b7cc0c94 714
09bf17c9 715 local macros_ver=$(rpm -E %?rpm_build_macros)
b7cc0c94
ER
716 if [ -z "$macros_ver" ]; then
717 REPO_DIR=$TOP_DIR/packages
718 PACKAGE_DIR=$TOP_DIR/packages/$ASSUMED_NAME
719 else
720 if awk "BEGIN{exit($macros_ver>=$RPM_MACROS_VER)}"; then
721 echo >&2 "builder requires rpm-build-macros >= $RPM_MACROS_VER"
722 exit 1
723 fi
724 REPO_DIR=$TOP_DIR
725 PACKAGE_DIR=$REPO_DIR/$ASSUMED_NAME
726 fi
29e90b02 727 else
9c909460
AM
728 REPO_DIR="."
729 PACKAGE_DIR="."
29e90b02 730 fi
cd445739 731
a8c9a155 732 __PWD=$(pwd)
00fcec7e 733}
734
f6711e2a 735get_spec() {
c890b916 736
2e5be021 737 update_shell_title "get_spec"
c890b916 738
cd445739 739 if [ -n "$DEBUG" ]; then
518ff1fb
ER
740 set -x
741 set -v
cd445739
AM
742 fi
743
9c909460
AM
744 cd "$REPO_DIR"
745 if [ ! -f "$ASSUMED_NAME/$SPECFILE" ]; then
6277f15f
ER
746 # XXX: still needed?
747 SPECFILE=$(basename $SPECFILE)
cd445739 748 fi
cd445739 749
1ef6f5de 750 if [ "$NOCVSSPEC" != "yes" ]; then
049ab214
ER
751 if [ ! -s CVS/Root -a "$NOCVSSPEC" != "yes" ]; then
752 echo "Warning: No CVS access defined - using local .spec file"
753 NOCVSSPEC="yes"
cd445739
AM
754 fi
755
1ef6f5de 756 if [ -d "$ASSUMED_NAME" -a -s "$ASSUMED_NAME/CVS/Root" ]; then
9c909460 757 cvsup "$ASSUMED_NAME/$SPECFILE" || Exit_error err_no_spec_in_repo
703b6deb 758 elif [ "$ADD_PACKAGE_CVS" = "yes" ]; then
1a135961 759 if [ ! -r "$ASSUMED_NAME/$SPECFILE" ]; then
760 echo "ERROR: No package to add ($ASSUMED_NAME/$SPECFILE)" >&2
761 exit 101
703b6deb
ER
762 fi
763 if [ ! -s "$ASSUMED_NAME/CVS/Root" ]; then
764 cvsup -a $ASSUMED_NAME || Exit_error err_cvs_add_failed
765 fi
766 cvsup -a "$ASSUMED_NAME/$SPECFILE" || Exit_error err_cvs_add_failed
9c909460 767 else
1ef6f5de
ER
768 cvsup -c -d $ASSUMED_NAME "packages/$ASSUMED_NAME/$SPECFILE" || {
769 # softfail if new package, i.e not yet added to cvs
770 [ ! -f "$ASSUMED_NAME/$SPECFILE" ] && Exit_error err_no_spec_in_repo
771 echo "Warning: package not in CVS - assuming new package"
772 NOCVSSPEC="yes"
773 NOCVS="yes"
774 }
f123bcb5
ER
775
776 # remove Entries.Static -- so 'cvs up' would update all files in a repo
1ef6f5de 777 rm -f "$ASSUMED_NAME/CVS/Entries.Static"
703b6deb 778 fi
0c3beb90 779
703b6deb
ER
780 cvsignore_df .cvsignore
781
782 # add default log format to .cvsignore if it is relative to package dir
783 if [ -n "$LOGFILE" -a "$LOGFILE" = "${LOGFILE##*/}" ]; then
784 # substitute known "macros" to glob
785 local logfile=$(echo "$LOGFILE" | sed -e 's,\$\(PACKAGE_NAME\|DATE\),*,g')
786 if [ "$logfile" ]; then
787 cvsignore_df "$logfile"
daeab113 788 fi
9c909460 789 fi
703b6deb
ER
790
791 # create symlinks for tools
792 if [ "$SYMLINK_TOOLS" != "no" ]; then
793 for a in dropin md5 adapter builder {relup,compile,repackage,rsync,pearize}.sh pldnotify.awk; do
046b715f 794 # skip tools that don't exist in top dir
703b6deb 795 [ -f $a ] || continue
046b715f
ER
796 # skip tools that already exist
797 [ -f $ASSUMED_NAME/$a ] && continue
f1081b0a 798 ln -s ../$a $ASSUMED_NAME
703b6deb
ER
799 cvsignore_df $a
800 done
801 fi
cd445739 802 fi
049ab214 803
9c909460 804 if [ ! -f "$ASSUMED_NAME/$SPECFILE" ]; then
518ff1fb 805 Exit_error err_no_spec_in_repo
cd445739
AM
806 fi
807
808 if [ "$CHMOD" = "yes" -a -n "$SPECFILE" ]; then
9c909460 809 chmod $CHMOD_MODE $ASSUMED_NAME/$SPECFILE
cd445739
AM
810 fi
811 unset OPTIONS
9c909460 812 [ -n "$DONT_PRINT_REVISION" ] || grep -E -m 1 "^#.*Revision:.*Date" $ASSUMED_NAME/$SPECFILE
7e40520f 813
ce40491a 814 set_spec_target
cd445739
AM
815}
816
f6711e2a 817find_mirror() {
9c909460 818 cd "$REPO_DIR"
782518da 819 local url="$1"
cd445739 820 if [ ! -f "mirrors" -a "$NOCVSSPEC" != "yes" ] ; then
939f2c12 821 $CVS_COMMAND update mirrors >&2
cd445739
AM
822 fi
823
824 IFS="|"
782518da
ER
825 local origin mirror name rest ol prefix
826 while read origin mirror name rest; do
827 # skip comments and empty lines
2faa4c32 828 if [ -z "$origin" ] || [ "${origin#\#}" != "$origin" ]; then
782518da
ER
829 continue
830 fi
a8c9a155
ER
831 ol=$(echo -n "$origin" | wc -c)
832 prefix=$(echo -n "$url" | head -c $ol)
cd445739 833 if [ "$prefix" = "$origin" ] ; then
a8c9a155 834 suffix=$(echo "$url" | cut -b $((ol+1))-)
cd445739
AM
835 echo -n "$mirror$suffix"
836 return 0
837 fi
838 done < mirrors
839 echo "$url"
840}
841
e7e0d4ec 842# Warning: unpredictable results if same URL used twice
f6711e2a 843src_no() {
8510980a
AM
844 local file="$1"
845 # escape some regexp characters if part of file name
298e75e2 846 file=$(echo "$file" | sed -e 's#\([\+\*\.\&\#\?]\)#\\\1#g')
9c909460 847 cd $PACKAGE_DIR
cd445739 848 rpm_dump | \
8510980a 849 grep -E "(SOURCE|PATCH)URL[0-9]*[ ]*${file}""[ ]*$" | \
647e3d7f
KK
850 sed -e 's/.*\(SOURCE\|PATCH\)URL\([0-9][0-9]*\).*/\1\2/' | \
851 head -n 1 | tr OURCEATH ourceath | xargs
1471f6d6 852}
853
f6711e2a 854src_md5() {
e7e0d4ec 855 [ "$NO5" = "yes" ] && return
cd445739
AM
856 no=$(src_no "$1")
857 [ -z "$no" ] && return
9c909460 858 cd $PACKAGE_DIR
e7e0d4ec
ER
859 local md5
860
861 if [ -f additional-md5sums ]; then
862 local spec_rev=$(grep $SPECFILE CVS/Entries 2>/dev/null | sed -e s:/$SPECFILE/:: -e s:/.*::)
863 if [ -z "$spec_rev" ]; then
a8c9a155 864 spec_rev=$(head -n 1 $SPECFILE | sed -e 's/.*\$Revision: \([0-9.]*\).*/\1/')
e7e0d4ec
ER
865 fi
866 local spec="$SPECFILE[0-9.,]*,$(echo $spec_rev | sed 's/\./\\./g')"
867 md5=$(grep -s -v '^#' additional-md5sums | \
868 grep -E "[ ]$(basename "$1")[ ]+${spec}([ ,]|\$)" | \
869 sed -e 's/^\([0-9a-f]\{32\}\).*/\1/' | \
870 grep -E '^[0-9a-f]{32}$')
871
872 if [ "$md5" ]; then
873 if [ $(echo "$md5" | wc -l) != 1 ] ; then
874 echo "$SPECFILE: more then one entry in additional-md5sums for $1" 1>&2
875 fi
876 echo "$md5" | tail -n 1
877 return
878 fi
cd445739 879 fi
e7e0d4ec 880
647e3d7f 881 source_md5=`grep -i "^#[ ]*$no-md5[ ]*:" $SPECFILE | sed -e 's/.*://'`
e7e0d4ec
ER
882 if [ -n "$source_md5" ]; then
883 echo $source_md5
cd445739 884 else
7805dcb4
PZ
885 source_md5=`grep -i "BuildRequires:[ ]*digest(%SOURCE$no)[ ]*=" $SPECFILE | sed -e 's/.*=//'`
886 if [ -n "$source_md5" ]; then
887 echo $source_md5
888 else
889 # we have empty SourceX-md5, but it is still possible
890 # that we have NoSourceX-md5 AND NoSource: X
647e3d7f 891 nosource_md5=`grep -i "^#[ ]*No$no-md5[ ]*:" $SPECFILE | sed -e 's/.*://'`
7805dcb4
PZ
892 if [ -n "$nosource_md5" -a -n "`grep -i "^NoSource:[ ]*$no$" $SPECFILE`" ] ; then
893 echo $nosource_md5
894 fi
cd445739 895 fi
cd445739
AM
896 fi
897}
898
f6711e2a 899distfiles_path() {
06541433
JR
900 echo "by-md5/$(src_md5 "$1" | sed -e 's|^\(.\)\(.\)|\1/\2/&|')/$(basename "$1")"
901}
902
f6711e2a 903distfiles_url() {
06541433 904 echo "$PROTOCOL$DISTFILES_SERVER/distfiles/$(distfiles_path "$1")"
cd445739
AM
905}
906
f6711e2a 907distfiles_attic_url() {
06541433 908 echo "$PROTOCOL$ATTICDISTFILES_SERVER/distfiles/Attic/$(distfiles_path "$1")"
cd445739
AM
909}
910
f6711e2a 911good_md5() {
cd445739
AM
912 md5=$(src_md5 "$1")
913 [ "$md5" = "" ] || \
914 [ "$md5" = "$(md5sum $(nourl "$1") 2> /dev/null | sed -e 's/ .*//')" ]
8ba5cdda
PG
915}
916
f6711e2a 917good_size() {
a8c9a155 918 size=$(find $(nourl "$1") -printf "%s" 2>/dev/null)
ce40491a 919 [ -n "$size" -a "$size" -gt 0 ]
a4b50627
AF
920}
921
f6711e2a 922cvsignore_df() {
947025e5 923 if [ "$CVSIGNORE_DF" != "yes" ]; then
924 return
925 fi
9c909460 926 cvsignore=${PACKAGE_DIR}/.cvsignore
7dc8d095
ER
927
928 # add only if not yet there
aa189b05 929 if ! awk -vf="$1" -vc=1 '$0 == f { c = 0 } END { exit c }' $cvsignore 2>/dev/null; then
947025e5 930 echo "$1" >> $cvsignore
931 fi
932}
933
f6711e2a 934cvsup() {
ce40491a 935 update_shell_title "cvsup"
703b6deb 936 local OPTIONS="" ACTION="up"
9c909460 937
703b6deb 938 # checkout
9c909460 939 if [ "$1" = "-c" ]; then
703b6deb
ER
940 ACTION="co"
941 shift
942 fi
943 # add
944 if [ "$1" = "-a" ]; then
945 ACTION="add"
9c909460
AM
946 shift
947 fi
703b6deb
ER
948
949 OPTIONS="$ACTION "
950
ce40491a
ER
951 if [ -n "$CVSROOT" ]; then
952 OPTIONS="-d $CVSROOT $OPTIONS"
953 fi
b1c6aace 954
703b6deb
ER
955 if [ "$ACTION" != "add" ]; then
956 if [ -z "$CVSDATE" -a -z "$CVSTAG" ]; then
957 OPTIONS="$OPTIONS -A"
958 else
959 if [ -n "$CVSDATE" ]; then
960 OPTIONS="$OPTIONS -D $CVSDATE"
961 fi
962 if [ -n "$CVSTAG" ]; then
963 # FIXME: cvs add actually works with -r ?
964 OPTIONS="$OPTIONS -r $CVSTAG"
965 fi
ce40491a
ER
966 fi
967 fi
e7fa3b9f 968
ce40491a
ER
969 local result=1
970 local retries_counter=0
971 if [ $# = 1 ]; then
972 update_shell_title "cvsup: $*"
973 else
974 update_shell_title "cvsup: $# files"
975 fi
976 while [ "$result" != "0" -a "$retries_counter" -le "$CVS_RETRIES" ]; do
977 retries_counter=$(( $retries_counter + 1 ))
939f2c12 978 output=$(LC_ALL=C $CVS_COMMAND $OPTIONS "$@" 2>&1)
ce40491a
ER
979 result=$?
980 [ -n "$output" ] && echo "$output"
703b6deb
ER
981 if echo "$output" | grep -qE "(Cannot connect to|connect to .* failed|Connection reset by peer|Connection timed out|Unknown host)" \
982 && [ "$result" -ne "0" -a "$retries_counter" -le "$CVS_RETRIES" ]; then
ce40491a
ER
983 echo "Trying again [$*]... ($retries_counter)"
984 update_shell_title "cvsup: retry #$retries_counter"
985 sleep 2
986 continue
987 else
988 break
989 fi
990 done
991 update_shell_title "cvsup: done!"
992 return $result
e7fa3b9f
ER
993}
994
71f8e908 995# returns true if "$1" is ftp, http or https protocol url
f6711e2a 996is_url() {
71f8e908
ER
997 case "$1" in
998 ftp://*|http://*|https://*)
999 return 0
1000 ;;
1001 esac
1002 return 1
1003}
1004
f6711e2a 1005update_md5() {
ecba6ad5
ER
1006 if [ $# -eq 0 ]; then
1007 return
1008 fi
1009
1010 update_shell_title "update md5"
1011 if [ -n "$DEBUG" ]; then
518ff1fb
ER
1012 set -x
1013 set -v
ecba6ad5
ER
1014 fi
1015
9c909460 1016 cd "$PACKAGE_DIR"
ecba6ad5
ER
1017
1018 # pass 1: check files to be fetched
1019 local todo
1020 local need_files
1021 for i in "$@"; do
1022 local fp=$(nourl "$i")
1023 local srcno=$(src_no "$i")
6392b5fd
ER
1024 if [ -n "$ADD5" ]; then
1025 [ "$fp" = "$i" ] && continue # FIXME what is this check doing?
647e3d7f 1026 grep -qiE '^#[ ]*'$srcno'-md5[ ]*:' $PACKAGE_DIR/$SPECFILE && continue
9c909460 1027 grep -qiE '^BuildRequires:[ ]*digest[(]%SOURCE'$srcno'[)][ ]*=' $PACKAGE_DIR/$SPECFILE && continue
6392b5fd 1028 else
647e3d7f 1029 grep -qiE '^#[ ]*'$srcno'-md5[ ]*:' $PACKAGE_DIR/$SPECFILE || grep -qiE '^BuildRequires:[ ]*digest[(]%SOURCE'$srcno'[)][ ]*=' $PACKAGE_DIR/$SPECFILE || continue
ecba6ad5
ER
1030 fi
1031 if [ ! -f "$fp" ] || [ $ALWAYS_CVSUP = "yes" ]; then
1032 need_files="$need_files $i"
1033 fi
1034 done
1035
1036 # pass 1a: get needed files
1037 if [ "$need_files" ]; then
1038 get_files $need_files
1039 fi
1040
1041 # pass 2: proceed with md5 adding or updating
1042 for i in "$@"; do
1043 local fp=$(nourl "$i")
1044 local srcno=$(src_no "$i")
647e3d7f 1045 local md5=$(grep -iE '^#[ ]*(No)?'$srcno'-md5[ ]*:' $PACKAGE_DIR/$SPECFILE )
e19ad799 1046 if [ -z "$md5" ]; then
9c909460 1047 md5=$(grep -iE '^[ ]*BuildRequires:[ ]*digest[(]%SOURCE'$srcno'[)][ ]*=' $PACKAGE_DIR/$SPECFILE )
6c1b2e7d 1048 fi
71f8e908 1049 if [ -n "$ADD5" ] && is_url $i || [ -n "$md5" ]; then
647e3d7f 1050 local tag="# $srcno-md5:\t"
71f8e908 1051 if [[ "$md5" == *NoSource* ]]; then
6c1b2e7d
PZ
1052 tag="# NoSource$srcno-md5:\t"
1053 elif [ -n "$USEDIGEST" ]; then
1054 tag="BuildRequires:\tdigest(%SOURCE$srcno) = "
71f8e908 1055 fi
ecba6ad5 1056 md5=$(md5sum "$fp" | cut -f1 -d' ')
647e3d7f 1057 echo "Updating $srcno ($md5: $fp)."
ecba6ad5 1058 perl -i -ne '
647e3d7f
KK
1059 print unless (/^\s*#\s*(No)?'$srcno'-md5\s*:/i or /^\s*BuildRequires:\s*digest\(%SOURCE'$srcno'\)/i);
1060 print "'"$tag$md5"'\n" if /^'$srcno'\s*:\s+/i;
ecba6ad5 1061 ' \
9c909460 1062 $PACKAGE_DIR/$SPECFILE
ecba6ad5
ER
1063 fi
1064 done
1065}
1066
f6711e2a 1067check_md5() {
60499e54 1068 local bad
3fbff8b8
ER
1069 [ "$NO5" = "yes" ] && return
1070
ecba6ad5
ER
1071 update_shell_title "check md5"
1072
1073 for i in "$@"; do
60499e54
AM
1074 bad=0
1075 if ! good_md5 "$i"; then
1076 echo -n "MD5 sum mismatch."
1077 bad=1
1078 fi
1079 if ! good_size "$i"; then
1080 echo -n "0 sized file."
1081 bad=1
ecba6ad5
ER
1082 fi
1083
60499e54
AM
1084 if [ $bad -eq 1 ]; then
1085 echo " Use -U to refetch sources,"
1086 echo "or -5 to update md5 sums, if you're sure files are correct."
1087 Exit_error err_no_source_in_repo $i
1088 fi
ecba6ad5
ER
1089 done
1090}
1091
f6711e2a 1092get_files() {
7cb24972 1093 update_shell_title "get_files"
cd445739
AM
1094
1095 if [ -n "$DEBUG" ]; then
518ff1fb
ER
1096 set -x
1097 set -v
cd445739
AM
1098 fi
1099
f09c0772 1100 if [ $# -gt 0 ]; then
9c909460 1101 cd "$PACKAGE_DIR"
cd445739 1102
b1c6aace 1103 if [ ! -s CVS/Root -a "$NOCVS" != "yes" ]; then
049ab214 1104 echo "Warning: No CVS access defined for SOURCES"
b1c6aace
ER
1105 NOCVS="yes"
1106 fi
1107
f2e42a41 1108 local nc=0
e7fa3b9f 1109 local get_files_cvs=""
3815b69a 1110 for i in "$@"; do
f2e42a41 1111 nc=$((nc + 1))
dcebdffb 1112 local cvsup=0
3815b69a 1113 SHELL_TITLE_PREFIX="get_files[$nc/$#]"
f2e42a41 1114 update_shell_title "$i"
f09c0772 1115 local fp=`nourl "$i"`
ecba6ad5 1116 if [ "$SKIP_EXISTING_FILES" = "yes" ] && [ -f "$fp" ]; then
ce40491a 1117 continue
18e5347d 1118 fi
ecba6ad5 1119
cd445739 1120 FROM_DISTFILES=0
e7e0d4ec 1121 local srcmd5=$(src_md5 "$i")
ecba6ad5
ER
1122
1123 # we know if source/patch is present in cvs/distfiles
1124 # - has md5 (in distfiles)
1125 # - in cvs... ideas?
1126
1127 # CHECK: local file didn't exist or always cvs up (first) requested.
f09c0772 1128 if [ ! -f "$fp" ] || [ $ALWAYS_CVSUP = "yes" ]; then
cd445739
AM
1129 if echo $i | grep -vE '(http|ftp|https|cvs|svn)://' | grep -qE '\.(gz|bz2)$']; then
1130 echo "Warning: no URL given for $i"
1131 fi
08908161 1132 target="$fp"
cd445739 1133
e7e0d4ec 1134 if [ -z "$NODIST" ] && [ -n "$srcmd5" ]; then
a4b50627 1135 if good_md5 "$i" && good_size "$i"; then
e7e0d4ec 1136 echo "$fp having proper md5sum already exists"
cd445739
AM
1137 continue
1138 fi
28b34560 1139
bc10f694 1140 # optionally prefer mirror over distfiles if there's mirror
28b34560 1141 # TODO: build url list and then try each url from the list
bc10f694 1142 if [ -n "$PREFMIRRORS" ] && [ -z "$NOMIRRORS" ] && im=$(find_mirror "$i") && [ "$im" != "$i" ]; then
28b34560
ER
1143 url="$im"
1144 else
1145 url=$(distfiles_url "$i")
1146 fi
1147
cd445739
AM
1148 url_attic=$(distfiles_attic_url "$i")
1149 FROM_DISTFILES=1
ecba6ad5
ER
1150 # is $url local file?
1151 if [[ "$url" = [./]* ]]; then
18a52930 1152 update_shell_title "${GETLOCAL%% *}: $url"
cd445739
AM
1153 ${GETLOCAL} $url $target
1154 else
1155 if [ -z "$NOMIRRORS" ]; then
9e9f8920 1156 url=$(find_mirror "$url")
cd445739 1157 fi
9e9f8920
ER
1158
1159 local uri=${url}
1160 # make shorter message for distfiles urls
1161 if [[ "$uri" = ${PROTOCOL}${DISTFILES_SERVER}* ]] || [[ "$uri" = ${PROTOCOL}${ATTICDISTFILES_SERVER}* ]]; then
1162 uri=${uri#${PROTOCOL}${DISTFILES_SERVER}/distfiles/by-md5/?/?/*/}
1163 uri=${uri#${PROTOCOL}${ATTICDISTFILES_SERVER}/distfiles/by-md5/?/?/*/}
1164 uri="df: $uri"
1165 fi
1166 update_shell_title "${GETURI%% *}: $uri"
ca2e6c31 1167 ${GETURI} ${OUTFILEOPT} "$target" "$url" || \
33da77c4 1168 if [ "`echo $url | grep -E 'ftp://'`" ]; then
18a52930 1169 update_shell_title "${GETURI2%% *}: $url"
ca2e6c31 1170 ${GETURI2} ${OUTFILEOPT} "$target" "$url"
cd445739
AM
1171 fi
1172 fi
ecba6ad5
ER
1173
1174 # is it empty file?
1175 if [ ! -s "$target" ]; then
cd445739
AM
1176 rm -f "$target"
1177 if [ `echo $url_attic | grep -E '^(\.|/)'` ]; then
18a52930 1178 update_shell_title "${GETLOCAL%% *}: $url_attic"
cd445739
AM
1179 ${GETLOCAL} $url_attic $target
1180 else
1181 if [ -z "$NOMIRRORS" ]; then
a8c9a155 1182 url_attic=$(find_mirror "$url_attic")
cd445739 1183 fi
18a52930 1184 update_shell_title "${GETURI%% *}: $url_attic"
ca2e6c31 1185 ${GETURI} ${OUTFILEOPT} "$target" "$url_attic" || \
33da77c4 1186 if [ "`echo $url_attic | grep -E 'ftp://'`" ]; then
ce40491a 1187 update_shell_title "${GETURI2%% *}: $url_attic"
ca2e6c31 1188 ${GETURI2} ${OUTFILEOPT} "$target" "$url_attic"
cd445739 1189 fi
1cd7fc57 1190 test -s "$target" || rm -f "$target"
cd445739
AM
1191 fi
1192 fi
ecba6ad5
ER
1193
1194 if [ -s "$target" ]; then
947025e5 1195 cvsignore_df $target
1196 else
cd445739
AM
1197 rm -f "$target"
1198 FROM_DISTFILES=0
1199 fi
e7e0d4ec 1200 elif [ "$NOCVS" != "yes" -a -z "$srcmd5" ]; then
d05e8080
ER
1201 if [ $# -gt 1 ]; then
1202 get_files_cvs="$get_files_cvs $fp"
1203 update_shell_title "$fp (will cvs up later)"
1204 cvsup=1
1205 else
1206 cvsup $fp
1207 fi
cd445739
AM
1208 fi
1209
f09c0772 1210 if [ -z "$NOURLS" ] && [ ! -f "$fp" -o -n "$UPDATE" ] && [ "`echo $i | grep -E 'ftp://|http://|https://'`" ]; then
cd445739 1211 if [ -z "$NOMIRRORS" ]; then
a8c9a155 1212 im=$(find_mirror "$i")
cd445739
AM
1213 else
1214 im="$i"
1215 fi
ce40491a 1216 update_shell_title "${GETURI%% *}: $im"
08908161 1217 ${GETURI} "$im" ${OUTFILEOPT} "$target" || \
33da77c4 1218 if [ "`echo $im | grep -E 'ftp://'`" ]; then
ce40491a 1219 update_shell_title "${GETURI2%% *}: $im"
08908161 1220 ${GETURI2} "$im" ${OUTFILEOPT} "$target"
7e365483 1221 fi
1cd7fc57 1222 test -s "$target" || rm -f "$target"
cd445739
AM
1223 fi
1224
dcebdffb
ER
1225 if [ "$cvsup" = 1 ]; then
1226 continue
1227 fi
1228
cd445739 1229 fi
ecba6ad5
ER
1230
1231 # the md5 check must be moved elsewhere as if we've called from update_md5 the md5 is wrong.
f09c0772 1232 if [ ! -f "$fp" -a "$FAIL_IF_NO_SOURCES" != "no" ]; then
cdbd30e8 1233 if [ -n "GREEDSRC" ]; then
1234 get_greed_sources $i
1235 else
1236 Exit_error err_no_source_in_repo $i
1237 fi
cd445739 1238 fi
2a5f078d 1239
ecba6ad5 1240 # we check md5 here just only to refetch immediately
a4b50627 1241 if good_md5 "$i" && good_size "$i"; then
cd445739
AM
1242 :
1243 elif [ "$FROM_DISTFILES" = 1 ]; then
deccc50e 1244 # wrong md5 from distfiles: remove the file and try again
cd445739 1245 # but only once ...
deccc50e 1246 echo "MD5 sum mismatch. Trying full fetch."
cd445739
AM
1247 FROM_DISTFILES=2
1248 rm -f $target
18a52930 1249 update_shell_title "${GETURI%% *}: $url"
ca2e6c31 1250 ${GETURI} ${OUTFILEOPT} "$target" "$url" || \
33da77c4 1251 if [ "`echo $url | grep -E 'ftp://'`" ]; then
ce40491a 1252 update_shell_title "${GETURI2%% *}: $url"
ca2e6c31 1253 ${GETURI2} ${OUTFILEOPT} "$target" "$url"
cd445739 1254 fi
ecba6ad5 1255 if [ ! -s "$target" ]; then
cd445739 1256 rm -f "$target"
18a52930 1257 update_shell_title "${GETURI%% *}: $url_attic"
ca2e6c31 1258 ${GETURI} ${OUTFILEOPT} "$target" "$url_attic" || \
33da77c4 1259 if [ "`echo $url_attic | grep -E 'ftp://'`" ]; then
ce40491a 1260 update_shell_title "${GETURI2%% *}: $url_attic"
ca2e6c31 1261 ${GETURI2} ${OUTFILEOPT} "$target" "$url_attic"
cd445739
AM
1262 fi
1263 fi
1264 test -s "$target" || rm -f "$target"
1265 fi
cd445739 1266 done
e7fa3b9f
ER
1267 SHELL_TITLE_PREFIX=""
1268
1269 if [ "$get_files_cvs" ]; then
ce40491a 1270 cvsup $get_files_cvs
e7fa3b9f 1271 fi
cd445739
AM
1272
1273 if [ "$CHMOD" = "yes" ]; then
a8c9a155 1274 CHMOD_FILES=$(nourl "$@")
cd445739
AM
1275 if [ -n "$CHMOD_FILES" ]; then
1276 chmod $CHMOD_MODE $CHMOD_FILES
1277 fi
1278 fi
cd445739
AM
1279 fi
1280}
1281
1282make_tagver() {
ce40491a 1283 if [ -n "$DEBUG" ]; then
518ff1fb
ER
1284 set -x
1285 set -v
ce40491a 1286 fi
78eab2a1 1287
ce40491a
ER
1288 # Check whether first character of PACKAGE_NAME is legal for tag name
1289 if [ -z "${PACKAGE_NAME##[_0-9]*}" -a -z "$TAG_PREFIX" ]; then
1290 TAG_PREFIX=tag_
1291 fi
d712cc12
ER
1292
1293 # NOTE: CVS tags may must not contain the characters `$,.:;@'
392187d9 1294 TAGVER=$(echo $TAG_PREFIX$PACKAGE_NAME-$PACKAGE_VERSION-$PACKAGE_RELEASE | tr '[.@]' '[_#]')
d712cc12 1295
ce40491a
ER
1296 # Remove #kernel.version_release from TAGVER because tagging sources
1297 # could occur with different kernel-headers than kernel-headers used at build time.
d712cc12
ER
1298 # besides, %{_kernel_ver_str} is not expanded.
1299
1300 # TAGVER=auto-ac-madwifi-ng-0-0_20070225_1#%{_kernel_ver_str}
1301 # TAGVER=auto-ac-madwifi-ng-0-0_20070225_1
1302
1303 TAGVER=${TAGVER%#*}
ce40491a 1304 echo -n "$TAGVER"
cd445739
AM
1305}
1306
c713aac3
AG
1307# bool is_tag_a_branch(tag)
1308#
1309# returns 1 if a tag is a branch set on SPECFILE
1310is_tag_a_branch() {
1311 if [ -n "$DEBUG" ]; then
1312 set -x
1313 set -v
1314 fi
1315
1316 if [ $# -ne 1 ]; then
1317 return 0;
1318 fi
1319
1320 TAG=$1
607e2d9d
AM
1321 # escape some regexp characters if part of TAG
1322 TAG=$(echo "$TAG" | sed -e 's#\([\+\*\.]\)#\\\1#g')
1323
f5141bbc 1324
9c909460 1325 cd "$PACKAGE_DIR"
939f2c12 1326 $CVS_COMMAND status -v $SPECFILE | grep -Eiq "${TAG}.+(branch: [0-9.]+)"
c713aac3
AG
1327 return $?
1328}
1329
f6711e2a 1330tag_files() {
cd445739
AM
1331 TAG_FILES="$@"
1332
1333 if [ -n "$DEBUG" ]; then
518ff1fb
ER
1334 set -x
1335 set -v
cd445739
AM
1336 fi
1337
73ef63d8
ER
1338 echo "Version: $PACKAGE_VERSION"
1339 echo "Release: $PACKAGE_RELEASE"
cd445739 1340
a18a9761 1341 local TAGVER
73ef63d8 1342 if [ "$TAG_VERSION" = "yes" ]; then
a18a9761 1343 TAGVER=`make_tagver`
73ef63d8
ER
1344 echo "CVS tag: $TAGVER"
1345 fi
1346 if [ -n "$TAG" ]; then
1347 echo "CVS tag: $TAG"
1348 fi
cd445739 1349
39dcc7de 1350 local OPTIONS="tag $CVS_FORCE"
73ef63d8
ER
1351 if [ -n "$CVSROOT" ]; then
1352 OPTIONS="-d $CVSROOT $OPTIONS"
1353 fi
cd445739 1354
c713aac3
AG
1355 # if a tagname we are about to set already exists
1356 # and happens to be a branch (common case with AC-branch)
1357 # pass -B (allows -F to disturb branch tag)
1358 local _tag=$TAG
1359 if [ "$TAG_VERSION" = "yes" ]; then
1360 _tag=$TAGVER
1361 fi;
1362 is_tag_a_branch $_tag
28c23a72 1363 if [ $? -eq 0 -a $CVS_NSERVER -eq 0 ]; then
c713aac3
AG
1364 OPTIONS="$OPTIONS -B"
1365 fi;
1366
9c909460 1367 cd "$PACKAGE_DIR"
73ef63d8
ER
1368 local tag_files
1369 for i in $TAG_FILES; do
1370 # don't tag files stored on distfiles
1371 [ -n "`src_md5 $i`" ] && continue
1372 local fp=`nourl "$i"`
1373 if [ -f "$fp" ]; then
1374 tag_files="$tag_files $fp"
cdbd30e8 1375 elif [ -n "GREEDSRC" ]; then
1376 get_greed_sources $i
73ef63d8
ER
1377 else
1378 Exit_error err_no_source_in_repo $i
03db3356 1379 fi
73ef63d8 1380 done
03db3356 1381
73ef63d8 1382 if [ "$tag_files" ]; then
cd445739 1383 if [ "$TAG_VERSION" = "yes" ]; then
73ef63d8 1384 update_shell_title "tag sources: $TAGVER"
19ca62ef 1385 printf "Tagging %d files\n" $(echo $tag_files | wc -w)
939f2c12 1386 $CVS_COMMAND $OPTIONS $TAGVER $tag_files || exit
cd445739
AM
1387 fi
1388 if [ -n "$TAG" ]; then
73ef63d8 1389 update_shell_title "tag sources: $TAG"
c4ff623e
ER
1390
1391 while [ "$tag_files" ]; do
1392 local chunk=$(echo $tag_files | tr ' ' '\n' | head -n 100)
1393 printf "Tagging %d files\n" $(echo $chunk | wc -w)
939f2c12 1394 $CVS_COMMAND $OPTIONS $TAG $chunk || exit
c4ff623e
ER
1395 tag_files=$(echo $tag_files | tr ' ' '\n' | tail +101)
1396 done
cd445739 1397 fi
73ef63d8
ER
1398 fi
1399
9c909460 1400 cd "$PACKAGE_DIR"
73ef63d8
ER
1401 if [ "$TAG_VERSION" = "yes" ]; then
1402 update_shell_title "tag spec: $TAGVER"
939f2c12 1403 $CVS_COMMAND $OPTIONS $TAGVER $SPECFILE || exit
73ef63d8
ER
1404 fi
1405 if [ -n "$TAG" ]; then
1406 update_shell_title "tag spec: $TAG"
939f2c12 1407 $CVS_COMMAND $OPTIONS $TAG $SPECFILE || exit
73ef63d8 1408 fi
cd445739
AM
1409}
1410
f6711e2a 1411branch_files() {
cd445739
AM
1412 TAG=$1
1413 echo "CVS branch tag: $TAG"
518ff1fb 1414 shift
cd445739
AM
1415
1416 TAG_FILES="$@"
1417
1418 if [ -n "$DEBUG" ]; then
518ff1fb
ER
1419 set -x
1420 set -v
cd445739
AM
1421 fi
1422
78a5a506 1423 local OPTIONS="tag $CVS_FORCE -b"
ec6b5dfc
AG
1424
1425 # branch exists?
1426 is_tag_a_branch $TAG
1427 if [ $? -eq 1 ]; then
1428 OPTIONS="$OPTIONS -B"
1429 fi
1430
78a5a506
ER
1431 if [ -n "$CVSROOT" ]; then
1432 OPTIONS="-d $CVSROOT $OPTIONS"
1433 fi
9c909460 1434 cd "$PACKAGE_DIR"
78a5a506
ER
1435 local tag_files
1436 for i in $TAG_FILES; do
1437 local fp=`nourl "$i"`
1438 if [ -f "$fp" ]; then
1439 tag_files="$tag_files $fp"
cdbd30e8 1440 elif [ -n "GREEDSRC" ]; then
1441 get_greed_sources $i
78a5a506
ER
1442 else
1443 Exit_error err_no_source_in_repo $i
c10fa4fa 1444 fi
78a5a506
ER
1445 done
1446 if [ "$tag_files" ]; then
939f2c12 1447 $CVS_COMMAND $OPTIONS $TAG $tag_files || exit
78a5a506 1448 fi
c10fa4fa 1449
9c909460 1450 cd "$PACKAGE_DIR"
939f2c12 1451 $CVS_COMMAND $OPTIONS $TAG $SPECFILE || exit
cd445739
AM
1452}
1453
1454
3009b80d
ER
1455# this function should exit early if package can't be built for this arch
1456# this avoids unneccessary BR filling.
1457check_buildarch() {
1458 local out ret
13c3c336 1459 out=$(minirpm --short-circuit -bp --define "'prep exit 0'" --nodeps $SPECFILE 2>&1)
3009b80d
ER
1460 ret=$?
1461 if [ $ret -ne 0 ]; then
1462 echo >&2 "$out"
1463 exit $ret
1464 fi
1465}
1466
9d99a240
ER
1467# from relup.sh
1468set_release() {
1469 local specfile="$1"
1470 local rel="$2"
1471 local newrel="$3"
1472 sed -i -e "
1473 s/^\(%define[ \t]\+_\?rel[ \t]\+\)$rel\$/\1$newrel/
1474 s/^\(Release:[ \t]\+\)$rel\$/\1$newrel/
1475 " $specfile
1476}
1477
1478set_version() {
1479 local specfile="$1"
bc8502e6
ER
1480 local ver="$2" subver=$ver
1481 local newver="$3" newsubver=$newver
1482
1483 # try handling subver, everything that's not numeric-dotted in version
1484 if grep -Eq '%define\s+subver' $specfile; then
1485 subver=$(echo "$ver" | sed -re 's,^[0-9.]+,,')
1486 ver=${ver%$subver}
1487 newsubver=$(echo "$newver" | sed -re 's,^[0-9.]+,,')
1488 newver=${newver%$newsubver}
1489 fi
9d99a240
ER
1490 sed -i -e "
1491 s/^\(%define[ \t]\+_\?ver[ \t]\+\)$ver\$/\1$newver/
bc8502e6 1492 s/^\(%define[ \t]\+subver[ \t]\+\)$subver\$/\1$newsubver/
9d99a240
ER
1493 s/^\(Version:[ \t]\+\)$ver\$/\1$newver/
1494 " $specfile
1495}
1496
cacef28c 1497try_upgrade() {
cd445739 1498 if [ -n "$TRY_UPGRADE" ]; then
9d99a240 1499 local TNOTIFY TNEWVER TOLDVER
ce40491a 1500 update_shell_title "build_package: try_upgrade"
9d99a240 1501
cacef28c 1502 cd "$PACKAGE_DIR"
1503
0907f1eb
ER
1504 if [ "$UPGRADE_VERSION" ]; then
1505 TNEWVER=$UPGRADE_VERSION
cd445739 1506 else
0907f1eb
ER
1507 if [ -n "$FLOAT_VERSION" ]; then
1508 TNOTIFY=$($APPDIR/pldnotify.awk ${BE_VERBOSE:+-vDEBUG=1} $SPECFILE -n) || exit 1
1509 else
1510 TNOTIFY=$($APPDIR/pldnotify.awk ${BE_VERBOSE:+-vDEBUG=1} $SPECFILE) || exit 1
1511 fi
cd445739 1512
b9bef5ce
ER
1513 # pldnotify.awk does not set exit codes, but it has match for ERROR
1514 # in output which means so.
1515 if [[ "$TNOTIFY" = *ERROR* ]]; then
1516 echo >&2 "$TNOTIFY"
1517 exit 1
1518 fi
1519
0907f1eb
ER
1520 TNEWVER=$(echo $TNOTIFY | awk '{ match($4,/\[NEW\]/); print $5 }')
1521 fi
cd445739
AM
1522
1523 if [ -n "$TNEWVER" ]; then
1524 TOLDVER=`echo $TNOTIFY | awk '{ print $3; }'`
9d99a240 1525 echo "New version found, updating spec file from $TOLDVER to version $TNEWVER"
257af81b
ER
1526 if [ "$REVERT_BROKEN_UPGRADE" = "yes" ]; then
1527 cp -f $SPECFILE $SPECFILE.bak
1528 fi
cd445739 1529 chmod +w $SPECFILE
cacef28c 1530 set_release $SPECFILE $PACKAGE_RELEASE 1
9d99a240 1531 set_version $SPECFILE $PACKAGE_VERSION $TNEWVER
518ff1fb 1532 parse_spec
6decb793
ER
1533 if [ "$PACKAGE_VERSION" != "$TNEWVER" ]; then
1534 echo >&2 "Upgrading version failed, you need to update spec yourself"
1535 exit 1
1536 fi
cacef28c 1537 return 1
cd445739
AM
1538 fi
1539 fi
cacef28c 1540 return 0
1541}
1542
1543build_package() {
1544 update_shell_title "build_package"
1545 if [ -n "$DEBUG" ]; then
1546 set -x
1547 set -v
1548 fi
1549
9c909460 1550 cd "$PACKAGE_DIR"
2a5f078d 1551
cd445739
AM
1552 case "$COMMAND" in
1553 build )
1554 BUILD_SWITCH="-ba" ;;
1555 build-binary )
1556 BUILD_SWITCH="-bb" ;;
1557 build-source )
1558 BUILD_SWITCH="-bs --nodeps" ;;
1559 build-prep )
1560 BUILD_SWITCH="-bp --nodeps" ;;
6594293d
AG
1561 build-build )
1562 BUILD_SWITCH="-bc" ;;
1563 build-install )
1564 BUILD_SWITCH="-bi" ;;
1565 build-list )
1566 BUILD_SWITCH="-bl" ;;
1567
cd445739 1568 esac
c890b916 1569
2e5be021 1570 update_shell_title "build_package: $COMMAND"
cd445739
AM
1571 if [ -n "$LOGFILE" ]; then
1572 LOG=`eval echo $LOGFILE`
1573 if [ -d "$LOG" ]; then
1574 echo "Log file $LOG is a directory."
1575 echo "Parse error in the spec?"
518ff1fb 1576 Exit_error err_build_fail
cd445739
AM
1577 fi
1578 if [ -n "$LASTLOG_FILE" ]; then
1579 echo "LASTLOG=$LOG" > $LASTLOG_FILE
1580 fi
390d081d 1581 RES_FILE=$(tempfile)
525bf240 1582
9c909460 1583 (time eval ${NICE_COMMAND} $RPMBUILD $TARGET_SWITCH $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $RPMBUILDOPTS $BCOND --define \'_specdir $PACKAGE_DIR\' --define \'_sourcedir $PACKAGE_DIR\' $SPECFILE; echo $? > $RES_FILE) 2>&1 |tee $LOG
cd445739
AM
1584 RETVAL=`cat $RES_FILE`
1585 rm $RES_FILE
1586 if [ -n "$LOGDIROK" ] && [ -n "$LOGDIRFAIL" ]; then
1587 if [ "$RETVAL" -eq "0" ]; then
1588 mv $LOG $LOGDIROK
1589 else
1590 mv $LOG $LOGDIRFAIL
1591 fi
1592 fi
8ba5cdda 1593 else
9c909460 1594 eval ${NICE_COMMAND} $RPMBUILD $TARGET_SWITCH $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $RPMBUILDOPTS $BCOND --define \'_specdir $PACKAGE_DIR\' --define \'_sourcedir $PACKAGE_DIR\' $SPECFILE
cd445739 1595 RETVAL=$?
8ba5cdda 1596 fi
cd445739
AM
1597 if [ "$RETVAL" -ne "0" ]; then
1598 if [ -n "$TRY_UPGRADE" ]; then
cacef28c 1599 echo "\nUpgrade package to new version failed."
257af81b 1600 if [ "$REVERT_BROKEN_UPGRADE" = "yes" ]; then
cacef28c 1601 echo "Restoring old spec file."
257af81b
ER
1602 mv -f $SPECFILE.bak $SPECFILE
1603 fi
cacef28c 1604 echo ""
cd445739 1605 fi
518ff1fb 1606 Exit_error err_build_fail
cd445739
AM
1607 fi
1608 unset BUILD_SWITCH
1609}
1610
f6711e2a 1611nourl() {
cd445739
AM
1612 echo "$@" | sed 's#\<\(ftp\|http\|https\|cvs\|svn\)://[^ ]*/##g'
1613}
1614
f6711e2a 1615install_required_packages() {
cd445739
AM
1616 run_poldek -vi $1
1617 return $?
1618}
1619
f6711e2a 1620find_spec_bcond() { # originally from /usr/lib/rpm/find-spec-bcond
fa4bf1df 1621 local SPEC="$1"
fa4bf1df
ER
1622 awk -F"\n" '
1623 /^%changelog/ { exit }
fa4bf1df
ER
1624 /^%bcond_with/{
1625 match($0, /bcond_with(out)?[ \t]+[_a-zA-Z0-9]+/);
8ff97f39
ER
1626 bcond = substr($0, RSTART + 6, RLENGTH - 6);
1627 gsub(/[ \t]+/, "_", bcond);
fa4bf1df
ER
1628 print bcond
1629 }' $SPEC | LC_ALL=C sort -u
1630}
1631
117d9861 1632process_bcondrc() {
301cbfd4
ER
1633 # expand bconds from ~/.bcondrc
1634 # The file structure is like gentoo's package.use:
1635 # ---
1636 # * -selinux
1637 # samba -mysql -pgsql
47a8df12 1638 # w32codec-installer license_agreement
301cbfd4
ER
1639 # php +mysqli
1640 # ---
fa4bf1df 1641 if ([ -f $HOME/.bcondrc ] || ([ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ])); then
117d9861
ER
1642 :
1643 else
1644 return
1645 fi
301cbfd4 1646
117d9861 1647 SN=${SPECFILE%%\.spec}
c8a3e2ac 1648
117d9861
ER
1649 local bcondrc=$HOME/.bcondrc
1650 [ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ] && bcondrc=$HOME_ETC/.bcondrc
301cbfd4 1651
117d9861 1652 local bcond_avail=$(find_spec_bcond $SPECFILE)
301cbfd4 1653
117d9861
ER
1654 while read pkg flags; do
1655 # ignore comments
1656 [[ "$pkg" == \#* ]] && continue
1657
1658 # any package or current package?
1659 if [ "$pkg" = "*" ] || [ "$pkg" = "$PACKAGE_NAME" ] || [ "$pkg" = "$SN" ]; then
1660 for flag in $flags; do
1661 local opt=${flag#[+-]}
1662
1663 # use only flags which are in this package.
1664 if [[ $bcond_avail = *${opt}* ]]; then
1665 if [[ $flag = -* ]]; then
1666 if [[ $BCOND != *--with?${opt}* ]]; then
1667 BCOND="$BCOND --without $opt"
1668 fi
1669 else
1670 if [[ $BCOND != *--without?${opt}* ]]; then
1671 BCOND="$BCOND --with $opt"
301cbfd4
ER
1672 fi
1673 fi
117d9861
ER
1674 fi
1675 done
1676 fi
1677 done < $bcondrc
1678 update_shell_title "parse ~/.bcondrc: DONE!"
1679}
1680
8ff97f39 1681set_bconds_values() {
117d9861
ER
1682 update_shell_title "set bcond values"
1683
1684 AVAIL_BCONDS_WITHOUT=""
1685 AVAIL_BCONDS_WITH=""
8ff97f39 1686
5a28189c 1687 if grep -Eq '^# *_with' ${SPECFILE}; then
8ff97f39
ER
1688 echo >&2 "ERROR: This spec has old style bconds."
1689 exit 1
1690 fi
1691
1692 if ! grep -q '^%bcond' ${SPECFILE}; then
117d9861 1693 return
301cbfd4
ER
1694 fi
1695
117d9861
ER
1696 local bcond_avail=$(find_spec_bcond $SPECFILE)
1697 process_bcondrc "$SPECFILE"
1698
fa4bf1df 1699 update_shell_title "parse bconds"
2a5f078d 1700
8ff97f39
ER
1701 local opt bcond
1702 for opt in $bcond_avail; do
1703 case "$opt" in
1704 without_*)
1705 bcond=${opt#without_}
10d92f71
ER
1706 case "$BCOND" in
1707 *--without?${bcond}*)
8ff97f39 1708 AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT <$bcond>"
10d92f71
ER
1709 ;;
1710 *)
8ff97f39 1711 AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT $bcond"
10d92f71
ER
1712 ;;
1713 esac
cd445739 1714 ;;
8ff97f39
ER
1715 with_*)
1716 bcond=${opt#with_}
10d92f71
ER
1717 case "$BCOND" in
1718 *--with?${bcond}*)
8ff97f39 1719 AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH <$bcond>"
10d92f71
ER
1720 ;;
1721 *)
8ff97f39 1722 AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH $bcond"
10d92f71
ER
1723 ;;
1724 esac
cd445739 1725 ;;
8ff97f39
ER
1726 *)
1727 echo >&2 "ERROR: unexpected '$opt' in set_bconds_values"
1728 exit 1
1729 ;;
1730 esac
1731 done
cd445739
AM
1732}
1733
f6711e2a 1734run_sub_builder() {
cd445739 1735 package_name="${1}"
af6b168f 1736 update_shell_title "run_sub_builder $package_name"
cd445739
AM
1737 #
1738