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