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