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