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