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