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