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