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