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