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