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