]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - builder.sh
No need to download sources for tagging or branching
[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 .gitignore
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 [ -n "$CVSTAG" ]; then
788                 GIT_WORK_TREE=$PACKAGE_DIR GIT_DIR=$PACKAGE_DIR/.git git checkout "$CVSTAG" || exit
789         fi
790
791         if [ ! -f "$ASSUMED_NAME/$SPECFILE" ]; then
792                 Exit_error err_no_spec_in_repo
793         fi
794
795         if [ "$CHMOD" = "yes" -a -n "$SPECFILE" ]; then
796                 chmod $CHMOD_MODE $ASSUMED_NAME/$SPECFILE
797         fi
798         unset OPTIONS
799         [ -n "$DONT_PRINT_REVISION" ] || grep -E -m 1 "^#.*Revision:.*Date" $ASSUMED_NAME/$SPECFILE
800
801         set_spec_target
802 }
803
804 find_mirror() {
805         cd "$REPO_DIR"
806         local url="$1"
807         if [ ! -f "mirrors" -a "$NOCVSSPEC" != "yes" ] ; then
808                 $CVS_COMMAND update mirrors >&2
809         fi
810
811         IFS="|"
812         local origin mirror name rest ol prefix
813         while read origin mirror name rest; do
814                 # skip comments and empty lines
815                 if [ -z "$origin" ] || [ "${origin#\#}" != "$origin" ]; then
816                         continue
817                 fi
818                 ol=$(echo -n "$origin" | wc -c)
819                 prefix=$(echo -n "$url" | head -c $ol)
820                 if [ "$prefix" = "$origin" ] ; then
821                         suffix=$(echo "$url" | cut -b $((ol+1))-)
822                         echo -n "$mirror$suffix"
823                         return 0
824                 fi
825         done < mirrors
826         echo "$url"
827 }
828
829 # Warning: unpredictable results if same URL used twice
830 src_no() {
831         cd $PACKAGE_DIR
832         rpm_dump | \
833         grep "SOURCEURL[0-9]*[  ]*$1""[         ]*$" | \
834         sed -e 's/.*SOURCEURL\([0-9][0-9]*\).*/\1/' | \
835         head -n 1 | xargs
836 }
837
838 src_md5() {
839         [ "$NO5" = "yes" ] && return
840         no=$(src_no "$1")
841         [ -z "$no" ] && return
842         cd $PACKAGE_DIR
843         local md5
844
845         if [ -f additional-md5sums ]; then
846                 local spec_rev=$(grep $SPECFILE CVS/Entries 2>/dev/null | sed -e s:/$SPECFILE/:: -e s:/.*::)
847                 if [ -z "$spec_rev" ]; then
848                         spec_rev=$(head -n 1 $SPECFILE | sed -e 's/.*\$Revision: \([0-9.]*\).*/\1/')
849                 fi
850                 local spec="$SPECFILE[0-9.,]*,$(echo $spec_rev | sed 's/\./\\./g')"
851                 md5=$(grep -s -v '^#' additional-md5sums | \
852                 grep -E "[      ]$(basename "$1")[      ]+${spec}([     ,]|\$)" | \
853                 sed -e 's/^\([0-9a-f]\{32\}\).*/\1/' | \
854                 grep -E '^[0-9a-f]{32}$')
855
856                 if [ "$md5" ]; then
857                         if [ $(echo "$md5" | wc -l) != 1 ] ; then
858                                 echo "$SPECFILE: more then one entry in additional-md5sums for $1" 1>&2
859                         fi
860                         echo "$md5" | tail -n 1
861                         return
862                 fi
863         fi
864
865         source_md5=`grep -i "^#[        ]*Source$no-md5[        ]*:" $SPECFILE | sed -e 's/.*://'`
866         if [ -n "$source_md5" ]; then
867                 echo $source_md5
868         else
869                 source_md5=`grep -i "BuildRequires:[    ]*digest(%SOURCE$no)[   ]*=" $SPECFILE | sed -e 's/.*=//'`
870                 if [ -n "$source_md5" ]; then
871                         echo $source_md5
872                 else
873                         # we have empty SourceX-md5, but it is still possible
874                         # that we have NoSourceX-md5 AND NoSource: X
875                         nosource_md5=`grep -i "^#[       ]*NoSource$no-md5[      ]*:" $SPECFILE | sed -e 's/.*://'`
876                         if [ -n "$nosource_md5" -a -n "`grep -i "^NoSource:[     ]*$no$" $SPECFILE`" ] ; then
877                                 echo $nosource_md5
878                         fi
879                 fi
880         fi
881 }
882
883 distfiles_path() {
884         echo "by-md5/$(src_md5 "$1" | sed -e 's|^\(.\)\(.\)|\1/\2/&|')/$(basename "$1")"
885 }
886
887 distfiles_url() {
888         echo "$PROTOCOL$DISTFILES_SERVER/distfiles/$(distfiles_path "$1")"
889 }
890
891 distfiles_attic_url() {
892         echo "$PROTOCOL$ATTICDISTFILES_SERVER/distfiles/Attic/$(distfiles_path "$1")"
893 }
894
895 good_md5() {
896         md5=$(src_md5 "$1")
897         [ "$md5" = "" ] || \
898         [ "$md5" = "$(md5sum $(nourl "$1") 2> /dev/null | sed -e 's/ .*//')" ]
899 }
900
901 good_size() {
902         size=$(find $(nourl "$1") -printf "%s" 2>/dev/null)
903         [ -n "$size" -a "$size" -gt 0 ]
904 }
905
906 cvsignore_df() {
907         if [ "$CVSIGNORE_DF" != "yes" ]; then
908                 return
909         fi
910         cvsignore=${PACKAGE_DIR}/.gitignore
911
912         # add only if not yet there
913         if ! awk -vf="$1" -vc=1 '$0 == f { c = 0 } END { exit c }' $cvsignore 2>/dev/null; then
914                 echo "$1" >> $cvsignore
915         fi
916 }
917
918 cvsup() {
919         update_shell_title "cvsup"
920         local OPTIONS="" ACTION="up"
921
922         # checkout
923         if [ "$1" = "-c" ]; then
924                 ACTION="co"
925                 shift
926         fi
927         # add
928         if [ "$1" = "-a" ]; then
929                 ACTION="add"
930                 shift
931         fi
932
933         OPTIONS="$ACTION "
934
935         if [ -n "$CVSROOT" ]; then
936                 OPTIONS="-d $CVSROOT $OPTIONS"
937         fi
938
939         if [ "$ACTION" != "add" ]; then
940                 if [ -z "$CVSDATE" -a -z "$CVSTAG" ]; then
941                         OPTIONS="$OPTIONS -A"
942                 else
943                         if [ -n "$CVSDATE" ]; then
944                                 OPTIONS="$OPTIONS -D $CVSDATE"
945                         fi
946                         if [ -n "$CVSTAG" ]; then
947                                 # FIXME: cvs add actually works with -r ?
948                                 OPTIONS="$OPTIONS -r $CVSTAG"
949                         fi
950                 fi
951         fi
952
953         local result=1
954         local retries_counter=0
955         if [ $# = 1 ]; then
956                 update_shell_title "cvsup: $*"
957         else
958                 update_shell_title "cvsup: $# files"
959         fi
960         while [ "$result" != "0" -a "$retries_counter" -le "$CVS_RETRIES" ]; do
961                 retries_counter=$(( $retries_counter + 1 ))
962                 output=$(LC_ALL=C $CVS_COMMAND $OPTIONS "$@" 2>&1)
963                 result=$?
964                 [ -n "$output" ] && echo "$output"
965                 if echo "$output" | grep -qE "(Cannot connect to|connect to .* failed|Connection reset by peer|Connection timed out|Unknown host)" \
966                         && [ "$result" -ne "0" -a "$retries_counter" -le "$CVS_RETRIES" ]; then
967                         echo "Trying again [$*]... ($retries_counter)"
968                         update_shell_title "cvsup: retry #$retries_counter"
969                         sleep 2
970                         continue
971                 else
972                         break
973                 fi
974         done
975         update_shell_title "cvsup: done!"
976         return $result
977 }
978
979 # returns true if "$1" is ftp, http or https protocol url
980 is_url() {
981         case "$1" in
982         ftp://*|http://*|https://*)
983                 return 0
984         ;;
985         esac
986         return 1
987 }
988
989 update_md5() {
990         if [ $# -eq 0 ]; then
991                 return
992         fi
993
994         update_shell_title "update md5"
995         if [ -n "$DEBUG" ]; then
996                 set -x
997                 set -v
998         fi
999
1000         cd "$PACKAGE_DIR"
1001
1002         # pass 1: check files to be fetched
1003         local todo
1004         local need_files
1005         for i in "$@"; do
1006                 local fp=$(nourl "$i")
1007                 local srcno=$(src_no "$i")
1008                 if [ -n "$ADD5" ]; then
1009                         [ "$fp" = "$i" ] && continue # FIXME what is this check doing?
1010                         grep -qiE '^#[  ]*Source'$srcno'-md5[   ]*:' $PACKAGE_DIR/$SPECFILE && continue
1011                         grep -qiE '^BuildRequires:[     ]*digest[(]%SOURCE'$srcno'[)][  ]*=' $PACKAGE_DIR/$SPECFILE && continue
1012                 else
1013                         grep -qiE '^#[  ]*Source'$srcno'-md5[   ]*:' $PACKAGE_DIR/$SPECFILE || grep -qiE '^BuildRequires:[      ]*digest[(]%SOURCE'$srcno'[)][  ]*=' $PACKAGE_DIR/$SPECFILE || continue
1014                 fi
1015                 if [ ! -f "$fp" ] || [ $ALWAYS_CVSUP = "yes" ]; then
1016                         need_files="$need_files $i"
1017                 fi
1018         done
1019
1020         # pass 1a: get needed files
1021         if [ "$need_files" ]; then
1022                 get_files $need_files
1023         fi
1024
1025         # pass 2: proceed with md5 adding or updating
1026         for i in "$@"; do
1027                 local fp=$(nourl "$i")
1028                 local srcno=$(src_no "$i")
1029                 local md5=$(grep -iE '^#[       ]*(No)?Source'$srcno'-md5[      ]*:' $PACKAGE_DIR/$SPECFILE )
1030                 if [ -z "$md5" ]; then
1031                         md5=$(grep -iE '^[      ]*BuildRequires:[       ]*digest[(]%SOURCE'$srcno'[)][  ]*=' $PACKAGE_DIR/$SPECFILE )
1032                 fi
1033                 if [ -n "$ADD5" ] && is_url $i || [ -n "$md5" ]; then
1034                         local tag="# Source$srcno-md5:\t"
1035                         if [[ "$md5" == *NoSource* ]]; then
1036                                 tag="# NoSource$srcno-md5:\t"
1037                         elif [ -n "$USEDIGEST" ]; then
1038                                 tag="BuildRequires:\tdigest(%SOURCE$srcno) = "
1039                         fi
1040                         md5=$(md5sum "$fp" | cut -f1 -d' ')
1041                         echo "Updating Source$srcno ($md5: $fp)."
1042                         perl -i -ne '
1043                                 print unless (/^\s*#\s*(No)?Source'$srcno'-md5\s*:/i or /^\s*BuildRequires:\s*digest\(%SOURCE'$srcno'\)/i);
1044                                 print "'"$tag$md5"'\n" if /^Source'$srcno'\s*:\s+/;
1045                         ' \
1046                         $PACKAGE_DIR/$SPECFILE
1047                 fi
1048         done
1049 }
1050
1051 check_md5() {
1052         local bad
1053         [ "$NO5" = "yes" ] && return
1054
1055         update_shell_title "check md5"
1056
1057         for i in "$@"; do
1058                 bad=0
1059                 if ! good_md5 "$i"; then
1060                         echo -n "MD5 sum mismatch."
1061                         bad=1
1062                 fi
1063                 if ! good_size "$i"; then
1064                         echo -n "0 sized file."
1065                         bad=1
1066                 fi
1067
1068                 if [ $bad -eq 1 ]; then
1069                         echo " Use -U to refetch sources,"
1070                         echo "or -5 to update md5 sums, if you're sure files are correct."
1071                         Exit_error err_no_source_in_repo $i
1072                 fi
1073         done
1074 }
1075
1076 get_files() {
1077         update_shell_title "get_files"
1078
1079         if [ -n "$DEBUG" ]; then
1080                 set -x
1081                 set -v
1082         fi
1083
1084         if [ $# -gt 0 ]; then
1085                 cd "$PACKAGE_DIR"
1086
1087                 if [ ! -s .git -a "$NOCVS" != "yes" ]; then
1088                         echo "Warning: No git access defined for SOURCES"
1089                         NOCVS="yes"
1090                 fi
1091
1092                 local nc=0
1093                 local get_files_cvs=""
1094                 for i in "$@"; do
1095                         nc=$((nc + 1))
1096                         local cvsup=0
1097                         SHELL_TITLE_PREFIX="get_files[$nc/$#]"
1098                         update_shell_title "$i"
1099                         local fp=`nourl "$i"`
1100                         if [ "$SKIP_EXISTING_FILES" = "yes" ] && [ -f "$fp" ]; then
1101                                 continue
1102                         fi
1103
1104                         FROM_DISTFILES=0
1105                         local srcmd5=$(src_md5 "$i")
1106
1107                         # we know if source/patch is present in cvs/distfiles
1108                         # - has md5 (in distfiles)
1109                         # - in cvs... ideas?
1110
1111                         # CHECK: local file didn't exist or always cvs up (first) requested.
1112                         if [ ! -f "$fp" ] || [ $ALWAYS_CVSUP = "yes" ]; then
1113                                 if echo $i | grep -vE '(http|ftp|https|cvs|svn)://' | grep -qE '\.(gz|bz2)$']; then
1114                                         echo "Warning: no URL given for $i"
1115                                 fi
1116                                 target="$fp"
1117
1118                                 if [ -z "$NODIST" ] && [ -n "$srcmd5" ]; then
1119                                         if good_md5 "$i" && good_size "$i"; then
1120                                                 echo "$fp having proper md5sum already exists"
1121                                                 continue
1122                                         fi
1123
1124                                         # optionally prefer mirror over distfiles if there's mirror
1125                                         # TODO: build url list and then try each url from the list
1126                                         if [ -n "$PREFMIRRORS" ] && [ -z "$NOMIRRORS" ] && im=$(find_mirror "$i") && [ "$im" != "$i" ]; then
1127                                                 url="$im"
1128                                         else
1129                                                 url=$(distfiles_url "$i")
1130                                         fi
1131
1132                                         url_attic=$(distfiles_attic_url "$i")
1133                                         FROM_DISTFILES=1
1134                                         # is $url local file?
1135                                         if [[ "$url" = [./]* ]]; then
1136                                                 update_shell_title "${GETLOCAL%% *}: $url"
1137                                                 ${GETLOCAL} $url $target
1138                                         else
1139                                                 if [ -z "$NOMIRRORS" ]; then
1140                                                         url=$(find_mirror "$url")
1141                                                 fi
1142
1143                                                 local uri=${url}
1144                                                 # make shorter message for distfiles urls
1145                                                 if [[ "$uri" = ${PROTOCOL}${DISTFILES_SERVER}* ]] || [[ "$uri" = ${PROTOCOL}${ATTICDISTFILES_SERVER}* ]]; then
1146                                                         uri=${uri#${PROTOCOL}${DISTFILES_SERVER}/distfiles/by-md5/?/?/*/}
1147                                                         uri=${uri#${PROTOCOL}${ATTICDISTFILES_SERVER}/distfiles/by-md5/?/?/*/}
1148                                                         uri="df: $uri"
1149                                                 fi
1150                                                 update_shell_title "${GETURI%% *}: $uri"
1151                                                 ${GETURI} ${OUTFILEOPT} "$target" "$url" || \
1152                                                 if [ "`echo $url | grep -E 'ftp://'`" ]; then
1153                                                         update_shell_title "${GETURI2%% *}: $url"
1154                                                         ${GETURI2} ${OUTFILEOPT} "$target" "$url"
1155                                                 fi
1156                                         fi
1157
1158                                         # is it empty file?
1159                                         if [ ! -s "$target" ]; then
1160                                                 rm -f "$target"
1161                                                 if [ `echo $url_attic | grep -E '^(\.|/)'` ]; then
1162                                                         update_shell_title "${GETLOCAL%% *}: $url_attic"
1163                                                         ${GETLOCAL} $url_attic $target
1164                                                 else
1165                                                         if [ -z "$NOMIRRORS" ]; then
1166                                                                 url_attic=$(find_mirror "$url_attic")
1167                                                         fi
1168                                                         update_shell_title "${GETURI%% *}: $url_attic"
1169                                                         ${GETURI} ${OUTFILEOPT} "$target" "$url_attic" || \
1170                                                         if [ "`echo $url_attic | grep -E 'ftp://'`" ]; then
1171                                                                 update_shell_title "${GETURI2%% *}: $url_attic"
1172                                                                 ${GETURI2} ${OUTFILEOPT} "$target" "$url_attic"
1173                                                         fi
1174                                                         test -s "$target" || rm -f "$target"
1175                                                 fi
1176                                         fi
1177
1178                                         if [ -s "$target" ]; then
1179                                                 cvsignore_df $target
1180                                         else
1181                                                 rm -f "$target"
1182                                                 FROM_DISTFILES=0
1183                                         fi
1184                                 fi
1185
1186                                 if [ -z "$NOURLS" ] && [ ! -f "$fp" -o -n "$UPDATE" ] && [ "`echo $i | grep -E 'ftp://|http://|https://'`" ]; then
1187                                         if [ -z "$NOMIRRORS" ]; then
1188                                                 im=$(find_mirror "$i")
1189                                         else
1190                                                 im="$i"
1191                                         fi
1192                                         update_shell_title "${GETURI%% *}: $im"
1193                                         ${GETURI} "$im" ${OUTFILEOPT} "$target" || \
1194                                         if [ "`echo $im | grep -E 'ftp://'`" ]; then
1195                                                 update_shell_title "${GETURI2%% *}: $im"
1196                                                 ${GETURI2} "$im" ${OUTFILEOPT} "$target"
1197                                         fi
1198                                         test -s "$target" || rm -f "$target"
1199                                 fi
1200
1201                                 if [ "$cvsup" = 1 ]; then
1202                                         continue
1203                                 fi
1204
1205                         fi
1206
1207                         # the md5 check must be moved elsewhere as if we've called from update_md5 the md5 is wrong.
1208                         if [ ! -f "$fp" -a "$FAIL_IF_NO_SOURCES" != "no" ]; then
1209                                 if [ -n "GREEDSRC" ]; then
1210                                         get_greed_sources $i
1211                                 else
1212                                         Exit_error err_no_source_in_repo $i
1213                                 fi
1214                         fi
1215
1216                         # we check md5 here just only to refetch immediately
1217                         if good_md5 "$i" && good_size "$i"; then
1218                                 :
1219                         elif [ "$FROM_DISTFILES" = 1 ]; then
1220                                 # wrong md5 from distfiles: remove the file and try again
1221                                 # but only once ...
1222                                 echo "MD5 sum mismatch. Trying full fetch."
1223                                 FROM_DISTFILES=2
1224                                 rm -f $target
1225                                 update_shell_title "${GETURI%% *}: $url"
1226                                 ${GETURI} ${OUTFILEOPT} "$target" "$url" || \
1227                                 if [ "`echo $url | grep -E 'ftp://'`" ]; then
1228                                         update_shell_title "${GETURI2%% *}: $url"
1229                                         ${GETURI2} ${OUTFILEOPT} "$target" "$url"
1230                                 fi
1231                                 if [ ! -s "$target" ]; then
1232                                         rm -f "$target"
1233                                         update_shell_title "${GETURI%% *}: $url_attic"
1234                                         ${GETURI} ${OUTFILEOPT} "$target" "$url_attic" || \
1235                                         if [ "`echo $url_attic | grep -E 'ftp://'`" ]; then
1236                                                 update_shell_title "${GETURI2%% *}: $url_attic"
1237                                                 ${GETURI2} ${OUTFILEOPT} "$target" "$url_attic"
1238                                         fi
1239                                 fi
1240                                 test -s "$target" || rm -f "$target"
1241                         fi
1242                 done
1243                 SHELL_TITLE_PREFIX=""
1244
1245
1246                 if [ "$CHMOD" = "yes" ]; then
1247                         CHMOD_FILES=$(nourl "$@")
1248                         if [ -n "$CHMOD_FILES" ]; then
1249                                 chmod $CHMOD_MODE $CHMOD_FILES
1250                         fi
1251                 fi
1252         fi
1253 }
1254
1255 make_tagver() {
1256         if [ -n "$DEBUG" ]; then
1257                 set -x
1258                 set -v
1259         fi
1260
1261         # Check whether first character of PACKAGE_NAME is legal for tag name
1262         if [ -z "${PACKAGE_NAME##[_0-9]*}" -a -z "$TAG_PREFIX" ]; then
1263                 TAG_PREFIX=tag_
1264         fi
1265
1266         # NOTE: CVS tags may must not contain the characters `$,.:;@'
1267         TAGVER=$(echo $TAG_PREFIX$PACKAGE_NAME-$PACKAGE_VERSION-$PACKAGE_RELEASE | tr '[.@]' '[_#]')
1268
1269         # Remove #kernel.version_release from TAGVER because tagging sources
1270         # could occur with different kernel-headers than kernel-headers used at build time.
1271         # besides, %{_kernel_ver_str} is not expanded.
1272
1273         # TAGVER=auto-ac-madwifi-ng-0-0_20070225_1#%{_kernel_ver_str}
1274         # TAGVER=auto-ac-madwifi-ng-0-0_20070225_1
1275
1276         TAGVER=${TAGVER%#*}
1277         echo -n "$TAGVER"
1278 }
1279
1280 # bool is_tag_a_branch(tag)
1281 #
1282 # returns 1 if a tag is a branch set on SPECFILE
1283 is_tag_a_branch() {
1284         if [ -n "$DEBUG" ]; then
1285                 set -x
1286                 set -v
1287         fi
1288
1289         if [ $# -ne 1 ]; then
1290                 return 0;
1291         fi
1292
1293         TAG=$1
1294
1295         cd "$PACKAGE_DIR"
1296         $CVS_COMMAND status -v $SPECFILE | grep -Eiq "${TAG}.+(branch: [0-9.]+)"
1297         return $?
1298 }
1299
1300 tag_files() {
1301         TAG_FILES="$@"
1302
1303         if [ -n "$DEBUG" ]; then
1304                 set -x
1305                 set -v
1306         fi
1307
1308         echo "Version: $PACKAGE_VERSION"
1309         echo "Release: $PACKAGE_RELEASE"
1310
1311         local TAGVER
1312         if [ "$TAG_VERSION" = "yes" ]; then
1313                 TAGVER=`make_tagver`
1314                 echo "tag: $TAGVER"
1315         fi
1316         if [ -n "$TAG" ]; then
1317                 echo "tag: $TAG"
1318         fi
1319
1320         local OPTIONS="tag $CVS_FORCE"
1321
1322         local _tag=$TAG
1323         if [ "$TAG_VERSION" = "yes" ]; then
1324                 _tag=$TAGVER
1325         fi;
1326
1327         cd "$PACKAGE_DIR"
1328
1329         if [ "$TAG_VERSION" = "yes" ]; then
1330                 update_shell_title "tag sources: $TAGVER"
1331                 git $OPTIONS $TAGVER || exit
1332         fi
1333         if [ -n "$TAG" ]; then
1334                 update_shell_title "tag sources: $TAG"
1335                 git $OPTIONS $TAG $chunk || exit
1336         fi
1337 }
1338
1339 branch_files() {
1340         TAG=$1
1341         echo "CVS branch tag: $TAG"
1342         shift
1343
1344         TAG_FILES="$@"
1345
1346         if [ -n "$DEBUG" ]; then
1347                 set -x
1348                 set -v
1349         fi
1350
1351         local OPTIONS="tag $CVS_FORCE -b"
1352
1353         # branch exists?
1354         is_tag_a_branch $TAG
1355         if [ $? -eq 1 ]; then
1356                 OPTIONS="$OPTIONS -B"
1357         fi
1358
1359         if [ -n "$CVSROOT" ]; then
1360                 OPTIONS="-d $CVSROOT $OPTIONS"
1361         fi
1362         cd "$PACKAGE_DIR"
1363         local tag_files
1364         for i in $TAG_FILES; do
1365                 local fp=`nourl "$i"`
1366                 if [ -f "$fp" ]; then
1367                         tag_files="$tag_files $fp"
1368                 elif [ -n "GREEDSRC" ]; then
1369                         get_greed_sources $i
1370                 else
1371                         Exit_error err_no_source_in_repo $i
1372                 fi
1373         done
1374         if [ "$tag_files" ]; then
1375                 $CVS_COMMAND $OPTIONS $TAG $tag_files || exit
1376         fi
1377
1378         cd "$PACKAGE_DIR"
1379         $CVS_COMMAND $OPTIONS $TAG $SPECFILE || exit
1380 }
1381
1382
1383 # this function should exit early if package can't be built for this arch
1384 # this avoids unneccessary BR filling.
1385 check_buildarch() {
1386         local out ret
1387         out=$(minirpm --short-circuit -bp --define "'prep exit 0'" --nodeps $SPECFILE 2>&1)
1388         ret=$?
1389         if [ $ret -ne 0 ]; then
1390                 echo >&2 "$out"
1391                 exit $ret
1392         fi
1393 }
1394
1395 # from relup.sh
1396 set_release() {
1397         local specfile="$1"
1398         local rel="$2"
1399         local newrel="$3"
1400         sed -i -e "
1401                 s/^\(%define[ \t]\+_\?rel[ \t]\+\)$rel\$/\1$newrel/
1402                 s/^\(Release:[ \t]\+\)$rel\$/\1$newrel/
1403         " $specfile
1404 }
1405
1406 set_version() {
1407         local specfile="$1"
1408         local ver="$2" subver=$ver
1409         local newver="$3" newsubver=$newver
1410
1411         # try handling subver, everything that's not numeric-dotted in version
1412         if grep -Eq '%define\s+subver' $specfile; then
1413                 subver=$(echo "$ver" | sed -re 's,^[0-9.]+,,')
1414                 ver=${ver%$subver}
1415                 newsubver=$(echo "$newver" | sed -re 's,^[0-9.]+,,')
1416                 newver=${newver%$newsubver}
1417         fi
1418         sed -i -e "
1419                 s/^\(%define[ \t]\+_\?ver[ \t]\+\)$ver\$/\1$newver/
1420                 s/^\(%define[ \t]\+subver[ \t]\+\)$subver\$/\1$newsubver/
1421                 s/^\(Version:[ \t]\+\)$ver\$/\1$newver/
1422         " $specfile
1423 }
1424
1425 try_upgrade() {
1426         if [ -n "$TRY_UPGRADE" ]; then
1427                 local TNOTIFY TNEWVER TOLDVER
1428                 update_shell_title "build_package: try_upgrade"
1429
1430                 cd "$PACKAGE_DIR"
1431                 
1432                 if [ "$UPGRADE_VERSION" ]; then
1433                         TNEWVER=$UPGRADE_VERSION
1434                 else
1435                         if [ -n "$FLOAT_VERSION" ]; then
1436                                 TNOTIFY=$($APPDIR/pldnotify.awk ${BE_VERBOSE:+-vDEBUG=1} $SPECFILE -n) || exit 1
1437                         else
1438                                 TNOTIFY=$($APPDIR/pldnotify.awk ${BE_VERBOSE:+-vDEBUG=1} $SPECFILE) || exit 1
1439                         fi
1440
1441                         # pldnotify.awk does not set exit codes, but it has match for ERROR
1442                         # in output which means so.
1443                         if [[ "$TNOTIFY" = *ERROR* ]]; then
1444                                 echo >&2 "$TNOTIFY"
1445                                 exit 1
1446                         fi
1447
1448                         TNEWVER=$(echo $TNOTIFY | awk '{ match($4,/\[NEW\]/); print $5 }')
1449                 fi
1450
1451                 if [ -n "$TNEWVER" ]; then
1452                         TOLDVER=`echo $TNOTIFY | awk '{ print $3; }'`
1453                         echo "New version found, updating spec file from $TOLDVER to version $TNEWVER"
1454                         if [ "$REVERT_BROKEN_UPGRADE" = "yes" ]; then
1455                                 cp -f $SPECFILE $SPECFILE.bak
1456                         fi
1457                         chmod +w $SPECFILE
1458                         set_release $SPECFILE $PACKAGE_RELEASE 1
1459                         set_version $SPECFILE $PACKAGE_VERSION $TNEWVER
1460                         parse_spec
1461                         if [ "$PACKAGE_VERSION" != "$TNEWVER" ]; then
1462                                 echo >&2 "Upgrading version failed, you need to update spec yourself"
1463                                 exit 1
1464                         fi
1465                         return 1
1466                 fi
1467         fi
1468         return 0
1469 }
1470
1471 build_package() {
1472         update_shell_title "build_package"
1473         if [ -n "$DEBUG" ]; then
1474                 set -x
1475                 set -v
1476         fi
1477
1478         cd "$PACKAGE_DIR"
1479
1480         case "$COMMAND" in
1481                 build )
1482                         BUILD_SWITCH="-ba" ;;
1483                 build-binary )
1484                         BUILD_SWITCH="-bb" ;;
1485                 build-source )
1486                         BUILD_SWITCH="-bs --nodeps" ;;
1487                 build-prep )
1488                         BUILD_SWITCH="-bp --nodeps" ;;
1489                 build-build )
1490                         BUILD_SWITCH="-bc" ;;
1491                 build-install )
1492                         BUILD_SWITCH="-bi" ;;
1493                 build-list )
1494                         BUILD_SWITCH="-bl" ;;
1495
1496         esac
1497
1498         update_shell_title "build_package: $COMMAND"
1499         if [ -n "$LOGFILE" ]; then
1500                 LOG=`eval echo $LOGFILE`
1501                 if [ -d "$LOG" ]; then
1502                         echo "Log file $LOG is a directory."
1503                         echo "Parse error in the spec?"
1504                         Exit_error err_build_fail
1505                 fi
1506                 if [ -n "$LASTLOG_FILE" ]; then
1507                         echo "LASTLOG=$LOG" > $LASTLOG_FILE
1508                 fi
1509                 RES_FILE=$(tempfile)
1510
1511                 (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
1512                 RETVAL=`cat $RES_FILE`
1513                 rm $RES_FILE
1514                 if [ -n "$LOGDIROK" ] && [ -n "$LOGDIRFAIL" ]; then
1515                         if [ "$RETVAL" -eq "0" ]; then
1516                                 mv $LOG $LOGDIROK
1517                         else
1518                                 mv $LOG $LOGDIRFAIL
1519                         fi
1520                 fi
1521         else
1522                 eval ${NICE_COMMAND} $RPMBUILD $TARGET_SWITCH $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $RPMBUILDOPTS $BCOND --define \'_specdir $PACKAGE_DIR\' --define \'_sourcedir $PACKAGE_DIR\' $SPECFILE
1523                 RETVAL=$?
1524         fi
1525         if [ "$RETVAL" -ne "0" ]; then
1526                 if [ -n "$TRY_UPGRADE" ]; then
1527                         echo "\nUpgrade package to new version failed."
1528                         if [ "$REVERT_BROKEN_UPGRADE" = "yes" ]; then
1529                                 echo "Restoring old spec file."
1530                                 mv -f $SPECFILE.bak $SPECFILE
1531                         fi
1532                         echo ""
1533                 fi
1534                 Exit_error err_build_fail
1535         fi
1536         unset BUILD_SWITCH
1537 }
1538
1539 nourl() {
1540         echo "$@" | sed 's#\<\(ftp\|http\|https\|cvs\|svn\)://[^ ]*/##g'
1541 }
1542
1543 install_required_packages() {
1544         run_poldek -vi $1
1545         return $?
1546 }
1547
1548 find_spec_bcond() { # originally from /usr/lib/rpm/find-spec-bcond
1549         local SPEC="$1"
1550         awk -F"\n" '
1551         /^%changelog/ { exit }
1552         /^%bcond_with/{
1553                 match($0, /bcond_with(out)?[ \t]+[_a-zA-Z0-9]+/);
1554                 bcond = substr($0, RSTART + 6, RLENGTH - 6);
1555                 gsub(/[ \t]+/, "_", bcond);
1556                 print bcond
1557         }' $SPEC | LC_ALL=C sort -u
1558 }
1559
1560 process_bcondrc() {
1561         # expand bconds from ~/.bcondrc
1562         # The file structure is like gentoo's package.use:
1563         # ---
1564         # * -selinux
1565         # samba -mysql -pgsql
1566         # w32codec-installer license_agreement
1567         # php +mysqli
1568         # ---
1569         if ([ -f $HOME/.bcondrc ] || ([ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ])); then
1570                 :
1571         else
1572                 return
1573         fi
1574
1575         SN=${SPECFILE%%\.spec}
1576
1577         local bcondrc=$HOME/.bcondrc
1578         [ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ] && bcondrc=$HOME_ETC/.bcondrc
1579
1580         local bcond_avail=$(find_spec_bcond $SPECFILE)
1581
1582         while read pkg flags; do
1583                 # ignore comments
1584                 [[ "$pkg" == \#* ]] && continue
1585
1586                 # any package or current package?
1587                 if [ "$pkg" = "*" ] || [ "$pkg" = "$PACKAGE_NAME" ] || [ "$pkg" = "$SN" ]; then
1588                         for flag in $flags; do
1589                                 local opt=${flag#[+-]}
1590
1591                                 # use only flags which are in this package.
1592                                 if [[ $bcond_avail = *${opt}* ]]; then
1593                                         if [[ $flag = -* ]]; then
1594                                                 if [[ $BCOND != *--with?${opt}* ]]; then
1595                                                         BCOND="$BCOND --without $opt"
1596                                                 fi
1597                                         else
1598                                                 if [[ $BCOND != *--without?${opt}* ]]; then
1599                                                         BCOND="$BCOND --with $opt"
1600                                                 fi
1601                                         fi
1602                                 fi
1603                         done
1604                 fi
1605         done < $bcondrc
1606         update_shell_title "parse ~/.bcondrc: DONE!"
1607 }
1608
1609 set_bconds_values() {
1610         update_shell_title "set bcond values"
1611
1612         AVAIL_BCONDS_WITHOUT=""
1613         AVAIL_BCONDS_WITH=""
1614
1615         if grep -Eq '^# *_with' ${SPECFILE}; then
1616                 echo >&2 "ERROR: This spec has old style bconds."
1617                 exit 1
1618         fi
1619
1620         if ! grep -q '^%bcond' ${SPECFILE}; then
1621                 return
1622         fi
1623
1624         local bcond_avail=$(find_spec_bcond $SPECFILE)
1625         process_bcondrc "$SPECFILE"
1626
1627         update_shell_title "parse bconds"
1628
1629         local opt bcond
1630         for opt in $bcond_avail; do
1631                 case "$opt" in
1632                 without_*)
1633                         bcond=${opt#without_}
1634                         case "$BCOND" in
1635                         *--without?${bcond}*)
1636                                 AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT <$bcond>"
1637                                 ;;
1638                         *)
1639                                 AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT $bcond"
1640                                 ;;
1641                         esac
1642                         ;;
1643                 with_*)
1644                         bcond=${opt#with_}
1645                         case "$BCOND" in
1646                         *--with?${bcond}*)
1647                                 AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH <$bcond>"
1648                                 ;;
1649                         *)
1650                                 AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH $bcond"
1651                                 ;;
1652                         esac
1653                         ;;
1654                 *)
1655                         echo >&2 "ERROR: unexpected '$opt' in set_bconds_values"
1656                         exit 1
1657                         ;;
1658                 esac
1659         done
1660 }
1661
1662 run_sub_builder() {
1663         package_name="${1}"
1664         update_shell_title "run_sub_builder $package_name"
1665         #
1666         # No i tutaj bym chcia³ zrobiæ sztuczn± inteligencjê, która spróbuje tego
1667         # pakieta zbudowaæ. Aktualnie niewiele dziala, bo generalnie nie widze do
1668         # konca algorytmu... Ale damy rade. :) Na razie po prostu sie wyjebie tak samo
1669         # jakby nie bylo tego kawalka kodu.
1670         #
1671         # Update: Poprawi³em parê rzeczy i zaczê³o generowaæ pakiety spoza zadanej listy.
1672         #         Jednym s³owem budowanie niespoldkowanych zale¿no¶ci dzia³a w paru przypadkach.
1673         #
1674         #
1675         # y0shi.
1676         # kurwa. translate that ^^^^
1677
1678         parent_spec_name=''
1679
1680         # Istnieje taki spec? ${package}.spec
1681         if [ -f "${PACKAGE_DIR}/${package}.spec" ]; then
1682                 parent_spec_name=${package}.spec
1683         elif [ -f "${PACKAGE_DIR}/$(echo ${package_name} | sed -e s,-devel.*,,g -e s,-static,,g).spec" ]; then
1684                 parent_spec_name="$(echo ${package_name} | sed -e s,-devel.*,,g -e s,-static,,g).spec"
1685         else
1686                 for provides_line in $(grep -r ^Provides:.*$package ${PACKAGE_DIR}); do
1687                         echo $provides_line
1688                 done
1689         fi
1690
1691         if [ "${parent_spec_name}" != "" ]; then
1692                 spawn_sub_builder $parent_spec_name
1693         fi
1694         NOT_INSTALLED_PACKAGES="$NOT_INSTALLED_PACKAGES $package_name"
1695 }
1696
1697 spawn_sub_builder() {
1698         package_name="${1}"
1699         update_shell_title "spawn_sub_builder $package_name"
1700
1701         sub_builder_opts=''
1702         if [ "${FETCH_BUILD_REQUIRES}" = "yes" ]; then
1703                 sub_builder_opts="${sub_builder_opts} -R"
1704         fi
1705         if [ "${REMOVE_BUILD_REQUIRES}" = "nice" ]; then
1706                 sub_builder_opts="${sub_builder_opts} -RB"
1707         elif [ "${REMOVE_BUILD_REQUIRES}" = "force" ]; then
1708                 sub_builder_opts="${sub_builder_opts} -FRB"
1709         fi
1710         if [ "${UPDATE_POLDEK_INDEXES}" = "yes" ]; then
1711                 sub_builder_opts="${sub_builder_opts} -Upi"
1712         fi
1713
1714         cd "${PACKAGE_DIR}"
1715         ./builder ${sub_builder_opts} "$@"
1716 }
1717
1718 remove_build_requires() {
1719         if [ "$INSTALLED_PACKAGES" != "" ]; then
1720                 case "$REMOVE_BUILD_REQUIRES" in
1721                         "force")
1722                                 run_poldek --noask -ve $INSTALLED_PACKAGES
1723                                 ;;
1724                         "nice")
1725                                 run_poldek --ask -ve $INSTALLED_PACKAGES
1726                                 ;;
1727                         *)
1728                                 echo You may want to manually remove following BuildRequires fetched:
1729                                 echo $INSTALLED_PACKAGES
1730                                 echo "Try poldek -e \`cat $(pwd)/.${SPECFILE}_INSTALLED_PACKAGES\`"
1731                                 ;;
1732                 esac
1733         fi
1734 }
1735
1736 display_bconds() {
1737         if [ "$AVAIL_BCONDS_WITH" -o "$AVAIL_BCONDS_WITHOUT" ]; then
1738                 if [ "$BCOND" ]; then
1739                         echo ""
1740                         echo "Building $SPECFILE with the following conditional flags:"
1741                         echo -n "$BCOND"
1742                 else
1743                         echo ""
1744                         echo "No conditional flags passed"
1745                 fi
1746                 echo ""
1747                 echo "from available:"
1748                 echo "--with   :\t$AVAIL_BCONDS_WITH"
1749                 echo "--without:\t$AVAIL_BCONDS_WITHOUT"
1750                 echo ""
1751         fi
1752 }
1753
1754 display_branches() {
1755         if [ "$NOCVSSPEC" != "yes" ]; then
1756                 echo -n "Available branches: "
1757                 $CVS_COMMAND status -v "${SPECFILE}" | awk '!/Sticky Tag:/ && /\(branch:/ { print $1 } ' | xargs
1758         fi
1759 }
1760
1761 # checks a given list of packages/files/provides agains current rpmdb.
1762 # outputs all dependencies which current rpmdb doesn't satisfy.
1763 # input can be either STDIN or parameters
1764 _rpm_prov_check() {
1765         local DEPS
1766
1767         if [ $# -gt 0 ]; then
1768                 DEPS="$@"
1769         else
1770                 DEPS=$(cat)
1771         fi
1772
1773         DEPS=$(LANG=C rpm -q --whatprovides $DEPS 2>&1 | awk '/^(error:|no package provides)/ { print }')
1774
1775         # packages
1776         echo "$DEPS" | awk '/^no package provides/ { print $NF }'
1777
1778         # other deps (files)
1779         echo "$DEPS" | awk -F: '/^error:.*No such file/{o = $2; gsub("^ file ", "", o); print o}'
1780 }
1781
1782 # checks if given package/files/provides exists in rpmdb.
1783 # input can be either stdin or parameters
1784 # returns packages which are present in the rpmdb
1785 _rpm_cnfl_check() {
1786         local DEPS
1787
1788         if [ $# -gt 0 ]; then
1789                 DEPS="$@"
1790         else
1791                 DEPS=$(cat)
1792         fi
1793
1794         LANG=C rpm -q --whatprovides $DEPS 2>/dev/null | awk '!/no package provides/ { print }'
1795 }
1796
1797 # install deps via information from 'rpm-getdeps' or 'rpm --specsrpm'
1798 install_build_requires_rpmdeps() {
1799         if [ "$FETCH_BUILD_REQUIRES_RPMGETDEPS" = "yes" ]; then
1800                 # TODO: Conflicts list doesn't check versions
1801                 local CNFL=$(rpm-getdeps $BCOND $SPECFILE 2> /dev/null | awk '/^\-/ { print $3 } ' | _rpm_cnfl_check | xargs)
1802                 local DEPS=$(rpm-getdeps $BCOND $SPECFILE 2> /dev/null | awk '/^\+/ { print $3 } ' | _rpm_prov_check | xargs)
1803         fi
1804         if [ "$FETCH_BUILD_REQUIRES_RPMSPECSRPM" = "yes" ]; then
1805                 local CNFL=$(rpm -q --specsrpm --conflicts $BCOND $SPECFILE | awk '{print $1}' | _rpm_cnfl_check | xargs)
1806                 local DEPS=$(rpm -q --specsrpm --requires $BCOND $SPECFILE | awk '{print $1}' | _rpm_prov_check | xargs)
1807         fi
1808
1809         if [ -n "$CNFL" ] || [ -n "$DEPS" ]; then
1810                 echo "fetch BuildRequires: install [$DEPS]; remove [$CNFL]"
1811                 update_shell_title "poldek: install [$DEPS]; remove [$CNFL]"
1812                 $SU_SUDO /usr/bin/poldek -q --update || $SU_SUDO /usr/bin/poldek -q --upa
1813         fi
1814         if [ -n "$CNFL" ]; then
1815                 update_shell_title "uninstall conflicting packages: $CNFL"
1816                 echo "Trying to uninstall conflicting packages ($CNFL):"
1817                 $SU_SUDO /usr/bin/poldek --noask --nofollow -ev $CNFL
1818         fi
1819
1820         while [ "$DEPS" ]; do
1821                         update_shell_title "install deps: $DEPS"
1822                         echo "Trying to install dependencies ($DEPS):"
1823                         local log=.${SPECFILE}_poldek.log
1824                         LANG=C $SU_SUDO /usr/bin/poldek --noask --caplookup -uGqQ $DEPS | tee $log
1825                         failed=$(awk '/^error:/{a=$2; sub(/^error: /, "", a); sub(/:$/, "", a); print a}' $log)
1826                         rm -f $log
1827                         local ok
1828                         if [ -n "$failed" ]; then
1829                                 for package in $failed; do
1830                                         spawn_sub_builder -bb $(depspecname $package) && ok="$ok $package"
1831                                 done
1832                                 DEPS="$ok"
1833                         else
1834                                 DEPS=""
1835                         fi
1836         done
1837 }
1838
1839 fetch_build_requires()
1840 {
1841         if [ "${FETCH_BUILD_REQUIRES}" != "yes" ]; then
1842                 return
1843         fi
1844
1845         update_shell_title "fetch build requires"
1846         if [ "$FETCH_BUILD_REQUIRES_RPMGETDEPS" = "yes" ] || [ "$FETCH_BUILD_REQUIRES_RPMSPECSRPM" = "yes" ]; then
1847                 install_build_requires_rpmdeps
1848                 return
1849         fi
1850
1851                 # XXX is this ugliest code written in human history still needed?
1852                 echo "All packages installed by fetch_build_requires() are written to:"
1853                 echo "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES"
1854                 echo ""
1855                 echo "If anything fails, you may get rid of them by executing:"
1856                 echo "poldek -e \`cat `pwd`/.${SPECFILE}_INSTALLED_PACKAGES\`"
1857                 echo ""
1858                 echo > `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1859                 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)
1860                 do
1861                         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)
1862                         GO="yes"
1863                         package=$(basename "$package_item"|sed -e "s/}$//g")
1864                         COND_ARCH_TST=$(cat $SPECFILE|grep -B1 BuildRequires|grep -B1 $package|grep ifarch|sed -e "s/^.*ifarch//g")
1865                         mach=$(uname -m)
1866
1867                         COND_TST=`cat $SPECFILE|grep BuildRequires|grep "$package"`
1868                         if `echo $COND_TST|grep -q '^BuildRequires:'`; then
1869                                 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
1870                                         GO="yes"
1871                                 fi
1872                         # bcond:
1873                         else
1874                                 COND_NAME=`echo $COND_TST|sed -e s,:BuildRequires:.*$,,g`
1875                                 GO=""
1876                                 # %{without}
1877                                 if `echo $COND_TST|grep -q 'without_'`; then
1878                                         COND_NAME=`echo $COND_NAME|sed -e s,^.*without_,,g`
1879                                         if `echo $COND_TST|grep -q !`; then
1880                                                 COND_STATE="with"
1881                                         else
1882                                                 COND_STATE="wout"
1883                                         fi
1884                                         COND_WITH=`echo $AVAIL_BCONDS_WITH|grep "<$COND_NAME>"`
1885                                         COND_WITHOUT=`echo $AVAIL_BCONDS_WITHOUT|grep "<$COND_NAME>"`
1886                                         if [ -n "$COND_WITHOUT" ] || [ -z "$COND_WITH" ]; then
1887                                                 COND_ARGV="wout"
1888                                         else
1889                                                 COND_ARGV="with"
1890                                         fi
1891                                 # %{with}
1892                                 elif `echo $COND_TST|grep -q 'with_'`; then
1893                                         COND_NAME=`echo $COND_NAME|sed -e s,^.*with_,,g`
1894                                         if `echo $COND_TST|grep -q !`; then
1895                                                 COND_STATE="wout"
1896                                         else
1897                                                 COND_STATE="with"
1898                                         fi
1899                                         COND_WITH=`echo $AVAIL_BCONDS_WITH|grep "<$COND_NAME>"`
1900                                         COND_WITHOUT=`echo $AVAIL_BCONDS_WITHOUT|grep "<$COND_NAME>"`
1901                                         if [ -n "$COND_WITH" ] || [ -z "$COND_WITHOUT" ]; then
1902                                                 COND_ARGV="with"
1903                                         else
1904                                                 COND_ARGV="wout"
1905                                         fi
1906                                 fi
1907                                 RESULT="${COND_STATE}-${COND_ARGV}"
1908                                 case "$RESULT" in
1909                                         "with-wout" | "wout-with" )
1910                                                 GO=""
1911                                                 ;;
1912                                         "wout-wout" | "with-with" )
1913                                                 GO="yes"
1914                                                 ;;
1915                                         * )
1916                                                 echo "Action '$RESULT' was not defined for package '$package_item'"
1917                                                 GO="yes"
1918                                                 ;;
1919                                 esac
1920                         fi
1921
1922                         if [ "$GO" = "yes" ]; then
1923                                 if [ "`rpm -q $package|sed -e "s/$package.*/$package/g"`" != "$package" ]; then
1924                                         echo "Testing if $package has subrequirements..."
1925                                         run_poldek -t -i $package --dumpn=".$package-req.txt"
1926                                         if [ -f ".$package-req.txt" ]; then
1927                                                 for package_name in `cat ".$package-req.txt"|grep -v ^#`
1928                                                 do
1929                                                         if [ "$package_name" = "$package" ]; then
1930                                                                 echo "Installing BuildRequired package:\t$package_name"
1931                                                                 update_shell_title "Installing BuildRequired package: ${package_name}"
1932                                                                 install_required_packages $package
1933                                                         else
1934                                                                 echo "Installing (sub)Required package:\t$package_name"
1935                                                                 update_shell_title "Installing (sub)Required package: ${package_name}"
1936                                                                 install_required_packages $package_name
1937                                                         fi
1938                                                         case $? in
1939                                                                 0)
1940                                                                         INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
1941                                                                         echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1942                                                                         ;;
1943                                                                 *)
1944                                                                         echo "Attempting to run spawn sub - builder..."
1945                                                                         echo "Package installation failed:\t$package_name"
1946                                                                         run_sub_builder $package_name
1947                                                                         if [ $? -eq 0 ]; then
1948                                                                                 install_required_packages $package_name
1949                                                                                 case $? in
1950                                                                                         0)
1951                                                                                                 INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
1952                                                                                                 echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1953                                                                                                 ;;
1954                                                                                         *)
1955                                                                                                 NOT_INSTALLED_PACKAGES="$package_name $NOT_INSTALLED_PACKAGES"
1956                                                                                                 ;;
1957                                                                                 esac
1958                                                                         fi
1959                                                                         ;;
1960                                                         esac
1961                                                 done
1962                                                 rm -f ".$package-req.txt"
1963                                         else
1964                                                 echo "Attempting to run spawn sub - builder..."
1965                                                 echo "Package installation failed:\t$package"
1966                                                 run_sub_builder $package
1967                                                 if [ $? -eq 0 ]; then
1968                                                         install_required_packages $package
1969                                                         case $? in
1970                                                                 0)
1971                                                                         INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
1972                                                                         echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1973                                                                         ;;
1974                                                                 *)
1975                                                                         NOT_INSTALLED_PACKAGES="$package_name $NOT_INSTALLED_PACKAGES"
1976                                                                         ;;
1977                                                         esac
1978                                                 fi
1979                                         fi
1980                                 else
1981                                         echo "Package $package is already installed. BuildRequirement satisfied."
1982                                 fi
1983                         fi
1984                 done
1985                 if [ "$NOT_INSTALLED_PACKAGES" != "" ]; then
1986                         echo >&2 "Unable to install following packages and their dependencies:"
1987                         for pkg in "$NOT_INSTALLED_PACKAGES"
1988                         do
1989                                 echo $pkg
1990                         done
1991                         remove_build_requires
1992                         exit 8
1993                 fi
1994 }
1995
1996 init_rpm_dir() {
1997         local CVSROOT=":pserver:cvs@$CVS_SERVER:/cvsroot"
1998         local TOP_DIR=$(eval $RPM $RPMOPTS --eval '%{_topdir}')
1999         local rpmdir=$(eval $RPM $RPMOPTS --eval '%{_rpmdir}')
2000         local buildir=$(eval $RPM $RPMOPTS --eval '%{_builddir}')
2001         local srpmdir=$(eval $RPM $RPMOPTS --eval '%{_srcrpmdir}')
2002         local tmp
2003
2004         echo "Initializing rpm directories to $TOP_DIR from $CVSROOT"
2005         mkdir -p $TOP_DIR $rpmdir $buildir $srpmdir
2006
2007         # need to checkout to tmp dir or we can't name our topdir
2008         tmp=$(TMPDIR= TEMPDIR= mktemp -p $TOP_DIR -d) || exit 1
2009         cd $tmp
2010         $CVS_COMMAND -d $CVSROOT co packages/{.cvsignore,rpm.groups,dropin,mirrors,md5,adapter{,.awk},fetchsrc_request,builder,{relup,compile,repackage}.sh,ci,civim}
2011         cd -
2012         mv $tmp/packages/* $TOP_DIR
2013         rm -rf $tmp
2014
2015         init_builder
2016
2017         echo "To checkout *all* .spec files (read-only):"
2018         echo "- run cvs co SPECS"
2019
2020         echo "To checkout *all* packages:"
2021         echo "- run cvs up -dP in $TOP_DIR dir"
2022
2023         echo ""
2024         echo "To commit with your developer account:"
2025         echo "- edit $TOP_DIR/CVS/Root"
2026 }
2027
2028 get_greed_sources() {
2029         CVSROOT=":pserver:cvs@$CVS_SERVER:/cvsroot"
2030         if [ -n "BE_VERBOSE" ]; then
2031                 echo "Try greed download: $1 from: $CVSROOT"
2032         fi
2033         $CVS_COMMAND -d $CVSROOT get SOURCES/$1
2034         if [ $? != 0 ]; then
2035                 Exit_error err_no_source_in_repo $1
2036         fi
2037
2038 }
2039
2040 # remove entries from CVS/Entries
2041 cvs_entry_remove() {
2042         local cvsdir="$1"; shift
2043         if [ ! -d "$cvsdir" ]; then
2044                 echo >&2 "cvs_entry_remove: $cvsdir is not a directory"
2045                 exit 1
2046         fi
2047
2048         for file in "$@"; do
2049                 rm -f $cvsdir/CVS/Entries.new || return 1
2050                 awk -ve="${file##*/}" -F/ '$2 != e {print}' $cvsdir/CVS/Entries > $cvsdir/CVS/Entries.new || return 1
2051                 mv -f $cvsdir/CVS/Entries.new $cvsdir/CVS/Entries || return 1
2052         done
2053         return 0
2054 }
2055
2056 mr_proper() {
2057         init_builder
2058         NOCVSSPEC="yes"
2059         DONT_PRINT_REVISION="yes"
2060         get_spec
2061         parse_spec
2062
2063         # remove from CVS/Entries
2064         cvs_entry_remove $PACKAGE_DIR $SPECFILE $SOURCES $PATCHES
2065
2066         # remove spec and sources
2067         $RPMBUILD --clean --rmsource --rmspec --nodeps --define \'_specdir $PACKAGE_DIR\' --define \'_sourcedir $PACKAGE_DIR\' $SPECFILE
2068 }
2069
2070 #---------------------------------------------
2071 # main()
2072
2073 if [ $# = 0 ]; then
2074         usage
2075         exit 1
2076 fi
2077
2078 while [ $# -gt 0 ]; do
2079         case "${1}" in
2080                 -5 | --update-md5)
2081                         COMMAND="update_md5"
2082                         NODIST="yes"
2083                         NOCVSSPEC="yes"
2084                         shift ;;
2085                 -a5 | --add-md5 )
2086                         COMMAND="update_md5"
2087                         NODIST="yes"
2088                         NOCVS="yes"
2089                         NOCVSSPEC="yes"
2090                         ADD5="yes"
2091                         shift ;;
2092                 -n5 | --no-md5 )
2093                         NO5="yes"
2094                         shift ;;
2095                 -D | --debug )
2096                         DEBUG="yes"; shift ;;
2097                 -V | --version )
2098                         COMMAND="version"; shift ;;
2099                 --short-version )
2100                         COMMAND="short-version"; shift ;;
2101                 --as_anon )
2102                         CVSROOT=":pserver:cvs@$CVS_SERVER:/cvsroot"; shift ;;
2103                 -a | --add_cvs)
2104                         COMMAND="add_cvs";
2105                         shift ;;
2106                 -b | -ba | --build )
2107                         COMMAND="build"; shift ;;
2108                 -bb | --build-binary )
2109                         COMMAND="build-binary"; shift ;;
2110                 -bc )
2111                         COMMAND="build-build"; shift ;;
2112                 -bi )
2113                         COMMAND="build-install"; shift ;;
2114                 -bl )
2115                         COMMAND="build-list"; shift ;;
2116                 -bp | --build-prep )
2117                         COMMAND="build-prep"; shift ;;
2118                 -bs | --build-source )
2119                         COMMAND="build-source"; shift ;;
2120                 -B | --branch )
2121                         COMMAND="branch"; shift; TAG="${1}"; shift;;
2122                 -c | --clean )
2123                         CLEAN="--clean"; shift ;;
2124                 -cf | --cvs-force )
2125                         CVS_FORCE="-f"; shift;;
2126                 -d | --cvsroot )
2127                         shift; CVSROOT="${1}"; shift ;;
2128                 -g | --get )
2129                         COMMAND="get"; shift ;;
2130                 -h | --help )
2131                         COMMAND="usage"; shift ;;
2132                 --ftp )
2133                         PROTOCOL="ftp"; shift ;;
2134                 --http )
2135                         PROTOCOL="http"; shift ;;
2136                 -j)
2137                         RPMOPTS="${RPMOPTS} --define \"_smp_mflags -j$2\""
2138                         shift 2
2139                         ;;
2140                 -j[0-9]*)
2141                         RPMOPTS="${RPMOPTS} --define \"_smp_mflags $1\""
2142                         shift
2143                         ;;
2144                 -l | --logtofile )
2145                         shift; LOGFILE="${1}"; shift ;;
2146                 -ni| --nice )
2147                         shift; DEF_NICE_LEVEL=${1}; shift ;;
2148                 -ske | --skip-existing-files)
2149                         SKIP_EXISTING_FILES="yes"; shift ;;
2150                 -m | --mr-proper )
2151                         COMMAND="mr-proper"; shift ;;
2152                 -nc | --no-cvs )
2153                         NOCVS="yes"; shift ;;
2154                 -ncs | --no-cvs-specs )
2155                         NOCVSSPEC="yes"; shift ;;
2156                 -nd | --no-distfiles )
2157                         NODIST="yes"; shift ;;
2158                 -nm | --no-mirrors )
2159                         NOMIRRORS="yes"; shift ;;
2160                 -nu | --no-urls )
2161                         NOURLS="yes"; shift ;;
2162                 -ns | --no-srcs )
2163                         NOSRCS="yes"; shift ;;
2164                 -ns0 | --no-source0 )
2165                         NOSOURCE0="yes"; shift ;;
2166                 -nn | --no-net )
2167                         NOCVS="yes"
2168                         NOCVSSPEC="yes"
2169                         NODIST="yes"
2170                         NOMIRRORS="yes"
2171                         NOURLS="yes"
2172                         NOSRCS="yes"
2173                         ALWAYS_CVSUP="no"
2174                         shift;;
2175                 -pm | --prefer-mirrors )
2176                         PREFMIRRORS="yes"
2177                         shift;;
2178                 --no-init )
2179                         NOINIT="yes"
2180                         shift;;
2181                 --opts )
2182                         shift; RPMOPTS="${RPMOPTS} ${1}"; shift ;;
2183                 --nopatch | -np )
2184                         shift; RPMOPTS="${RPMOPTS} --define \"patch${1} : ignoring patch${1}; exit 1; \""; shift ;;
2185                 --skip-patch | -sp )
2186                         shift; RPMOPTS="${RPMOPTS} --define \"patch${1} : skiping patch${1}\""; shift ;;
2187                 --topdir)
2188                         RPMOPTS="${RPMOPTS} --define \"_topdir $2\""
2189                         shift 2
2190                         ;;
2191                 --with | --without )
2192                         case $GROUP_BCONDS in
2193                                 "yes")
2194                                         COND=${1}
2195                                         shift
2196                                         # XXX: broken: ./builder -bb ucspi-tcp.spec --without mysql
2197                                         while ! `echo ${1}|grep -qE '(^-|spec)'`
2198                                         do
2199                                                 BCOND="$BCOND $COND $1"
2200                                                 shift
2201                                         done;;
2202                                 "no")
2203                                         if [[ "$2" = *,* ]]; then
2204                                                 for a in $(echo "$2" | tr , ' '); do
2205                                                         BCOND="$BCOND $1 $a"
2206                                                 done
2207                                         else
2208                                                 BCOND="$BCOND $1 $2"
2209                                         fi
2210                                         shift 2 ;;
2211                         esac
2212                         ;;
2213                 --target )
2214                         shift; TARGET="${1}"; shift ;;
2215                 --target=* )
2216                         TARGET=$(echo "${1}" | sed 's/^--target=//'); shift ;;
2217                 -q | --quiet )
2218                         QUIET="--quiet"; shift ;;
2219                 --date )
2220                         CVSDATE="${2}"; shift 2 ;;
2221                 -r | --cvstag )
2222                         CVSTAG="$2"
2223                         shift 2
2224                         ;;
2225                 -A)
2226                         shift
2227                         CVSTAG="HEAD"
2228                         ;;
2229                 -R | --fetch-build-requires)
2230                         FETCH_BUILD_REQUIRES="yes"
2231                         NOT_INSTALLED_PACKAGES=
2232                         shift ;;
2233                 -RB | --remove-build-requires)
2234                         REMOVE_BUILD_REQUIRES="nice"
2235                         shift ;;
2236                 -FRB | --force-remove-build-requires)
2237                         REMOVE_BUILD_REQUIRES="force"
2238                         shift ;;
2239                 -sc | --sources-cvs)
2240                         COMMAND="list-sources-cvs"
2241                         shift ;;
2242                 -sd | --source-distfiles)
2243                         COMMAND="list-sources-distfiles"
2244                         shift ;;
2245                 -sdp | --source-distfiles-paths)
2246                         COMMAND="list-sources-distfiles-paths"
2247                         shift ;;
2248                 -sf | --source-files)
2249                         COMMAND="list-sources-files"
2250                         shift ;;
2251                 -lsp | --source-paths)
2252                         COMMAND="list-sources-local-paths"
2253                         shift ;;
2254                 -su | --source-urls)
2255                         COMMAND="list-sources-urls"
2256                         shift ;;
2257                 -Tvs | --tag-version-stable )
2258                         COMMAND="tag"
2259                         TAG="STABLE"
2260                         TAG_VERSION="yes"
2261                         shift;;
2262                 -Ts | --tag-stable )
2263                         COMMAND="tag"
2264                         TAG="STABLE"
2265                         TAG_VERSION="no"
2266                         shift;;
2267                 -Tv | --tag-version )
2268                         COMMAND="tag"
2269                         TAG=""
2270                         TAG_VERSION="yes"
2271                         shift;;
2272                 -Tp | --tag-prefix )
2273                         TAG_PREFIX="$2"
2274                         shift 2;;
2275                 -tt | --test-tag )
2276                         TEST_TAG="yes"
2277                         shift;;
2278                 -T | --tag )
2279                         COMMAND="tag"
2280                         shift
2281                         TAG="$1"
2282                         TAG_VERSION="no"
2283                         shift;;
2284                 -ir | --integer-release-only )
2285                         INTEGER_RELEASE="yes"
2286                         shift;;
2287                 -U | --update )
2288                         COMMAND="update_md5"
2289                         UPDATE="yes"
2290                         NOCVSSPEC="yes"
2291                         NODIST="yes"
2292                         shift ;;
2293                 -Upi | --update-poldek-indexes )
2294                         UPDATE_POLDEK_INDEXES="yes"
2295                         shift ;;
2296                 --init-rpm-dir)
2297                         COMMAND="init_rpm_dir"
2298                         shift ;;
2299                 --use-greed-sources )
2300                         GREEDSRC="1"
2301                         shift;;
2302                 -u | --try-upgrade )
2303                         TRY_UPGRADE="1"; shift ;;
2304                 --upgrade-version )
2305                         shift; UPGRADE_VERSION="$1"; shift;;
2306                 -un | --try-upgrade-with-float-version )
2307                         TRY_UPGRADE="1"; FLOAT_VERSION="1"; shift ;;
2308                 -v | --verbose )
2309                         BE_VERBOSE="1"; shift ;;
2310                 --define)
2311                         shift
2312                         MACRO="${1}"
2313                         shift
2314                         if echo "${MACRO}" | grep -q '\W'; then
2315                                 RPMOPTS="${RPMOPTS} --define \"${MACRO}\""
2316                         else
2317                                 VALUE="${1}"
2318                                 shift
2319                                 RPMOPTS="${RPMOPTS} --define \"${MACRO} ${VALUE}\""
2320                         fi
2321                         ;;
2322                 --alt_kernel)
2323                         shift
2324                         RPMOPTS="${RPMOPTS} --define \"alt_kernel $1\""
2325                         shift
2326                         ;;
2327                 --short-circuit)
2328                         RPMBUILDOPTS="${RPMBUILDOPTS} --short-circuit"
2329                         shift
2330                         ;;
2331                 --show-bconds | -show-bconds | -print-bconds | --print-bconds | -display-bconds | --display-bconds )
2332                         COMMAND="show_bconds"
2333                         shift
2334                         ;;
2335                 --show-bcond-args)
2336                         COMMAND="show_bcond_args"
2337                         shift
2338                         ;;
2339                 --show-avail-bconds)
2340                         COMMAND="show_avail_bconds"
2341                         shift
2342                         ;;
2343                 --nodeps)
2344                         shift
2345                         RPMOPTS="${RPMOPTS} --nodeps"
2346                         ;;
2347                 -debug)
2348                         RPMBUILDOPTS="${RPMBUILDOPTS} -debug"; shift
2349                         ;;
2350                 -*)
2351                         Exit_error err_invalid_cmdline "$1"
2352                         ;;
2353                 *)
2354                         SPECFILE=${1%/}; shift
2355                         # check if specname was passed as specname:cvstag
2356                         if [ "${SPECFILE##*:}" != "${SPECFILE}" ]; then
2357                                 CVSTAG="${SPECFILE##*:}"
2358                                 SPECFILE="${SPECFILE%%:*}"
2359                         fi
2360                         # always have SPECFILE ending with .spec extension
2361                         SPECFILE=${SPECFILE%%.spec}.spec
2362                         ASSUMED_NAME=$(basename ${SPECFILE%%.spec})
2363         esac
2364 done
2365
2366 [ -d "$ASSUMED_NAME" ] && CVS_ENTRIES="$ASSUMED_NAME/CVS/Entries" || CVS_ENTRIES="CVS/Entries"
2367 if [ -f "$CVS_ENTRIES" ] && [ -z "$CVSTAG" ]; then
2368         CVSTAG=$(awk -vSPECFILE=$(basename $SPECFILE) -F/ '$2 == SPECFILE && $6 ~ /^T/{print substr($6, 2)}' ${CVS_ENTRIES})
2369         if [ "$CVSTAG" ]; then
2370                 echo >&2 "builder: Sticky tag $CVSTAG active. Use -r TAGNAME to override."
2371         fi
2372 elif [ "$CVSTAG" = "HEAD" ]; then
2373         # assume -r HEAD is same as -A
2374         CVSTAG=""
2375 fi
2376
2377 if [ "$CVSTAG" ]; then
2378         # pass $CVSTAG used by builder to rpmbuild too, so specs could use it
2379         RPMOPTS="$RPMOPTS --define \"_cvstag $CVSTAG\""
2380 fi
2381
2382 if [ -n "$DEBUG" ]; then
2383         set -x
2384         set -v
2385 fi
2386
2387 if [ -n "$TARGET" ]; then
2388         case "$RPMBUILD" in
2389                 "rpmbuild")
2390                         TARGET_SWITCH="--target $TARGET" ;;
2391                 "rpm")
2392                         TARGET_SWITCH="--target=$TARGET" ;;
2393         esac
2394 fi
2395
2396 if [ "$SCHEDTOOL" != "no" ]; then
2397         NICE_COMMAND="$SCHEDTOOL"
2398 else
2399         NICE_COMMAND="nice -n ${DEF_NICE_LEVEL}"
2400 fi
2401
2402 update_shell_title "$COMMAND"
2403 case "$COMMAND" in
2404         "show_bconds")
2405                 init_builder
2406                 if [ -z "$SPECFILE" ]; then
2407                         Exit_error err_no_spec_in_cmdl
2408                 fi
2409                 get_spec > /dev/null
2410                 parse_spec
2411                 set_bconds_values
2412                 display_bconds
2413                 ;;
2414         "show_bcond_args")
2415                 init_builder
2416                 if [ -z "$SPECFILE" ]; then
2417                         Exit_error err_no_spec_in_cmdl
2418                 fi
2419                 get_spec > /dev/null
2420                 parse_spec
2421                 set_bconds_values
2422                 echo "$BCOND"
2423                 ;;
2424         "show_avail_bconds")
2425                 init_builder
2426                 if [ -z "$SPECFILE" ]; then
2427                         Exit_error err_no_spec_in_cmdl
2428                 fi
2429
2430                 get_spec > /dev/null
2431                 parse_spec
2432                 local bcond_avail=$(find_spec_bcond $SPECFILE)
2433                 local opt bcond bconds
2434                 for opt in $bcond_avail; do
2435                         case "$opt" in
2436                         without_*)
2437                                 bcond=${opt#without_}
2438                                 bconds="$bconds $bcond"
2439                                 ;;
2440                         with_*)
2441                                 bcond=${opt#with_}
2442                                 bconds="$bconds $bcond"
2443                                 ;;
2444                         *)
2445                                 echo >&2 "ERROR: unexpected '$opt' in show_avail_bconds"
2446                                 exit 1
2447                                 ;;
2448                         esac
2449                 done
2450                 echo $bconds
2451
2452                 ;;
2453         "build" | "build-binary" | "build-source" | "build-prep" | "build-build" | "build-install" | "build-list")
2454                 init_builder
2455                 if [ -z "$SPECFILE" ]; then
2456                         Exit_error err_no_spec_in_cmdl
2457                 fi
2458
2459                 # display SMP make flags if set
2460                 smp_mflags=$(rpm -E %{?_smp_mflags})
2461                 if [ "$smp_mflags" ]; then
2462                         echo "builder: SMP make flags are set to $smp_mflags"
2463                 fi
2464
2465                 get_spec
2466                 parse_spec
2467                 set_bconds_values
2468                 display_bconds
2469                 display_branches
2470                 if [ "$COMMAND" != "build-source" ]; then
2471                         check_buildarch
2472                 fi
2473                 fetch_build_requires
2474                 if [ "$INTEGER_RELEASE" = "yes" ]; then
2475                         echo "Checking release $PACKAGE_RELEASE..."
2476                         if echo $PACKAGE_RELEASE | grep -q '^[^.]*\.[^.]*$' 2>/dev/null ; then
2477                                 Exit_error err_fract_rel "$PACKAGE_RELEASE"
2478                         fi
2479                 fi
2480
2481                 # ./builder -bs test.spec -r AC-branch -Tp auto-ac- -tt
2482                 if [ -n "$TEST_TAG" ]; then
2483                         local TAGVER=`make_tagver`
2484                         echo "Searching for tag $TAGVER..."
2485                         TAGREL=$($CVS_COMMAND status -v $SPECFILE | grep -E "^[[:space:]]*${TAGVER}[[[:space:]]" | sed -e 's#.*(revision: ##g' -e 's#).*##g')
2486                         if [ -n "$TAGREL" ]; then
2487                                 Exit_error err_tag_exists "$TAGVER" "$TAGREL"
2488                         fi
2489
2490                         # - do not allow to build from HEAD when XX-branch exists
2491                         TREE_PREFIX=$(echo "$TAG_PREFIX" | sed -e 's#^auto-\([a-zA-Z]\+\)-.*#\1#g')
2492                         if [ "$TREE_PREFIX" != "$TAG_PREFIX" ]; then
2493                                 TAG_BRANCH="${TREE_PREFIX}-branch"
2494                                 TAG_STATUS=$($CVS_COMMAND status -v $SPECFILE | grep -Ei "${TAG_BRANCH}.+(branch: [0-9.]+)")
2495                                 if [ -n "$TAG_STATUS" -a "$CVSTAG" = "HEAD" ]; then
2496                                         Exit_error err_branch_exists "$TAG_STATUS"
2497                                 fi
2498                         fi
2499
2500                 fi
2501
2502                 if [ -n "$NOSOURCE0" ] ; then
2503                         SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2504                 fi
2505                 try_upgrade
2506                 case $? in
2507                         0)
2508                                 get_files $SOURCES $PATCHES
2509                                 check_md5 $SOURCES
2510                                 ;;
2511                         *)
2512                                 NODIST="yes" get_files $SOURCES $PATCHES
2513                                 update_md5 $SOURCES
2514                                 ;;
2515                 esac
2516                 build_package
2517                 if [ "$UPDATE_POLDEK_INDEXES" = "yes" -a "$COMMAND" != "build-prep" ]; then
2518                         run_poldek --sdir="${POLDEK_INDEX_DIR}" --mkidxz
2519                 fi
2520                 remove_build_requires
2521                 ;;
2522         "branch" )
2523                 init_builder
2524                 if [ -z "$SPECFILE" ]; then
2525                         Exit_error err_no_spec_in_cmdl
2526                 fi
2527
2528                 get_spec
2529                 parse_spec
2530                 branch_files $TAG $SOURCES $PATCHES $ICONS
2531                 ;;
2532         "add_cvs" )
2533                 init_builder
2534                 if [ -z "$SPECFILE" ]; then
2535                         Exit_error err_no_spec_in_cmdl
2536                 fi
2537
2538                 ADD_PACKAGE_CVS=yes get_spec
2539                 parse_spec
2540
2541                 if [ -n "$NOSOURCE0" ] ; then
2542                         SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2543                 fi
2544                 get_files $SOURCES $PATCHES
2545                 check_md5 $SOURCES
2546                 ;;
2547         "get" )
2548                 init_builder
2549                 if [ -z "$SPECFILE" ]; then
2550                         Exit_error err_no_spec_in_cmdl
2551                 fi
2552
2553                 get_spec
2554                 parse_spec
2555
2556                 if [ -n "$NOSOURCE0" ] ; then
2557                         SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2558                 fi
2559                 get_files $SOURCES $PATCHES
2560                 check_md5 $SOURCES
2561                 ;;
2562         "update_md5" )
2563                 init_builder
2564                 if [ -z "$SPECFILE" ]; then
2565                         Exit_error err_no_spec_in_cmdl
2566                 fi
2567
2568                 get_spec
2569                 parse_spec
2570
2571                 if [ -n "$NOSOURCE0" ] ; then
2572                         SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2573                 fi
2574                 update_md5 $SOURCES
2575                 ;;
2576         "tag" )
2577                 NOURLS=1
2578                 NODIST="yes"
2579                 init_builder
2580                 if [ -z "$SPECFILE" ]; then
2581                         Exit_error err_no_spec_in_cmdl
2582                 fi
2583
2584                 get_spec
2585                 parse_spec
2586                 tag_files
2587                 ;;
2588         "mr-proper" )
2589                 mr_proper
2590                 ;;
2591         "list-sources-files" )
2592                 init_builder
2593                 NOCVSSPEC="yes"
2594                 DONT_PRINT_REVISION="yes"
2595                 get_spec
2596                 parse_spec
2597                 for SAP in $SOURCES $PATCHES; do
2598                         echo $SAP | awk '{gsub(/.*\//,"") ; print}'
2599                 done
2600                 ;;
2601         "list-sources-urls" )
2602                 init_builder
2603                 NOCVSSPEC="yes"
2604                 DONT_PRINT_REVISION="yes"
2605                 get_spec
2606                 parse_spec
2607                 SAPS="$SOURCES $PATCHES"
2608                 for SAP in $SAPS ; do
2609                         echo $SAP
2610                 done
2611                 ;;
2612         "list-sources-local-paths" )
2613                 init_builder
2614                 NOCVSSPEC="yes"
2615                 DONT_PRINT_REVISION="yes"
2616                 get_spec
2617                 parse_spec
2618                 for SAP in $SOURCES $PATCHES; do
2619                         echo $PACKAGE_DIR/$(echo $SAP | awk '{gsub(/.*\//,"") ; print }')
2620                 done
2621                 ;;
2622         "list-sources-distfiles-paths" )
2623                 init_builder
2624                 NOCVSSPEC="yes"
2625                 DONT_PRINT_REVISION="yes"
2626                 get_spec
2627                 parse_spec
2628                 for SAP in $SOURCES $PATCHES; do
2629                         if [ -n "$(src_md5 "$SAP")" ]; then
2630                                 distfiles_path "$SAP"
2631                         fi
2632                 done
2633                 ;;
2634         "list-sources-distfiles" )
2635                 init_builder
2636                 NOCVSSPEC="yes"
2637                 DONT_PRINT_REVISION="yes"
2638                 get_spec
2639                 parse_spec
2640                 for SAP in $SOURCES $PATCHES; do
2641                         if [ -n "$(src_md5 "$SAP")" ]; then
2642                                 distfiles_url "$SAP"
2643                         fi
2644                 done
2645                 ;;
2646         "list-sources-cvs" )
2647                 init_builder
2648 #               NOCVSSPEC="yes"
2649                 DONT_PRINT_REVISION="yes"
2650                 get_spec
2651                 parse_spec
2652                 for SAP in $SOURCES $PATCHES; do
2653                         if [ -z "$(src_md5 "$SAP")" ]; then
2654                                 echo $SAP | awk '{gsub(/.*\//,"") ; print}'
2655                         fi
2656                 done
2657                 ;;
2658         "init_rpm_dir")
2659                 init_rpm_dir
2660                 ;;
2661         "usage" )
2662                 usage
2663                 ;;
2664         "short-version" )
2665                 echo "$VERSION"
2666                 ;;
2667         "version" )
2668                 echo "$VERSIONSTRING"
2669                 ;;
2670 esac
2671 if [ -f "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES" -a "$REMOVE_BUILD_REQUIRES" != "" ]; then
2672         rm "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES"
2673 fi
2674 cd "$__PWD"
2675
2676 # vi:syntax=sh:ts=4:sw=4:noet
This page took 0.546692 seconds and 4 git commands to generate.