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