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