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