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