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