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