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