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