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