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