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