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