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