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