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