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