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