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