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