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