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