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