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