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