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