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