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