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