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