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