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