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