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