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