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