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