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