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