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