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