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