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