]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - builder.sh
Push new and changed tags upstream
[packages/rpm-build-tools.git] / builder.sh
1 #!/bin/ksh
2 #
3 # This program is free software, distributed under the terms of
4 # the GNU General Public License Version 2.
5 #
6 # -----------
7 # Exit codes:
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)
20 #   110 - Functions not yet implemented
21
22 # Notes (todo/bugs):
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)
25 # - does not respect NoSource: X, and tries to cvs up such files [ example: VirtualBox-bin.spec and its Source0 ]
26 # TODO:
27 # - ability to do ./builder -bb foo.spec foo2.spec foo3.spec
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
31 # - builder --help:
32 #       basename: missing operand
33 #       Try `basename --help' for more information.
34 #       -- and the normal usage info --
35
36 PROGRAM=${0##*/}
37 APPDIR=$(d=$0; [ -L "$d" ] && d=$(readlink -f "$d"); dirname "$d")
38 RCSID='$Id: builder,v 1.645 2011/02/13 17:54:10 glen Exp $' r=${RCSID#* * } rev=${r%% *}
39 VERSION="v0.35/$rev"
40 VERSIONSTRING="\
41 Build package utility from PLD Linux Packages repository
42 $VERSION (C) 1999-2011 Free Penguins".
43
44 PATH="/bin:/usr/bin:/usr/sbin:/sbin:/usr/X11R6/bin"
45
46 # required rpm-build-macros
47 RPM_MACROS_VER=1.534
48
49 COMMAND="build"
50 TARGET=""
51
52 SPECFILE=""
53 BE_VERBOSE=""
54 QUIET=""
55 CLEAN=""
56 DEBUG=""
57 NOURLS=""
58 NOCVSSPEC=""
59 NODIST=""
60 NOINIT=""
61 PREFMIRRORS=""
62 UPDATE=""
63 ADD5=""
64 NO5=""
65 ALWAYS_CVSUP=${ALWAYS_CVSUP:-"yes"}
66
67 # use rpm 4.4.6+ digest format instead of comments if non-zero
68 USEDIGEST=
69
70 # user agent when fetching files
71 USER_AGENT="PLD/Builder($VERSION)"
72
73 # It can be used i.e. in log file naming.
74 # See LOGFILE example.
75 DATE=`date +%Y-%m-%d_%H-%M-%S`
76
77 # Example: LOGFILE='../log.$PACKAGE_NAME'
78 # Example: LOGFILE='../LOGS/log.$PACKAGE_NAME.$DATE'
79 # Example: LOGFILE='$PACKAGE_NAME/$PACKAGE_NAME.$DATE.log'
80 # Example: LOGFILE='$PACKAGE_NAME.$DATE.log'
81 # Yes, you can use variable name! Note _single_ quotes!
82 LOGFILE=''
83
84 LOGDIR=""
85 LOGDIROK=""
86 LOGDIRFAIL=""
87 LASTLOG_FILE=""
88
89 CHMOD="no"
90 CHMOD_MODE="0644"
91 RPMOPTS=""
92 RPMBUILDOPTS=""
93 BCOND=""
94 GROUP_BCONDS="no"
95
96 # create symlinks for tools in PACKAGE_DIR, see get_spec()
97 SYMLINK_TOOLS="no"
98
99 PATCHES=""
100 SOURCES=""
101 ICONS=""
102 PACKAGE_RELEASE=""
103 PACKAGE_VERSION=""
104 PACKAGE_NAME=""
105 ASSUMED_NAME=""
106 PROTOCOL="http"
107 WGET_RETRIES=${MAX_WGET_RETRIES:-0}
108
109 CVS_FORCE=""
110 CVSIGNORE_DF="yes"
111 CVSTAG=""
112 GIT_SERVER="git://github.com/draenog"
113 HEAD_DETACHED=""
114 DEPTH=""
115 REMOTE_PLD="origin"
116
117 RES_FILE=""
118
119 DISTFILES_SERVER="://distfiles.pld-linux.org"
120 ATTICDISTFILES_SERVER="://attic-distfiles.pld-linux.org"
121
122 DEF_NICE_LEVEL=19
123 SCHEDTOOL="auto"
124
125 FAIL_IF_NO_SOURCES="yes"
126
127 # let get_files skip over files which are present to get those damn files fetched
128 SKIP_EXISTING_FILES="no"
129
130 TRY_UPGRADE=""
131 # should the specfile be restored if upgrade failed?
132 REVERT_BROKEN_UPGRADE="yes"
133
134 if rpm --specsrpm 2>/dev/null; then
135         FETCH_BUILD_REQUIRES_RPMSPECSRPM="yes"
136         FETCH_BUILD_REQUIRES_RPMGETDEPS="no"
137 else
138         FETCH_BUILD_REQUIRES_RPMSPECSRPM="no"
139         if [ -x /usr/bin/rpm-getdeps ]; then
140                 FETCH_BUILD_REQUIRES_RPMGETDEPS="yes"
141         else
142                 FETCH_BUILD_REQUIRES_RPMGETDEPS="no"
143         fi
144 fi
145
146
147 # Here we load saved user environment used to
148 # predefine options set above, or passed to builder
149 # in command line.
150 # This one reads global system environment settings:
151 if [ -f ~/etc/builderrc ]; then
152         . ~/etc/builderrc
153 fi
154 # And this one cascades settings using user personal
155 # builder settings.
156 # Example of ~/.builderrc:
157 #
158 #UPDATE_POLDEK_INDEXES="yes"
159 #FETCH_BUILD_REQUIRES="yes"
160 #REMOVE_BUILD_REQUIRES="force"
161 #GROUP_BCONDS="yes"
162 #LOGFILE='../LOGS/log.$PACKAGE_NAME.$DATE'
163 #TITLECHANGE=no
164 #
165 SU_SUDO=""
166 if [ -n "$HOME_ETC" ]; then
167         USER_CFG="$HOME_ETC/.builderrc"
168         BUILDER_MACROS="$HOME_ETC/.builder-rpmmacros"
169 else
170         USER_CFG=~/.builderrc
171         BUILDER_MACROS=~/.builder-rpmmacros
172 fi
173
174 [ -f "$USER_CFG" ] && . "$USER_CFG"
175
176 if [ -n "$GIT_LOGINNAME" ]; then
177         GIT_SERVER="git@github.com:${GIT_LOGINNAME}"
178 fi
179
180 if [ "$SCHEDTOOL" = "auto" ]; then
181         if [ -x /usr/bin/schedtool ] && schedtool -B -e echo >/dev/null; then
182                 SCHEDTOOL="schedtool -B -e"
183         else
184                 SCHEDTOOL="no"
185         fi
186 fi
187
188 if [ -n "$USE_PROZILLA" ]; then
189         GETURI="proz --no-getch -r -P ./ -t$WGET_RETRIES $PROZILLA_OPTS"
190         GETURI2="$GETURI"
191         OUTFILEOPT="-O"
192 elif [ -n "$USE_AXEL" ]; then
193         GETURI="axel -a $AXEL_OPTS"
194         GETURI2="$GETURI"
195         OUTFILEOPT="-o"
196 else
197         wget --help 2>&1 | grep -q -- ' --no-check-certificate ' && WGET_OPTS="$WGET_OPTS --no-check-certificate"
198         wget --help 2>&1 | grep -q -- ' --inet ' && WGET_OPTS="$WGET_OPTS --inet"
199         wget --help 2>&1 | grep -q -- ' --retry-connrefused ' && WGET_OPTS="$WGET_OPTS --retry-connrefused"
200         WGET_OPTS="$WGET_OPTS --user-agent=$USER_AGENT"
201
202         GETURI="wget --passive-ftp -c -nd -t$WGET_RETRIES $WGET_OPTS"
203         GETURI2="wget -c -nd -t$WGET_RETRIES $WGET_OPTS"
204         OUTFILEOPT="-O"
205 fi
206
207 GETLOCAL="cp -a"
208
209 if (rpm --version 2>&1 | grep -q '4.0.[0-2]'); then
210         RPM="rpm"
211         RPMBUILD="rpm"
212 else
213         RPM="rpm"
214         RPMBUILD="rpmbuild"
215 fi
216
217 #
218 # sanity checks
219 #
220 if [ -d $HOME/rpm/SOURCES ]; then
221         echo "ERROR: ~/rpm/{SPECS,SOURCES} structure is obsolete" >&2
222         echo "ERROR: get rid of your ~/rpm/SOURCES" >&2
223         exit 1
224 fi
225
226 POLDEK_INDEX_DIR="$($RPM --eval %_rpmdir)/"
227 POLDEK_CMD="$SU_SUDO /usr/bin/poldek --noask"
228
229 run_poldek() {
230         RES_FILE=$(tempfile)
231         if [ -n "$LOGFILE" ]; then
232                 LOG=`eval echo $LOGFILE`
233                 if [ -n "$LASTLOG_FILE" ]; then
234                         echo "LASTLOG=$LOG" > $LASTLOG_FILE
235                 fi
236                 (${NICE_COMMAND} ${POLDEK_CMD} `while test $# -gt 0; do echo "$1 ";shift;done` ; echo $? > ${RES_FILE})|tee -a $LOG
237                 return $exit_pldk
238         else
239                 (${NICE_COMMAND} ${POLDEK_CMD} `while test $# -gt 0; do echo "$1 ";shift;done` ; echo $? > ${RES_FILE}) 1>&2 >/dev/null
240                 return `cat ${RES_FILE}`
241                 rm -rf ${RES_FILE}
242         fi
243 }
244
245 #---------------------------------------------
246 # functions
247
248 usage() {
249         if [ -n "$DEBUG" ]; then set -xv; fi
250         echo "\
251 Usage: builder [-D|--debug] [-V|--version] [--short-version]  [-a|--add_cvs] [-b|-ba|--build]
252 [-bb|--build-binary] [-bs|--build-source] [-bc] [-bi] [-bl] [-u|--try-upgrade]
253 [{-cf|--cvs-force}] [{-B|--branch} <branch>] [--depth <number>]
254 [-g|--get] [-h|--help] [--ftp] [--http] [{-l|--logtofile} <logfile>] [-m|--mr-proper]
255 [-q|--quiet] [--date <yyyy-mm-dd> [-r <tag>] [{-T|--tag <tag>]
256 [-Tvs|--tag-version-stable] [-Ts|--tag-stable] [-Tv|--tag-version]
257 [{-Tp|--tag-prefix} <prefix>] [{-tt|--test-tag}]
258 [-nu|--no-urls] [-v|--verbose] [--opts <rpm opts>] [--short-circuit]
259 [--show-bconds] [--with/--without <feature>] [--define <macro> <value>]
260 <package>[.spec][:tag]
261
262 -5, --update-md5    - update md5 comments in spec, implies -nd -ncs
263 -a5, --add-md5      - add md5 comments to URL sources, implies -nc -nd -ncs
264 -n5, --no-md5       - ignore md5 comments in spec
265 -D, --debug         - enable builder script debugging mode,
266 -debug              - produce rpm debug package (same as --opts -debug)
267 -V, --version       - output builder version string
268 --short-version     - output builder short version
269 -a, --add_cvs       - try add new package to PLD repo.
270 -b, -ba, --build    - get all files from PLD repo or HTTP/FTP and build package
271                       from <package>.spec,
272 -bb, --build-binary - get all files from PLD repo or HTTP/FTP and build binary
273                       only package from <package>.spec,
274 -bp, --build-prep   - execute the %prep phase of <package>.spec,
275 -bc                 - execute the %build phase of <package>.spec,
276 -bi                 - execute the %install phase of <package>.spec
277 -bl                                     - execute the %files phase of <package>.spec
278 -bs, --build-source - get all files from PLD repo or HTTP/FTP and only pack
279                       them into src.rpm,
280 --short-circuit     - short-circuit build
281 -B, --branch        - add branch
282 -c, --clean         - clean all temporarily created files (in BUILD\$RPM_BUILD_ROOT) after rpmbuild commands.
283                       may be used with building process.
284 -m, --mr-proper     - clean all temporarily created files (in BUILD, SOURCES,
285                                           SPECS and \$RPM_BUILD_ROOT). Doesn't run
286                                           any rpm building.
287 -cf, --cvs-force        - use -f when tagging
288 --define <macro> <value>
289                     - define a macro <macro> with value <value>,
290 --depth <number>
291                                         - make shallow fetch
292 --alt_kernel <kernel>
293                     - same as --define 'alt_kernel <kernel>'
294 --nodeps            - rpm won't check any dependences
295 -g, --get           - get <package>.spec and all related files from PLD repo
296                       or HTTP/FTP,
297 -h, --help          - this message,
298 -jN, -j N           - set %_smp_mflags to propagate concurrent jobs
299 --ftp, --http       - use ftp or http protocol to access distfiles server
300 -l <logfile>, --logtofile <logfile>
301                     - log all to file,
302 -ncs, --no-cvs-specs
303                     - don't pull from PLD repo
304 -nd, --no-distfiles - don't download from distfiles
305 -nm, --no-mirrors   - don't download from mirror, if source URL is given,
306 -nu, --no-urls      - don't try to download from FTP/HTTP location,
307 -ns, --no-srcs      - don't download Sources/Patches
308 -ns0, --no-source0  - don't download Source0
309 -nn, --no-net       - don't download anything from the net
310 -pm, --prefer-mirrors - prefer mirrors (if any) over distfiles for SOURCES
311 --no-init           - don't initialize builder paths (SPECS and SOURCES)
312 -ske,
313 --skip-existing-files - skip existing files in get_files
314 --opts <rpm opts>   - additional options for rpm
315 -q, --quiet         - be quiet,
316 --date yyyy-mm-dd   - build package using resources from specified date,
317 -r <tag>, --cvstag <ref>
318                     - build package using resources from specified branch/tag,
319 -A                  - build package using master branch as any sticky tags/branch/date being reset.
320 -R, --fetch-build-requires
321                     - fetch what is BuildRequired,
322 -RB, --remove-build-requires
323                     - remove all you fetched with -R or --fetch-build-requires
324                       remember, this option requires confirmation,
325 -FRB, --force-remove-build-requires
326                     - remove all you fetched with -R or --fetch-build-requires
327                       remember, this option works without confirmation,
328 -sd, --source-distfiles - list sources available from distfiles (intended for offline
329                       operations; does not work when Icon field is present
330                       but icon file is absent),
331 -sc, --source-cvs - list sources available from PLD repo
332 -sdp, --source-distfiles-paths - list sources available from distfiles -
333                       paths relative to distfiles directory (intended for offline
334                       operations; does not work when Icon field is present
335                       but icon file is absent),
336 -sf, --source-files - list sources - bare filenames (intended for offline
337                       operations; does not work when Icon field is present
338                       but icon file is absent),
339 -lsp, --source-paths - list sources - filenames with full local paths (intended for
340                       offline operations; does not work when Icon field is present
341                       but icon file is absent),
342 -su, --source-urls  - list urls - urls to sources and patches
343                       intended for copying urls with spec with lots of macros in urls
344 -T <tag> , --tag <tag>
345                     - add git tag <tag> for files,
346 -Tvs, --tag-version-stable
347                     - add git tags STABLE and NAME-VERSION-RELEASE for files,
348 -Ts, --tag-stable
349                     - add git tag STABLE for files,
350 -Tv, --tag-version
351                     - add git tag NAME-VERSION-RELEASE for files,
352 -Tp, --tag-prefix <prefix>
353                     - add <prefix> to NAME-VERSION-RELEASE tags,
354 -tt, --test-tag <prefix>
355                     - fail if tag is already present,
356 -ir, --integer-release-only
357                     - allow only integer and snapshot releases
358 -v, --verbose       - be verbose,
359 -u, --try-upgrade   - check version, and try to upgrade package
360 -un, --try-upgrade-with-float-version
361                     - as above, but allow float version
362                                         php-pear-Services_Digg/
363 --upgrade-version   - upgrade to specified version in try-upgrade
364 -U, --update        - refetch sources, don't use distfiles, and update md5 comments
365 -Upi, --update-poldek-indexes
366                     - refresh or make poldek package index files.
367 -sp, --skip-patch <patchnumber>
368                     - don't apply <patchnumber>. may be repeated.
369 -np, --nopatch <patchnumber>
370                     - abort instead of applying patch <patchnumber>
371 --show-bconds       - show available conditional builds, which can be used
372                     - with --with and/or --without switches.
373 --show-bcond-args   - show active bconds, from ~/.bcondrc. this is used by
374                       ./repackage.sh script. in other words, the output is
375                       parseable by scripts.
376 --show-avail-bconds - show available bconds
377 --with/--without <feature>
378                     - conditional build package depending on %_with_<feature>/
379                       %_without_<feature> macro switch.  You may now use
380                       --with feat1 feat2 feat3 --without feat4 feat5 --with feat6
381                       constructions. Set GROUP_BCONDS to yes to make use of it.
382 --target <platform>, --target=<platform>
383                      - build for platform <platform>.
384 --init-rpm-dir       - initialize ~/rpm directory structure
385 "
386 }
387
388 # create tempfile. as secure as possible
389 tempfile() {
390         mktemp -t builder.XXXXXX || ${TMPDIR:-/tmp}/builder.$RANDOM.$$
391 }
392
393 # inserts git log instead of %changelog
394 # outputs name of modified file created by tempfile
395 insert_gitlog() {
396         local SPECFILE=$1 specfile=$(tempfile) gitlog=$(tempfile) speclog=$(tempfile) 
397
398         # allow this being customized
399         local log_entries=$(rpm -E '%{?_buildchangelogtruncate}')
400
401         # rpm5.org/rpm.org do not parse any other date format than 'Wed Jan 1 1997'
402         # otherwise i'd use --date=iso here
403         # http://rpm5.org/cvs/fileview?f=rpm/build/parseChangelog.c&v=2.44.2.1
404         # http://rpm.org/gitweb?p=rpm.git;a=blob;f=build/parseChangelog.c#l31
405         # NOTE: changelog date is always in UTC for rpmbuild
406         # * 1265749244 +0000 Random Hacker <nikt@pld-linux.org> 9370900
407         git log -${log_entries:-20} --format=format:"* %ad %an <%ae> %h%n%s%n" --date=raw > $gitlog
408         gawk '/^\* /{printf("* %s %s\n", strftime("%a %b %d %Y", $2), substr($0, length($1)+length($2)+length($3)+4)); next}{print}' $gitlog > $speclog
409         sed '/^%changelog/,$d' $SPECFILE | sed -e "\${
410                         a%changelog
411                         r $speclog
412                 }
413         " > $specfile
414         rm -f $gitlog $speclog
415         echo $specfile
416 }
417
418 # change dependency to specname
419 # common changes:
420 # - perl(Package::Name) -> perl-Package-Name
421 depspecname() {
422         local package="$1"
423
424         package=$(echo "$package" | sed -e '/perl(.*)/{s,perl(\(.*\)),perl-\1,;s,::,-,g};' -e 's/-\(devel\|static\)$//' )
425         echo "$package"
426 }
427
428 update_shell_title() {
429         [ -t 1 ] || return
430         local len=${COLUMNS:-80}
431         local msg="$(echo "$*" | cut -c-$len)"
432
433         if [ -n "$BE_VERBOSE" ]; then
434                 echo >&2 "$(date +%s.%N) $*"
435         fi
436
437         if [ "x$TITLECHANGE" = "xyes" -o "x$TITLECHANGE" = "x" ]; then
438                 local pkg
439                 if [ -n "$PACKAGE_NAME" ]; then
440                         pkg=${PACKAGE_NAME}-${PACKAGE_VERSION}-${PACKAGE_RELEASE}
441                 else
442                         pkg=${SPECFILE}
443                 fi
444
445                 msg="$pkg: ${SHELL_TITLE_PREFIX:+$SHELL_TITLE_PREFIX }$msg"
446                 msg=$(echo $msg | tr -d '\n\r')
447                 case "$TERM" in
448                         cygwin|xterm*)
449                         echo >&2 -ne "\033]1;$msg\007\033]2;$msg\007"
450                 ;;
451                         screen*)
452                         echo >&2 -ne "\033]0;$msg\007"
453                 ;;
454                 esac
455         fi
456 }
457
458 # set TARGET from BuildArch: from SPECFILE
459 set_spec_target() {
460         if [ -n "$SPECFILE" ] && [ -z "$TARGET" ]; then
461                 tmp=$(awk '/^BuildArch:/ { print $NF; exit }' $ASSUMED_NAME/$SPECFILE)
462                 if [ "$tmp" ]; then
463                                 target_platform=$(rpm -E '%{_target_vendor}-%{_target_os}%{?_gnu}')
464                                 TARGET="$tmp"
465                                 case "$RPMBUILD" in
466                                 "rpmbuild")
467                                         TARGET_SWITCH="--target ${TARGET}-${target_platform}" ;;
468                                 "rpm")
469                                         TARGET_SWITCH="--target=$TARGET" ;;
470                                 esac
471                 fi
472         fi
473 }
474
475 # runs rpm with minimal macroset
476 minirpm() {
477         # we reset macros not to contain macros.build as all the %() macros are
478         # executed here, while none of them are actually needed.
479         # at the time of this writing macros.build + macros contained 70 "%(...)" macros.
480         safe_macrofiles=$(rpm $TARGET_SWITCH --showrc | awk -F: '/^macrofiles/ { gsub(/^macrofiles[ \t]+:/, "", $0); gsub(/:.*macros.build:/, ":", $0); print $0 } ')
481
482         # TODO: move these to /usr/lib/rpm/macros
483         cat > $BUILDER_MACROS <<'EOF'
484 %x8664 x86_64 amd64 ia32e
485 %alt_kernel %{nil}
486 %_alt_kernel %{nil}
487 %bootstrap_release() %{1}
488 %requires_releq_kernel_up(s:n:) %{nil}
489 %requires_releq_kernel_smp(s:n:) %{nil}
490 %requires_releq_kernel(s:n:) %{nil}
491 %requires_releq() %{nil}
492 %pyrequires_eq() %{nil}
493 %requires_eq() %{nil}
494 %requires_eq_to() %{nil}
495 %requires_ge() %{nil}
496 %requires_ge_to() %{nil}
497 %requires_ge() %{nil}
498 %releq_kernel_up(n:) ERROR
499 %releq_kernel_smp(n:) ERROR
500 %releq_kernel(n:) ERROR
501 %kgcc_package ERROR
502 %_fontsdir ERROR
503 %ruby_version ERROR
504 %ruby_ver_requires_eq() %{nil}
505 %ruby_mod_ver_requires_eq() %{nil}
506 %__php_api_requires() %{nil}
507 %php_major_version ERROR
508 %php_api_version ERROR
509 %requires_xorg_xserver_extension %{nil}
510 %requires_xorg_xserver_xinput %{nil}
511 %requires_xorg_xserver_font %{nil}
512 %requires_xorg_xserver_videodrv %{nil}
513 %py_ver ERROR
514 %perl_vendorarch ERROR
515 %perl_vendorlib ERROR
516 # damn. need it here! - copied from /usr/lib/rpm/macros.build
517 %tmpdir         %(echo "${TMPDIR:-/tmp}")
518 %patchset_source(f:b:) %(
519         base=%{-b*}%{!-b*:10000};
520         start=$(expr $base + %1);
521         end=$(expr $base + %{?2}%{!?2:%{1}});
522         # we need to call seq twice as it doesn't allow two formats
523         seq -f 'Patch%g:' $start $end > %{tmpdir}/__ps1;
524         seq -f '%{-f*}' %1 %{?2}%{!?2:%{1}} > %{tmpdir}/__ps2;
525         paste %{tmpdir}/__ps{1,2};
526         rm -f %{tmpdir}/__ps{1,2};
527 ) \
528 %{nil}
529 %add_etc_shells(p) %{p:<lua>}
530 %remove_etc_shells(p) %{p:<lua>}
531 %lua_add_etc_shells()  %{nil}
532 %lua_remove_etc_shells() %{nil}
533 %required_jdk %{nil}
534 %buildrequires_jdk %{nil}
535 %pear_package_print_optionalpackages %{nil}
536 EOF
537         if [ "$NOINIT" = "yes" ] ; then
538                 cat >> $BUILDER_MACROS <<'EOF'
539 %_specdir ./
540 %_sourcedir ./
541 EOF
542         fi
543         eval $RPMBUILD $TARGET_SWITCH --macros "$safe_macrofiles:$BUILDER_MACROS" $QUIET $RPMOPTS $RPMBUILDOPTS $BCOND $* 2>&1
544 }
545
546 cache_rpm_dump() {
547         if [ -n "$DEBUG" ]; then
548                 set -x
549                 set -v
550         fi
551
552         if [ -x /usr/bin/rpm-specdump ]; then
553                 update_shell_title "cache_rpm_dump using rpm-specdump command"
554                 rpm_dump_cache=$(rpm-specdump $TARGET_SWITCH $BCOND $SPECFILE)
555         else
556                 update_shell_title "cache_rpm_dump using rpmbuild command"
557                 local rpm_dump
558                 rpm_dump=`
559                         # what we need from dump is NAME, VERSION, RELEASE and PATCHES/SOURCES.
560                         dump='%{echo:dummy: PACKAGE_NAME %{name} }%dump'
561                         case "$RPMBUILD" in
562                         rpm)
563                                 ARGS='-bp'
564                                 ;;
565                         rpmbuild)
566                                 ARGS='--nodigest --nosignature --nobuild'
567                                 ;;
568                         esac
569                         minirpm $ARGS --define "'prep $dump'" --nodeps $SPECFILE
570                 `
571                 if [ $? -gt 0 ]; then
572                         error=$(echo "$rpm_dump" | sed -ne '/^error:/,$p')
573                         echo "$error" >&2
574                         Exit_error err_build_fail
575                 fi
576
577                 # make small dump cache
578                 rpm_dump_cache=`echo "$rpm_dump" | awk '
579                         $2 ~ /^SOURCEURL/ {print}
580                         $2 ~ /^PATCHURL/  {print}
581                         $2 ~ /^nosource/ {print}
582                         $2 ~ /^PACKAGE_/ {print}
583                 '`
584         fi
585
586         update_shell_title "cache_rpm_dump: OK!"
587 }
588
589 rpm_dump() {
590         if [ -z "$rpm_dump_cache" ] ; then
591                 echo >&2 "internal error: cache_rpm_dump not called! (missing %prep?)"
592         fi
593         echo "$rpm_dump_cache"
594 }
595
596 get_icons() {
597         update_shell_title "get icons"
598         ICONS=$(awk '/^Icon:/ {print $2}' $PACKAGE_DIR/${SPECFILE})
599         if [ -z "$ICONS" ]; then
600                 return
601         fi
602
603         rpm_dump_cache="kalasaba" NODIST="yes" get_files $ICONS
604 }
605
606 parse_spec() {
607         update_shell_title "parsing specfile"
608         if [ -n "$DEBUG" ]; then
609                 set -x
610                 set -v
611         fi
612
613         # icons are needed for successful spec parse
614         get_icons
615
616         cd $PACKAGE_DIR
617         cache_rpm_dump
618
619         if (rpm_dump | grep -qEi ":.*nosource.*1"); then
620                 FAIL_IF_NO_SOURCES="no"
621         fi
622
623         if [ "$NOSRCS" != "yes" ]; then
624                 SOURCES=$(rpm_dump | awk '$2 ~ /^SOURCEURL[0-9]+/ {print substr($2, length("SOURCEURL") + 1), $3}' | LC_ALL=C sort -n | awk '{print $2}')
625                 PATCHES=$(rpm_dump | awk '$2 ~ /^PATCHURL[0-9]+/ {print substr($2, length("PATCHURL") + 1), $3}' | LC_ALL=C sort -n | awk '{print $2}')
626                 ICONS=$(awk '/^Icon:/ {print $2}' ${SPECFILE})
627         fi
628
629         PACKAGE_NAME=$(rpm_dump | awk '$2 == "PACKAGE_NAME" { print $3; exit}')
630         PACKAGE_VERSION=$(rpm_dump | awk '$2 == "PACKAGE_VERSION" { print $3; exit}')
631         PACKAGE_RELEASE=$(rpm_dump | awk '$2 == "PACKAGE_RELEASE" { print $3; exit}')
632
633         if [ "$PACKAGE_NAME" != "$ASSUMED_NAME" ]; then
634                 echo >&2 "WARNING! Spec name ($ASSUMED_NAME) does not agree with package name ($PACKAGE_NAME)"
635         fi
636
637         if [ -n "$BE_VERBOSE" ]; then
638                 echo "- Sources :  `nourl $SOURCES`"
639                 if [ -n "$PATCHES" ]; then
640                         echo "- Patches :  `nourl $PATCHES`"
641                 else
642                         echo "- Patches :  *no patches needed*"
643                 fi
644                 if [ -n "$ICONS" ]; then
645                         echo "- Icon    :  `nourl $ICONS`"
646                 else
647                         echo "- Icon    :  *no package icon*"
648                 fi
649                 echo "- Name    : $PACKAGE_NAME"
650                 echo "- Version : $PACKAGE_VERSION"
651                 echo "- Release : $PACKAGE_RELEASE"
652         fi
653
654         update_shell_title "parse_spec: OK!"
655 }
656
657 Exit_error() {
658         if [ -n "$DEBUG" ]; then
659                 set -x
660                 set -v
661         fi
662
663         cd "$__PWD"
664
665         case "$1" in
666                 "err_no_spec_in_cmdl" )
667                         remove_build_requires
668                         echo >&2 "ERROR: spec file name not specified."
669                         exit 2 ;;
670                 "err_invalid_cmdline" )
671                         echo >&2 "ERROR: invalid command line arg ($2)."
672                         exit 2 ;;
673                 "err_no_spec_in_repo" )
674                         remove_build_requires
675                         echo >&2 "Error: spec file not stored in PLD repo."
676                         exit 3 ;;
677                 "err_no_source_in_repo" )
678                         remove_build_requires
679                         echo >&2 "Error: some source, patch or icon files not stored in PLD repo. ($2)"
680                         exit 4 ;;
681                 "err_cvs_add_failed" )
682                         echo >&2 "Error: failed to add package to PLD repo."
683                         exit 4 ;;
684                 "err_build_fail" )
685                         remove_build_requires
686                         echo >&2 "Error: package build failed. (${2:-no more info})"
687                         exit 5 ;;
688                 "err_no_package_data" )
689                         remove_build_requires
690                         echo >&2 "Error: couldn't get out package name/version/release from spec file."
691                         exit 6 ;;
692                 "err_tag_exists" )
693                         remove_build_requires
694                         echo >&2 "Tag ${2} already exists (spec release: ${3})."
695                         exit 9 ;;
696                 "err_fract_rel" )
697                         remove_build_requires
698                         echo >&2 "Release ${2} not integer and not a snapshot."
699                         exit 10 ;;
700                 "err_branch_exists" )
701                         remove_build_requires
702                         echo >&2 "Tree branch already exists (${2})."
703                         exit 11 ;;
704                 "err_acl_deny" )
705                         remove_build_requires
706                         echo >&2 "Error: conditions reject building this spec (${2})."
707                         exit 12 ;;
708                 "err_not_implemented" )
709                         remove_build_requires
710                         echo >&2 "Error: functionality not yet imlemented"
711                         exit 110 ;;
712         esac
713         echo >&2 "Unknown error."
714         exit 100
715 }
716
717 init_builder() {
718         if [ -n "$DEBUG" ]; then
719                 set -x
720                 set -v
721         fi
722
723         if [ "$NOINIT" != "yes" ] ; then
724                 TOP_DIR=$(eval $RPM $RPMOPTS --eval '%{_topdir}')
725
726                 local macros_ver=$(rpm -E %?rpm_build_macros)
727                 if [ -z "$macros_ver" ]; then
728                         REPO_DIR=$TOP_DIR/packages
729                         PACKAGE_DIR=$TOP_DIR/packages/$ASSUMED_NAME
730                 else
731                         if awk "BEGIN{exit($macros_ver>=$RPM_MACROS_VER)}"; then
732                                 echo >&2 "builder requires rpm-build-macros >= $RPM_MACROS_VER"
733                                 exit 1
734                         fi
735                         REPO_DIR=$TOP_DIR
736                         PACKAGE_DIR=$REPO_DIR/$ASSUMED_NAME
737                 fi
738         else
739                 REPO_DIR="."
740                 PACKAGE_DIR="."
741         fi
742         export GIT_WORK_TREE=$PACKAGE_DIR
743         export GIT_DIR=$PACKAGE_DIR/.git
744
745         __PWD=$(pwd)
746 }
747
748 get_spec() {
749
750         update_shell_title "get_spec"
751
752         if [ -n "$DEBUG" ]; then
753                 set -x
754                 set -v
755         fi
756
757         cd "$REPO_DIR"
758         if [ ! -f "$ASSUMED_NAME/$SPECFILE" ]; then
759                 # XXX: still needed?
760                 SPECFILE=$(basename $SPECFILE)
761         fi
762
763         if [ "$NOCVSSPEC" != "yes" ]; then
764                 if [ -z "$DEPTH" ]; then
765                         if [ -d "$ASSUMED_NAME/.git" ]; then
766                                 git fetch $REMOTE_PLD || Exit_error err_no_spec_in_repo
767                         elif [ "$ADD_PACKAGE_CVS" = "yes" ]; then
768                                 if [ ! -r "$ASSUMED_NAME/$SPECFILE" ]; then
769                                         echo "ERROR: No package to add ($ASSUMED_NAME/$SPECFILE)" >&2
770                                         exit 101
771                                 fi
772                                 Exit_error err_not_implemented
773                         else
774                                 (
775                                         unset GIT_WORK_TREE
776                                         git clone  -o $REMOTE_PLD ${GIT_SERVER}/${ASSUMED_NAME}.git || {
777                                                 # softfail if new package, i.e not yet added to PLD rep
778                                                 [ ! -f "$ASSUMED_NAME/$SPECFILE" ] && Exit_error err_no_spec_in_repo
779                                                 echo "Warning: package not in CVS - assuming new package"
780                                                 NOCVSSPEC="yes"
781                                         }
782                                 )
783                         fi
784                 else
785                         if [ ! -d "$ASSUMED_NAME/.git" ]; then
786                                 if [ ! -d "$ASSUMED_NAME" ]; then
787                                         mkdir $ASSUMED_NAME
788                                 fi
789                                 git init
790                                 git remote add $REMOTE_PLD ${GIT_SERVER}/${ASSUMED_NAME}.git
791                                 CVSTAG=${CVSTAG:-"master"}
792                         fi
793                         git fetch "$DEPTH" $REMOTE_PLD ${CVSTAG}:remotes/${REMOTE_PLD}/${CVSTAG} || {
794                                 echo >&2 "Error: branch $CVSTAG does not exist"
795                                 exit 3
796                         }
797                 fi
798
799                 cvsignore_df .gitignore
800
801                 # add default log format to .gitignore if it is relative to package dir
802                 if [ -n "$LOGFILE" -a "$LOGFILE" = "${LOGFILE##*/}" ]; then
803                         # substitute known "macros" to glob
804                         local logfile=$(echo "$LOGFILE" | sed -e 's,\$\(PACKAGE_NAME\|DATE\),*,g')
805                         if [ "$logfile" ]; then
806                                 cvsignore_df "$logfile"
807                         fi
808                 fi
809
810                 # create symlinks for tools
811                 if [ "$SYMLINK_TOOLS" != "no" ]; then
812                         for a in dropin md5 adapter builder {relup,compile,repackage,rsync,pearize}.sh pldnotify.awk; do
813                                 [ -f $a ] || continue
814                                 ln -sf ../$a $ASSUMED_NAME
815                                 cvsignore_df $a
816                         done
817                 fi
818         fi
819
820         if [ -n "$CVSTAG" ]; then
821                 git checkout "$CVSTAG" -- 2>/dev/null || git checkout -t "${REMOTE_PLD}/$CVSTAG" > /dev/null || exit
822                 git symbolic-ref -q HEAD > /dev/null &&
823                         git merge '@{u}'
824                 if [ -n "$CVSDATE" ]; then
825                         git checkout $(git rev-list -n1 --before="'$CVSDATE'" $CVSTAG) || exit 1
826                 fi
827         fi
828
829         if [ ! -f "$ASSUMED_NAME/$SPECFILE" ]; then
830                 Exit_error err_no_spec_in_repo
831         fi
832
833         if [ "$CHMOD" = "yes" -a -n "$SPECFILE" ]; then
834                 chmod $CHMOD_MODE $ASSUMED_NAME/$SPECFILE
835         fi
836         unset OPTIONS
837         [ -n "$DONT_PRINT_REVISION" ] || grep -E -m 1 "^#.*Revision:.*Date" $ASSUMED_NAME/$SPECFILE
838
839         set_spec_target
840 }
841
842 find_mirror() {
843         cd "$REPO_DIR"
844         local url="$1"
845         if [ ! -f "mirrors"  ] ; then
846                 ln -s rpm-build-toos/mirrors .
847         fi
848
849         IFS="|"
850         local origin mirror name rest ol prefix
851         while read origin mirror name rest; do
852                 # skip comments and empty lines
853                 if [ -z "$origin" ] || [ "${origin#\#}" != "$origin" ]; then
854                         continue
855                 fi
856                 ol=$(echo -n "$origin" | wc -c)
857                 prefix=$(echo -n "$url" | head -c $ol)
858                 if [ "$prefix" = "$origin" ] ; then
859                         suffix=$(echo "$url" | cut -b $((ol+1))-)
860                         echo -n "$mirror$suffix"
861                         return 0
862                 fi
863         done < mirrors
864         echo "$url"
865 }
866
867 # Warning: unpredictable results if same URL used twice
868 src_no() {
869         cd $PACKAGE_DIR
870         rpm_dump | \
871         grep "SOURCEURL[0-9]*[  ]*$1""[         ]*$" | \
872         sed -e 's/.*SOURCEURL\([0-9][0-9]*\).*/\1/' | \
873         head -n 1 | xargs
874 }
875
876 src_md5() {
877         [ "$NO5" = "yes" ] && return
878         no=$(src_no "$1")
879         [ -z "$no" ] && return
880         cd $PACKAGE_DIR
881         local md5
882
883         if [ -f additional-md5sums ]; then
884                 md5=$(grep -s -v '^#' additional-md5sums | \
885                 grep -E "[      ]$(basename "$1")([     ,]|\$)" | \
886                 sed -e 's/^\([0-9a-f]\{32\}\).*/\1/' | \
887                 grep -E '^[0-9a-f]{32}$')
888
889                 if [ "$md5" ]; then
890                         if [ $(echo "$md5" | wc -l) != 1 ] ; then
891                                 echo "$SPECFILE: more then one entry in additional-md5sums for $1" 1>&2
892                         fi
893                         echo "$md5" | tail -n 1
894                         return
895                 fi
896         fi
897
898         source_md5=`grep -i "^#[        ]*Source$no-md5[        ]*:" $SPECFILE | sed -e 's/.*://'`
899         if [ -n "$source_md5" ]; then
900                 echo $source_md5
901         else
902                 source_md5=`grep -i "BuildRequires:[    ]*digest(%SOURCE$no)[   ]*=" $SPECFILE | sed -e 's/.*=//'`
903                 if [ -n "$source_md5" ]; then
904                         echo $source_md5
905                 else
906                         # we have empty SourceX-md5, but it is still possible
907                         # that we have NoSourceX-md5 AND NoSource: X
908                         nosource_md5=`grep -i "^#[       ]*NoSource$no-md5[      ]*:" $SPECFILE | sed -e 's/.*://'`
909                         if [ -n "$nosource_md5" -a -n "`grep -i "^NoSource:[     ]*$no$" $SPECFILE`" ] ; then
910                                 echo $nosource_md5
911                         fi
912                 fi
913         fi
914 }
915
916 distfiles_path() {
917         echo "by-md5/$(src_md5 "$1" | sed -e 's|^\(.\)\(.\)|\1/\2/&|')/$(basename "$1")"
918 }
919
920 distfiles_url() {
921         echo "$PROTOCOL$DISTFILES_SERVER/distfiles/$(distfiles_path "$1")"
922 }
923
924 distfiles_attic_url() {
925         echo "$PROTOCOL$ATTICDISTFILES_SERVER/distfiles/Attic/$(distfiles_path "$1")"
926 }
927
928 good_md5() {
929         md5=$(src_md5 "$1")
930         [ "$md5" = "" ] || \
931         [ "$md5" = "$(md5sum $(nourl "$1") 2> /dev/null | sed -e 's/ .*//')" ]
932 }
933
934 good_size() {
935         size=$(find $(nourl "$1") -printf "%s" 2>/dev/null)
936         [ -n "$size" -a "$size" -gt 0 ]
937 }
938
939 cvsignore_df() {
940         if [ "$CVSIGNORE_DF" != "yes" ]; then
941                 return
942         fi
943         cvsignore=${PACKAGE_DIR}/.gitignore
944
945         # add only if not yet there
946         if ! awk -vf="$1" -vc=1 '$0 == f { c = 0 } END { exit c }' $cvsignore 2>/dev/null; then
947                 echo "$1" >> $cvsignore
948         fi
949 }
950
951 # returns true if "$1" is ftp, http or https protocol url
952 is_url() {
953         case "$1" in
954         ftp://*|http://*|https://*)
955                 return 0
956         ;;
957         esac
958         return 1
959 }
960
961 update_md5() {
962         if [ $# -eq 0 ]; then
963                 return
964         fi
965
966         update_shell_title "update md5"
967         if [ -n "$DEBUG" ]; then
968                 set -x
969                 set -v
970         fi
971
972         cd "$PACKAGE_DIR"
973
974         # pass 1: check files to be fetched
975         local todo
976         local need_files
977         for i in "$@"; do
978                 local fp=$(nourl "$i")
979                 local srcno=$(src_no "$i")
980                 if [ -n "$ADD5" ]; then
981                         [ "$fp" = "$i" ] && continue # FIXME what is this check doing?
982                         grep -qiE '^#[  ]*Source'$srcno'-md5[   ]*:' $PACKAGE_DIR/$SPECFILE && continue
983                         grep -qiE '^BuildRequires:[     ]*digest[(]%SOURCE'$srcno'[)][  ]*=' $PACKAGE_DIR/$SPECFILE && continue
984                 else
985                         grep -qiE '^#[  ]*Source'$srcno'-md5[   ]*:' $PACKAGE_DIR/$SPECFILE || grep -qiE '^BuildRequires:[      ]*digest[(]%SOURCE'$srcno'[)][  ]*=' $PACKAGE_DIR/$SPECFILE || continue
986                 fi
987                 if [ ! -f "$fp" ] || [ $ALWAYS_CVSUP = "yes" ]; then
988                         need_files="$need_files $i"
989                 fi
990         done
991
992         # pass 1a: get needed files
993         if [ "$need_files" ]; then
994                 get_files $need_files
995         fi
996
997         # pass 2: proceed with md5 adding or updating
998         for i in "$@"; do
999                 local fp=$(nourl "$i")
1000                 local srcno=$(src_no "$i")
1001                 local md5=$(grep -iE '^#[       ]*(No)?Source'$srcno'-md5[      ]*:' $PACKAGE_DIR/$SPECFILE )
1002                 if [ -z "$md5" ]; then
1003                         md5=$(grep -iE '^[      ]*BuildRequires:[       ]*digest[(]%SOURCE'$srcno'[)][  ]*=' $PACKAGE_DIR/$SPECFILE )
1004                 fi
1005                 if [ -n "$ADD5" ] && is_url $i || [ -n "$md5" ]; then
1006                         local tag="# Source$srcno-md5:\t"
1007                         if [[ "$md5" == *NoSource* ]]; then
1008                                 tag="# NoSource$srcno-md5:\t"
1009                         elif [ -n "$USEDIGEST" ]; then
1010                                 tag="BuildRequires:\tdigest(%SOURCE$srcno) = "
1011                         fi
1012                         md5=$(md5sum "$fp" | cut -f1 -d' ')
1013                         echo "Updating Source$srcno ($md5: $fp)."
1014                         perl -i -ne '
1015                                 print unless (/^\s*#\s*(No)?Source'$srcno'-md5\s*:/i or /^\s*BuildRequires:\s*digest\(%SOURCE'$srcno'\)/i);
1016                                 print "'"$tag$md5"'\n" if /^Source'$srcno'\s*:\s+/;
1017                         ' \
1018                         $PACKAGE_DIR/$SPECFILE
1019                 fi
1020         done
1021 }
1022
1023 check_md5() {
1024         local bad
1025         [ "$NO5" = "yes" ] && return
1026
1027         update_shell_title "check md5"
1028
1029         for i in "$@"; do
1030                 bad=0
1031                 if ! good_md5 "$i"; then
1032                         echo -n "MD5 sum mismatch."
1033                         bad=1
1034                 fi
1035                 if ! good_size "$i"; then
1036                         echo -n "0 sized file."
1037                         bad=1
1038                 fi
1039
1040                 if [ $bad -eq 1 ]; then
1041                         echo " Use -U to refetch sources,"
1042                         echo "or -5 to update md5 sums, if you're sure files are correct."
1043                         Exit_error err_no_source_in_repo $i
1044                 fi
1045         done
1046 }
1047
1048 get_files() {
1049         update_shell_title "get_files"
1050
1051         if [ -n "$DEBUG" ]; then
1052                 set -x
1053                 set -v
1054         fi
1055
1056         if [ $# -gt 0 ]; then
1057                 cd "$PACKAGE_DIR"
1058
1059                 local nc=0
1060                 local get_files_cvs=""
1061                 for i in "$@"; do
1062                         nc=$((nc + 1))
1063                         local cvsup=0
1064                         SHELL_TITLE_PREFIX="get_files[$nc/$#]"
1065                         update_shell_title "$i"
1066                         local fp=`nourl "$i"`
1067                         if [ "$SKIP_EXISTING_FILES" = "yes" ] && [ -f "$fp" ]; then
1068                                 continue
1069                         fi
1070
1071                         FROM_DISTFILES=0
1072                         local srcmd5=$(src_md5 "$i")
1073
1074                         # we know if source/patch is present in cvs/distfiles
1075                         # - has md5 (in distfiles)
1076                         # - in cvs... ideas?
1077
1078                         # CHECK: local file didn't exist or always cvs up (first) requested.
1079                         if [ ! -f "$fp" ] || [ $ALWAYS_CVSUP = "yes" ]; then
1080                                 if echo $i | grep -vE '(http|ftp|https|cvs|svn)://' | grep -qE '\.(gz|bz2)$']; then
1081                                         echo "Warning: no URL given for $i"
1082                                 fi
1083                                 target="$fp"
1084
1085                                 if [ -z "$NODIST" ] && [ -n "$srcmd5" ]; then
1086                                         if good_md5 "$i" && good_size "$i"; then
1087                                                 echo "$fp having proper md5sum already exists"
1088                                                 continue
1089                                         fi
1090
1091                                         # optionally prefer mirror over distfiles if there's mirror
1092                                         # TODO: build url list and then try each url from the list
1093                                         if [ -n "$PREFMIRRORS" ] && [ -z "$NOMIRRORS" ] && im=$(find_mirror "$i") && [ "$im" != "$i" ]; then
1094                                                 url="$im"
1095                                         else
1096                                                 url=$(distfiles_url "$i")
1097                                         fi
1098
1099                                         url_attic=$(distfiles_attic_url "$i")
1100                                         FROM_DISTFILES=1
1101                                         # is $url local file?
1102                                         if [[ "$url" = [./]* ]]; then
1103                                                 update_shell_title "${GETLOCAL%% *}: $url"
1104                                                 ${GETLOCAL} $url $target
1105                                         else
1106                                                 if [ -z "$NOMIRRORS" ]; then
1107                                                         url=$(find_mirror "$url")
1108                                                 fi
1109
1110                                                 local uri=${url}
1111                                                 # make shorter message for distfiles urls
1112                                                 if [[ "$uri" = ${PROTOCOL}${DISTFILES_SERVER}* ]] || [[ "$uri" = ${PROTOCOL}${ATTICDISTFILES_SERVER}* ]]; then
1113                                                         uri=${uri#${PROTOCOL}${DISTFILES_SERVER}/distfiles/by-md5/?/?/*/}
1114                                                         uri=${uri#${PROTOCOL}${ATTICDISTFILES_SERVER}/distfiles/by-md5/?/?/*/}
1115                                                         uri="df: $uri"
1116                                                 fi
1117                                                 update_shell_title "${GETURI%% *}: $uri"
1118                                                 ${GETURI} ${OUTFILEOPT} "$target" "$url" || \
1119                                                 if [ "`echo $url | grep -E 'ftp://'`" ]; then
1120                                                         update_shell_title "${GETURI2%% *}: $url"
1121                                                         ${GETURI2} ${OUTFILEOPT} "$target" "$url"
1122                                                 fi
1123                                         fi
1124
1125                                         # is it empty file?
1126                                         if [ ! -s "$target" ]; then
1127                                                 rm -f "$target"
1128                                                 if [ `echo $url_attic | grep -E '^(\.|/)'` ]; then
1129                                                         update_shell_title "${GETLOCAL%% *}: $url_attic"
1130                                                         ${GETLOCAL} $url_attic $target
1131                                                 else
1132                                                         if [ -z "$NOMIRRORS" ]; then
1133                                                                 url_attic=$(find_mirror "$url_attic")
1134                                                         fi
1135                                                         update_shell_title "${GETURI%% *}: $url_attic"
1136                                                         ${GETURI} ${OUTFILEOPT} "$target" "$url_attic" || \
1137                                                         if [ "`echo $url_attic | grep -E 'ftp://'`" ]; then
1138                                                                 update_shell_title "${GETURI2%% *}: $url_attic"
1139                                                                 ${GETURI2} ${OUTFILEOPT} "$target" "$url_attic"
1140                                                         fi
1141                                                         test -s "$target" || rm -f "$target"
1142                                                 fi
1143                                         fi
1144
1145                                         if [ -s "$target" ]; then
1146                                                 cvsignore_df $target
1147                                         else
1148                                                 rm -f "$target"
1149                                                 FROM_DISTFILES=0
1150                                         fi
1151                                 fi
1152
1153                                 if [ -z "$NOURLS" ] && [ ! -f "$fp" -o -n "$UPDATE" ] && [ "`echo $i | grep -E 'ftp://|http://|https://'`" ]; then
1154                                         if [ -z "$NOMIRRORS" ]; then
1155                                                 im=$(find_mirror "$i")
1156                                         else
1157                                                 im="$i"
1158                                         fi
1159                                         update_shell_title "${GETURI%% *}: $im"
1160                                         ${GETURI} "$im" ${OUTFILEOPT} "$target" || \
1161                                         if [ "`echo $im | grep -E 'ftp://'`" ]; then
1162                                                 update_shell_title "${GETURI2%% *}: $im"
1163                                                 ${GETURI2} "$im" ${OUTFILEOPT} "$target"
1164                                         fi
1165                                         test -s "$target" || rm -f "$target"
1166                                 fi
1167
1168                                 if [ "$cvsup" = 1 ]; then
1169                                         continue
1170                                 fi
1171
1172                         fi
1173
1174                         # the md5 check must be moved elsewhere as if we've called from update_md5 the md5 is wrong.
1175                         if [ ! -f "$fp" -a "$FAIL_IF_NO_SOURCES" != "no" ]; then
1176                                 Exit_error err_no_source_in_repo $i
1177                         fi
1178
1179                         # we check md5 here just only to refetch immediately
1180                         if good_md5 "$i" && good_size "$i"; then
1181                                 :
1182                         elif [ "$FROM_DISTFILES" = 1 ]; then
1183                                 # wrong md5 from distfiles: remove the file and try again
1184                                 # but only once ...
1185                                 echo "MD5 sum mismatch. Trying full fetch."
1186                                 FROM_DISTFILES=2
1187                                 rm -f $target
1188                                 update_shell_title "${GETURI%% *}: $url"
1189                                 ${GETURI} ${OUTFILEOPT} "$target" "$url" || \
1190                                 if [ "`echo $url | grep -E 'ftp://'`" ]; then
1191                                         update_shell_title "${GETURI2%% *}: $url"
1192                                         ${GETURI2} ${OUTFILEOPT} "$target" "$url"
1193                                 fi
1194                                 if [ ! -s "$target" ]; then
1195                                         rm -f "$target"
1196                                         update_shell_title "${GETURI%% *}: $url_attic"
1197                                         ${GETURI} ${OUTFILEOPT} "$target" "$url_attic" || \
1198                                         if [ "`echo $url_attic | grep -E 'ftp://'`" ]; then
1199                                                 update_shell_title "${GETURI2%% *}: $url_attic"
1200                                                 ${GETURI2} ${OUTFILEOPT} "$target" "$url_attic"
1201                                         fi
1202                                 fi
1203                                 test -s "$target" || rm -f "$target"
1204                         fi
1205                 done
1206                 SHELL_TITLE_PREFIX=""
1207
1208
1209                 if [ "$CHMOD" = "yes" ]; then
1210                         CHMOD_FILES=$(nourl "$@")
1211                         if [ -n "$CHMOD_FILES" ]; then
1212                                 chmod $CHMOD_MODE $CHMOD_FILES
1213                         fi
1214                 fi
1215         fi
1216 }
1217
1218 make_tagver() {
1219         if [ -n "$DEBUG" ]; then
1220                 set -x
1221                 set -v
1222         fi
1223
1224         # Check whether first character of PACKAGE_NAME is legal for tag name
1225         if [ -z "${PACKAGE_NAME##[_0-9]*}" -a -z "$TAG_PREFIX" ]; then
1226                 TAG_PREFIX=tag_
1227         fi
1228
1229         # NOTE: CVS tags may must not contain the characters `$,.:;@'
1230         TAGVER=$(echo $TAG_PREFIX$PACKAGE_NAME-$PACKAGE_VERSION-$PACKAGE_RELEASE | tr '[.@]' '[_#]')
1231
1232         # Remove #kernel.version_release from TAGVER because tagging sources
1233         # could occur with different kernel-headers than kernel-headers used at build time.
1234         # besides, %{_kernel_ver_str} is not expanded.
1235
1236         # TAGVER=auto-ac-madwifi-ng-0-0_20070225_1#%{_kernel_ver_str}
1237         # TAGVER=auto-ac-madwifi-ng-0-0_20070225_1
1238
1239         TAGVER=${TAGVER%#*}
1240         echo -n "$TAGVER"
1241 }
1242
1243 tag_files() {
1244         TAG_FILES="$@"
1245
1246         if [ -n "$DEBUG" ]; then
1247                 set -x
1248                 set -v
1249         fi
1250
1251         echo "Version: $PACKAGE_VERSION"
1252         echo "Release: $PACKAGE_RELEASE"
1253
1254         local TAGVER
1255         if [ "$TAG_VERSION" = "yes" ]; then
1256                 TAGVER=`make_tagver`
1257                 echo "tag: $TAGVER"
1258         fi
1259         if [ -n "$TAG" ]; then
1260                 echo "tag: $TAG"
1261         fi
1262
1263         local OPTIONS="tag $CVS_FORCE"
1264
1265         local _tag=$TAG
1266         if [ "$TAG_VERSION" = "yes" ]; then
1267                 _tag=$TAGVER
1268         fi;
1269
1270         cd "$PACKAGE_DIR"
1271
1272         if [ "$TAG_VERSION" = "yes" ]; then
1273                 update_shell_title "tag sources: $TAGVER"
1274                 git $OPTIONS $TAGVER || exit
1275                 git push $CVS_FORCE $REMOTE_PLD tag $TAGVER
1276         fi
1277         if [ -n "$TAG" ]; then
1278                 update_shell_title "tag sources: $TAG"
1279                 git $OPTIONS $TAG $chunk || exit
1280                 git push $CVS_FORCE $REMOTE_PLD tag $TAG
1281         fi
1282 }
1283
1284 branch_files() {
1285         TAG=$1
1286         echo "Git branch: $TAG"
1287         shift
1288
1289         if [ -n "$DEBUG" ]; then
1290                 set -x
1291                 set -v
1292         fi
1293
1294         local OPTIONS="branch $CVS_FORCE"
1295
1296         cd "$PACKAGE_DIR"
1297         git $OPTIONS $TAG || exit
1298 }
1299
1300
1301 # this function should exit early if package can't be built for this arch
1302 # this avoids unneccessary BR filling.
1303 check_buildarch() {
1304         local out ret
1305         out=$(minirpm --short-circuit -bp --define "'prep exit 0'" --nodeps $SPECFILE 2>&1)
1306         ret=$?
1307         if [ $ret -ne 0 ]; then
1308                 echo >&2 "$out"
1309                 exit $ret
1310         fi
1311 }
1312
1313 # from relup.sh
1314 set_release() {
1315         local specfile="$1"
1316         local rel="$2"
1317         local newrel="$3"
1318         sed -i -e "
1319                 s/^\(%define[ \t]\+_\?rel[ \t]\+\)$rel\$/\1$newrel/
1320                 s/^\(Release:[ \t]\+\)$rel\$/\1$newrel/
1321         " $specfile
1322 }
1323
1324 set_version() {
1325         local specfile="$1"
1326         local ver="$2" subver=$ver
1327         local newver="$3" newsubver=$newver
1328
1329         # try handling subver, everything that's not numeric-dotted in version
1330         if grep -Eq '%define\s+subver' $specfile; then
1331                 subver=$(echo "$ver" | sed -re 's,^[0-9.]+,,')
1332                 ver=${ver%$subver}
1333                 newsubver=$(echo "$newver" | sed -re 's,^[0-9.]+,,')
1334                 newver=${newver%$newsubver}
1335         fi
1336         sed -i -e "
1337                 s/^\(%define[ \t]\+_\?ver[ \t]\+\)$ver\$/\1$newver/
1338                 s/^\(%define[ \t]\+subver[ \t]\+\)$subver\$/\1$newsubver/
1339                 s/^\(Version:[ \t]\+\)$ver\$/\1$newver/
1340         " $specfile
1341 }
1342
1343 try_upgrade() {
1344         if [ -n "$TRY_UPGRADE" ]; then
1345                 local TNOTIFY TNEWVER TOLDVER
1346                 update_shell_title "build_package: try_upgrade"
1347
1348                 cd "$PACKAGE_DIR"
1349                 
1350                 if [ "$UPGRADE_VERSION" ]; then
1351                         TNEWVER=$UPGRADE_VERSION
1352                 else
1353                         if [ -n "$FLOAT_VERSION" ]; then
1354                                 TNOTIFY=$($APPDIR/pldnotify.awk ${BE_VERBOSE:+-vDEBUG=1} $SPECFILE -n) || exit 1
1355                         else
1356                                 TNOTIFY=$($APPDIR/pldnotify.awk ${BE_VERBOSE:+-vDEBUG=1} $SPECFILE) || exit 1
1357                         fi
1358
1359                         # pldnotify.awk does not set exit codes, but it has match for ERROR
1360                         # in output which means so.
1361                         if [[ "$TNOTIFY" = *ERROR* ]]; then
1362                                 echo >&2 "$TNOTIFY"
1363                                 exit 1
1364                         fi
1365
1366                         TNEWVER=$(echo $TNOTIFY | awk '{ match($4,/\[NEW\]/); print $5 }')
1367                 fi
1368
1369                 if [ -n "$TNEWVER" ]; then
1370                         TOLDVER=`echo $TNOTIFY | awk '{ print $3; }'`
1371                         echo "New version found, updating spec file from $TOLDVER to version $TNEWVER"
1372                         if [ "$REVERT_BROKEN_UPGRADE" = "yes" ]; then
1373                                 cp -f $SPECFILE $SPECFILE.bak
1374                         fi
1375                         chmod +w $SPECFILE
1376                         set_release $SPECFILE $PACKAGE_RELEASE 1
1377                         set_version $SPECFILE $PACKAGE_VERSION $TNEWVER
1378                         parse_spec
1379                         if [ "$PACKAGE_VERSION" != "$TNEWVER" ]; then
1380                                 echo >&2 "Upgrading version failed, you need to update spec yourself"
1381                                 exit 1
1382                         fi
1383                         return 1
1384                 fi
1385         fi
1386         return 0
1387 }
1388
1389 build_package() {
1390         update_shell_title "build_package"
1391         if [ -n "$DEBUG" ]; then
1392                 set -x
1393                 set -v
1394         fi
1395
1396         cd "$PACKAGE_DIR"
1397
1398         case "$COMMAND" in
1399                 build )
1400                         BUILD_SWITCH="-ba" ;;
1401                 build-binary )
1402                         BUILD_SWITCH="-bb" ;;
1403                 build-source )
1404                         BUILD_SWITCH="-bs --nodeps" ;;
1405                 build-prep )
1406                         BUILD_SWITCH="-bp --nodeps" ;;
1407                 build-build )
1408                         BUILD_SWITCH="-bc" ;;
1409                 build-install )
1410                         BUILD_SWITCH="-bi" ;;
1411                 build-list )
1412                         BUILD_SWITCH="-bl" ;;
1413
1414         esac
1415
1416         update_shell_title "build_package: $COMMAND"
1417         if [ -n "$LOGFILE" ]; then
1418                 LOG=`eval echo $LOGFILE`
1419                 if [ -d "$LOG" ]; then
1420                         echo "Log file $LOG is a directory."
1421                         echo "Parse error in the spec?"
1422                         Exit_error err_build_fail
1423                 fi
1424                 if [ -n "$LASTLOG_FILE" ]; then
1425                         echo "LASTLOG=$LOG" > $LASTLOG_FILE
1426                 fi
1427                 RES_FILE=$(tempfile)
1428                 local specfile=$(insert_gitlog $SPECFILE)
1429
1430                 (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
1431                 RETVAL=`cat $RES_FILE`
1432                 rm $RES_FILE $specfile
1433                 if [ -n "$LOGDIROK" ] && [ -n "$LOGDIRFAIL" ]; then
1434                         if [ "$RETVAL" -eq "0" ]; then
1435                                 mv $LOG $LOGDIROK
1436                         else
1437                                 mv $LOG $LOGDIRFAIL
1438                         fi
1439                 fi
1440         else
1441                 eval ${NICE_COMMAND} $RPMBUILD $TARGET_SWITCH $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $RPMBUILDOPTS $BCOND --define \'_specdir $PACKAGE_DIR\' --define \'_sourcedir $PACKAGE_DIR\' $SPECFILE
1442                 RETVAL=$?
1443         fi
1444         if [ "$RETVAL" -ne "0" ]; then
1445                 if [ -n "$TRY_UPGRADE" ]; then
1446                         echo "\nUpgrade package to new version failed."
1447                         if [ "$REVERT_BROKEN_UPGRADE" = "yes" ]; then
1448                                 echo "Restoring old spec file."
1449                                 mv -f $SPECFILE.bak $SPECFILE
1450                         fi
1451                         echo ""
1452                 fi
1453                 Exit_error err_build_fail
1454         fi
1455         unset BUILD_SWITCH
1456 }
1457
1458 nourl() {
1459         echo "$@" | sed 's#\<\(ftp\|http\|https\|cvs\|svn\)://[^ ]*/##g'
1460 }
1461
1462 install_required_packages() {
1463         run_poldek -vi $1
1464         return $?
1465 }
1466
1467 find_spec_bcond() { # originally from /usr/lib/rpm/find-spec-bcond
1468         local SPEC="$1"
1469         awk -F"\n" '
1470         /^%changelog/ { exit }
1471         /^%bcond_with/{
1472                 match($0, /bcond_with(out)?[ \t]+[_a-zA-Z0-9]+/);
1473                 bcond = substr($0, RSTART + 6, RLENGTH - 6);
1474                 gsub(/[ \t]+/, "_", bcond);
1475                 print bcond
1476         }' $SPEC | LC_ALL=C sort -u
1477 }
1478
1479 process_bcondrc() {
1480         # expand bconds from ~/.bcondrc
1481         # The file structure is like gentoo's package.use:
1482         # ---
1483         # * -selinux
1484         # samba -mysql -pgsql
1485         # w32codec-installer license_agreement
1486         # php +mysqli
1487         # ---
1488         if ([ -f $HOME/.bcondrc ] || ([ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ])); then
1489                 :
1490         else
1491                 return
1492         fi
1493
1494         SN=${SPECFILE%%\.spec}
1495
1496         local bcondrc=$HOME/.bcondrc
1497         [ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ] && bcondrc=$HOME_ETC/.bcondrc
1498
1499         local bcond_avail=$(find_spec_bcond $SPECFILE)
1500
1501         while read pkg flags; do
1502                 # ignore comments
1503                 [[ "$pkg" == \#* ]] && continue
1504
1505                 # any package or current package?
1506                 if [ "$pkg" = "*" ] || [ "$pkg" = "$PACKAGE_NAME" ] || [ "$pkg" = "$SN" ]; then
1507                         for flag in $flags; do
1508                                 local opt=${flag#[+-]}
1509
1510                                 # use only flags which are in this package.
1511                                 if [[ $bcond_avail = *${opt}* ]]; then
1512                                         if [[ $flag = -* ]]; then
1513                                                 if [[ $BCOND != *--with?${opt}* ]]; then
1514                                                         BCOND="$BCOND --without $opt"
1515                                                 fi
1516                                         else
1517                                                 if [[ $BCOND != *--without?${opt}* ]]; then
1518                                                         BCOND="$BCOND --with $opt"
1519                                                 fi
1520                                         fi
1521                                 fi
1522                         done
1523                 fi
1524         done < $bcondrc
1525         update_shell_title "parse ~/.bcondrc: DONE!"
1526 }
1527
1528 set_bconds_values() {
1529         update_shell_title "set bcond values"
1530
1531         AVAIL_BCONDS_WITHOUT=""
1532         AVAIL_BCONDS_WITH=""
1533
1534         if grep -Eq '^# *_with' ${SPECFILE}; then
1535                 echo >&2 "ERROR: This spec has old style bconds."
1536                 exit 1
1537         fi
1538
1539         if ! grep -q '^%bcond' ${SPECFILE}; then
1540                 return
1541         fi
1542
1543         local bcond_avail=$(find_spec_bcond $SPECFILE)
1544         process_bcondrc "$SPECFILE"
1545
1546         update_shell_title "parse bconds"
1547
1548         local opt bcond
1549         for opt in $bcond_avail; do
1550                 case "$opt" in
1551                 without_*)
1552                         bcond=${opt#without_}
1553                         case "$BCOND" in
1554                         *--without?${bcond}*)
1555                                 AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT <$bcond>"
1556                                 ;;
1557                         *)
1558                                 AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT $bcond"
1559                                 ;;
1560                         esac
1561                         ;;
1562                 with_*)
1563                         bcond=${opt#with_}
1564                         case "$BCOND" in
1565                         *--with?${bcond}*)
1566                                 AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH <$bcond>"
1567                                 ;;
1568                         *)
1569                                 AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH $bcond"
1570                                 ;;
1571                         esac
1572                         ;;
1573                 *)
1574                         echo >&2 "ERROR: unexpected '$opt' in set_bconds_values"
1575                         exit 1
1576                         ;;
1577                 esac
1578         done
1579 }
1580
1581 run_sub_builder() {
1582         package_name="${1}"
1583         update_shell_title "run_sub_builder $package_name"
1584         #
1585         # No i tutaj bym chcia³ zrobiæ sztuczn± inteligencjê, która spróbuje tego
1586         # pakieta zbudowaæ. Aktualnie niewiele dziala, bo generalnie nie widze do
1587         # konca algorytmu... Ale damy rade. :) Na razie po prostu sie wyjebie tak samo
1588         # jakby nie bylo tego kawalka kodu.
1589         #
1590         # Update: Poprawi³em parê rzeczy i zaczê³o generowaæ pakiety spoza zadanej listy.
1591         #         Jednym s³owem budowanie niespoldkowanych zale¿no¶ci dzia³a w paru przypadkach.
1592         #
1593         #
1594         # y0shi.
1595         # kurwa. translate that ^^^^
1596
1597         parent_spec_name=''
1598
1599         # Istnieje taki spec? ${package}.spec
1600         if [ -f "${PACKAGE_DIR}/${package}.spec" ]; then
1601                 parent_spec_name=${package}.spec
1602         elif [ -f "${PACKAGE_DIR}/$(echo ${package_name} | sed -e s,-devel.*,,g -e s,-static,,g).spec" ]; then
1603                 parent_spec_name="$(echo ${package_name} | sed -e s,-devel.*,,g -e s,-static,,g).spec"
1604         else
1605                 for provides_line in $(grep -r ^Provides:.*$package ${PACKAGE_DIR}); do
1606                         echo $provides_line
1607                 done
1608         fi
1609
1610         if [ "${parent_spec_name}" != "" ]; then
1611                 spawn_sub_builder $parent_spec_name
1612         fi
1613         NOT_INSTALLED_PACKAGES="$NOT_INSTALLED_PACKAGES $package_name"
1614 }
1615
1616 spawn_sub_builder() {
1617         package_name="${1}"
1618         update_shell_title "spawn_sub_builder $package_name"
1619
1620         sub_builder_opts=''
1621         if [ "${FETCH_BUILD_REQUIRES}" = "yes" ]; then
1622                 sub_builder_opts="${sub_builder_opts} -R"
1623         fi
1624         if [ "${REMOVE_BUILD_REQUIRES}" = "nice" ]; then
1625                 sub_builder_opts="${sub_builder_opts} -RB"
1626         elif [ "${REMOVE_BUILD_REQUIRES}" = "force" ]; then
1627                 sub_builder_opts="${sub_builder_opts} -FRB"
1628         fi
1629         if [ "${UPDATE_POLDEK_INDEXES}" = "yes" ]; then
1630                 sub_builder_opts="${sub_builder_opts} -Upi"
1631         fi
1632
1633         cd "${PACKAGE_DIR}"
1634         ./builder ${sub_builder_opts} "$@"
1635 }
1636
1637 remove_build_requires() {
1638         if [ "$INSTALLED_PACKAGES" != "" ]; then
1639                 case "$REMOVE_BUILD_REQUIRES" in
1640                         "force")
1641                                 run_poldek --noask -ve $INSTALLED_PACKAGES
1642                                 ;;
1643                         "nice")
1644                                 run_poldek --ask -ve $INSTALLED_PACKAGES
1645                                 ;;
1646                         *)
1647                                 echo You may want to manually remove following BuildRequires fetched:
1648                                 echo $INSTALLED_PACKAGES
1649                                 echo "Try poldek -e \`cat $(pwd)/.${SPECFILE}_INSTALLED_PACKAGES\`"
1650                                 ;;
1651                 esac
1652         fi
1653 }
1654
1655 display_bconds() {
1656         if [ "$AVAIL_BCONDS_WITH" -o "$AVAIL_BCONDS_WITHOUT" ]; then
1657                 if [ "$BCOND" ]; then
1658                         echo ""
1659                         echo "Building $SPECFILE with the following conditional flags:"
1660                         echo -n "$BCOND"
1661                 else
1662                         echo ""
1663                         echo "No conditional flags passed"
1664                 fi
1665                 echo ""
1666                 echo "from available:"
1667                 echo "--with   :\t$AVAIL_BCONDS_WITH"
1668                 echo "--without:\t$AVAIL_BCONDS_WITHOUT"
1669                 echo ""
1670         fi
1671 }
1672
1673 display_branches() {
1674         echo -n "Available branches: "
1675         git branch -r | grep "^  ${REMOTE_PLD}" | grep -v ${REMOTE_PLD}/HEAD | sed "s#^ *${REMOTE_PLD}/##" | xargs
1676 }
1677
1678 # checks a given list of packages/files/provides agains current rpmdb.
1679 # outputs all dependencies which current rpmdb doesn't satisfy.
1680 # input can be either STDIN or parameters
1681 _rpm_prov_check() {
1682         local DEPS
1683
1684         if [ $# -gt 0 ]; then
1685                 DEPS="$@"
1686         else
1687                 DEPS=$(cat)
1688         fi
1689
1690         DEPS=$(LANG=C rpm -q --whatprovides $DEPS 2>&1 | awk '/^(error:|no package provides)/ { print }')
1691
1692         # packages
1693         echo "$DEPS" | awk '/^no package provides/ { print $NF }'
1694
1695         # other deps (files)
1696         echo "$DEPS" | awk -F: '/^error:.*No such file/{o = $2; gsub("^ file ", "", o); print o}'
1697 }
1698
1699 # checks if given package/files/provides exists in rpmdb.
1700 # input can be either stdin or parameters
1701 # returns packages which are present in the rpmdb
1702 _rpm_cnfl_check() {
1703         local DEPS
1704
1705         if [ $# -gt 0 ]; then
1706                 DEPS="$@"
1707         else
1708                 DEPS=$(cat)
1709         fi
1710
1711         LANG=C rpm -q --whatprovides $DEPS 2>/dev/null | awk '!/no package provides/ { print }'
1712 }
1713
1714 # install deps via information from 'rpm-getdeps' or 'rpm --specsrpm'
1715 install_build_requires_rpmdeps() {
1716         if [ "$FETCH_BUILD_REQUIRES_RPMGETDEPS" = "yes" ]; then
1717                 # TODO: Conflicts list doesn't check versions
1718                 local CNFL=$(rpm-getdeps $BCOND $SPECFILE 2> /dev/null | awk '/^\-/ { print $3 } ' | _rpm_cnfl_check | xargs)
1719                 local DEPS=$(rpm-getdeps $BCOND $SPECFILE 2> /dev/null | awk '/^\+/ { print $3 } ' | _rpm_prov_check | xargs)
1720         fi
1721         if [ "$FETCH_BUILD_REQUIRES_RPMSPECSRPM" = "yes" ]; then
1722                 local CNFL=$(rpm -q --specsrpm --conflicts $BCOND $SPECFILE | awk '{print $1}' | _rpm_cnfl_check | xargs)
1723                 local DEPS=$(rpm -q --specsrpm --requires $BCOND $SPECFILE | awk '{print $1}' | _rpm_prov_check | xargs)
1724         fi
1725
1726         if [ -n "$CNFL" ] || [ -n "$DEPS" ]; then
1727                 echo "fetch BuildRequires: install [$DEPS]; remove [$CNFL]"
1728                 update_shell_title "poldek: install [$DEPS]; remove [$CNFL]"
1729                 $SU_SUDO /usr/bin/poldek -q --update || $SU_SUDO /usr/bin/poldek -q --upa
1730         fi
1731         if [ -n "$CNFL" ]; then
1732                 update_shell_title "uninstall conflicting packages: $CNFL"
1733                 echo "Trying to uninstall conflicting packages ($CNFL):"
1734                 $SU_SUDO /usr/bin/poldek --noask --nofollow -ev $CNFL
1735         fi
1736
1737         while [ "$DEPS" ]; do
1738                         update_shell_title "install deps: $DEPS"
1739                         echo "Trying to install dependencies ($DEPS):"
1740                         local log=.${SPECFILE}_poldek.log
1741                         LANG=C $SU_SUDO /usr/bin/poldek --noask --caplookup -uGqQ $DEPS | tee $log
1742                         failed=$(awk '/^error:/{a=$2; sub(/^error: /, "", a); sub(/:$/, "", a); print a}' $log)
1743                         rm -f $log
1744                         local ok
1745                         if [ -n "$failed" ]; then
1746                                 for package in $failed; do
1747                                         spawn_sub_builder -bb $(depspecname $package) && ok="$ok $package"
1748                                 done
1749                                 DEPS="$ok"
1750                         else
1751                                 DEPS=""
1752                         fi
1753         done
1754 }
1755
1756 fetch_build_requires()
1757 {
1758         if [ "${FETCH_BUILD_REQUIRES}" != "yes" ]; then
1759                 return
1760         fi
1761
1762         update_shell_title "fetch build requires"
1763         if [ "$FETCH_BUILD_REQUIRES_RPMGETDEPS" = "yes" ] || [ "$FETCH_BUILD_REQUIRES_RPMSPECSRPM" = "yes" ]; then
1764                 install_build_requires_rpmdeps
1765                 return
1766         fi
1767
1768                 # XXX is this ugliest code written in human history still needed?
1769                 echo "All packages installed by fetch_build_requires() are written to:"
1770                 echo "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES"
1771                 echo ""
1772                 echo "If anything fails, you may get rid of them by executing:"
1773                 echo "poldek -e \`cat `pwd`/.${SPECFILE}_INSTALLED_PACKAGES\`"
1774                 echo ""
1775                 echo > `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1776                 for package_item in $(cat $SPECFILE | grep -B100000 ^%changelog|grep -v ^#|grep BuildRequires|grep -v ^-|sed -e "s/^.*BuildRequires://g"|awk '{print $1}'|sed -e s,perl\(,perl-,g -e s,::,-,g -e s,\(.*\),,g -e s,%{,,g -e s,},,g|grep -v OpenGL-devel|sed -e s,sh-utils,coreutils,g -e s,fileutils,coreutils,g -e s,textutils,coreutils,g -e s,kgcc_package,gcc,g -e s,\),,g)
1777                 do
1778                         package_item=$(echo $package_item|sed -e s,rpmbuild,rpm-build,g |sed -e s,__perl,perl,g |sed -e s,gasp,binutils-gasp,g -e s,binutils-binutils,binutils,g -e s,apxs,apache,g|sed -e s,apache\(EAPI\)-devel,apache-devel,g -e s,kernel-headers\(netfilter\),kernel-headers,g -e s,awk,mawk,g -e s,mmawk,mawk,g -e s,motif,openmotif,g -e s,openopenmotif,openmotif,g)
1779                         GO="yes"
1780                         package=$(basename "$package_item"|sed -e "s/}$//g")
1781                         COND_ARCH_TST=$(cat $SPECFILE|grep -B1 BuildRequires|grep -B1 $package|grep ifarch|sed -e "s/^.*ifarch//g")
1782                         mach=$(uname -m)
1783
1784                         COND_TST=`cat $SPECFILE|grep BuildRequires|grep "$package"`
1785                         if `echo $COND_TST|grep -q '^BuildRequires:'`; then
1786                                 if [ "$COND_ARCH_TST" != "" ] && [ "`echo $COND_ARCH_TST|sed -e "s/i.86/ix86/g"`" != "`echo $mach|sed -e "s/i.86/ix86/g"`" ]; then
1787                                         GO="yes"
1788                                 fi
1789                         # bcond:
1790                         else
1791                                 COND_NAME=`echo $COND_TST|sed -e s,:BuildRequires:.*$,,g`
1792                                 GO=""
1793                                 # %{without}
1794                                 if `echo $COND_TST|grep -q 'without_'`; then
1795                                         COND_NAME=`echo $COND_NAME|sed -e s,^.*without_,,g`
1796                                         if `echo $COND_TST|grep -q !`; then
1797                                                 COND_STATE="with"
1798                                         else
1799                                                 COND_STATE="wout"
1800                                         fi
1801                                         COND_WITH=`echo $AVAIL_BCONDS_WITH|grep "<$COND_NAME>"`
1802                                         COND_WITHOUT=`echo $AVAIL_BCONDS_WITHOUT|grep "<$COND_NAME>"`
1803                                         if [ -n "$COND_WITHOUT" ] || [ -z "$COND_WITH" ]; then
1804                                                 COND_ARGV="wout"
1805                                         else
1806                                                 COND_ARGV="with"
1807                                         fi
1808                                 # %{with}
1809                                 elif `echo $COND_TST|grep -q 'with_'`; then
1810                                         COND_NAME=`echo $COND_NAME|sed -e s,^.*with_,,g`
1811                                         if `echo $COND_TST|grep -q !`; then
1812                                                 COND_STATE="wout"
1813                                         else
1814                                                 COND_STATE="with"
1815                                         fi
1816                                         COND_WITH=`echo $AVAIL_BCONDS_WITH|grep "<$COND_NAME>"`
1817                                         COND_WITHOUT=`echo $AVAIL_BCONDS_WITHOUT|grep "<$COND_NAME>"`
1818                                         if [ -n "$COND_WITH" ] || [ -z "$COND_WITHOUT" ]; then
1819                                                 COND_ARGV="with"
1820                                         else
1821                                                 COND_ARGV="wout"
1822                                         fi
1823                                 fi
1824                                 RESULT="${COND_STATE}-${COND_ARGV}"
1825                                 case "$RESULT" in
1826                                         "with-wout" | "wout-with" )
1827                                                 GO=""
1828                                                 ;;
1829                                         "wout-wout" | "with-with" )
1830                                                 GO="yes"
1831                                                 ;;
1832                                         * )
1833                                                 echo "Action '$RESULT' was not defined for package '$package_item'"
1834                                                 GO="yes"
1835                                                 ;;
1836                                 esac
1837                         fi
1838
1839                         if [ "$GO" = "yes" ]; then
1840                                 if [ "`rpm -q $package|sed -e "s/$package.*/$package/g"`" != "$package" ]; then
1841                                         echo "Testing if $package has subrequirements..."
1842                                         run_poldek -t -i $package --dumpn=".$package-req.txt"
1843                                         if [ -f ".$package-req.txt" ]; then
1844                                                 for package_name in `cat ".$package-req.txt"|grep -v ^#`
1845                                                 do
1846                                                         if [ "$package_name" = "$package" ]; then
1847                                                                 echo "Installing BuildRequired package:\t$package_name"
1848                                                                 update_shell_title "Installing BuildRequired package: ${package_name}"
1849                                                                 install_required_packages $package
1850                                                         else
1851                                                                 echo "Installing (sub)Required package:\t$package_name"
1852                                                                 update_shell_title "Installing (sub)Required package: ${package_name}"
1853                                                                 install_required_packages $package_name
1854                                                         fi
1855                                                         case $? in
1856                                                                 0)
1857                                                                         INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
1858                                                                         echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1859                                                                         ;;
1860                                                                 *)
1861                                                                         echo "Attempting to run spawn sub - builder..."
1862                                                                         echo "Package installation failed:\t$package_name"
1863                                                                         run_sub_builder $package_name
1864                                                                         if [ $? -eq 0 ]; then
1865                                                                                 install_required_packages $package_name
1866                                                                                 case $? in
1867                                                                                         0)
1868                                                                                                 INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
1869                                                                                                 echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1870                                                                                                 ;;
1871                                                                                         *)
1872                                                                                                 NOT_INSTALLED_PACKAGES="$package_name $NOT_INSTALLED_PACKAGES"
1873                                                                                                 ;;
1874                                                                                 esac
1875                                                                         fi
1876                                                                         ;;
1877                                                         esac
1878                                                 done
1879                                                 rm -f ".$package-req.txt"
1880                                         else
1881                                                 echo "Attempting to run spawn sub - builder..."
1882                                                 echo "Package installation failed:\t$package"
1883                                                 run_sub_builder $package
1884                                                 if [ $? -eq 0 ]; then
1885                                                         install_required_packages $package
1886                                                         case $? in
1887                                                                 0)
1888                                                                         INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
1889                                                                         echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1890                                                                         ;;
1891                                                                 *)
1892                                                                         NOT_INSTALLED_PACKAGES="$package_name $NOT_INSTALLED_PACKAGES"
1893                                                                         ;;
1894                                                         esac
1895                                                 fi
1896                                         fi
1897                                 else
1898                                         echo "Package $package is already installed. BuildRequirement satisfied."
1899                                 fi
1900                         fi
1901                 done
1902                 if [ "$NOT_INSTALLED_PACKAGES" != "" ]; then
1903                         echo >&2 "Unable to install following packages and their dependencies:"
1904                         for pkg in "$NOT_INSTALLED_PACKAGES"
1905                         do
1906                                 echo $pkg
1907                         done
1908                         remove_build_requires
1909                         exit 8
1910                 fi
1911 }
1912
1913 init_rpm_dir() {
1914         local TOP_DIR=$(eval $RPM $RPMOPTS --eval '%{_topdir}')
1915         local rpmdir=$(eval $RPM $RPMOPTS --eval '%{_rpmdir}')
1916         local buildir=$(eval $RPM $RPMOPTS --eval '%{_builddir}')
1917         local srpmdir=$(eval $RPM $RPMOPTS --eval '%{_srcrpmdir}')
1918         local tmp
1919
1920         echo "Initializing rpm directories to $TOP_DIR from $GIT_SERVER"
1921         mkdir -p $TOP_DIR $rpmdir $buildir $srpmdir
1922
1923         GIT_WORK_TREE=${TOP_DIR}/rpm-build-tools git clone  ${GIT_SERVER}/rpm-build-tools.git || 
1924         for a in dropin md5 adapter builder mirrors {relup,compile,repackage,rsync,pearize}.sh pldnotify.awk; do
1925                 ln -s rpm-build-tools/$a .
1926         done
1927         init_builder
1928 }
1929
1930 mr_proper() {
1931         init_builder
1932         NOCVSSPEC="yes"
1933         DONT_PRINT_REVISION="yes"
1934         get_spec
1935         parse_spec
1936
1937         # remove spec and sources
1938         $RPMBUILD --clean --rmsource --rmspec --nodeps --define "_specdir $PACKAGE_DIR" --define "_sourcedir $PACKAGE_DIR" $SPECFILE
1939         rm -rf $PACKAGE_DIR/.git .gitignore
1940         cd $__PWD
1941         rmdir --ignore-fail-on-non-empty $PACKAGE_DIR
1942 }
1943
1944 #---------------------------------------------
1945 # main()
1946
1947 if [ $# = 0 ]; then
1948         usage
1949         exit 1
1950 fi
1951
1952 while [ $# -gt 0 ]; do
1953         case "${1}" in
1954                 -5 | --update-md5)
1955                         COMMAND="update_md5"
1956                         NODIST="yes"
1957                         NOCVSSPEC="yes"
1958                         shift ;;
1959                 -a5 | --add-md5 )
1960                         COMMAND="update_md5"
1961                         NODIST="yes"
1962                         NOCVSSPEC="yes"
1963                         ADD5="yes"
1964                         shift ;;
1965                 -n5 | --no-md5 )
1966                         NO5="yes"
1967                         shift ;;
1968                 -D | --debug )
1969                         DEBUG="yes"; shift ;;
1970                 -V | --version )
1971                         COMMAND="version"; shift ;;
1972                 --short-version )
1973                         COMMAND="short-version"; shift ;;
1974                 -a | --add_cvs)
1975                         COMMAND="add_cvs";
1976                         shift ;;
1977                 -b | -ba | --build )
1978                         COMMAND="build"; shift ;;
1979                 -bb | --build-binary )
1980                         COMMAND="build-binary"; shift ;;
1981                 -bc )
1982                         COMMAND="build-build"; shift ;;
1983                 -bi )
1984                         COMMAND="build-install"; shift ;;
1985                 -bl )
1986                         COMMAND="build-list"; shift ;;
1987                 -bp | --build-prep )
1988                         COMMAND="build-prep"; shift ;;
1989                 -bs | --build-source )
1990                         COMMAND="build-source"; shift ;;
1991                 -B | --branch )
1992                         COMMAND="branch"; shift; TAG="${1}"; shift;;
1993                 -c | --clean )
1994                         CLEAN="--clean"; shift ;;
1995                 -cf | --cvs-force )
1996                         CVS_FORCE="-f"; shift;;
1997                 --depth )
1998                         DEPTH="--depth=$2"
1999                         shift 2
2000                         ;;
2001                 -g | --get )
2002                         COMMAND="get"; shift ;;
2003                 -h | --help )
2004                         COMMAND="usage"; shift ;;
2005                 --ftp )
2006                         PROTOCOL="ftp"; shift ;;
2007                 --http )
2008                         PROTOCOL="http"; shift ;;
2009                 -j)
2010                         RPMOPTS="${RPMOPTS} --define \"_smp_mflags -j$2\""
2011                         shift 2
2012                         ;;
2013                 -j[0-9]*)
2014                         RPMOPTS="${RPMOPTS} --define \"_smp_mflags $1\""
2015                         shift
2016                         ;;
2017                 -l | --logtofile )
2018                         shift; LOGFILE="${1}"; shift ;;
2019                 -ni| --nice )
2020                         shift; DEF_NICE_LEVEL=${1}; shift ;;
2021                 -ske | --skip-existing-files)
2022                         SKIP_EXISTING_FILES="yes"; shift ;;
2023                 -m | --mr-proper )
2024                         COMMAND="mr-proper"; shift ;;
2025                 -ncs | --no-cvs-specs )
2026                         NOCVSSPEC="yes"; shift ;;
2027                 -nd | --no-distfiles )
2028                         NODIST="yes"; shift ;;
2029                 -nm | --no-mirrors )
2030                         NOMIRRORS="yes"; shift ;;
2031                 -nu | --no-urls )
2032                         NOURLS="yes"; shift ;;
2033                 -ns | --no-srcs )
2034                         NOSRCS="yes"; shift ;;
2035                 -ns0 | --no-source0 )
2036                         NOSOURCE0="yes"; shift ;;
2037                 -nn | --no-net )
2038                         NOCVSSPEC="yes"
2039                         NODIST="yes"
2040                         NOMIRRORS="yes"
2041                         NOURLS="yes"
2042                         NOSRCS="yes"
2043                         ALWAYS_CVSUP="no"
2044                         shift;;
2045                 -pm | --prefer-mirrors )
2046                         PREFMIRRORS="yes"
2047                         shift;;
2048                 --no-init )
2049                         NOINIT="yes"
2050                         shift;;
2051                 --opts )
2052                         shift; RPMOPTS="${RPMOPTS} ${1}"; shift ;;
2053                 --nopatch | -np )
2054                         shift; RPMOPTS="${RPMOPTS} --define \"patch${1} : ignoring patch${1}; exit 1; \""; shift ;;
2055                 --skip-patch | -sp )
2056                         shift; RPMOPTS="${RPMOPTS} --define \"patch${1} : skiping patch${1}\""; shift ;;
2057                 --topdir)
2058                         RPMOPTS="${RPMOPTS} --define \"_topdir $2\""
2059                         shift 2
2060                         ;;
2061                 --with | --without )
2062                         case $GROUP_BCONDS in
2063                                 "yes")
2064                                         COND=${1}
2065                                         shift
2066                                         # XXX: broken: ./builder -bb ucspi-tcp.spec --without mysql
2067                                         while ! `echo ${1}|grep -qE '(^-|spec)'`
2068                                         do
2069                                                 BCOND="$BCOND $COND $1"
2070                                                 shift
2071                                         done;;
2072                                 "no")
2073                                         if [[ "$2" = *,* ]]; then
2074                                                 for a in $(echo "$2" | tr , ' '); do
2075                                                         BCOND="$BCOND $1 $a"
2076                                                 done
2077                                         else
2078                                                 BCOND="$BCOND $1 $2"
2079                                         fi
2080                                         shift 2 ;;
2081                         esac
2082                         ;;
2083                 --target )
2084                         shift; TARGET="${1}"; shift ;;
2085                 --target=* )
2086                         TARGET=$(echo "${1}" | sed 's/^--target=//'); shift ;;
2087                 -q | --quiet )
2088                         QUIET="--quiet"; shift ;;
2089                 --date )
2090                         CVSDATE="${2}"; shift 2
2091                         date -d "$CVSDATE" > /dev/null 2>&1 || { echo >&2 "No valid date specified"; exit 3; }
2092                         ;;
2093                 -r | --cvstag )
2094                         CVSTAG="$2"
2095                         shift 2
2096                         ;;
2097                 -A)
2098                         shift
2099                         CVSTAG="master"
2100                         ;;
2101                 -R | --fetch-build-requires)
2102                         FETCH_BUILD_REQUIRES="yes"
2103                         NOT_INSTALLED_PACKAGES=
2104                         shift ;;
2105                 -RB | --remove-build-requires)
2106                         REMOVE_BUILD_REQUIRES="nice"
2107                         shift ;;
2108                 -FRB | --force-remove-build-requires)
2109                         REMOVE_BUILD_REQUIRES="force"
2110                         shift ;;
2111                 -sc | --sources-cvs)
2112                         COMMAND="list-sources-cvs"
2113                         shift ;;
2114                 -sd | --source-distfiles)
2115                         COMMAND="list-sources-distfiles"
2116                         shift ;;
2117                 -sdp | --source-distfiles-paths)
2118                         COMMAND="list-sources-distfiles-paths"
2119                         shift ;;
2120                 -sf | --source-files)
2121                         COMMAND="list-sources-files"
2122                         shift ;;
2123                 -lsp | --source-paths)
2124                         COMMAND="list-sources-local-paths"
2125                         shift ;;
2126                 -su | --source-urls)
2127                         COMMAND="list-sources-urls"
2128                         shift ;;
2129                 -Tvs | --tag-version-stable )
2130                         COMMAND="tag"
2131                         TAG="STABLE"
2132                         TAG_VERSION="yes"
2133                         shift;;
2134                 -Ts | --tag-stable )
2135                         COMMAND="tag"
2136                         TAG="STABLE"
2137                         TAG_VERSION="no"
2138                         shift;;
2139                 -Tv | --tag-version )
2140                         COMMAND="tag"
2141                         TAG=""
2142                         TAG_VERSION="yes"
2143                         shift;;
2144                 -Tp | --tag-prefix )
2145                         TAG_PREFIX="$2"
2146                         shift 2;;
2147                 -tt | --test-tag )
2148                         TEST_TAG="yes"
2149                         shift;;
2150                 -T | --tag )
2151                         COMMAND="tag"
2152                         shift
2153                         TAG="$1"
2154                         TAG_VERSION="no"
2155                         shift;;
2156                 -ir | --integer-release-only )
2157                         INTEGER_RELEASE="yes"
2158                         shift;;
2159                 -U | --update )
2160                         COMMAND="update_md5"
2161                         UPDATE="yes"
2162                         NOCVSSPEC="yes"
2163                         NODIST="yes"
2164                         shift ;;
2165                 -Upi | --update-poldek-indexes )
2166                         UPDATE_POLDEK_INDEXES="yes"
2167                         shift ;;
2168                 --init-rpm-dir)
2169                         COMMAND="init_rpm_dir"
2170                         shift ;;
2171                 -u | --try-upgrade )
2172                         TRY_UPGRADE="1"; shift ;;
2173                 --upgrade-version )
2174                         shift; UPGRADE_VERSION="$1"; shift;;
2175                 -un | --try-upgrade-with-float-version )
2176                         TRY_UPGRADE="1"; FLOAT_VERSION="1"; shift ;;
2177                 -v | --verbose )
2178                         BE_VERBOSE="1"; shift ;;
2179                 --define)
2180                         shift
2181                         MACRO="${1}"
2182                         shift
2183                         if echo "${MACRO}" | grep -q '\W'; then
2184                                 RPMOPTS="${RPMOPTS} --define \"${MACRO}\""
2185                         else
2186                                 VALUE="${1}"
2187                                 shift
2188                                 RPMOPTS="${RPMOPTS} --define \"${MACRO} ${VALUE}\""
2189                         fi
2190                         ;;
2191                 --alt_kernel)
2192                         shift
2193                         RPMOPTS="${RPMOPTS} --define \"alt_kernel $1\""
2194                         shift
2195                         ;;
2196                 --short-circuit)
2197                         RPMBUILDOPTS="${RPMBUILDOPTS} --short-circuit"
2198                         shift
2199                         ;;
2200                 --show-bconds | -show-bconds | -print-bconds | --print-bconds | -display-bconds | --display-bconds )
2201                         COMMAND="show_bconds"
2202                         shift
2203                         ;;
2204                 --show-bcond-args)
2205                         COMMAND="show_bcond_args"
2206                         shift
2207                         ;;
2208                 --show-avail-bconds)
2209                         COMMAND="show_avail_bconds"
2210                         shift
2211                         ;;
2212                 --nodeps)
2213                         shift
2214                         RPMOPTS="${RPMOPTS} --nodeps"
2215                         ;;
2216                 -debug)
2217                         RPMBUILDOPTS="${RPMBUILDOPTS} -debug"; shift
2218                         ;;
2219                 -*)
2220                         Exit_error err_invalid_cmdline "$1"
2221                         ;;
2222                 *)
2223                         SPECFILE=${1%/}; shift
2224                         # check if specname was passed as specname:cvstag
2225                         if [ "${SPECFILE##*:}" != "${SPECFILE}" ]; then
2226                                 CVSTAG="${SPECFILE##*:}"
2227                                 SPECFILE="${SPECFILE%%:*}"
2228                         fi
2229                         # always have SPECFILE ending with .spec extension
2230                         SPECFILE=${SPECFILE%%.spec}.spec
2231                         ASSUMED_NAME=$(basename ${SPECFILE%%.spec})
2232         esac
2233 done
2234
2235 [ -d "$ASSUMED_NAME" ] && GIT_DIR="$ASSUMED_NAME/.git" || GIT_DIR=.git
2236 if [ -d "$GIT_DIR" ] && [ -z "$CVSTAG" ]; then
2237         if CVSTAG=$(GIT_DIR=$GIT_DIR git symbolic-ref HEAD) 2>/dev/null; then
2238                 CVSTAG=${CVSTAG#refs/heads/}
2239                 if [ "$CVSTAG" != "master" ]; then
2240                         echo >&2 "builder: Active branch $CVSTAG. Use -r BRANCHNAME to override"
2241                 fi
2242         else
2243                 echo >&2 "On detached HEAD. Use -r BRANCHNAME to override"
2244                 HEAD_DETACHED="yes"
2245         fi
2246 elif [ "$CVSTAG" = "HEAD" ]; then
2247         # assume -r HEAD is same as -A
2248         CVSTAG="master"
2249 fi
2250
2251 if [ "$CVSTAG" ]; then
2252         # pass $CVSTAG used by builder to rpmbuild too, so specs could use it
2253         RPMOPTS="$RPMOPTS --define \"_cvstag $CVSTAG\""
2254 fi
2255
2256 if [ -n "$DEBUG" ]; then
2257         set -x
2258         set -v
2259 fi
2260
2261 if [ -n "$TARGET" ]; then
2262         case "$RPMBUILD" in
2263                 "rpmbuild")
2264                         TARGET_SWITCH="--target $TARGET" ;;
2265                 "rpm")
2266                         TARGET_SWITCH="--target=$TARGET" ;;
2267         esac
2268 fi
2269
2270 if [ "$SCHEDTOOL" != "no" ]; then
2271         NICE_COMMAND="$SCHEDTOOL"
2272 else
2273         NICE_COMMAND="nice -n ${DEF_NICE_LEVEL}"
2274 fi
2275
2276 update_shell_title "$COMMAND"
2277 case "$COMMAND" in
2278         "show_bconds")
2279                 init_builder
2280                 if [ -z "$SPECFILE" ]; then
2281                         Exit_error err_no_spec_in_cmdl
2282                 fi
2283                 get_spec > /dev/null
2284                 parse_spec
2285                 set_bconds_values
2286                 display_bconds
2287                 ;;
2288         "show_bcond_args")
2289                 init_builder
2290                 if [ -z "$SPECFILE" ]; then
2291                         Exit_error err_no_spec_in_cmdl
2292                 fi
2293                 get_spec > /dev/null
2294                 parse_spec
2295                 set_bconds_values
2296                 echo "$BCOND"
2297                 ;;
2298         "show_avail_bconds")
2299                 init_builder
2300                 if [ -z "$SPECFILE" ]; then
2301                         Exit_error err_no_spec_in_cmdl
2302                 fi
2303
2304                 get_spec > /dev/null
2305                 parse_spec
2306                 local bcond_avail=$(find_spec_bcond $SPECFILE)
2307                 local opt bcond bconds
2308                 for opt in $bcond_avail; do
2309                         case "$opt" in
2310                         without_*)
2311                                 bcond=${opt#without_}
2312                                 bconds="$bconds $bcond"
2313                                 ;;
2314                         with_*)
2315                                 bcond=${opt#with_}
2316                                 bconds="$bconds $bcond"
2317                                 ;;
2318                         *)
2319                                 echo >&2 "ERROR: unexpected '$opt' in show_avail_bconds"
2320                                 exit 1
2321                                 ;;
2322                         esac
2323                 done
2324                 echo $bconds
2325
2326                 ;;
2327         "build" | "build-binary" | "build-source" | "build-prep" | "build-build" | "build-install" | "build-list")
2328                 init_builder
2329                 if [ -z "$SPECFILE" ]; then
2330                         Exit_error err_no_spec_in_cmdl
2331                 fi
2332
2333                 # display SMP make flags if set
2334                 smp_mflags=$(rpm -E %{?_smp_mflags})
2335                 if [ "$smp_mflags" ]; then
2336                         echo "builder: SMP make flags are set to $smp_mflags"
2337                 fi
2338
2339                 get_spec
2340                 parse_spec
2341                 set_bconds_values
2342                 display_bconds
2343                 display_branches
2344                 if [ "$COMMAND" != "build-source" ]; then
2345                         check_buildarch
2346                 fi
2347                 fetch_build_requires
2348                 if [ "$INTEGER_RELEASE" = "yes" ]; then
2349                         echo "Checking release $PACKAGE_RELEASE..."
2350                         if echo $PACKAGE_RELEASE | grep -q '^[^.]*\.[^.]*$' 2>/dev/null ; then
2351                                 Exit_error err_fract_rel "$PACKAGE_RELEASE"
2352                         fi
2353                 fi
2354
2355                 # ./builder -bs test.spec -r AC-branch -Tp auto-ac- -tt
2356                 if [ -n "$TEST_TAG" ]; then
2357                         local TAGVER=`make_tagver`
2358                         echo "Searching for tag $TAGVER..."
2359                         git show-ref --quiet --verify "refs/tags/$TAGVER" && Exit_error err_tag_exists "$TAGVER" "$TAGREL"
2360
2361                         # - do not allow to build from HEAD when XX-branch exists
2362                         TREE_PREFIX=$(echo "$TAG_PREFIX" | sed -e 's#^auto-\([a-zA-Z]\+\)-.*#\1#g')
2363                         if [ "$TREE_PREFIX" != "$TAG_PREFIX" ]; then
2364                                 TAG_BRANCH="${TREE_PREFIX}-branch"
2365                                 TAG_STATUS=$(git branch -r | grep -i "${REMOTE_PLD}/$TAG_BRANCH$" | sed "s# *${REMOTE_PLD}/##")
2366                                 if [ -n "$TAG_STATUS" -a -z "$CVSTAG" ]; then
2367                                         Exit_error err_branch_exists "$TAG_STATUS"
2368                                 fi
2369                         fi
2370
2371                 fi
2372
2373                 if [ -n "$NOSOURCE0" ] ; then
2374                         SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2375                 fi
2376                 try_upgrade
2377                 case $? in
2378                         0)
2379                                 get_files $SOURCES $PATCHES
2380                                 check_md5 $SOURCES
2381                                 ;;
2382                         *)
2383                                 NODIST="yes" get_files $SOURCES $PATCHES
2384                                 update_md5 $SOURCES
2385                                 ;;
2386                 esac
2387                 build_package
2388                 if [ "$UPDATE_POLDEK_INDEXES" = "yes" -a "$COMMAND" != "build-prep" ]; then
2389                         run_poldek --sdir="${POLDEK_INDEX_DIR}" --mkidxz
2390                 fi
2391                 remove_build_requires
2392                 ;;
2393         "branch" )
2394                 init_builder
2395                 if [ -z "$SPECFILE" ]; then
2396                         Exit_error err_no_spec_in_cmdl
2397                 fi
2398
2399                 get_spec
2400                 parse_spec
2401                 branch_files $TAG
2402                 ;;
2403         "add_cvs" )
2404                 init_builder
2405                 if [ -z "$SPECFILE" ]; then
2406                         Exit_error err_no_spec_in_cmdl
2407                 fi
2408
2409                 ADD_PACKAGE_CVS=yes get_spec
2410                 parse_spec
2411
2412                 if [ -n "$NOSOURCE0" ] ; then
2413                         SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2414                 fi
2415                 get_files $SOURCES $PATCHES
2416                 check_md5 $SOURCES
2417                 ;;
2418         "get" )
2419                 init_builder
2420                 if [ -z "$SPECFILE" ]; then
2421                         Exit_error err_no_spec_in_cmdl
2422                 fi
2423
2424                 get_spec
2425                 parse_spec
2426
2427                 if [ -n "$NOSOURCE0" ] ; then
2428                         SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2429                 fi
2430                 get_files $SOURCES $PATCHES
2431                 check_md5 $SOURCES
2432                 ;;
2433         "update_md5" )
2434                 init_builder
2435                 if [ -z "$SPECFILE" ]; then
2436                         Exit_error err_no_spec_in_cmdl
2437                 fi
2438
2439                 get_spec
2440                 parse_spec
2441
2442                 if [ -n "$NOSOURCE0" ] ; then
2443                         SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2444                 fi
2445                 update_md5 $SOURCES
2446                 ;;
2447         "tag" )
2448                 NOURLS=1
2449                 NODIST="yes"
2450                 init_builder
2451                 if [ -z "$SPECFILE" ]; then
2452                         Exit_error err_no_spec_in_cmdl
2453                 fi
2454
2455                 get_spec
2456                 parse_spec
2457                 tag_files
2458                 ;;
2459         "mr-proper" )
2460                 mr_proper
2461                 ;;
2462         "list-sources-files" )
2463                 init_builder
2464                 NOCVSSPEC="yes"
2465                 DONT_PRINT_REVISION="yes"
2466                 get_spec
2467                 parse_spec
2468                 for SAP in $SOURCES $PATCHES; do
2469                         echo $SAP | awk '{gsub(/.*\//,"") ; print}'
2470                 done
2471                 ;;
2472         "list-sources-urls" )
2473                 init_builder
2474                 NOCVSSPEC="yes"
2475                 DONT_PRINT_REVISION="yes"
2476                 get_spec
2477                 parse_spec
2478                 SAPS="$SOURCES $PATCHES"
2479                 for SAP in $SAPS ; do
2480                         echo $SAP
2481                 done
2482                 ;;
2483         "list-sources-local-paths" )
2484                 init_builder
2485                 NOCVSSPEC="yes"
2486                 DONT_PRINT_REVISION="yes"
2487                 get_spec
2488                 parse_spec
2489                 for SAP in $SOURCES $PATCHES; do
2490                         echo $PACKAGE_DIR/$(echo $SAP | awk '{gsub(/.*\//,"") ; print }')
2491                 done
2492                 ;;
2493         "list-sources-distfiles-paths" )
2494                 init_builder
2495                 NOCVSSPEC="yes"
2496                 DONT_PRINT_REVISION="yes"
2497                 get_spec
2498                 parse_spec
2499                 for SAP in $SOURCES $PATCHES; do
2500                         if [ -n "$(src_md5 "$SAP")" ]; then
2501                                 distfiles_path "$SAP"
2502                         fi
2503                 done
2504                 ;;
2505         "list-sources-distfiles" )
2506                 init_builder
2507                 NOCVSSPEC="yes"
2508                 DONT_PRINT_REVISION="yes"
2509                 get_spec
2510                 parse_spec
2511                 for SAP in $SOURCES $PATCHES; do
2512                         if [ -n "$(src_md5 "$SAP")" ]; then
2513                                 distfiles_url "$SAP"
2514                         fi
2515                 done
2516                 ;;
2517         "list-sources-cvs" )
2518                 init_builder
2519 #               NOCVSSPEC="yes"
2520                 DONT_PRINT_REVISION="yes"
2521                 get_spec
2522                 parse_spec
2523                 for SAP in $SOURCES $PATCHES; do
2524                         if [ -z "$(src_md5 "$SAP")" ]; then
2525                                 echo $SAP | awk '{gsub(/.*\//,"") ; print}'
2526                         fi
2527                 done
2528                 ;;
2529         "init_rpm_dir")
2530                 init_rpm_dir
2531                 ;;
2532         "usage" )
2533                 usage
2534                 ;;
2535         "short-version" )
2536                 echo "$VERSION"
2537                 ;;
2538         "version" )
2539                 echo "$VERSIONSTRING"
2540                 ;;
2541 esac
2542 if [ -f "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES" -a "$REMOVE_BUILD_REQUIRES" != "" ]; then
2543         rm "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES"
2544 fi
2545 cd "$__PWD"
2546
2547 # vi:syntax=sh:ts=4:sw=4:noet
This page took 0.235587 seconds and 4 git commands to generate.