]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - builder.sh
- allow forcing ipv4/ipv6
[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
21 # Notes (todo/bugs):
22 # - when Icon: field is present, -5 and -a5 doesn't work
23 # - builder -R skips installing BR if spec is not present before builder invocation (need to run builder twice)
24 # - does not respect NoSource: X, and tries to cvs up such files [ example: VirtualBox-bin.spec and its Source0 ]
25 # TODO:
26 # - ability to do ./builder -bb foo.spec foo2.spec foo3.spec
27 # - funny bug, if source-md5 is set then builder will download from distfiles even if there is no url present:
28 #   Source10:   forwardfix.pl
29 #   # Source10-md5:     8bf85f7368933a4e0cb4f875bac28733
30 # - builder --help:
31 #       basename: missing operand
32 #       Try `basename --help' for more information.
33 #       -- and the normal usage info --
34
35 PROGRAM=${0##*/}
36 APPDIR=$(d=$0; [ -L "$d" ] && d=$(readlink -f "$d"); dirname "$d")
37 RCSID='$Id$' r=${RCSID#* * } rev=${r%% *}
38 VERSION="v0.35/$rev"
39 VERSIONSTRING="\
40 Build package utility from PLD Linux Packages repository
41 $VERSION (C) 1999-2011 Free Penguins".
42
43 PATH="/bin:/usr/bin:/usr/sbin:/sbin:/usr/X11R6/bin"
44
45 # required rpm-build-macros
46 RPM_MACROS_VER=1.534
47
48 COMMAND="build"
49 TARGET=""
50
51 SPECFILE=""
52 BE_VERBOSE=""
53 QUIET=""
54 CLEAN=""
55 DEBUG=""
56 NOURLS=""
57 NOCVS=""
58 NOCVSSPEC=""
59 NODIST=""
60 NOINIT=""
61 PREFMIRRORS=""
62 UPDATE=""
63 ADD5=""
64 NO5=""
65 ALWAYS_CVSUP=${ALWAYS_CVSUP:-"yes"}
66 CVSROOT=""
67 GREEDSRC=""
68
69 # use rpm 4.4.6+ digest format instead of comments if non-zero
70 USEDIGEST=
71
72 # user agent when fetching files
73 USER_AGENT="PLD/Builder($VERSION)"
74
75 # It can be used i.e. in log file naming.
76 # See LOGFILE example.
77 DATE=`date +%Y-%m-%d_%H-%M-%S`
78
79 # Example: LOGFILE='../log.$PACKAGE_NAME'
80 # Example: LOGFILE='../LOGS/log.$PACKAGE_NAME.$DATE'
81 # Example: LOGFILE='$PACKAGE_NAME/$PACKAGE_NAME.$DATE.log'
82 # Example: LOGFILE='$PACKAGE_NAME.$DATE.log'
83 # Yes, you can use variable name! Note _single_ quotes!
84 LOGFILE=''
85
86 LOGDIR=""
87 LOGDIROK=""
88 LOGDIRFAIL=""
89 LASTLOG_FILE=""
90
91 CHMOD="no"
92 CHMOD_MODE="0644"
93 RPMOPTS=""
94 RPMBUILDOPTS=""
95 BCOND=""
96 GROUP_BCONDS="no"
97
98 # create symlinks for tools in PACKAGE_DIR, see get_spec()
99 SYMLINK_TOOLS="no"
100
101 PATCHES=""
102 SOURCES=""
103 ICONS=""
104 PACKAGE_RELEASE=""
105 PACKAGE_VERSION=""
106 PACKAGE_NAME=""
107 ASSUMED_NAME=""
108 PROTOCOL="http"
109 WGET_RETRIES=${MAX_WGET_RETRIES:-0}
110
111 CVS_COMMAND=${CVS_COMMAND:-cvs}
112 CVS_FORCE=""
113 CVSIGNORE_DF="yes"
114 CVS_RETRIES=${MAX_CVS_RETRIES:-1000}
115 CVS_SERVER="cvs.pld-linux.org"
116 CVSTAG=""
117
118 RES_FILE=""
119
120 DISTFILES_SERVER="://distfiles.pld-linux.org"
121 ATTICDISTFILES_SERVER="://attic-distfiles.pld-linux.org"
122
123 DEF_NICE_LEVEL=19
124 SCHEDTOOL="auto"
125
126 FAIL_IF_NO_SOURCES="yes"
127
128 # let get_files skip over files which are present to get those damn files fetched
129 SKIP_EXISTING_FILES="no"
130
131 TRY_UPGRADE=""
132 # should the specfile be restored if upgrade failed?
133 REVERT_BROKEN_UPGRADE="yes"
134
135 if rpm --specsrpm 2>/dev/null; then
136         FETCH_BUILD_REQUIRES_RPMSPECSRPM="yes"
137         FETCH_BUILD_REQUIRES_RPMGETDEPS="no"
138 else
139         FETCH_BUILD_REQUIRES_RPMSPECSRPM="no"
140         if [ -x /usr/bin/rpm-getdeps ]; then
141                 FETCH_BUILD_REQUIRES_RPMGETDEPS="yes"
142         else
143                 FETCH_BUILD_REQUIRES_RPMGETDEPS="no"
144         fi
145 fi
146
147
148 # Here we load saved user environment used to
149 # predefine options set above, or passed to builder
150 # in command line.
151 # This one reads global system environment settings:
152 if [ -f ~/etc/builderrc ]; then
153         . ~/etc/builderrc
154 fi
155 # And this one cascades settings using user personal
156 # builder settings.
157 # Example of ~/.builderrc:
158 #
159 #UPDATE_POLDEK_INDEXES="yes"
160 #FETCH_BUILD_REQUIRES="yes"
161 #REMOVE_BUILD_REQUIRES="force"
162 #GROUP_BCONDS="yes"
163 #LOGFILE='../LOGS/log.$PACKAGE_NAME.$DATE'
164 #TITLECHANGE=no
165 #
166 SU_SUDO=""
167 if [ -n "$HOME_ETC" ]; then
168         USER_CFG="$HOME_ETC/.builderrc"
169         BUILDER_MACROS="$HOME_ETC/.builder-rpmmacros"
170 else
171         USER_CFG=~/.builderrc
172         BUILDER_MACROS=~/.builder-rpmmacros
173 fi
174
175 [ -f "$USER_CFG" ] && . "$USER_CFG"
176
177 if [ "$SCHEDTOOL" = "auto" ]; then
178         if [ -x /usr/bin/schedtool ] && schedtool -B -e echo >/dev/null; then
179                 SCHEDTOOL="schedtool -B -e"
180         else
181                 SCHEDTOOL="no"
182         fi
183 fi
184
185 if [ -n "$USE_PROZILLA" ]; then
186         GETURI="proz --no-getch -r -P ./ -t$WGET_RETRIES $PROZILLA_OPTS"
187         GETURI2="$GETURI"
188         OUTFILEOPT="-O"
189 elif [ -n "$USE_AXEL" ]; then
190         GETURI="axel -a $AXEL_OPTS"
191         GETURI2="$GETURI"
192         OUTFILEOPT="-o"
193 else
194         wget --help 2>&1 | grep -q -- ' --no-check-certificate ' && WGET_OPTS="$WGET_OPTS --no-check-certificate"
195         wget --help 2>&1 | grep -q -- ' --inet ' && WGET_OPTS="$WGET_OPTS --inet"
196         wget --help 2>&1 | grep -q -- ' --retry-connrefused ' && WGET_OPTS="$WGET_OPTS --retry-connrefused"
197         WGET_OPTS="$WGET_OPTS --user-agent=$USER_AGENT"
198
199         GETURI="wget --passive-ftp -c -nd -t$WGET_RETRIES $WGET_OPTS"
200         GETURI2="wget -c -nd -t$WGET_RETRIES $WGET_OPTS"
201         OUTFILEOPT="-O"
202 fi
203
204 GETLOCAL="cp -a"
205
206 if (rpm --version 2>&1 | grep -q '4.0.[0-2]'); then
207         RPM="rpm"
208         RPMBUILD="rpm"
209 else
210         RPM="rpm"
211         RPMBUILD="rpmbuild"
212 fi
213
214 #
215 # sanity checks
216 #
217 if [ -d $HOME/rpm/SOURCES ]; then
218         echo "ERROR: ~/rpm/{SPECS,SOURCES} structure is obsolete" >&2
219         echo "ERROR: get rid of your ~/rpm/SOURCES" >&2
220         exit 1
221 fi
222
223 #
224 # are we using cvs-nserver ?
225 #
226 CVS_NSERVER=0
227 $CVS_COMMAND --version 2>&1 | grep -q 'CVS-nserver'
228 [ $? -eq 0 ] && CVS_NSERVER=1
229
230 POLDEK_INDEX_DIR="$($RPM --eval %_rpmdir)/"
231 POLDEK_CMD="$SU_SUDO /usr/bin/poldek --noask"
232
233 run_poldek() {
234         RES_FILE=$(tempfile)
235         if [ -n "$LOGFILE" ]; then
236                 LOG=`eval echo $LOGFILE`
237                 if [ -n "$LASTLOG_FILE" ]; then
238                         echo "LASTLOG=$LOG" > $LASTLOG_FILE
239                 fi
240                 (${NICE_COMMAND} ${POLDEK_CMD} `while test $# -gt 0; do echo "$1 ";shift;done` ; echo $? > ${RES_FILE})|tee -a $LOG
241                 return $exit_pldk
242         else
243                 (${NICE_COMMAND} ${POLDEK_CMD} `while test $# -gt 0; do echo "$1 ";shift;done` ; echo $? > ${RES_FILE}) 1>&2 >/dev/null
244                 return `cat ${RES_FILE}`
245                 rm -rf ${RES_FILE}
246         fi
247 }
248
249 #---------------------------------------------
250 # functions
251
252 usage() {
253         if [ -n "$DEBUG" ]; then set -xv; fi
254         echo "\
255 Usage: builder [-D|--debug] [-V|--version] [--short-version] [--as_anon] [-a|--add_cvs] [-b|-ba|--build]
256 [-bb|--build-binary] [-bs|--build-source] [-bc] [-bi] [-bl] [-u|--try-upgrade]
257 [{-cf|--cvs-force}] [{-B|--branch} <branch>] [{-d|--cvsroot} <cvsroot>]
258 [-g|--get] [-h|--help] [--ftp] [--http] [{-l|--logtofile} <logfile>] [-m|--mr-proper]
259 [-q|--quiet] [--date <yyyy-mm-dd> [-r <cvstag>] [{-T|--tag <cvstag>]
260 [-Tvs|--tag-version-stable] [-Ts|--tag-stable] [-Tv|--tag-version]
261 [{-Tp|--tag-prefix} <prefix>] [{-tt|--test-tag}] [--use-greed-sources]
262 [-nu|--no-urls] [-v|--verbose] [--opts <rpm opts>] [--short-circuit]
263 [--show-bconds] [--with/--without <feature>] [--define <macro> <value>]
264 <package>[.spec][:cvstag]
265
266 -4                  - force ipv4 when transferring files
267 -5, --update-md5    - update md5 comments in spec, implies -nd -ncs
268 -6                  - force ipv6 when transferring files
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         esac
695         echo >&2 "Unknown error."
696         exit 100
697 }
698
699 init_builder() {
700         if [ -n "$DEBUG" ]; then
701                 set -x
702                 set -v
703         fi
704
705         if [ "$NOINIT" != "yes" ] ; then
706                 TOP_DIR=$(eval $RPM $RPMOPTS --eval '%{_topdir}')
707
708                 local macros_ver=$(rpm -E %?rpm_build_macros)
709                 if [ -z "$macros_ver" ]; then
710                         REPO_DIR=$TOP_DIR/packages
711                         PACKAGE_DIR=$TOP_DIR/packages/$ASSUMED_NAME
712                 else
713                         if awk "BEGIN{exit($macros_ver>=$RPM_MACROS_VER)}"; then
714                                 echo >&2 "builder requires rpm-build-macros >= $RPM_MACROS_VER"
715                                 exit 1
716                         fi
717                         REPO_DIR=$TOP_DIR
718                         PACKAGE_DIR=$REPO_DIR/$ASSUMED_NAME
719                 fi
720         else
721                 REPO_DIR="."
722                 PACKAGE_DIR="."
723         fi
724
725         __PWD=$(pwd)
726 }
727
728 get_spec() {
729
730         update_shell_title "get_spec"
731
732         if [ -n "$DEBUG" ]; then
733                 set -x
734                 set -v
735         fi
736
737         cd "$REPO_DIR"
738         if [ ! -f "$ASSUMED_NAME/$SPECFILE" ]; then
739                 # XXX: still needed?
740                 SPECFILE=$(basename $SPECFILE)
741         fi
742
743         if [ "$NOCVSSPEC" != "yes" ]; then
744                 if [ ! -s CVS/Root -a "$NOCVSSPEC" != "yes" ]; then
745                         echo "Warning: No CVS access defined - using local .spec file"
746                         NOCVSSPEC="yes"
747                 fi
748
749                 if [ -d "$ASSUMED_NAME" -a -s "$ASSUMED_NAME/CVS/Root" ]; then
750                         cvsup "$ASSUMED_NAME/$SPECFILE" || Exit_error err_no_spec_in_repo
751                 elif [ "$ADD_PACKAGE_CVS" = "yes" ]; then
752                         if [ ! -r "$ASSUMED_NAME/$SPECFILE" ]; then
753                                 echo "ERROR: No package to add ($ASSUMED_NAME/$SPECFILE)" >&2
754                                 exit 101
755                         fi
756                         if [ ! -s "$ASSUMED_NAME/CVS/Root" ]; then
757                                 cvsup -a $ASSUMED_NAME || Exit_error err_cvs_add_failed
758                         fi
759                         cvsup -a "$ASSUMED_NAME/$SPECFILE" || Exit_error err_cvs_add_failed
760                 else
761                         cvsup -c -d $ASSUMED_NAME "packages/$ASSUMED_NAME/$SPECFILE" || {
762                                 # softfail if new package, i.e not yet added to cvs
763                                 [ ! -f "$ASSUMED_NAME/$SPECFILE" ] && Exit_error err_no_spec_in_repo
764                                 echo "Warning: package not in CVS - assuming new package"
765                                 NOCVSSPEC="yes"
766                                 NOCVS="yes"
767                         }
768
769                         # remove Entries.Static -- so 'cvs up' would update all files in a repo
770                         rm -f "$ASSUMED_NAME/CVS/Entries.Static"
771                 fi
772
773                 cvsignore_df .cvsignore
774
775                 # add default log format to .cvsignore if it is relative to package dir
776                 if [ -n "$LOGFILE" -a "$LOGFILE" = "${LOGFILE##*/}" ]; then
777                         # substitute known "macros" to glob
778                         local logfile=$(echo "$LOGFILE" | sed -e 's,\$\(PACKAGE_NAME\|DATE\),*,g')
779                         if [ "$logfile" ]; then
780                                 cvsignore_df "$logfile"
781                         fi
782                 fi
783
784                 # create symlinks for tools
785                 if [ "$SYMLINK_TOOLS" != "no" ]; then
786                         for a in dropin md5 adapter builder {relup,compile,repackage,rsync,pearize}.sh pldnotify.awk; do
787                                 [ -f $a ] || continue
788                                 ln -sf ../$a $ASSUMED_NAME
789                                 cvsignore_df $a
790                         done
791                 fi
792         fi
793
794         if [ ! -f "$ASSUMED_NAME/$SPECFILE" ]; then
795                 Exit_error err_no_spec_in_repo
796         fi
797
798         if [ "$CHMOD" = "yes" -a -n "$SPECFILE" ]; then
799                 chmod $CHMOD_MODE $ASSUMED_NAME/$SPECFILE
800         fi
801         unset OPTIONS
802         [ -n "$DONT_PRINT_REVISION" ] || grep -E -m 1 "^#.*Revision:.*Date" $ASSUMED_NAME/$SPECFILE
803
804         set_spec_target
805 }
806
807 find_mirror() {
808         cd "$REPO_DIR"
809         local url="$1"
810         if [ ! -f "mirrors" -a "$NOCVSSPEC" != "yes" ] ; then
811                 $CVS_COMMAND update mirrors >&2
812         fi
813
814         IFS="|"
815         local origin mirror name rest ol prefix
816         while read origin mirror name rest; do
817                 # skip comments and empty lines
818                 if [ -z "$origin" ] || [ "${origin#\#}" != "$origin" ]; then
819                         continue
820                 fi
821                 ol=$(echo -n "$origin" | wc -c)
822                 prefix=$(echo -n "$url" | head -c $ol)
823                 if [ "$prefix" = "$origin" ] ; then
824                         suffix=$(echo "$url" | cut -b $((ol+1))-)
825                         echo -n "$mirror$suffix"
826                         return 0
827                 fi
828         done < mirrors
829         echo "$url"
830 }
831
832 # Warning: unpredictable results if same URL used twice
833 src_no() {
834         cd $PACKAGE_DIR
835         rpm_dump | \
836         grep "SOURCEURL[0-9]*[  ]*$1""[         ]*$" | \
837         sed -e 's/.*SOURCEURL\([0-9][0-9]*\).*/\1/' | \
838         head -n 1 | xargs
839 }
840
841 src_md5() {
842         [ "$NO5" = "yes" ] && return
843         no=$(src_no "$1")
844         [ -z "$no" ] && return
845         cd $PACKAGE_DIR
846         local md5
847
848         if [ -f additional-md5sums ]; then
849                 local spec_rev=$(grep $SPECFILE CVS/Entries 2>/dev/null | sed -e s:/$SPECFILE/:: -e s:/.*::)
850                 if [ -z "$spec_rev" ]; then
851                         spec_rev=$(head -n 1 $SPECFILE | sed -e 's/.*\$Revision: \([0-9.]*\).*/\1/')
852                 fi
853                 local spec="$SPECFILE[0-9.,]*,$(echo $spec_rev | sed 's/\./\\./g')"
854                 md5=$(grep -s -v '^#' additional-md5sums | \
855                 grep -E "[      ]$(basename "$1")[      ]+${spec}([     ,]|\$)" | \
856                 sed -e 's/^\([0-9a-f]\{32\}\).*/\1/' | \
857                 grep -E '^[0-9a-f]{32}$')
858
859                 if [ "$md5" ]; then
860                         if [ $(echo "$md5" | wc -l) != 1 ] ; then
861                                 echo "$SPECFILE: more then one entry in additional-md5sums for $1" 1>&2
862                         fi
863                         echo "$md5" | tail -n 1
864                         return
865                 fi
866         fi
867
868         source_md5=`grep -i "^#[        ]*Source$no-md5[        ]*:" $SPECFILE | sed -e 's/.*://'`
869         if [ -n "$source_md5" ]; then
870                 echo $source_md5
871         else
872                 source_md5=`grep -i "BuildRequires:[    ]*digest(%SOURCE$no)[   ]*=" $SPECFILE | sed -e 's/.*=//'`
873                 if [ -n "$source_md5" ]; then
874                         echo $source_md5
875                 else
876                         # we have empty SourceX-md5, but it is still possible
877                         # that we have NoSourceX-md5 AND NoSource: X
878                         nosource_md5=`grep -i "^#[       ]*NoSource$no-md5[      ]*:" $SPECFILE | sed -e 's/.*://'`
879                         if [ -n "$nosource_md5" -a -n "`grep -i "^NoSource:[     ]*$no$" $SPECFILE`" ] ; then
880                                 echo $nosource_md5
881                         fi
882                 fi
883         fi
884 }
885
886 distfiles_path() {
887         echo "by-md5/$(src_md5 "$1" | sed -e 's|^\(.\)\(.\)|\1/\2/&|')/$(basename "$1")"
888 }
889
890 distfiles_url() {
891         echo "$PROTOCOL$DISTFILES_SERVER/distfiles/$(distfiles_path "$1")"
892 }
893
894 distfiles_attic_url() {
895         echo "$PROTOCOL$ATTICDISTFILES_SERVER/distfiles/Attic/$(distfiles_path "$1")"
896 }
897
898 good_md5() {
899         md5=$(src_md5 "$1")
900         [ "$md5" = "" ] || \
901         [ "$md5" = "$(md5sum $(nourl "$1") 2> /dev/null | sed -e 's/ .*//')" ]
902 }
903
904 good_size() {
905         size=$(find $(nourl "$1") -printf "%s" 2>/dev/null)
906         [ -n "$size" -a "$size" -gt 0 ]
907 }
908
909 cvsignore_df() {
910         if [ "$CVSIGNORE_DF" != "yes" ]; then
911                 return
912         fi
913         cvsignore=${PACKAGE_DIR}/.cvsignore
914
915         # add only if not yet there
916         if ! awk -vf="$1" -vc=1 '$0 == f { c = 0 } END { exit c }' $cvsignore 2>/dev/null; then
917                 echo "$1" >> $cvsignore
918         fi
919 }
920
921 cvsup() {
922         update_shell_title "cvsup"
923         local OPTIONS="" ACTION="up"
924
925         # checkout
926         if [ "$1" = "-c" ]; then
927                 ACTION="co"
928                 shift
929         fi
930         # add
931         if [ "$1" = "-a" ]; then
932                 ACTION="add"
933                 shift
934         fi
935
936         OPTIONS="$ACTION "
937
938         if [ -n "$CVSROOT" ]; then
939                 OPTIONS="-d $CVSROOT $OPTIONS"
940         fi
941
942         if [ "$ACTION" != "add" ]; then
943                 if [ -z "$CVSDATE" -a -z "$CVSTAG" ]; then
944                         OPTIONS="$OPTIONS -A"
945                 else
946                         if [ -n "$CVSDATE" ]; then
947                                 OPTIONS="$OPTIONS -D $CVSDATE"
948                         fi
949                         if [ -n "$CVSTAG" ]; then
950                                 # FIXME: cvs add actually works with -r ?
951                                 OPTIONS="$OPTIONS -r $CVSTAG"
952                         fi
953                 fi
954         fi
955
956         local result=1
957         local retries_counter=0
958         if [ $# = 1 ]; then
959                 update_shell_title "cvsup: $*"
960         else
961                 update_shell_title "cvsup: $# files"
962         fi
963         while [ "$result" != "0" -a "$retries_counter" -le "$CVS_RETRIES" ]; do
964                 retries_counter=$(( $retries_counter + 1 ))
965                 output=$(LC_ALL=C $CVS_COMMAND $OPTIONS "$@" 2>&1)
966                 result=$?
967                 [ -n "$output" ] && echo "$output"
968                 if echo "$output" | grep -qE "(Cannot connect to|connect to .* failed|Connection reset by peer|Connection timed out|Unknown host)" \
969                         && [ "$result" -ne "0" -a "$retries_counter" -le "$CVS_RETRIES" ]; then
970                         echo "Trying again [$*]... ($retries_counter)"
971                         update_shell_title "cvsup: retry #$retries_counter"
972                         sleep 2
973                         continue
974                 else
975                         break
976                 fi
977         done
978         update_shell_title "cvsup: done!"
979         return $result
980 }
981
982 # returns true if "$1" is ftp, http or https protocol url
983 is_url() {
984         case "$1" in
985         ftp://*|http://*|https://*)
986                 return 0
987         ;;
988         esac
989         return 1
990 }
991
992 update_md5() {
993         if [ $# -eq 0 ]; then
994                 return
995         fi
996
997         update_shell_title "update md5"
998         if [ -n "$DEBUG" ]; then
999                 set -x
1000                 set -v
1001         fi
1002
1003         cd "$PACKAGE_DIR"
1004
1005         # pass 1: check files to be fetched
1006         local todo
1007         local need_files
1008         for i in "$@"; do
1009                 local fp=$(nourl "$i")
1010                 local srcno=$(src_no "$i")
1011                 if [ -n "$ADD5" ]; then
1012                         [ "$fp" = "$i" ] && continue # FIXME what is this check doing?
1013                         grep -qiE '^#[  ]*Source'$srcno'-md5[   ]*:' $PACKAGE_DIR/$SPECFILE && continue
1014                         grep -qiE '^BuildRequires:[     ]*digest[(]%SOURCE'$srcno'[)][  ]*=' $PACKAGE_DIR/$SPECFILE && continue
1015                 else
1016                         grep -qiE '^#[  ]*Source'$srcno'-md5[   ]*:' $PACKAGE_DIR/$SPECFILE || grep -qiE '^BuildRequires:[      ]*digest[(]%SOURCE'$srcno'[)][  ]*=' $PACKAGE_DIR/$SPECFILE || continue
1017                 fi
1018                 if [ ! -f "$fp" ] || [ $ALWAYS_CVSUP = "yes" ]; then
1019                         need_files="$need_files $i"
1020                 fi
1021         done
1022
1023         # pass 1a: get needed files
1024         if [ "$need_files" ]; then
1025                 get_files $need_files
1026         fi
1027
1028         # pass 2: proceed with md5 adding or updating
1029         for i in "$@"; do
1030                 local fp=$(nourl "$i")
1031                 local srcno=$(src_no "$i")
1032                 local md5=$(grep -iE '^#[       ]*(No)?Source'$srcno'-md5[      ]*:' $PACKAGE_DIR/$SPECFILE )
1033                 if [ -z "$md5" ]; then
1034                         md5=$(grep -iE '^[      ]*BuildRequires:[       ]*digest[(]%SOURCE'$srcno'[)][  ]*=' $PACKAGE_DIR/$SPECFILE )
1035                 fi
1036                 if [ -n "$ADD5" ] && is_url $i || [ -n "$md5" ]; then
1037                         local tag="# Source$srcno-md5:\t"
1038                         if [[ "$md5" == *NoSource* ]]; then
1039                                 tag="# NoSource$srcno-md5:\t"
1040                         elif [ -n "$USEDIGEST" ]; then
1041                                 tag="BuildRequires:\tdigest(%SOURCE$srcno) = "
1042                         fi
1043                         md5=$(md5sum "$fp" | cut -f1 -d' ')
1044                         echo "Updating Source$srcno ($md5: $fp)."
1045                         perl -i -ne '
1046                                 print unless (/^\s*#\s*(No)?Source'$srcno'-md5\s*:/i or /^\s*BuildRequires:\s*digest\(%SOURCE'$srcno'\)/i);
1047                                 print "'"$tag$md5"'\n" if /^Source'$srcno'\s*:\s+/;
1048                         ' \
1049                         $PACKAGE_DIR/$SPECFILE
1050                 fi
1051         done
1052 }
1053
1054 check_md5() {
1055         local bad
1056         [ "$NO5" = "yes" ] && return
1057
1058         update_shell_title "check md5"
1059
1060         for i in "$@"; do
1061                 bad=0
1062                 if ! good_md5 "$i"; then
1063                         echo -n "MD5 sum mismatch."
1064                         bad=1
1065                 fi
1066                 if ! good_size "$i"; then
1067                         echo -n "0 sized file."
1068                         bad=1
1069                 fi
1070
1071                 if [ $bad -eq 1 ]; then
1072                         echo " Use -U to refetch sources,"
1073                         echo "or -5 to update md5 sums, if you're sure files are correct."
1074                         Exit_error err_no_source_in_repo $i
1075                 fi
1076         done
1077 }
1078
1079 get_files() {
1080         update_shell_title "get_files"
1081
1082         if [ -n "$DEBUG" ]; then
1083                 set -x
1084                 set -v
1085         fi
1086
1087         if [ $# -gt 0 ]; then
1088                 cd "$PACKAGE_DIR"
1089
1090                 if [ ! -s CVS/Root -a "$NOCVS" != "yes" ]; then
1091                         echo "Warning: No CVS access defined for SOURCES"
1092                         NOCVS="yes"
1093                 fi
1094
1095                 local nc=0
1096                 local get_files_cvs=""
1097                 for i in "$@"; do
1098                         nc=$((nc + 1))
1099                         local cvsup=0
1100                         SHELL_TITLE_PREFIX="get_files[$nc/$#]"
1101                         update_shell_title "$i"
1102                         local fp=`nourl "$i"`
1103                         if [ "$SKIP_EXISTING_FILES" = "yes" ] && [ -f "$fp" ]; then
1104                                 continue
1105                         fi
1106
1107                         FROM_DISTFILES=0
1108                         local srcmd5=$(src_md5 "$i")
1109
1110                         # we know if source/patch is present in cvs/distfiles
1111                         # - has md5 (in distfiles)
1112                         # - in cvs... ideas?
1113
1114                         # CHECK: local file didn't exist or always cvs up (first) requested.
1115                         if [ ! -f "$fp" ] || [ $ALWAYS_CVSUP = "yes" ]; then
1116                                 if echo $i | grep -vE '(http|ftp|https|cvs|svn)://' | grep -qE '\.(gz|bz2)$']; then
1117                                         echo "Warning: no URL given for $i"
1118                                 fi
1119                                 target="$fp"
1120
1121                                 if [ -z "$NODIST" ] && [ -n "$srcmd5" ]; then
1122                                         if good_md5 "$i" && good_size "$i"; then
1123                                                 echo "$fp having proper md5sum already exists"
1124                                                 continue
1125                                         fi
1126
1127                                         # optionally prefer mirror over distfiles if there's mirror
1128                                         # TODO: build url list and then try each url from the list
1129                                         if [ -n "$PREFMIRRORS" ] && [ -z "$NOMIRRORS" ] && im=$(find_mirror "$i") && [ "$im" != "$i" ]; then
1130                                                 url="$im"
1131                                         else
1132                                                 url=$(distfiles_url "$i")
1133                                         fi
1134
1135                                         url_attic=$(distfiles_attic_url "$i")
1136                                         FROM_DISTFILES=1
1137                                         # is $url local file?
1138                                         if [[ "$url" = [./]* ]]; then
1139                                                 update_shell_title "${GETLOCAL%% *}: $url"
1140                                                 ${GETLOCAL} $url $target
1141                                         else
1142                                                 if [ -z "$NOMIRRORS" ]; then
1143                                                         url=$(find_mirror "$url")
1144                                                 fi
1145
1146                                                 local uri=${url}
1147                                                 # make shorter message for distfiles urls
1148                                                 if [[ "$uri" = ${PROTOCOL}${DISTFILES_SERVER}* ]] || [[ "$uri" = ${PROTOCOL}${ATTICDISTFILES_SERVER}* ]]; then
1149                                                         uri=${uri#${PROTOCOL}${DISTFILES_SERVER}/distfiles/by-md5/?/?/*/}
1150                                                         uri=${uri#${PROTOCOL}${ATTICDISTFILES_SERVER}/distfiles/by-md5/?/?/*/}
1151                                                         uri="df: $uri"
1152                                                 fi
1153                                                 update_shell_title "${GETURI%% *}: $uri"
1154                                                 ${GETURI} ${OUTFILEOPT} "$target" "$url" || \
1155                                                 if [ "`echo $url | grep -E 'ftp://'`" ]; then
1156                                                         update_shell_title "${GETURI2%% *}: $url"
1157                                                         ${GETURI2} ${OUTFILEOPT} "$target" "$url"
1158                                                 fi
1159                                         fi
1160
1161                                         # is it empty file?
1162                                         if [ ! -s "$target" ]; then
1163                                                 rm -f "$target"
1164                                                 if [ `echo $url_attic | grep -E '^(\.|/)'` ]; then
1165                                                         update_shell_title "${GETLOCAL%% *}: $url_attic"
1166                                                         ${GETLOCAL} $url_attic $target
1167                                                 else
1168                                                         if [ -z "$NOMIRRORS" ]; then
1169                                                                 url_attic=$(find_mirror "$url_attic")
1170                                                         fi
1171                                                         update_shell_title "${GETURI%% *}: $url_attic"
1172                                                         ${GETURI} ${OUTFILEOPT} "$target" "$url_attic" || \
1173                                                         if [ "`echo $url_attic | grep -E 'ftp://'`" ]; then
1174                                                                 update_shell_title "${GETURI2%% *}: $url_attic"
1175                                                                 ${GETURI2} ${OUTFILEOPT} "$target" "$url_attic"
1176                                                         fi
1177                                                         test -s "$target" || rm -f "$target"
1178                                                 fi
1179                                         fi
1180
1181                                         if [ -s "$target" ]; then
1182                                                 cvsignore_df $target
1183                                         else
1184                                                 rm -f "$target"
1185                                                 FROM_DISTFILES=0
1186                                         fi
1187                                 elif [ "$NOCVS" != "yes" -a -z "$srcmd5" ]; then
1188                                         if [ $# -gt 1 ]; then
1189                                                 get_files_cvs="$get_files_cvs $fp"
1190                                                 update_shell_title "$fp (will cvs up later)"
1191                                                 cvsup=1
1192                                         else
1193                                                 cvsup $fp
1194                                         fi
1195                                 fi
1196
1197                                 if [ -z "$NOURLS" ] && [ ! -f "$fp" -o -n "$UPDATE" ] && [ "`echo $i | grep -E 'ftp://|http://|https://'`" ]; then
1198                                         if [ -z "$NOMIRRORS" ]; then
1199                                                 im=$(find_mirror "$i")
1200                                         else
1201                                                 im="$i"
1202                                         fi
1203                                         update_shell_title "${GETURI%% *}: $im"
1204                                         ${GETURI} "$im" ${OUTFILEOPT} "$target" || \
1205                                         if [ "`echo $im | grep -E 'ftp://'`" ]; then
1206                                                 update_shell_title "${GETURI2%% *}: $im"
1207                                                 ${GETURI2} "$im" ${OUTFILEOPT} "$target"
1208                                         fi
1209                                         test -s "$target" || rm -f "$target"
1210                                 fi
1211
1212                                 if [ "$cvsup" = 1 ]; then
1213                                         continue
1214                                 fi
1215
1216                         fi
1217
1218                         # the md5 check must be moved elsewhere as if we've called from update_md5 the md5 is wrong.
1219                         if [ ! -f "$fp" -a "$FAIL_IF_NO_SOURCES" != "no" ]; then
1220                                 if [ -n "GREEDSRC" ]; then
1221                                         get_greed_sources $i
1222                                 else
1223                                         Exit_error err_no_source_in_repo $i
1224                                 fi
1225                         fi
1226
1227                         # we check md5 here just only to refetch immediately
1228                         if good_md5 "$i" && good_size "$i"; then
1229                                 :
1230                         elif [ "$FROM_DISTFILES" = 1 ]; then
1231                                 # wrong md5 from distfiles: remove the file and try again
1232                                 # but only once ...
1233                                 echo "MD5 sum mismatch. Trying full fetch."
1234                                 FROM_DISTFILES=2
1235                                 rm -f $target
1236                                 update_shell_title "${GETURI%% *}: $url"
1237                                 ${GETURI} ${OUTFILEOPT} "$target" "$url" || \
1238                                 if [ "`echo $url | grep -E 'ftp://'`" ]; then
1239                                         update_shell_title "${GETURI2%% *}: $url"
1240                                         ${GETURI2} ${OUTFILEOPT} "$target" "$url"
1241                                 fi
1242                                 if [ ! -s "$target" ]; then
1243                                         rm -f "$target"
1244                                         update_shell_title "${GETURI%% *}: $url_attic"
1245                                         ${GETURI} ${OUTFILEOPT} "$target" "$url_attic" || \
1246                                         if [ "`echo $url_attic | grep -E 'ftp://'`" ]; then
1247                                                 update_shell_title "${GETURI2%% *}: $url_attic"
1248                                                 ${GETURI2} ${OUTFILEOPT} "$target" "$url_attic"
1249                                         fi
1250                                 fi
1251                                 test -s "$target" || rm -f "$target"
1252                         fi
1253                 done
1254                 SHELL_TITLE_PREFIX=""
1255
1256                 if [ "$get_files_cvs" ]; then
1257                         cvsup $get_files_cvs
1258                 fi
1259
1260                 if [ "$CHMOD" = "yes" ]; then
1261                         CHMOD_FILES=$(nourl "$@")
1262                         if [ -n "$CHMOD_FILES" ]; then
1263                                 chmod $CHMOD_MODE $CHMOD_FILES
1264                         fi
1265                 fi
1266         fi
1267 }
1268
1269 make_tagver() {
1270         if [ -n "$DEBUG" ]; then
1271                 set -x
1272                 set -v
1273         fi
1274
1275         # Check whether first character of PACKAGE_NAME is legal for tag name
1276         if [ -z "${PACKAGE_NAME##[_0-9]*}" -a -z "$TAG_PREFIX" ]; then
1277                 TAG_PREFIX=tag_
1278         fi
1279
1280         # NOTE: CVS tags may must not contain the characters `$,.:;@'
1281         TAGVER=$(echo $TAG_PREFIX$PACKAGE_NAME-$PACKAGE_VERSION-$PACKAGE_RELEASE | tr '[.@]' '[_#]')
1282
1283         # Remove #kernel.version_release from TAGVER because tagging sources
1284         # could occur with different kernel-headers than kernel-headers used at build time.
1285         # besides, %{_kernel_ver_str} is not expanded.
1286
1287         # TAGVER=auto-ac-madwifi-ng-0-0_20070225_1#%{_kernel_ver_str}
1288         # TAGVER=auto-ac-madwifi-ng-0-0_20070225_1
1289
1290         TAGVER=${TAGVER%#*}
1291         echo -n "$TAGVER"
1292 }
1293
1294 # bool is_tag_a_branch(tag)
1295 #
1296 # returns 1 if a tag is a branch set on SPECFILE
1297 is_tag_a_branch() {
1298         if [ -n "$DEBUG" ]; then
1299                 set -x
1300                 set -v
1301         fi
1302
1303         if [ $# -ne 1 ]; then
1304                 return 0;
1305         fi
1306
1307         TAG=$1
1308
1309         cd "$PACKAGE_DIR"
1310         $CVS_COMMAND status -v $SPECFILE | grep -Eiq "${TAG}.+(branch: [0-9.]+)"
1311         return $?
1312 }
1313
1314 tag_files() {
1315         TAG_FILES="$@"
1316
1317         if [ -n "$DEBUG" ]; then
1318                 set -x
1319                 set -v
1320         fi
1321
1322         echo "Version: $PACKAGE_VERSION"
1323         echo "Release: $PACKAGE_RELEASE"
1324
1325         local TAGVER
1326         if [ "$TAG_VERSION" = "yes" ]; then
1327                 TAGVER=`make_tagver`
1328                 echo "CVS tag: $TAGVER"
1329         fi
1330         if [ -n "$TAG" ]; then
1331                 echo "CVS tag: $TAG"
1332         fi
1333
1334         local OPTIONS="tag $CVS_FORCE"
1335         if [ -n "$CVSROOT" ]; then
1336                 OPTIONS="-d $CVSROOT $OPTIONS"
1337         fi
1338
1339         # if a tagname we are about to set already exists
1340         # and happens to be a branch (common case with AC-branch)
1341         # pass -B (allows -F to disturb branch tag)
1342         local _tag=$TAG
1343         if [ "$TAG_VERSION" = "yes" ]; then
1344                 _tag=$TAGVER
1345         fi;
1346         is_tag_a_branch $_tag
1347         if [ $? -eq 0 -a $CVS_NSERVER -eq 0 ]; then
1348                 OPTIONS="$OPTIONS -B"
1349         fi;
1350
1351         cd "$PACKAGE_DIR"
1352         local tag_files
1353         for i in $TAG_FILES; do
1354                 # don't tag files stored on distfiles
1355                 [ -n "`src_md5 $i`" ] && continue
1356                 local fp=`nourl "$i"`
1357                 if [ -f "$fp" ]; then
1358                         tag_files="$tag_files $fp"
1359                 elif [ -n "GREEDSRC" ]; then
1360                         get_greed_sources $i
1361                 else
1362                         Exit_error err_no_source_in_repo $i
1363                 fi
1364         done
1365
1366         if [ "$tag_files" ]; then
1367                 if [ "$TAG_VERSION" = "yes" ]; then
1368                         update_shell_title "tag sources: $TAGVER"
1369                         printf "Tagging %d files\n" $(echo $tag_files | wc -w)
1370                         $CVS_COMMAND $OPTIONS $TAGVER $tag_files || exit
1371                 fi
1372                 if [ -n "$TAG" ]; then
1373                         update_shell_title "tag sources: $TAG"
1374
1375                         while [ "$tag_files" ]; do
1376                                 local chunk=$(echo $tag_files | tr ' ' '\n' | head -n 100)
1377                                 printf "Tagging %d files\n" $(echo $chunk | wc -w)
1378                                 $CVS_COMMAND $OPTIONS $TAG $chunk || exit
1379                                 tag_files=$(echo $tag_files | tr ' ' '\n' | tail +101)
1380                         done
1381                 fi
1382         fi
1383
1384         cd "$PACKAGE_DIR"
1385         if [ "$TAG_VERSION" = "yes" ]; then
1386                 update_shell_title "tag spec: $TAGVER"
1387                 $CVS_COMMAND $OPTIONS $TAGVER $SPECFILE || exit
1388         fi
1389         if [ -n "$TAG" ]; then
1390                 update_shell_title "tag spec: $TAG"
1391                 $CVS_COMMAND $OPTIONS $TAG $SPECFILE || exit
1392         fi
1393 }
1394
1395 branch_files() {
1396         TAG=$1
1397         echo "CVS branch tag: $TAG"
1398         shift
1399
1400         TAG_FILES="$@"
1401
1402         if [ -n "$DEBUG" ]; then
1403                 set -x
1404                 set -v
1405         fi
1406
1407         local OPTIONS="tag $CVS_FORCE -b"
1408
1409         # branch exists?
1410         is_tag_a_branch $TAG
1411         if [ $? -eq 1 ]; then
1412                 OPTIONS="$OPTIONS -B"
1413         fi
1414
1415         if [ -n "$CVSROOT" ]; then
1416                 OPTIONS="-d $CVSROOT $OPTIONS"
1417         fi
1418         cd "$PACKAGE_DIR"
1419         local tag_files
1420         for i in $TAG_FILES; do
1421                 local fp=`nourl "$i"`
1422                 if [ -f "$fp" ]; then
1423                         tag_files="$tag_files $fp"
1424                 elif [ -n "GREEDSRC" ]; then
1425                         get_greed_sources $i
1426                 else
1427                         Exit_error err_no_source_in_repo $i
1428                 fi
1429         done
1430         if [ "$tag_files" ]; then
1431                 $CVS_COMMAND $OPTIONS $TAG $tag_files || exit
1432         fi
1433
1434         cd "$PACKAGE_DIR"
1435         $CVS_COMMAND $OPTIONS $TAG $SPECFILE || exit
1436 }
1437
1438
1439 # this function should exit early if package can't be built for this arch
1440 # this avoids unneccessary BR filling.
1441 check_buildarch() {
1442         local out ret
1443         out=$(minirpm --short-circuit -bp --define "'prep exit 0'" --nodeps $SPECFILE 2>&1)
1444         ret=$?
1445         if [ $ret -ne 0 ]; then
1446                 echo >&2 "$out"
1447                 exit $ret
1448         fi
1449 }
1450
1451 # from relup.sh
1452 set_release() {
1453         local specfile="$1"
1454         local rel="$2"
1455         local newrel="$3"
1456         sed -i -e "
1457                 s/^\(%define[ \t]\+_\?rel[ \t]\+\)$rel\$/\1$newrel/
1458                 s/^\(Release:[ \t]\+\)$rel\$/\1$newrel/
1459         " $specfile
1460 }
1461
1462 set_version() {
1463         local specfile="$1"
1464         local ver="$2" subver=$ver
1465         local newver="$3" newsubver=$newver
1466
1467         # try handling subver, everything that's not numeric-dotted in version
1468         if grep -Eq '%define\s+subver' $specfile; then
1469                 subver=$(echo "$ver" | sed -re 's,^[0-9.]+,,')
1470                 ver=${ver%$subver}
1471                 newsubver=$(echo "$newver" | sed -re 's,^[0-9.]+,,')
1472                 newver=${newver%$newsubver}
1473         fi
1474         sed -i -e "
1475                 s/^\(%define[ \t]\+_\?ver[ \t]\+\)$ver\$/\1$newver/
1476                 s/^\(%define[ \t]\+subver[ \t]\+\)$subver\$/\1$newsubver/
1477                 s/^\(Version:[ \t]\+\)$ver\$/\1$newver/
1478         " $specfile
1479 }
1480
1481 try_upgrade() {
1482         if [ -n "$TRY_UPGRADE" ]; then
1483                 local TNOTIFY TNEWVER TOLDVER
1484                 update_shell_title "build_package: try_upgrade"
1485
1486                 cd "$PACKAGE_DIR"
1487                 
1488                 if [ "$UPGRADE_VERSION" ]; then
1489                         TNEWVER=$UPGRADE_VERSION
1490                 else
1491                         if [ -n "$FLOAT_VERSION" ]; then
1492                                 TNOTIFY=$($APPDIR/pldnotify.awk ${BE_VERBOSE:+-vDEBUG=1} $SPECFILE -n) || exit 1
1493                         else
1494                                 TNOTIFY=$($APPDIR/pldnotify.awk ${BE_VERBOSE:+-vDEBUG=1} $SPECFILE) || exit 1
1495                         fi
1496
1497                         # pldnotify.awk does not set exit codes, but it has match for ERROR
1498                         # in output which means so.
1499                         if [[ "$TNOTIFY" = *ERROR* ]]; then
1500                                 echo >&2 "$TNOTIFY"
1501                                 exit 1
1502                         fi
1503
1504                         TNEWVER=$(echo $TNOTIFY | awk '{ match($4,/\[NEW\]/); print $5 }')
1505                 fi
1506
1507                 if [ -n "$TNEWVER" ]; then
1508                         TOLDVER=`echo $TNOTIFY | awk '{ print $3; }'`
1509                         echo "New version found, updating spec file from $TOLDVER to version $TNEWVER"
1510                         if [ "$REVERT_BROKEN_UPGRADE" = "yes" ]; then
1511                                 cp -f $SPECFILE $SPECFILE.bak
1512                         fi
1513                         chmod +w $SPECFILE
1514                         set_release $SPECFILE $PACKAGE_RELEASE 1
1515                         set_version $SPECFILE $PACKAGE_VERSION $TNEWVER
1516                         parse_spec
1517                         if [ "$PACKAGE_VERSION" != "$TNEWVER" ]; then
1518                                 echo >&2 "Upgrading version failed, you need to update spec yourself"
1519                                 exit 1
1520                         fi
1521                         return 1
1522                 fi
1523         fi
1524         return 0
1525 }
1526
1527 build_package() {
1528         update_shell_title "build_package"
1529         if [ -n "$DEBUG" ]; then
1530                 set -x
1531                 set -v
1532         fi
1533
1534         cd "$PACKAGE_DIR"
1535
1536         case "$COMMAND" in
1537                 build )
1538                         BUILD_SWITCH="-ba" ;;
1539                 build-binary )
1540                         BUILD_SWITCH="-bb" ;;
1541                 build-source )
1542                         BUILD_SWITCH="-bs --nodeps" ;;
1543                 build-prep )
1544                         BUILD_SWITCH="-bp --nodeps" ;;
1545                 build-build )
1546                         BUILD_SWITCH="-bc" ;;
1547                 build-install )
1548                         BUILD_SWITCH="-bi" ;;
1549                 build-list )
1550                         BUILD_SWITCH="-bl" ;;
1551
1552         esac
1553
1554         update_shell_title "build_package: $COMMAND"
1555         if [ -n "$LOGFILE" ]; then
1556                 LOG=`eval echo $LOGFILE`
1557                 if [ -d "$LOG" ]; then
1558                         echo "Log file $LOG is a directory."
1559                         echo "Parse error in the spec?"
1560                         Exit_error err_build_fail
1561                 fi
1562                 if [ -n "$LASTLOG_FILE" ]; then
1563                         echo "LASTLOG=$LOG" > $LASTLOG_FILE
1564                 fi
1565                 RES_FILE=$(tempfile)
1566
1567                 (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
1568                 RETVAL=`cat $RES_FILE`
1569                 rm $RES_FILE
1570                 if [ -n "$LOGDIROK" ] && [ -n "$LOGDIRFAIL" ]; then
1571                         if [ "$RETVAL" -eq "0" ]; then
1572                                 mv $LOG $LOGDIROK
1573                         else
1574                                 mv $LOG $LOGDIRFAIL
1575                         fi
1576                 fi
1577         else
1578                 eval ${NICE_COMMAND} $RPMBUILD $TARGET_SWITCH $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $RPMBUILDOPTS $BCOND --define \'_specdir $PACKAGE_DIR\' --define \'_sourcedir $PACKAGE_DIR\' $SPECFILE
1579                 RETVAL=$?
1580         fi
1581         if [ "$RETVAL" -ne "0" ]; then
1582                 if [ -n "$TRY_UPGRADE" ]; then
1583                         echo "\nUpgrade package to new version failed."
1584                         if [ "$REVERT_BROKEN_UPGRADE" = "yes" ]; then
1585                                 echo "Restoring old spec file."
1586                                 mv -f $SPECFILE.bak $SPECFILE
1587                         fi
1588                         echo ""
1589                 fi
1590                 Exit_error err_build_fail
1591         fi
1592         unset BUILD_SWITCH
1593 }
1594
1595 nourl() {
1596         echo "$@" | sed 's#\<\(ftp\|http\|https\|cvs\|svn\)://[^ ]*/##g'
1597 }
1598
1599 install_required_packages() {
1600         run_poldek -vi $1
1601         return $?
1602 }
1603
1604 find_spec_bcond() { # originally from /usr/lib/rpm/find-spec-bcond
1605         local SPEC="$1"
1606         awk -F"\n" '
1607         /^%changelog/ { exit }
1608         /^%bcond_with/{
1609                 match($0, /bcond_with(out)?[ \t]+[_a-zA-Z0-9]+/);
1610                 bcond = substr($0, RSTART + 6, RLENGTH - 6);
1611                 gsub(/[ \t]+/, "_", bcond);
1612                 print bcond
1613         }' $SPEC | LC_ALL=C sort -u
1614 }
1615
1616 process_bcondrc() {
1617         # expand bconds from ~/.bcondrc
1618         # The file structure is like gentoo's package.use:
1619         # ---
1620         # * -selinux
1621         # samba -mysql -pgsql
1622         # w32codec-installer license_agreement
1623         # php +mysqli
1624         # ---
1625         if ([ -f $HOME/.bcondrc ] || ([ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ])); then
1626                 :
1627         else
1628                 return
1629         fi
1630
1631         SN=${SPECFILE%%\.spec}
1632
1633         local bcondrc=$HOME/.bcondrc
1634         [ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ] && bcondrc=$HOME_ETC/.bcondrc
1635
1636         local bcond_avail=$(find_spec_bcond $SPECFILE)
1637
1638         while read pkg flags; do
1639                 # ignore comments
1640                 [[ "$pkg" == \#* ]] && continue
1641
1642                 # any package or current package?
1643                 if [ "$pkg" = "*" ] || [ "$pkg" = "$PACKAGE_NAME" ] || [ "$pkg" = "$SN" ]; then
1644                         for flag in $flags; do
1645                                 local opt=${flag#[+-]}
1646
1647                                 # use only flags which are in this package.
1648                                 if [[ $bcond_avail = *${opt}* ]]; then
1649                                         if [[ $flag = -* ]]; then
1650                                                 if [[ $BCOND != *--with?${opt}* ]]; then
1651                                                         BCOND="$BCOND --without $opt"
1652                                                 fi
1653                                         else
1654                                                 if [[ $BCOND != *--without?${opt}* ]]; then
1655                                                         BCOND="$BCOND --with $opt"
1656                                                 fi
1657                                         fi
1658                                 fi
1659                         done
1660                 fi
1661         done < $bcondrc
1662         update_shell_title "parse ~/.bcondrc: DONE!"
1663 }
1664
1665 set_bconds_values() {
1666         update_shell_title "set bcond values"
1667
1668         AVAIL_BCONDS_WITHOUT=""
1669         AVAIL_BCONDS_WITH=""
1670
1671         if grep -Eq '^# *_with' ${SPECFILE}; then
1672                 echo >&2 "ERROR: This spec has old style bconds."
1673                 exit 1
1674         fi
1675
1676         if ! grep -q '^%bcond' ${SPECFILE}; then
1677                 return
1678         fi
1679
1680         local bcond_avail=$(find_spec_bcond $SPECFILE)
1681         process_bcondrc "$SPECFILE"
1682
1683         update_shell_title "parse bconds"
1684
1685         local opt bcond
1686         for opt in $bcond_avail; do
1687                 case "$opt" in
1688                 without_*)
1689                         bcond=${opt#without_}
1690                         case "$BCOND" in
1691                         *--without?${bcond}*)
1692                                 AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT <$bcond>"
1693                                 ;;
1694                         *)
1695                                 AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT $bcond"
1696                                 ;;
1697                         esac
1698                         ;;
1699                 with_*)
1700                         bcond=${opt#with_}
1701                         case "$BCOND" in
1702                         *--with?${bcond}*)
1703                                 AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH <$bcond>"
1704                                 ;;
1705                         *)
1706                                 AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH $bcond"
1707                                 ;;
1708                         esac
1709                         ;;
1710                 *)
1711                         echo >&2 "ERROR: unexpected '$opt' in set_bconds_values"
1712                         exit 1
1713                         ;;
1714                 esac
1715         done
1716 }
1717
1718 run_sub_builder() {
1719         package_name="${1}"
1720         update_shell_title "run_sub_builder $package_name"
1721         #
1722         # No i tutaj bym chcia³ zrobiæ sztuczn± inteligencjê, która spróbuje tego
1723         # pakieta zbudowaæ. Aktualnie niewiele dziala, bo generalnie nie widze do
1724         # konca algorytmu... Ale damy rade. :) Na razie po prostu sie wyjebie tak samo
1725         # jakby nie bylo tego kawalka kodu.
1726         #
1727         # Update: Poprawi³em parê rzeczy i zaczê³o generowaæ pakiety spoza zadanej listy.
1728         #         Jednym s³owem budowanie niespoldkowanych zale¿no¶ci dzia³a w paru przypadkach.
1729         #
1730         #
1731         # y0shi.
1732         # kurwa. translate that ^^^^
1733
1734         parent_spec_name=''
1735
1736         # Istnieje taki spec? ${package}.spec
1737         if [ -f "${PACKAGE_DIR}/${package}.spec" ]; then
1738                 parent_spec_name=${package}.spec
1739         elif [ -f "${PACKAGE_DIR}/$(echo ${package_name} | sed -e s,-devel.*,,g -e s,-static,,g).spec" ]; then
1740                 parent_spec_name="$(echo ${package_name} | sed -e s,-devel.*,,g -e s,-static,,g).spec"
1741         else
1742                 for provides_line in $(grep -r ^Provides:.*$package ${PACKAGE_DIR}); do
1743                         echo $provides_line
1744                 done
1745         fi
1746
1747         if [ "${parent_spec_name}" != "" ]; then
1748                 spawn_sub_builder $parent_spec_name
1749         fi
1750         NOT_INSTALLED_PACKAGES="$NOT_INSTALLED_PACKAGES $package_name"
1751 }
1752
1753 spawn_sub_builder() {
1754         package_name="${1}"
1755         update_shell_title "spawn_sub_builder $package_name"
1756
1757         sub_builder_opts=''
1758         if [ "${FETCH_BUILD_REQUIRES}" = "yes" ]; then
1759                 sub_builder_opts="${sub_builder_opts} -R"
1760         fi
1761         if [ "${REMOVE_BUILD_REQUIRES}" = "nice" ]; then
1762                 sub_builder_opts="${sub_builder_opts} -RB"
1763         elif [ "${REMOVE_BUILD_REQUIRES}" = "force" ]; then
1764                 sub_builder_opts="${sub_builder_opts} -FRB"
1765         fi
1766         if [ "${UPDATE_POLDEK_INDEXES}" = "yes" ]; then
1767                 sub_builder_opts="${sub_builder_opts} -Upi"
1768         fi
1769
1770         cd "${PACKAGE_DIR}"
1771         ./builder ${sub_builder_opts} "$@"
1772 }
1773
1774 remove_build_requires() {
1775         if [ "$INSTALLED_PACKAGES" != "" ]; then
1776                 case "$REMOVE_BUILD_REQUIRES" in
1777                         "force")
1778                                 run_poldek --noask -ve $INSTALLED_PACKAGES
1779                                 ;;
1780                         "nice")
1781                                 run_poldek --ask -ve $INSTALLED_PACKAGES
1782                                 ;;
1783                         *)
1784                                 echo You may want to manually remove following BuildRequires fetched:
1785                                 echo $INSTALLED_PACKAGES
1786                                 echo "Try poldek -e \`cat $(pwd)/.${SPECFILE}_INSTALLED_PACKAGES\`"
1787                                 ;;
1788                 esac
1789         fi
1790 }
1791
1792 display_bconds() {
1793         if [ "$AVAIL_BCONDS_WITH" -o "$AVAIL_BCONDS_WITHOUT" ]; then
1794                 if [ "$BCOND" ]; then
1795                         echo ""
1796                         echo "Building $SPECFILE with the following conditional flags:"
1797                         echo -n "$BCOND"
1798                 else
1799                         echo ""
1800                         echo "No conditional flags passed"
1801                 fi
1802                 echo ""
1803                 echo "from available:"
1804                 echo "--with   :\t$AVAIL_BCONDS_WITH"
1805                 echo "--without:\t$AVAIL_BCONDS_WITHOUT"
1806                 echo ""
1807         fi
1808 }
1809
1810 display_branches() {
1811         if [ "$NOCVSSPEC" != "yes" ]; then
1812                 echo -n "Available branches: "
1813                 $CVS_COMMAND status -v "${SPECFILE}" | awk '!/Sticky Tag:/ && /\(branch:/ { print $1 } ' | xargs
1814         fi
1815 }
1816
1817 # checks a given list of packages/files/provides agains current rpmdb.
1818 # outputs all dependencies which current rpmdb doesn't satisfy.
1819 # input can be either STDIN or parameters
1820 _rpm_prov_check() {
1821         local DEPS
1822
1823         if [ $# -gt 0 ]; then
1824                 DEPS="$@"
1825         else
1826                 DEPS=$(cat)
1827         fi
1828
1829         DEPS=$(LANG=C rpm -q --whatprovides $DEPS 2>&1 | awk '/^(error:|no package provides)/ { print }')
1830
1831         # packages
1832         echo "$DEPS" | awk '/^no package provides/ { print $NF }'
1833
1834         # other deps (files)
1835         echo "$DEPS" | awk -F: '/^error:.*No such file/{o = $2; gsub("^ file ", "", o); print o}'
1836 }
1837
1838 # checks if given package/files/provides exists in rpmdb.
1839 # input can be either stdin or parameters
1840 # returns packages which are present in the rpmdb
1841 _rpm_cnfl_check() {
1842         local DEPS
1843
1844         if [ $# -gt 0 ]; then
1845                 DEPS="$@"
1846         else
1847                 DEPS=$(cat)
1848         fi
1849
1850         LANG=C rpm -q --whatprovides $DEPS 2>/dev/null | awk '!/no package provides/ { print }'
1851 }
1852
1853 # install deps via information from 'rpm-getdeps' or 'rpm --specsrpm'
1854 install_build_requires_rpmdeps() {
1855         if [ "$FETCH_BUILD_REQUIRES_RPMGETDEPS" = "yes" ]; then
1856                 # TODO: Conflicts list doesn't check versions
1857                 local CNFL=$(rpm-getdeps $BCOND $SPECFILE 2> /dev/null | awk '/^\-/ { print $3 } ' | _rpm_cnfl_check | xargs)
1858                 local DEPS=$(rpm-getdeps $BCOND $SPECFILE 2> /dev/null | awk '/^\+/ { print $3 } ' | _rpm_prov_check | xargs)
1859         fi
1860         if [ "$FETCH_BUILD_REQUIRES_RPMSPECSRPM" = "yes" ]; then
1861                 local CNFL=$(rpm -q --specsrpm --conflicts $BCOND $SPECFILE | awk '{print $1}' | _rpm_cnfl_check | xargs)
1862                 local DEPS=$(rpm -q --specsrpm --requires $BCOND $SPECFILE | awk '{print $1}' | _rpm_prov_check | xargs)
1863         fi
1864
1865         if [ -n "$CNFL" ] || [ -n "$DEPS" ]; then
1866                 echo "fetch BuildRequires: install [$DEPS]; remove [$CNFL]"
1867                 update_shell_title "poldek: install [$DEPS]; remove [$CNFL]"
1868                 $SU_SUDO /usr/bin/poldek -q --update || $SU_SUDO /usr/bin/poldek -q --upa
1869         fi
1870         if [ -n "$CNFL" ]; then
1871                 update_shell_title "uninstall conflicting packages: $CNFL"
1872                 echo "Trying to uninstall conflicting packages ($CNFL):"
1873                 $SU_SUDO /usr/bin/poldek --noask --nofollow -ev $CNFL
1874         fi
1875
1876         while [ "$DEPS" ]; do
1877                         update_shell_title "install deps: $DEPS"
1878                         echo "Trying to install dependencies ($DEPS):"
1879                         local log=.${SPECFILE}_poldek.log
1880                         LANG=C $SU_SUDO /usr/bin/poldek --noask --caplookup -uGqQ $DEPS | tee $log
1881                         failed=$(awk '/^error:/{a=$2; sub(/^error: /, "", a); sub(/:$/, "", a); print a}' $log)
1882                         rm -f $log
1883                         local ok
1884                         if [ -n "$failed" ]; then
1885                                 for package in $failed; do
1886                                         spawn_sub_builder -bb $(depspecname $package) && ok="$ok $package"
1887                                 done
1888                                 DEPS="$ok"
1889                         else
1890                                 DEPS=""
1891                         fi
1892         done
1893 }
1894
1895 fetch_build_requires()
1896 {
1897         if [ "${FETCH_BUILD_REQUIRES}" != "yes" ]; then
1898                 return
1899         fi
1900
1901         update_shell_title "fetch build requires"
1902         if [ "$FETCH_BUILD_REQUIRES_RPMGETDEPS" = "yes" ] || [ "$FETCH_BUILD_REQUIRES_RPMSPECSRPM" = "yes" ]; then
1903                 install_build_requires_rpmdeps
1904                 return
1905         fi
1906
1907                 # XXX is this ugliest code written in human history still needed?
1908                 echo "All packages installed by fetch_build_requires() are written to:"
1909                 echo "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES"
1910                 echo ""
1911                 echo "If anything fails, you may get rid of them by executing:"
1912                 echo "poldek -e \`cat `pwd`/.${SPECFILE}_INSTALLED_PACKAGES\`"
1913                 echo ""
1914                 echo > `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1915                 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)
1916                 do
1917                         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)
1918                         GO="yes"
1919                         package=$(basename "$package_item"|sed -e "s/}$//g")
1920                         COND_ARCH_TST=$(cat $SPECFILE|grep -B1 BuildRequires|grep -B1 $package|grep ifarch|sed -e "s/^.*ifarch//g")
1921                         mach=$(uname -m)
1922
1923                         COND_TST=`cat $SPECFILE|grep BuildRequires|grep "$package"`
1924                         if `echo $COND_TST|grep -q '^BuildRequires:'`; then
1925                                 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
1926                                         GO="yes"
1927                                 fi
1928                         # bcond:
1929                         else
1930                                 COND_NAME=`echo $COND_TST|sed -e s,:BuildRequires:.*$,,g`
1931                                 GO=""
1932                                 # %{without}
1933                                 if `echo $COND_TST|grep -q 'without_'`; then
1934                                         COND_NAME=`echo $COND_NAME|sed -e s,^.*without_,,g`
1935                                         if `echo $COND_TST|grep -q !`; then
1936                                                 COND_STATE="with"
1937                                         else
1938                                                 COND_STATE="wout"
1939                                         fi
1940                                         COND_WITH=`echo $AVAIL_BCONDS_WITH|grep "<$COND_NAME>"`
1941                                         COND_WITHOUT=`echo $AVAIL_BCONDS_WITHOUT|grep "<$COND_NAME>"`
1942                                         if [ -n "$COND_WITHOUT" ] || [ -z "$COND_WITH" ]; then
1943                                                 COND_ARGV="wout"
1944                                         else
1945                                                 COND_ARGV="with"
1946                                         fi
1947                                 # %{with}
1948                                 elif `echo $COND_TST|grep -q 'with_'`; then
1949                                         COND_NAME=`echo $COND_NAME|sed -e s,^.*with_,,g`
1950                                         if `echo $COND_TST|grep -q !`; then
1951                                                 COND_STATE="wout"
1952                                         else
1953                                                 COND_STATE="with"
1954                                         fi
1955                                         COND_WITH=`echo $AVAIL_BCONDS_WITH|grep "<$COND_NAME>"`
1956                                         COND_WITHOUT=`echo $AVAIL_BCONDS_WITHOUT|grep "<$COND_NAME>"`
1957                                         if [ -n "$COND_WITH" ] || [ -z "$COND_WITHOUT" ]; then
1958                                                 COND_ARGV="with"
1959                                         else
1960                                                 COND_ARGV="wout"
1961                                         fi
1962                                 fi
1963                                 RESULT="${COND_STATE}-${COND_ARGV}"
1964                                 case "$RESULT" in
1965                                         "with-wout" | "wout-with" )
1966                                                 GO=""
1967                                                 ;;
1968                                         "wout-wout" | "with-with" )
1969                                                 GO="yes"
1970                                                 ;;
1971                                         * )
1972                                                 echo "Action '$RESULT' was not defined for package '$package_item'"
1973                                                 GO="yes"
1974                                                 ;;
1975                                 esac
1976                         fi
1977
1978                         if [ "$GO" = "yes" ]; then
1979                                 if [ "`rpm -q $package|sed -e "s/$package.*/$package/g"`" != "$package" ]; then
1980                                         echo "Testing if $package has subrequirements..."
1981                                         run_poldek -t -i $package --dumpn=".$package-req.txt"
1982                                         if [ -f ".$package-req.txt" ]; then
1983                                                 for package_name in `cat ".$package-req.txt"|grep -v ^#`
1984                                                 do
1985                                                         if [ "$package_name" = "$package" ]; then
1986                                                                 echo "Installing BuildRequired package:\t$package_name"
1987                                                                 update_shell_title "Installing BuildRequired package: ${package_name}"
1988                                                                 install_required_packages $package
1989                                                         else
1990                                                                 echo "Installing (sub)Required package:\t$package_name"
1991                                                                 update_shell_title "Installing (sub)Required package: ${package_name}"
1992                                                                 install_required_packages $package_name
1993                                                         fi
1994                                                         case $? in
1995                                                                 0)
1996                                                                         INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
1997                                                                         echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1998                                                                         ;;
1999                                                                 *)
2000                                                                         echo "Attempting to run spawn sub - builder..."
2001                                                                         echo "Package installation failed:\t$package_name"
2002                                                                         run_sub_builder $package_name
2003                                                                         if [ $? -eq 0 ]; then
2004                                                                                 install_required_packages $package_name
2005                                                                                 case $? in
2006                                                                                         0)
2007                                                                                                 INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
2008                                                                                                 echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
2009                                                                                                 ;;
2010                                                                                         *)
2011                                                                                                 NOT_INSTALLED_PACKAGES="$package_name $NOT_INSTALLED_PACKAGES"
2012                                                                                                 ;;
2013                                                                                 esac
2014                                                                         fi
2015                                                                         ;;
2016                                                         esac
2017                                                 done
2018                                                 rm -f ".$package-req.txt"
2019                                         else
2020                                                 echo "Attempting to run spawn sub - builder..."
2021                                                 echo "Package installation failed:\t$package"
2022                                                 run_sub_builder $package
2023                                                 if [ $? -eq 0 ]; then
2024                                                         install_required_packages $package
2025                                                         case $? in
2026                                                                 0)
2027                                                                         INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
2028                                                                         echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
2029                                                                         ;;
2030                                                                 *)
2031                                                                         NOT_INSTALLED_PACKAGES="$package_name $NOT_INSTALLED_PACKAGES"
2032                                                                         ;;
2033                                                         esac
2034                                                 fi
2035                                         fi
2036                                 else
2037                                         echo "Package $package is already installed. BuildRequirement satisfied."
2038                                 fi
2039                         fi
2040                 done
2041                 if [ "$NOT_INSTALLED_PACKAGES" != "" ]; then
2042                         echo >&2 "Unable to install following packages and their dependencies:"
2043                         for pkg in "$NOT_INSTALLED_PACKAGES"
2044                         do
2045                                 echo $pkg
2046                         done
2047                         remove_build_requires
2048                         exit 8
2049                 fi
2050 }
2051
2052 init_rpm_dir() {
2053         local CVSROOT=":pserver:cvs@$CVS_SERVER:/cvsroot"
2054         local TOP_DIR=$(eval $RPM $RPMOPTS --eval '%{_topdir}')
2055         local rpmdir=$(eval $RPM $RPMOPTS --eval '%{_rpmdir}')
2056         local buildir=$(eval $RPM $RPMOPTS --eval '%{_builddir}')
2057         local srpmdir=$(eval $RPM $RPMOPTS --eval '%{_srcrpmdir}')
2058         local tmp
2059
2060         echo "Initializing rpm directories to $TOP_DIR from $CVSROOT"
2061         mkdir -p $TOP_DIR $rpmdir $buildir $srpmdir
2062
2063         # need to checkout to tmp dir or we can't name our topdir
2064         tmp=$(TMPDIR= TEMPDIR= mktemp -p $TOP_DIR -d) || exit 1
2065         cd $tmp
2066         $CVS_COMMAND -d $CVSROOT co packages/{.cvsignore,rpm.groups,dropin,mirrors,md5,adapter{,.awk},fetchsrc_request,builder,{relup,compile,repackage}.sh,ci,civim}
2067         cd -
2068         mv $tmp/packages/* $TOP_DIR
2069         rm -rf $tmp
2070
2071         init_builder
2072
2073         echo "To checkout *all* .spec files (read-only):"
2074         echo "- run cvs co SPECS"
2075
2076         echo "To checkout *all* packages:"
2077         echo "- run cvs up -dP in $TOP_DIR dir"
2078
2079         echo ""
2080         echo "To commit with your developer account:"
2081         echo "- edit $TOP_DIR/CVS/Root"
2082 }
2083
2084 get_greed_sources() {
2085         CVSROOT=":pserver:cvs@$CVS_SERVER:/cvsroot"
2086         if [ -n "BE_VERBOSE" ]; then
2087                 echo "Try greed download: $1 from: $CVSROOT"
2088         fi
2089         $CVS_COMMAND -d $CVSROOT get SOURCES/$1
2090         if [ $? != 0 ]; then
2091                 Exit_error err_no_source_in_repo $1
2092         fi
2093
2094 }
2095
2096 # remove entries from CVS/Entries
2097 cvs_entry_remove() {
2098         local cvsdir="$1"; shift
2099         if [ ! -d "$cvsdir" ]; then
2100                 echo >&2 "cvs_entry_remove: $cvsdir is not a directory"
2101                 exit 1
2102         fi
2103
2104         for file in "$@"; do
2105                 rm -f $cvsdir/CVS/Entries.new || return 1
2106                 awk -ve="${file##*/}" -F/ '$2 != e {print}' $cvsdir/CVS/Entries > $cvsdir/CVS/Entries.new || return 1
2107                 mv -f $cvsdir/CVS/Entries.new $cvsdir/CVS/Entries || return 1
2108         done
2109         return 0
2110 }
2111
2112 mr_proper() {
2113         init_builder
2114         NOCVSSPEC="yes"
2115         DONT_PRINT_REVISION="yes"
2116         get_spec
2117         parse_spec
2118
2119         # remove from CVS/Entries
2120         cvs_entry_remove $PACKAGE_DIR $SPECFILE $SOURCES $PATCHES
2121
2122         # remove spec and sources
2123         $RPMBUILD --clean --rmsource --rmspec --nodeps --define "_specdir $PACKAGE_DIR" --define "_sourcedir $PACKAGE_DIR" $SPECFILE
2124 }
2125
2126 #---------------------------------------------
2127 # main()
2128
2129 if [ $# = 0 ]; then
2130         usage
2131         exit 1
2132 fi
2133
2134 while [ $# -gt 0 ]; do
2135         case "${1}" in
2136                 -4|-6)
2137                         # NOTE: we should be fetcher specific, like fille WGET_OPTS, but
2138                         # unfortunately $GETURI is already formed
2139                         GETURI="$GETURI $1"
2140                         shift
2141                         ;;
2142                 -5 | --update-md5)
2143                         COMMAND="update_md5"
2144                         NODIST="yes"
2145                         NOCVSSPEC="yes"
2146                         shift ;;
2147                 -a5 | --add-md5 )
2148                         COMMAND="update_md5"
2149                         NODIST="yes"
2150                         NOCVS="yes"
2151                         NOCVSSPEC="yes"
2152                         ADD5="yes"
2153                         shift ;;
2154                 -n5 | --no-md5 )
2155                         NO5="yes"
2156                         shift ;;
2157                 -D | --debug )
2158                         DEBUG="yes"; shift ;;
2159                 -V | --version )
2160                         COMMAND="version"; shift ;;
2161                 --short-version )
2162                         COMMAND="short-version"; shift ;;
2163                 --as_anon )
2164                         CVSROOT=":pserver:cvs@$CVS_SERVER:/cvsroot"; shift ;;
2165                 -a | --add_cvs)
2166                         COMMAND="add_cvs";
2167                         shift ;;
2168                 -b | -ba | --build )
2169                         COMMAND="build"; shift ;;
2170                 -bb | --build-binary )
2171                         COMMAND="build-binary"; shift ;;
2172                 -bc )
2173                         COMMAND="build-build"; shift ;;
2174                 -bi )
2175                         COMMAND="build-install"; shift ;;
2176                 -bl )
2177                         COMMAND="build-list"; shift ;;
2178                 -bp | --build-prep )
2179                         COMMAND="build-prep"; shift ;;
2180                 -bs | --build-source )
2181                         COMMAND="build-source"; shift ;;
2182                 -B | --branch )
2183                         COMMAND="branch"; shift; TAG="${1}"; shift;;
2184                 -c | --clean )
2185                         CLEAN="--clean"; shift ;;
2186                 -cf | --cvs-force )
2187                         CVS_FORCE="-F"; shift;;
2188                 -d | --cvsroot )
2189                         shift; CVSROOT="${1}"; shift ;;
2190                 -g | --get )
2191                         COMMAND="get"; shift ;;
2192                 -h | --help )
2193                         COMMAND="usage"; shift ;;
2194                 --ftp )
2195                         PROTOCOL="ftp"; shift ;;
2196                 --http )
2197                         PROTOCOL="http"; shift ;;
2198                 -j)
2199                         RPMOPTS="${RPMOPTS} --define \"_smp_mflags -j$2\""
2200                         shift 2
2201                         ;;
2202                 -j[0-9]*)
2203                         RPMOPTS="${RPMOPTS} --define \"_smp_mflags $1\""
2204                         shift
2205                         ;;
2206                 -l | --logtofile )
2207                         shift; LOGFILE="${1}"; shift ;;
2208                 -ni| --nice )
2209                         shift; DEF_NICE_LEVEL=${1}; shift ;;
2210                 -ske | --skip-existing-files)
2211                         SKIP_EXISTING_FILES="yes"; shift ;;
2212                 -m | --mr-proper )
2213                         COMMAND="mr-proper"; shift ;;
2214                 -nc | --no-cvs )
2215                         NOCVS="yes"; shift ;;
2216                 -ncs | --no-cvs-specs )
2217                         NOCVSSPEC="yes"; shift ;;
2218                 -nd | --no-distfiles )
2219                         NODIST="yes"; shift ;;
2220                 -nm | --no-mirrors )
2221                         NOMIRRORS="yes"; shift ;;
2222                 -nu | --no-urls )
2223                         NOURLS="yes"; shift ;;
2224                 -ns | --no-srcs )
2225                         NOSRCS="yes"; shift ;;
2226                 -ns0 | --no-source0 )
2227                         NOSOURCE0="yes"; shift ;;
2228                 -nn | --no-net )
2229                         NOCVS="yes"
2230                         NOCVSSPEC="yes"
2231                         NODIST="yes"
2232                         NOMIRRORS="yes"
2233                         NOURLS="yes"
2234                         NOSRCS="yes"
2235                         ALWAYS_CVSUP="no"
2236                         shift;;
2237                 -pm | --prefer-mirrors )
2238                         PREFMIRRORS="yes"
2239                         shift;;
2240                 --no-init )
2241                         NOINIT="yes"
2242                         shift;;
2243                 --opts )
2244                         shift; RPMOPTS="${RPMOPTS} ${1}"; shift ;;
2245                 --nopatch | -np )
2246                         shift; RPMOPTS="${RPMOPTS} --define \"patch${1} : ignoring patch${1}; exit 1; \""; shift ;;
2247                 --skip-patch | -sp )
2248                         shift; RPMOPTS="${RPMOPTS} --define \"patch${1} : skiping patch${1}\""; shift ;;
2249                 --topdir)
2250                         RPMOPTS="${RPMOPTS} --define \"_topdir $2\""
2251                         shift 2
2252                         ;;
2253                 --with | --without )
2254                         case $GROUP_BCONDS in
2255                                 "yes")
2256                                         COND=${1}
2257                                         shift
2258                                         # XXX: broken: ./builder -bb ucspi-tcp.spec --without mysql
2259                                         while ! `echo ${1}|grep -qE '(^-|spec)'`
2260                                         do
2261                                                 BCOND="$BCOND $COND $1"
2262                                                 shift
2263                                         done;;
2264                                 "no")
2265                                         if [[ "$2" = *,* ]]; then
2266                                                 for a in $(echo "$2" | tr , ' '); do
2267                                                         BCOND="$BCOND $1 $a"
2268                                                 done
2269                                         else
2270                                                 BCOND="$BCOND $1 $2"
2271                                         fi
2272                                         shift 2 ;;
2273                         esac
2274                         ;;
2275                 --target )
2276                         shift; TARGET="${1}"; shift ;;
2277                 --target=* )
2278                         TARGET=$(echo "${1}" | sed 's/^--target=//'); shift ;;
2279                 -q | --quiet )
2280                         QUIET="--quiet"; shift ;;
2281                 --date )
2282                         CVSDATE="${2}"; shift 2 ;;
2283                 -r | --cvstag )
2284                         CVSTAG="$2"
2285                         shift 2
2286                         ;;
2287                 -A)
2288                         shift
2289                         CVSTAG="HEAD"
2290                         ;;
2291                 -R | --fetch-build-requires)
2292                         FETCH_BUILD_REQUIRES="yes"
2293                         NOT_INSTALLED_PACKAGES=
2294                         shift ;;
2295                 -RB | --remove-build-requires)
2296                         REMOVE_BUILD_REQUIRES="nice"
2297                         shift ;;
2298                 -FRB | --force-remove-build-requires)
2299                         REMOVE_BUILD_REQUIRES="force"
2300                         shift ;;
2301                 -sc | --sources-cvs)
2302                         COMMAND="list-sources-cvs"
2303                         shift ;;
2304                 -sd | --source-distfiles)
2305                         COMMAND="list-sources-distfiles"
2306                         shift ;;
2307                 -sdp | --source-distfiles-paths)
2308                         COMMAND="list-sources-distfiles-paths"
2309                         shift ;;
2310                 -sf | --source-files)
2311                         COMMAND="list-sources-files"
2312                         shift ;;
2313                 -lsp | --source-paths)
2314                         COMMAND="list-sources-local-paths"
2315                         shift ;;
2316                 -su | --source-urls)
2317                         COMMAND="list-sources-urls"
2318                         shift ;;
2319                 -Tvs | --tag-version-stable )
2320                         COMMAND="tag"
2321                         TAG="STABLE"
2322                         TAG_VERSION="yes"
2323                         shift;;
2324                 -Ts | --tag-stable )
2325                         COMMAND="tag"
2326                         TAG="STABLE"
2327                         TAG_VERSION="no"
2328                         shift;;
2329                 -Tv | --tag-version )
2330                         COMMAND="tag"
2331                         TAG=""
2332                         TAG_VERSION="yes"
2333                         shift;;
2334                 -Tp | --tag-prefix )
2335                         TAG_PREFIX="$2"
2336                         shift 2;;
2337                 -tt | --test-tag )
2338                         TEST_TAG="yes"
2339                         shift;;
2340                 -T | --tag )
2341                         COMMAND="tag"
2342                         shift
2343                         TAG="$1"
2344                         TAG_VERSION="no"
2345                         shift;;
2346                 -ir | --integer-release-only )
2347                         INTEGER_RELEASE="yes"
2348                         shift;;
2349                 -U | --update )
2350                         COMMAND="update_md5"
2351                         UPDATE="yes"
2352                         NOCVSSPEC="yes"
2353                         NODIST="yes"
2354                         shift ;;
2355                 -Upi | --update-poldek-indexes )
2356                         UPDATE_POLDEK_INDEXES="yes"
2357                         shift ;;
2358                 --init-rpm-dir)
2359                         COMMAND="init_rpm_dir"
2360                         shift ;;
2361                 --use-greed-sources )
2362                         GREEDSRC="1"
2363                         shift;;
2364                 -u | --try-upgrade )
2365                         TRY_UPGRADE="1"; shift ;;
2366                 --upgrade-version )
2367                         shift; UPGRADE_VERSION="$1"; shift;;
2368                 -un | --try-upgrade-with-float-version )
2369                         TRY_UPGRADE="1"; FLOAT_VERSION="1"; shift ;;
2370                 -v | --verbose )
2371                         BE_VERBOSE="1"; shift ;;
2372                 --define)
2373                         shift
2374                         MACRO="${1}"
2375                         shift
2376                         if echo "${MACRO}" | grep -q '\W'; then
2377                                 RPMOPTS="${RPMOPTS} --define \"${MACRO}\""
2378                         else
2379                                 VALUE="${1}"
2380                                 shift
2381                                 RPMOPTS="${RPMOPTS} --define \"${MACRO} ${VALUE}\""
2382                         fi
2383                         ;;
2384                 --alt_kernel)
2385                         shift
2386                         RPMOPTS="${RPMOPTS} --define \"alt_kernel $1\""
2387                         shift
2388                         ;;
2389                 --short-circuit)
2390                         RPMBUILDOPTS="${RPMBUILDOPTS} --short-circuit"
2391                         shift
2392                         ;;
2393                 --show-bconds | -show-bconds | -print-bconds | --print-bconds | -display-bconds | --display-bconds )
2394                         COMMAND="show_bconds"
2395                         shift
2396                         ;;
2397                 --show-bcond-args)
2398                         COMMAND="show_bcond_args"
2399                         shift
2400                         ;;
2401                 --show-avail-bconds)
2402                         COMMAND="show_avail_bconds"
2403                         shift
2404                         ;;
2405                 --nodeps)
2406                         shift
2407                         RPMOPTS="${RPMOPTS} --nodeps"
2408                         ;;
2409                 -debug)
2410                         RPMBUILDOPTS="${RPMBUILDOPTS} -debug"; shift
2411                         ;;
2412                 -*)
2413                         Exit_error err_invalid_cmdline "$1"
2414                         ;;
2415                 *)
2416                         SPECFILE=${1%/}; shift
2417                         # check if specname was passed as specname:cvstag
2418                         if [ "${SPECFILE##*:}" != "${SPECFILE}" ]; then
2419                                 CVSTAG="${SPECFILE##*:}"
2420                                 SPECFILE="${SPECFILE%%:*}"
2421                         fi
2422                         # always have SPECFILE ending with .spec extension
2423                         SPECFILE=${SPECFILE%%.spec}.spec
2424                         ASSUMED_NAME=$(basename ${SPECFILE%%.spec})
2425         esac
2426 done
2427
2428 [ -d "$ASSUMED_NAME" ] && CVS_ENTRIES="$ASSUMED_NAME/CVS/Entries" || CVS_ENTRIES="CVS/Entries"
2429 if [ -f "$CVS_ENTRIES" ] && [ -z "$CVSTAG" ]; then
2430         CVSTAG=$(awk -vSPECFILE=$(basename $SPECFILE) -F/ '$2 == SPECFILE && $6 ~ /^T/{print substr($6, 2)}' ${CVS_ENTRIES})
2431         if [ "$CVSTAG" ]; then
2432                 echo >&2 "builder: Sticky tag $CVSTAG active. Use -r TAGNAME to override."
2433         fi
2434 elif [ "$CVSTAG" = "HEAD" ]; then
2435         # assume -r HEAD is same as -A
2436         CVSTAG=""
2437 fi
2438
2439 if [ "$CVSTAG" ]; then
2440         # pass $CVSTAG used by builder to rpmbuild too, so specs could use it
2441         RPMOPTS="$RPMOPTS --define \"_cvstag $CVSTAG\""
2442 fi
2443
2444 if [ -n "$DEBUG" ]; then
2445         set -x
2446         set -v
2447 fi
2448
2449 if [ -n "$TARGET" ]; then
2450         case "$RPMBUILD" in
2451                 "rpmbuild")
2452                         TARGET_SWITCH="--target $TARGET" ;;
2453                 "rpm")
2454                         TARGET_SWITCH="--target=$TARGET" ;;
2455         esac
2456 fi
2457
2458 if [ "$SCHEDTOOL" != "no" ]; then
2459         NICE_COMMAND="$SCHEDTOOL"
2460 else
2461         NICE_COMMAND="nice -n ${DEF_NICE_LEVEL}"
2462 fi
2463
2464 update_shell_title "$COMMAND"
2465 case "$COMMAND" in
2466         "show_bconds")
2467                 init_builder
2468                 if [ -z "$SPECFILE" ]; then
2469                         Exit_error err_no_spec_in_cmdl
2470                 fi
2471                 get_spec > /dev/null
2472                 parse_spec
2473                 set_bconds_values
2474                 display_bconds
2475                 ;;
2476         "show_bcond_args")
2477                 init_builder
2478                 if [ -z "$SPECFILE" ]; then
2479                         Exit_error err_no_spec_in_cmdl
2480                 fi
2481                 get_spec > /dev/null
2482                 parse_spec
2483                 set_bconds_values
2484                 echo "$BCOND"
2485                 ;;
2486         "show_avail_bconds")
2487                 init_builder
2488                 if [ -z "$SPECFILE" ]; then
2489                         Exit_error err_no_spec_in_cmdl
2490                 fi
2491
2492                 get_spec > /dev/null
2493                 parse_spec
2494                 local bcond_avail=$(find_spec_bcond $SPECFILE)
2495                 local opt bcond bconds
2496                 for opt in $bcond_avail; do
2497                         case "$opt" in
2498                         without_*)
2499                                 bcond=${opt#without_}
2500                                 bconds="$bconds $bcond"
2501                                 ;;
2502                         with_*)
2503                                 bcond=${opt#with_}
2504                                 bconds="$bconds $bcond"
2505                                 ;;
2506                         *)
2507                                 echo >&2 "ERROR: unexpected '$opt' in show_avail_bconds"
2508                                 exit 1
2509                                 ;;
2510                         esac
2511                 done
2512                 echo $bconds
2513
2514                 ;;
2515         "build" | "build-binary" | "build-source" | "build-prep" | "build-build" | "build-install" | "build-list")
2516                 init_builder
2517                 if [ -z "$SPECFILE" ]; then
2518                         Exit_error err_no_spec_in_cmdl
2519                 fi
2520
2521                 # display SMP make flags if set
2522                 smp_mflags=$(rpm -E %{?_smp_mflags})
2523                 if [ "$smp_mflags" ]; then
2524                         echo "builder: SMP make flags are set to $smp_mflags"
2525                 fi
2526
2527                 get_spec
2528                 parse_spec
2529                 set_bconds_values
2530                 display_bconds
2531                 display_branches
2532                 if [ "$COMMAND" != "build-source" ]; then
2533                         check_buildarch
2534                 fi
2535                 fetch_build_requires
2536                 if [ "$INTEGER_RELEASE" = "yes" ]; then
2537                         echo "Checking release $PACKAGE_RELEASE..."
2538                         if echo $PACKAGE_RELEASE | grep -q '^[^.]*\.[^.]*$' 2>/dev/null ; then
2539                                 Exit_error err_fract_rel "$PACKAGE_RELEASE"
2540                         fi
2541                 fi
2542
2543                 # ./builder -bs test.spec -r AC-branch -Tp auto-ac- -tt
2544                 if [ -n "$TEST_TAG" ]; then
2545                         local TAGVER=`make_tagver`
2546                         echo "Searching for tag $TAGVER..."
2547                         TAGREL=$($CVS_COMMAND status -v $SPECFILE | grep -E "^[[:space:]]*${TAGVER}[[[:space:]]" | sed -e 's#.*(revision: ##g' -e 's#).*##g')
2548                         if [ -n "$TAGREL" ]; then
2549                                 Exit_error err_tag_exists "$TAGVER" "$TAGREL"
2550                         fi
2551
2552                         # - do not allow to build from HEAD when XX-branch exists
2553                         TREE_PREFIX=$(echo "$TAG_PREFIX" | sed -e 's#^auto-\([a-zA-Z]\+\)-.*#\1#g')
2554                         if [ "$TREE_PREFIX" != "$TAG_PREFIX" ]; then
2555                                 TAG_BRANCH="${TREE_PREFIX}-branch"
2556                                 TAG_STATUS=$($CVS_COMMAND status -v $SPECFILE | grep -Ei "${TAG_BRANCH}.+(branch: [0-9.]+)")
2557                                 if [ -n "$TAG_STATUS" -a "$CVSTAG" = "HEAD" ]; then
2558                                         Exit_error err_branch_exists "$TAG_STATUS"
2559                                 fi
2560                         fi
2561
2562                 fi
2563
2564                 if [ -n "$NOSOURCE0" ] ; then
2565                         SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2566                 fi
2567                 try_upgrade
2568                 case $? in
2569                         0)
2570                                 get_files $SOURCES $PATCHES
2571                                 check_md5 $SOURCES
2572                                 ;;
2573                         *)
2574                                 NODIST="yes" get_files $SOURCES $PATCHES
2575                                 update_md5 $SOURCES
2576                                 ;;
2577                 esac
2578                 build_package
2579                 if [ "$UPDATE_POLDEK_INDEXES" = "yes" -a "$COMMAND" != "build-prep" ]; then
2580                         run_poldek --sdir="${POLDEK_INDEX_DIR}" --mkidxz
2581                 fi
2582                 remove_build_requires
2583                 ;;
2584         "branch" )
2585                 init_builder
2586                 if [ -z "$SPECFILE" ]; then
2587                         Exit_error err_no_spec_in_cmdl
2588                 fi
2589
2590                 get_spec
2591                 parse_spec
2592                 # don't fetch sources from remote locations
2593                 new_SOURCES=""
2594                 for file in $SOURCES; do
2595                         [ -n "`src_md5 $file`" ] && continue
2596                         new_SOURCES="$new_SOURCES $file"
2597                 done
2598                 SOURCES="$new_SOURCES"
2599                 get_files $SOURCES $PATCHES
2600                 check_md5 $SOURCES
2601                 branch_files $TAG $SOURCES $PATCHES $ICONS
2602                 ;;
2603         "add_cvs" )
2604                 init_builder
2605                 if [ -z "$SPECFILE" ]; then
2606                         Exit_error err_no_spec_in_cmdl
2607                 fi
2608
2609                 ADD_PACKAGE_CVS=yes get_spec
2610                 parse_spec
2611
2612                 if [ -n "$NOSOURCE0" ] ; then
2613                         SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2614                 fi
2615                 get_files $SOURCES $PATCHES
2616                 check_md5 $SOURCES
2617                 ;;
2618         "get" )
2619                 init_builder
2620                 if [ -z "$SPECFILE" ]; then
2621                         Exit_error err_no_spec_in_cmdl
2622                 fi
2623
2624                 get_spec
2625                 parse_spec
2626
2627                 if [ -n "$NOSOURCE0" ] ; then
2628                         SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2629                 fi
2630                 get_files $SOURCES $PATCHES
2631                 check_md5 $SOURCES
2632                 ;;
2633         "update_md5" )
2634                 init_builder
2635                 if [ -z "$SPECFILE" ]; then
2636                         Exit_error err_no_spec_in_cmdl
2637                 fi
2638
2639                 get_spec
2640                 parse_spec
2641
2642                 if [ -n "$NOSOURCE0" ] ; then
2643                         SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2644                 fi
2645                 update_md5 $SOURCES
2646                 ;;
2647         "tag" )
2648                 NOURLS=1
2649                 NODIST="yes"
2650                 init_builder
2651                 if [ -z "$SPECFILE" ]; then
2652                         Exit_error err_no_spec_in_cmdl
2653                 fi
2654
2655                 get_spec
2656                 parse_spec
2657
2658                 # don't fetch sources from remote locations
2659                 new_SOURCES=""
2660                 for file in $SOURCES; do
2661                         [ -n "`src_md5 $file`" ] && continue
2662                         new_SOURCES="$new_SOURCES $file"
2663                 done
2664                 SOURCES="$new_SOURCES"
2665                 get_files $SOURCES $PATCHES
2666                 check_md5 $SOURCES
2667                 tag_files $SOURCES $PATCHES $ICONS
2668                 ;;
2669         "mr-proper" )
2670                 mr_proper
2671                 ;;
2672         "list-sources-files" )
2673                 init_builder
2674                 NOCVSSPEC="yes"
2675                 DONT_PRINT_REVISION="yes"
2676                 get_spec
2677                 parse_spec
2678                 for SAP in $SOURCES $PATCHES; do
2679                         echo $SAP | awk '{gsub(/.*\//,"") ; print}'
2680                 done
2681                 ;;
2682         "list-sources-urls" )
2683                 init_builder
2684                 NOCVSSPEC="yes"
2685                 DONT_PRINT_REVISION="yes"
2686                 get_spec
2687                 parse_spec
2688                 SAPS="$SOURCES $PATCHES"
2689                 for SAP in $SAPS ; do
2690                         echo $SAP
2691                 done
2692                 ;;
2693         "list-sources-local-paths" )
2694                 init_builder
2695                 NOCVSSPEC="yes"
2696                 DONT_PRINT_REVISION="yes"
2697                 get_spec
2698                 parse_spec
2699                 for SAP in $SOURCES $PATCHES; do
2700                         echo $PACKAGE_DIR/$(echo $SAP | awk '{gsub(/.*\//,"") ; print }')
2701                 done
2702                 ;;
2703         "list-sources-distfiles-paths" )
2704                 init_builder
2705                 NOCVSSPEC="yes"
2706                 DONT_PRINT_REVISION="yes"
2707                 get_spec
2708                 parse_spec
2709                 for SAP in $SOURCES $PATCHES; do
2710                         if [ -n "$(src_md5 "$SAP")" ]; then
2711                                 distfiles_path "$SAP"
2712                         fi
2713                 done
2714                 ;;
2715         "list-sources-distfiles" )
2716                 init_builder
2717                 NOCVSSPEC="yes"
2718                 DONT_PRINT_REVISION="yes"
2719                 get_spec
2720                 parse_spec
2721                 for SAP in $SOURCES $PATCHES; do
2722                         if [ -n "$(src_md5 "$SAP")" ]; then
2723                                 distfiles_url "$SAP"
2724                         fi
2725                 done
2726                 ;;
2727         "list-sources-cvs" )
2728                 init_builder
2729 #               NOCVSSPEC="yes"
2730                 DONT_PRINT_REVISION="yes"
2731                 get_spec
2732                 parse_spec
2733                 for SAP in $SOURCES $PATCHES; do
2734                         if [ -z "$(src_md5 "$SAP")" ]; then
2735                                 echo $SAP | awk '{gsub(/.*\//,"") ; print}'
2736                         fi
2737                 done
2738                 ;;
2739         "init_rpm_dir")
2740                 init_rpm_dir
2741                 ;;
2742         "usage" )
2743                 usage
2744                 ;;
2745         "short-version" )
2746                 echo "$VERSION"
2747                 ;;
2748         "version" )
2749                 echo "$VERSIONSTRING"
2750                 ;;
2751 esac
2752 if [ -f "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES" -a "$REMOVE_BUILD_REQUIRES" != "" ]; then
2753         rm "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES"
2754 fi
2755 cd "$__PWD"
2756
2757 # vi:syntax=sh:ts=4:sw=4:noet
This page took 0.286246 seconds and 4 git commands to generate.