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