]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - builder.sh
builder: add --noinit alias
[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         if [ -f additional-md5sums ]; then
1024                 md5=$(grep -s -v '^#' additional-md5sums | \
1025                 grep -E "[      ]$(basename "$1")([     ,]|\$)" | \
1026                 sed -e 's/^\([0-9a-f]\{32\}\).*/\1/' | \
1027                 grep -E '^[0-9a-f]{32}$')
1028
1029                 if [ "$md5" ]; then
1030                         if [ $(echo "$md5" | wc -l) != 1 ] ; then
1031                                 echo "$SPECFILE: more then one entry in additional-md5sums for $1" 1>&2
1032                         fi
1033                         echo "$md5" | tail -n 1
1034                         return
1035                 fi
1036         fi
1037
1038         source_md5=$(grep -iE "^#[      ]*(No)?$no-md5[         ]*:" $SPECFILE | sed -e 's/.*://')
1039         if [ -n "$source_md5" ]; then
1040                 echo $source_md5
1041         else
1042                 source_md5=`grep -i "BuildRequires:[    ]*digest(%SOURCE$no)[   ]*=" $SPECFILE | sed -e 's/.*=//'`
1043                 if [ -n "$source_md5" ]; then
1044                         echo $source_md5
1045                 else
1046                         # we have empty SourceX-md5, but it is still possible
1047                         # that we have NoSourceX-md5 AND NoSource: X
1048                         nosource_md5=`grep -i "^#[       ]*No$no-md5[    ]*:" $SPECFILE | sed -e 's/.*://'`
1049                         if [ -n "$nosource_md5" -a -n "`grep -i "^NoSource:[     ]*$no$" $SPECFILE`" ] ; then
1050                                 echo $nosource_md5
1051                         fi
1052                 fi
1053         fi
1054 }
1055
1056 distfiles_path() {
1057         echo "by-md5/$(src_md5 "$1" | sed -e 's|^\(.\)\(.\)|\1/\2/&|')/$(basename "$1")"
1058 }
1059
1060 distfiles_url() {
1061         echo "$PROTOCOL$DISTFILES_SERVER/distfiles/$(distfiles_path "$1")"
1062 }
1063
1064 distfiles_attic_url() {
1065         echo "$PROTOCOL$ATTICDISTFILES_SERVER/distfiles/Attic/$(distfiles_path "$1")"
1066 }
1067
1068 good_md5() {
1069         md5=$(src_md5 "$1")
1070         [ "$md5" = "" ] || \
1071         [ "$md5" = "$(md5sum $(nourl "$1") 2> /dev/null | sed -e 's/ .*//')" ]
1072 }
1073
1074 good_size() {
1075         size=$(find $(nourl "$1") -printf "%s" 2>/dev/null)
1076         [ -n "$size" -a "$size" -gt 0 ]
1077 }
1078
1079 cvsignore_df() {
1080         if [ "$CVSIGNORE_DF" != "yes" ]; then
1081                 return
1082         fi
1083         cvsignore=${PACKAGE_DIR}/.gitignore
1084
1085         # add only if not yet there
1086         if ! awk -vf="$1" -vc=1 '$0 == f { c = 0 } END { exit c }' $cvsignore 2>/dev/null; then
1087                 echo "$1" >> $cvsignore
1088         fi
1089 }
1090
1091 # returns true if "$1" is ftp, http or https protocol url
1092 is_url() {
1093         case "$1" in
1094         ftp://*|http://*|https://*)
1095                 return 0
1096         ;;
1097         esac
1098         return 1
1099 }
1100
1101 update_md5() {
1102         if [ $# -eq 0 ]; then
1103                 return
1104         fi
1105
1106         update_shell_title "update md5"
1107         if [ -n "$DEBUG" ]; then
1108                 set -x
1109                 set -v
1110         fi
1111
1112         cd "$PACKAGE_DIR"
1113
1114         # pass 1: check files to be fetched
1115         local todo
1116         local need_files
1117         for i in "$@"; do
1118                 local fp=$(nourl "$i")
1119                 local srcno=$(src_no "$i")
1120                 if [ -n "$ADD5" ]; then
1121                         [ "$fp" = "$i" ] && continue # FIXME what is this check doing?
1122                         grep -qiE '^#[  ]*'$srcno'-md5[         ]*:' $PACKAGE_DIR/$SPECFILE && continue
1123                         grep -qiE '^BuildRequires:[     ]*digest[(]%SOURCE'$srcno'[)][  ]*=' $PACKAGE_DIR/$SPECFILE && continue
1124                 else
1125                         grep -qiE '^#[  ]*'$srcno'-md5[         ]*:' $PACKAGE_DIR/$SPECFILE || grep -qiE '^BuildRequires:[      ]*digest[(]%SOURCE'$srcno'[)][  ]*=' $PACKAGE_DIR/$SPECFILE || continue
1126                 fi
1127                 if [ ! -f "$fp" ] || [ $ALWAYS_CVSUP = "yes" ]; then
1128                         need_files="$need_files $i"
1129                 fi
1130         done
1131
1132         # pass 1a: get needed files
1133         if [ "$need_files" ]; then
1134                 get_files $need_files
1135         fi
1136
1137         # pass 2: proceed with md5 adding or updating
1138         for i in "$@"; do
1139                 local fp=$(nourl "$i")
1140                 local srcno=$(src_no "$i")
1141                 local md5=$(grep -iE '^#[       ]*(No)?'$srcno'-md5[    ]*:' $PACKAGE_DIR/$SPECFILE )
1142                 if [ -z "$md5" ]; then
1143                         md5=$(grep -iE '^[      ]*BuildRequires:[       ]*digest[(]%SOURCE'$srcno'[)][  ]*=' $PACKAGE_DIR/$SPECFILE )
1144                 fi
1145                 if [ -n "$ADD5" ] && is_url $i || [ -n "$md5" ]; then
1146                         local tag="# $srcno-md5:\t"
1147                         if [[ "$md5" == *NoSource* ]]; then
1148                                 tag="# No$srcno-md5:\t"
1149                         elif [ -n "$USEDIGEST" ]; then
1150                                 tag="BuildRequires:\tdigest(%SOURCE$srcno) = "
1151                         fi
1152                         md5=$(md5sum "$fp" | cut -f1 -d' ')
1153                         echo "Updating $srcno ($md5: $fp)."
1154                         perl -i -ne '
1155                                 print unless (/^\s*#\s*(No)?'$srcno'-md5\s*:/i or /^\s*BuildRequires:\s*digest\(%SOURCE'$srcno'\)/i);
1156                                 print "'"$tag$md5"'\n" if /^'$srcno'\s*:\s+/i;
1157                         ' \
1158                         $PACKAGE_DIR/$SPECFILE
1159                 fi
1160         done
1161 }
1162
1163 check_md5() {
1164         local bad
1165         [ "$NO5" = "yes" ] && return
1166
1167         update_shell_title "check md5"
1168
1169         for i in "$@"; do
1170                 bad=0
1171                 if ! good_md5 "$i"; then
1172                         echo -n "MD5 sum mismatch."
1173                         bad=1
1174                 fi
1175                 if ! good_size "$i"; then
1176                         echo -n "0 sized file."
1177                         bad=1
1178                 fi
1179
1180                 if [ $bad -eq 1 ]; then
1181                         echo " Use -U to refetch sources,"
1182                         echo "or -5 to update md5 sums, if you're sure files are correct."
1183                         Exit_error err_no_source_in_repo $i
1184                 fi
1185         done
1186 }
1187
1188 get_files() {
1189         update_shell_title "get_files"
1190
1191         if [ -n "$DEBUG" ]; then
1192                 set -x
1193                 set -v
1194         fi
1195
1196         if [ $# -gt 0 ]; then
1197                 cd "$PACKAGE_DIR"
1198
1199                 local nc=0
1200                 local get_files_cvs=""
1201                 for i in "$@"; do
1202                         nc=$((nc + 1))
1203                         local cvsup=0
1204                         SHELL_TITLE_PREFIX="get_files[$nc/$#]"
1205                         update_shell_title "$i"
1206                         local fp=`nourl "$i"`
1207                         if [ "$SKIP_EXISTING_FILES" = "yes" ] && [ -f "$fp" ]; then
1208                                 continue
1209                         fi
1210
1211                         FROM_DISTFILES=0
1212                         local srcmd5=$(src_md5 "$i")
1213
1214                         # we know if source/patch is present in cvs/distfiles
1215                         # - has md5 (in distfiles)
1216                         # - in cvs... ideas?
1217
1218                         # CHECK: local file didn't exist or always cvs up (first) requested.
1219                         if [ ! -f "$fp" ] || [ $ALWAYS_CVSUP = "yes" ]; then
1220                                 if echo $i | grep -vE '(http|ftp|https|cvs|svn)://' | grep -qE '\.(gz|bz2)$']; then
1221                                         echo "Warning: no URL given for $i"
1222                                 fi
1223                                 target="$fp"
1224
1225                                 if [ -z "$NODIST" ] && [ -n "$srcmd5" ]; then
1226                                         if good_md5 "$i" && good_size "$i"; then
1227                                                 echo "$fp having proper md5sum already exists"
1228                                                 continue
1229                                         fi
1230
1231                                         # optionally prefer mirror over distfiles if there's mirror
1232                                         # TODO: build url list and then try each url from the list
1233                                         if [ -n "$PREFMIRRORS" ] && [ -z "$NOMIRRORS" ] && im=$(find_mirror "$i") && [ "$im" != "$i" ]; then
1234                                                 url="$im"
1235                                         else
1236                                                 url=$(distfiles_url "$i")
1237                                         fi
1238
1239                                         url_attic=$(distfiles_attic_url "$i")
1240                                         FROM_DISTFILES=1
1241                                         # is $url local file?
1242                                         if [[ "$url" = [./]* ]]; then
1243                                                 update_shell_title "${GETLOCAL%% *}: $url"
1244                                                 ${GETLOCAL} $url $target
1245                                         else
1246                                                 local uri=${url}
1247                                                 # make shorter message for distfiles urls
1248                                                 if [[ "$uri" = ${PROTOCOL}${DISTFILES_SERVER}* ]] || [[ "$uri" = ${PROTOCOL}${ATTICDISTFILES_SERVER}* ]]; then
1249                                                         uri=${uri#${PROTOCOL}${DISTFILES_SERVER}/distfiles/by-md5/?/?/*/}
1250                                                         uri=${uri#${PROTOCOL}${ATTICDISTFILES_SERVER}/distfiles/by-md5/?/?/*/}
1251                                                         uri="df: $uri"
1252                                                 fi
1253                                                 update_shell_title "${GETURI%% *}: $uri"
1254                                                 ${GETURI} ${OUTFILEOPT} "$target" "$url" || \
1255                                                 if [ "`echo $url | grep -E 'ftp://'`" ]; then
1256                                                         update_shell_title "${GETURI2%% *}: $url"
1257                                                         ${GETURI2} ${OUTFILEOPT} "$target" "$url"
1258                                                 fi
1259                                         fi
1260
1261                                         # is it empty file?
1262                                         if [ ! -s "$target" ]; then
1263                                                 rm -f "$target"
1264                                                 if [ `echo $url_attic | grep -E '^(\.|/)'` ]; then
1265                                                         update_shell_title "${GETLOCAL%% *}: $url_attic"
1266                                                         ${GETLOCAL} $url_attic $target
1267                                                 else
1268                                                         update_shell_title "${GETURI%% *}: $url_attic"
1269                                                         ${GETURI} ${OUTFILEOPT} "$target" "$url_attic" || \
1270                                                         if [ "`echo $url_attic | grep -E 'ftp://'`" ]; then
1271                                                                 update_shell_title "${GETURI2%% *}: $url_attic"
1272                                                                 ${GETURI2} ${OUTFILEOPT} "$target" "$url_attic"
1273                                                         fi
1274                                                         test -s "$target" || rm -f "$target"
1275                                                 fi
1276                                         fi
1277
1278                                         if [ -s "$target" ]; then
1279                                                 cvsignore_df $target
1280                                         else
1281                                                 rm -f "$target"
1282                                                 FROM_DISTFILES=0
1283                                         fi
1284                                 fi
1285
1286                                 if [ -z "$NOURLS" ] && [ ! -f "$fp" -o -n "$UPDATE" ] && [ "`echo $i | grep -E 'ftp://|http://|https://'`" ]; then
1287                                         if [ -z "$NOMIRRORS" ]; then
1288                                                 im=$(find_mirror "$i")
1289                                         else
1290                                                 im="$i"
1291                                         fi
1292                                         update_shell_title "${GETURI%% *}: $im"
1293                                         ${GETURI} ${OUTFILEOPT} "$target" "$im" || \
1294                                         if [ "`echo $im | grep -E 'ftp://'`" ]; then
1295                                                 update_shell_title "${GETURI2%% *}: $im"
1296                                                 ${GETURI2} ${OUTFILEOPT} "$target" "$im"
1297                                         fi
1298                                         test -s "$target" || rm -f "$target"
1299                                 fi
1300
1301                                 if [ "$cvsup" = 1 ]; then
1302                                         continue
1303                                 fi
1304
1305                         fi
1306
1307                         # the md5 check must be moved elsewhere as if we've called from update_md5 the md5 is wrong.
1308                         if [ ! -f "$fp" -a "$FAIL_IF_NO_SOURCES" != "no" ]; then
1309                                 Exit_error err_no_source_in_repo $i
1310                         fi
1311
1312                         # we check md5 here just only to refetch immediately
1313                         if good_md5 "$i" && good_size "$i"; then
1314                                 :
1315                         elif [ "$FROM_DISTFILES" = 1 ]; then
1316                                 # wrong md5 from distfiles: remove the file and try again
1317                                 # but only once ...
1318                                 echo "MD5 sum mismatch. Trying full fetch."
1319                                 FROM_DISTFILES=2
1320                                 rm -f $target
1321                                 update_shell_title "${GETURI%% *}: $url"
1322                                 ${GETURI} ${OUTFILEOPT} "$target" "$url" || \
1323                                 if [ "`echo $url | grep -E 'ftp://'`" ]; then
1324                                         update_shell_title "${GETURI2%% *}: $url"
1325                                         ${GETURI2} ${OUTFILEOPT} "$target" "$url"
1326                                 fi
1327                                 if [ ! -s "$target" ]; then
1328                                         rm -f "$target"
1329                                         update_shell_title "${GETURI%% *}: $url_attic"
1330                                         ${GETURI} ${OUTFILEOPT} "$target" "$url_attic" || \
1331                                         if [ "`echo $url_attic | grep -E 'ftp://'`" ]; then
1332                                                 update_shell_title "${GETURI2%% *}: $url_attic"
1333                                                 ${GETURI2} ${OUTFILEOPT} "$target" "$url_attic"
1334                                         fi
1335                                 fi
1336                                 test -s "$target" || rm -f "$target"
1337                         fi
1338                 done
1339                 SHELL_TITLE_PREFIX=""
1340
1341
1342                 if [ "$CHMOD" = "yes" ]; then
1343                         CHMOD_FILES=$(nourl "$@")
1344                         if [ -n "$CHMOD_FILES" ]; then
1345                                 chmod $CHMOD_MODE $CHMOD_FILES
1346                         fi
1347                 fi
1348         fi
1349 }
1350
1351 tag_exist() {
1352 # If tag exists and points to other commit exit with error
1353 # If it existsts and points to HEAD return 1
1354 # If it doesn't exist return 0
1355         local _tag="$1"
1356         local sha1=$(git rev-parse HEAD)
1357         echo "Searching for tag $_tag..."
1358         if [ -n "$DEPTH" ]; then
1359                 local ref=$(git ls-remote $REMOTE_PLD "refs/tags/$_tag"  | cut -c -40)
1360         else
1361                 local ref=$(git show-ref -s "refs/tags/$_tag")
1362         fi
1363         [ -z "$ref" ] && return 0
1364         [ "$ref" = "$sha1" ] || Exit_error err_tag_exists "$_tag"
1365         return 1
1366 }
1367
1368 make_tagver() {
1369         if [ -n "$DEBUG" ]; then
1370                 set -x
1371                 set -v
1372         fi
1373
1374         # Check whether first character of PACKAGE_NAME is legal for tag name
1375         if [ -z "${PACKAGE_NAME##[_0-9]*}" -a -z "$TAG_PREFIX" ]; then
1376                 TAG_PREFIX=tag_
1377         fi
1378
1379         # NOTE: CVS tags may must not contain the characters `$,.:;@'
1380         TAGVER=$(echo $TAG_PREFIX$PACKAGE_NAME-$PACKAGE_VERSION-$PACKAGE_RELEASE)
1381
1382         # Remove @kernel.version_release from TAGVER because tagging sources
1383         # could occur with different kernel-headers than kernel-headers used at build time.
1384         # besides, %{_kernel_ver_str} is not expanded.
1385
1386         # TAGVER=auto-ac-madwifi-ng-0-0_20070225_1@%{_kernel_ver_str}
1387         # TAGVER=auto-ac-madwifi-ng-0-0_20070225_1
1388
1389         TAGVER=${TAGVER%@*}
1390         echo -n "$TAGVER"
1391 }
1392
1393 tag_files() {
1394         if [ -n "$DEBUG" ]; then
1395                 set -x
1396                 set -v
1397         fi
1398
1399         echo "Version: $PACKAGE_VERSION"
1400         echo "Release: $PACKAGE_RELEASE"
1401
1402         local _tag
1403         if [ "$TAG_VERSION" = "yes" ]; then
1404                 _tag=`make_tagver`
1405         fi
1406         if [ -n "$TAG" ]; then
1407                 _tag="$TAG"
1408         fi
1409         echo "tag: $_tag"
1410
1411         local OPTIONS="tag $CVS_FORCE"
1412
1413         cd "$PACKAGE_DIR"
1414
1415         if tag_exist $_tag || [ -n "$CVS_FORCE" ]; then
1416                 update_shell_title "tag sources: $_tag"
1417                 git $OPTIONS $_tag || exit
1418                 git push $CVS_FORCE $REMOTE_PLD tag $_tag || Exit_error err_remote_problem $REMOTE_PLD
1419         else
1420                 echo "Tag $_tag already exists and points to the same commit"
1421         fi
1422 }
1423
1424 branch_files() {
1425         TAG=$1
1426         echo "Git branch: $TAG"
1427         shift
1428
1429         if [ -n "$DEBUG" ]; then
1430                 set -x
1431                 set -v
1432         fi
1433
1434         local OPTIONS="branch $CVS_FORCE"
1435
1436         cd "$PACKAGE_DIR"
1437         git $OPTIONS $TAG || exit
1438 }
1439
1440
1441 # this function should exit early if package can't be built for this arch
1442 # this avoids unneccessary BR filling.
1443 check_buildarch() {
1444         local out ret
1445         out=$(minirpm --short-circuit -bp --define "'prep exit 0'" --nodeps $SPECFILE 2>&1)
1446         ret=$?
1447         if [ $ret -ne 0 ]; then
1448                 echo >&2 "$out"
1449                 exit $ret
1450         fi
1451 }
1452
1453 # from relup.sh
1454 set_release() {
1455         local specfile="$1"
1456         local rel="$2"
1457         local newrel="$3"
1458         sed -i -e "
1459                 s/^\(%define[ \t]\+_\?rel[ \t]\+\)$rel\$/\1$newrel/
1460                 s/^\(Release:[ \t]\+\)$rel\$/\1$newrel/
1461         " $specfile
1462 }
1463
1464 set_version() {
1465         local specfile="$1"
1466         local ver="$2" subver=$ver
1467         local newver="$3" newsubver=$newver
1468
1469         # try handling subver, everything that's not numeric-dotted in version
1470         if grep -Eq '%define\s+subver' $specfile; then
1471                 subver=$(echo "$ver" | sed -re 's,^[0-9.]+,,')
1472                 ver=${ver%$subver}
1473                 newsubver=$(echo "$newver" | sed -re 's,^[0-9.]+,,')
1474                 newver=${newver%$newsubver}
1475         fi
1476         sed -i -e "
1477                 s/^\(%define[ \t]\+_\?ver[ \t]\+\)$ver\$/\1$newver/
1478                 s/^\(%define[ \t]\+subver[ \t]\+\)$subver\$/\1$newsubver/
1479                 s/^\(Version:[ \t]\+\)$ver\$/\1$newver/
1480         " $specfile
1481 }
1482
1483 try_upgrade() {
1484         if [ -n "$TRY_UPGRADE" ]; then
1485                 local TNOTIFY TNEWVER TOLDVER
1486                 update_shell_title "build_package: try_upgrade"
1487
1488                 cd "$PACKAGE_DIR"
1489                 
1490                 if [ "$UPGRADE_VERSION" ]; then
1491                         TNEWVER=$UPGRADE_VERSION
1492                 else
1493                         if [ -n "$FLOAT_VERSION" ]; then
1494                                 TNOTIFY=$($APPDIR/pldnotify.awk ${BE_VERBOSE:+-vDEBUG=1} $SPECFILE -n) || exit 1
1495                         else
1496                                 TNOTIFY=$($APPDIR/pldnotify.awk ${BE_VERBOSE:+-vDEBUG=1} $SPECFILE) || exit 1
1497                         fi
1498
1499                         # pldnotify.awk does not set exit codes, but it has match for ERROR
1500                         # in output which means so.
1501                         if [[ "$TNOTIFY" = *ERROR* ]]; then
1502                                 echo >&2 "$TNOTIFY"
1503                                 exit 1
1504                         fi
1505
1506                         TNEWVER=$(echo $TNOTIFY | awk '{ match($4,/\[NEW\]/); print $5 }')
1507                 fi
1508
1509                 if [ -n "$TNEWVER" ]; then
1510                         TOLDVER=`echo $TNOTIFY | awk '{ print $3; }'`
1511                         echo "New version found, updating spec file from $TOLDVER to version $TNEWVER"
1512                         if [ "$REVERT_BROKEN_UPGRADE" = "yes" ]; then
1513                                 cp -f $SPECFILE $SPECFILE.bak
1514                         fi
1515                         chmod +w $SPECFILE
1516                         set_release $SPECFILE $PACKAGE_RELEASE 1
1517                         set_version $SPECFILE $PACKAGE_VERSION $TNEWVER
1518                         parse_spec
1519                         if [ "$PACKAGE_VERSION" != "$TNEWVER" ]; then
1520                                 echo >&2 "Upgrading version failed, you need to update spec yourself"
1521                                 exit 1
1522                         fi
1523                         return 1
1524                 fi
1525         fi
1526         return 0
1527 }
1528
1529 build_package() {
1530         update_shell_title "build_package"
1531         if [ -n "$DEBUG" ]; then
1532                 set -x
1533                 set -v
1534         fi
1535
1536         cd "$PACKAGE_DIR"
1537
1538         case "$COMMAND" in
1539                 build )
1540                         BUILD_SWITCH="-ba" ;;
1541                 build-binary )
1542                         BUILD_SWITCH="-bb" ;;
1543                 build-source )
1544                         BUILD_SWITCH="-bs --nodeps" ;;
1545                 build-prep )
1546                         BUILD_SWITCH="-bp --nodeps" ;;
1547                 build-build )
1548                         BUILD_SWITCH="-bc" ;;
1549                 build-install )
1550                         BUILD_SWITCH="-bi" ;;
1551                 build-list )
1552                         BUILD_SWITCH="-bl" ;;
1553
1554         esac
1555
1556         update_shell_title "build_package: $COMMAND"
1557         if [ -n "$LOGFILE" ]; then
1558                 LOG=`eval echo $LOGFILE`
1559                 if [ -d "$LOG" ]; then
1560                         echo "Log file $LOG is a directory."
1561                         echo "Parse error in the spec?"
1562                         Exit_error err_build_fail
1563                 fi
1564                 if [ -n "$LASTLOG_FILE" ]; then
1565                         echo "LASTLOG=$LOG" > $LASTLOG_FILE
1566                 fi
1567                 RES_FILE=$(tempfile)
1568                 local specdir=$(insert_gitlog $SPECFILE)
1569
1570                 (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
1571                 RETVAL=`cat $RES_FILE`
1572                 rm -r $RES_FILE $specdir
1573                 if [ -n "$LOGDIROK" ] && [ -n "$LOGDIRFAIL" ]; then
1574                         if [ "$RETVAL" -eq "0" ]; then
1575                                 mv $LOG $LOGDIROK
1576                         else
1577                                 mv $LOG $LOGDIRFAIL
1578                         fi
1579                 fi
1580         else
1581                 local specdir=$(insert_gitlog $SPECFILE)
1582                 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
1583                 RETVAL=$?
1584                 rm -r $specdir
1585         fi
1586         if [ "$RETVAL" -ne "0" ]; then
1587                 if [ -n "$TRY_UPGRADE" ]; then
1588                         echo "\nUpgrade package to new version failed."
1589                         if [ "$REVERT_BROKEN_UPGRADE" = "yes" ]; then
1590                                 echo "Restoring old spec file."
1591                                 mv -f $SPECFILE.bak $SPECFILE
1592                         fi
1593                         echo ""
1594                 fi
1595                 Exit_error err_build_fail
1596         fi
1597         unset BUILD_SWITCH
1598 }
1599
1600 nourl() {
1601         echo "$@" | sed 's#\<\(ftp\|http\|https\|cvs\|svn\)://[^ ]*/##g'
1602 }
1603
1604 install_required_packages() {
1605         run_poldek -vi $1
1606         return $?
1607 }
1608
1609 find_spec_bcond() { # originally from /usr/lib/rpm/find-spec-bcond
1610         local SPEC="$1"
1611         awk -F"\n" '
1612         /^%changelog/ { exit }
1613         /^%bcond_with/{
1614                 match($0, /bcond_with(out)?[ \t]+[_a-zA-Z0-9]+/);
1615                 bcond = substr($0, RSTART + 6, RLENGTH - 6);
1616                 gsub(/[ \t]+/, "_", bcond);
1617                 print bcond
1618         }' $SPEC | LC_ALL=C sort -u
1619 }
1620
1621 process_bcondrc() {
1622         # expand bconds from ~/.bcondrc
1623         # The file structure is like gentoo's package.use:
1624         # ---
1625         # * -selinux
1626         # samba -mysql -pgsql
1627         # w32codec-installer license_agreement
1628         # php +mysqli
1629         # ---
1630         if [ -f $HOME/.bcondrc ] || ([ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ]); then
1631                 :
1632         else
1633                 return
1634         fi
1635
1636         SN=${SPECFILE%%\.spec}
1637
1638         local bcondrc=$HOME/.bcondrc
1639         [ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ] && bcondrc=$HOME_ETC/.bcondrc
1640
1641         local bcond_avail=$(find_spec_bcond $SPECFILE)
1642
1643         while read pkg flags; do
1644                 # ignore comments
1645                 [[ "$pkg" == \#* ]] && continue
1646
1647                 # any package or current package?
1648                 if [ "$pkg" = "*" ] || [ "$pkg" = "$PACKAGE_NAME" ] || [ "$pkg" = "$SN" ]; then
1649                         for flag in $flags; do
1650                                 local opt=${flag#[+-]}
1651
1652                                 # use only flags which are in this package.
1653                                 if [[ $bcond_avail = *${opt}* ]]; then
1654                                         if [[ $flag = -* ]]; then
1655                                                 if [[ $BCOND != *--with?${opt}* ]]; then
1656                                                         BCOND="$BCOND --without $opt"
1657                                                 fi
1658                                         else
1659                                                 if [[ $BCOND != *--without?${opt}* ]]; then
1660                                                         BCOND="$BCOND --with $opt"
1661                                                 fi
1662                                         fi
1663                                 fi
1664                         done
1665                 fi
1666         done < $bcondrc
1667         update_shell_title "parse ~/.bcondrc: DONE!"
1668 }
1669
1670 set_bconds_values() {
1671         update_shell_title "set bcond values"
1672
1673         AVAIL_BCONDS_WITHOUT=""
1674         AVAIL_BCONDS_WITH=""
1675
1676         if grep -Eq '^# *_with' ${SPECFILE}; then
1677                 echo >&2 "ERROR: This spec has old style bconds."
1678                 exit 1
1679         fi
1680
1681         if ! grep -q '^%bcond' ${SPECFILE}; then
1682                 return
1683         fi
1684
1685         local bcond_avail=$(find_spec_bcond $SPECFILE)
1686         process_bcondrc "$SPECFILE"
1687
1688         update_shell_title "parse bconds"
1689
1690         local opt bcond
1691         for opt in $bcond_avail; do
1692                 case "$opt" in
1693                 without_*)
1694                         bcond=${opt#without_}
1695                         case "$BCOND" in
1696                         *--without?${bcond}*)
1697                                 AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT <$bcond>"
1698                                 ;;
1699                         *)
1700                                 AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT $bcond"
1701                                 ;;
1702                         esac
1703                         ;;
1704                 with_*)
1705                         bcond=${opt#with_}
1706                         case "$BCOND" in
1707                         *--with?${bcond}*)
1708                                 AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH <$bcond>"
1709                                 ;;
1710                         *)
1711                                 AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH $bcond"
1712                                 ;;
1713                         esac
1714                         ;;
1715                 *)
1716                         echo >&2 "ERROR: unexpected '$opt' in set_bconds_values"
1717                         exit 1
1718                         ;;
1719                 esac
1720         done
1721 }
1722
1723 run_sub_builder() {
1724         package_name="${1}"
1725         update_shell_title "run_sub_builder $package_name"
1726         #
1727         # No i tutaj bym chcia³ zrobiæ sztuczn± inteligencjê, która spróbuje tego
1728         # pakieta zbudowaæ. Aktualnie niewiele dziala, bo generalnie nie widze do
1729         # konca algorytmu... Ale damy rade. :) Na razie po prostu sie wyjebie tak samo
1730         # jakby nie bylo tego kawalka kodu.
1731         #
1732         # Update: Poprawi³em parê rzeczy i zaczê³o generowaæ pakiety spoza zadanej listy.
1733         #         Jednym s³owem budowanie niespoldkowanych zale¿no¶ci dzia³a w paru przypadkach.
1734         #
1735         #
1736         # y0shi.
1737         # kurwa. translate that ^^^^
1738
1739         parent_spec_name=''
1740
1741         # Istnieje taki spec? ${package}.spec
1742         if [ -f "${PACKAGE_DIR}/${package}.spec" ]; then
1743                 parent_spec_name=${package}.spec
1744         elif [ -f "${PACKAGE_DIR}/$(echo ${package_name} | sed -e s,-devel.*,,g -e s,-static,,g).spec" ]; then
1745                 parent_spec_name="$(echo ${package_name} | sed -e s,-devel.*,,g -e s,-static,,g).spec"
1746         else
1747                 for provides_line in $(grep -r ^Provides:.*$package ${PACKAGE_DIR}); do
1748                         echo $provides_line
1749                 done
1750         fi
1751
1752         if [ "${parent_spec_name}" != "" ]; then
1753                 spawn_sub_builder $parent_spec_name
1754         fi
1755         NOT_INSTALLED_PACKAGES="$NOT_INSTALLED_PACKAGES $package_name"
1756 }
1757
1758 spawn_sub_builder() {
1759         package_name="${1}"
1760         update_shell_title "spawn_sub_builder $package_name"
1761
1762         sub_builder_opts=''
1763         if [ "${FETCH_BUILD_REQUIRES}" = "yes" ]; then
1764                 sub_builder_opts="${sub_builder_opts} -R"
1765         fi
1766         if [ "${REMOVE_BUILD_REQUIRES}" = "nice" ]; then
1767                 sub_builder_opts="${sub_builder_opts} -RB"
1768         elif [ "${REMOVE_BUILD_REQUIRES}" = "force" ]; then
1769                 sub_builder_opts="${sub_builder_opts} -FRB"
1770         fi
1771         if [ "${UPDATE_POLDEK_INDEXES}" = "yes" ]; then
1772                 sub_builder_opts="${sub_builder_opts} -Upi"
1773         fi
1774
1775         cd "${PACKAGE_DIR}"
1776         ./builder ${sub_builder_opts} "$@"
1777 }
1778
1779 remove_build_requires() {
1780         if [ "$INSTALLED_PACKAGES" != "" ]; then
1781                 case "$REMOVE_BUILD_REQUIRES" in
1782                         "force")
1783                                 run_poldek --noask -ve $INSTALLED_PACKAGES
1784                                 ;;
1785                         "nice")
1786                                 run_poldek --ask -ve $INSTALLED_PACKAGES
1787                                 ;;
1788                         *)
1789                                 echo You may want to manually remove following BuildRequires fetched:
1790                                 echo $INSTALLED_PACKAGES
1791                                 echo "Try poldek -e \`cat $(pwd)/.${SPECFILE}_INSTALLED_PACKAGES\`"
1792                                 ;;
1793                 esac
1794         fi
1795 }
1796
1797 display_bconds() {
1798         if [ "$AVAIL_BCONDS_WITH" -o "$AVAIL_BCONDS_WITHOUT" ]; then
1799                 if [ "$BCOND" ]; then
1800                         echo ""
1801                         echo "Building $SPECFILE with the following conditional flags:"
1802                         echo -n "$BCOND"
1803                 else
1804                         echo ""
1805                         echo "No conditional flags passed"
1806                 fi
1807                 echo ""
1808                 echo "from available:"
1809                 echo "--with   :\t$AVAIL_BCONDS_WITH"
1810                 echo "--without:\t$AVAIL_BCONDS_WITHOUT"
1811                 echo ""
1812         fi
1813 }
1814
1815 display_branches() {
1816         echo -n "Available branches: "
1817         git branch -r 2>/dev/null | grep "^  ${REMOTE_PLD}" | grep -v ${REMOTE_PLD}/HEAD | sed "s#^ *${REMOTE_PLD}/##" | xargs
1818 }
1819
1820 # checks a given list of packages/files/provides agains current rpmdb.
1821 # outputs all dependencies which current rpmdb doesn't satisfy.
1822 # input can be either STDIN or parameters
1823 _rpm_prov_check() {
1824         local DEPS
1825
1826         if [ $# -gt 0 ]; then
1827                 DEPS="$@"
1828         else
1829                 DEPS=$(cat)
1830         fi
1831
1832         DEPS=$(LANG=C rpm -q --whatprovides $DEPS 2>&1 | awk '/^(error:|no package provides)/ { print }')
1833
1834         # packages
1835         echo "$DEPS" | awk '/^no package provides/ { print $NF }'
1836
1837         # other deps (files)
1838         echo "$DEPS" | awk -F: '/^error:.*No such file/{o = $2; gsub("^ file ", "", o); print o}'
1839 }
1840
1841 # checks if given package/files/provides exists in rpmdb.
1842 # input can be either stdin or parameters
1843 # returns packages which are present in the rpmdb
1844 _rpm_cnfl_check() {
1845         local DEPS
1846
1847         if [ $# -gt 0 ]; then
1848                 DEPS="$@"
1849         else
1850                 DEPS=$(cat)
1851         fi
1852
1853         LANG=C rpm -q --whatprovides $DEPS 2>/dev/null | awk '!/no package provides/ { print }'
1854 }
1855
1856 # install deps via information from 'rpm-getdeps' or 'rpm --specsrpm'
1857 install_build_requires_rpmdeps() {
1858         if [ "$FETCH_BUILD_REQUIRES_RPMGETDEPS" = "yes" ]; then
1859                 # TODO: Conflicts list doesn't check versions
1860                 local CNFL=$(rpm-getdeps $BCOND $SPECFILE 2> /dev/null | awk '/^\-/ { print $3 } ' | _rpm_cnfl_check | xargs)
1861                 local DEPS=$(rpm-getdeps $BCOND $SPECFILE 2> /dev/null | awk '/^\+/ { print $3 } ' | _rpm_prov_check | xargs)
1862         fi
1863         if [ "$FETCH_BUILD_REQUIRES_RPMSPECSRPM" = "yes" ]; then
1864                 local CNFL=$(rpm -q --specsrpm --conflicts $BCOND $SPECFILE | awk '{print $1}' | _rpm_cnfl_check | xargs)
1865                 local DEPS=$(rpm -q --specsrpm --requires $BCOND $SPECFILE | awk '{print $1}' | _rpm_prov_check | xargs)
1866         fi
1867
1868         if [ -n "$CNFL" ] || [ -n "$DEPS" ]; then
1869                 echo "fetch BuildRequires: install [$DEPS]; remove [$CNFL]"
1870                 update_shell_title "poldek: install [$DEPS]; remove [$CNFL]"
1871                 $SU_SUDO /usr/bin/poldek -q --update || $SU_SUDO /usr/bin/poldek -q --upa
1872         fi
1873         if [ -n "$CNFL" ]; then
1874                 update_shell_title "uninstall conflicting packages: $CNFL"
1875                 echo "Trying to uninstall conflicting packages ($CNFL):"
1876                 $SU_SUDO /usr/bin/poldek --noask --nofollow -ev $CNFL
1877         fi
1878
1879         while [ "$DEPS" ]; do
1880                         update_shell_title "install deps: $DEPS"
1881                         echo "Trying to install dependencies ($DEPS):"
1882                         local log=.${SPECFILE}_poldek.log
1883                         LANG=C $SU_SUDO /usr/bin/poldek --noask --caplookup -uGqQ $DEPS | tee $log
1884                         failed=$(awk '/^error:/{a=$2; sub(/^error: /, "", a); sub(/:$/, "", a); print a}' $log)
1885                         rm -f $log
1886                         local ok
1887                         if [ -n "$failed" ]; then
1888                                 for package in $failed; do
1889                                         spawn_sub_builder -bb $(depspecname $package) && ok="$ok $package"
1890                                 done
1891                                 DEPS="$ok"
1892                         else
1893                                 DEPS=""
1894                         fi
1895         done
1896 }
1897
1898 fetch_build_requires()
1899 {
1900         if [ "${FETCH_BUILD_REQUIRES}" != "yes" ]; then
1901                 return
1902         fi
1903
1904         update_shell_title "fetch build requires"
1905         if [ "$FETCH_BUILD_REQUIRES_RPMGETDEPS" = "yes" ] || [ "$FETCH_BUILD_REQUIRES_RPMSPECSRPM" = "yes" ]; then
1906                 install_build_requires_rpmdeps
1907                 return
1908         fi
1909
1910                 # XXX is this ugliest code written in human history still needed?
1911                 echo "All packages installed by fetch_build_requires() are written to:"
1912                 echo "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES"
1913                 echo ""
1914                 echo "If anything fails, you may get rid of them by executing:"
1915                 echo "poldek -e \`cat `pwd`/.${SPECFILE}_INSTALLED_PACKAGES\`"
1916                 echo ""
1917                 echo > `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1918                 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)
1919                 do
1920                         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)
1921                         GO="yes"
1922                         package=$(basename "$package_item"|sed -e "s/}$//g")
1923                         COND_ARCH_TST=$(cat $SPECFILE|grep -B1 BuildRequires|grep -B1 $package|grep ifarch|sed -e "s/^.*ifarch//g")
1924                         mach=$(uname -m)
1925
1926                         COND_TST=`cat $SPECFILE|grep BuildRequires|grep "$package"`
1927                         if `echo $COND_TST|grep -q '^BuildRequires:'`; then
1928                                 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
1929                                         GO="yes"
1930                                 fi
1931                         # bcond:
1932                         else
1933                                 COND_NAME=`echo $COND_TST|sed -e s,:BuildRequires:.*$,,g`
1934                                 GO=""
1935                                 # %{without}
1936                                 if `echo $COND_TST|grep -q 'without_'`; then
1937                                         COND_NAME=`echo $COND_NAME|sed -e s,^.*without_,,g`
1938                                         if `echo $COND_TST|grep -q !`; then
1939                                                 COND_STATE="with"
1940                                         else
1941                                                 COND_STATE="wout"
1942                                         fi
1943                                         COND_WITH=`echo $AVAIL_BCONDS_WITH|grep "<$COND_NAME>"`
1944                                         COND_WITHOUT=`echo $AVAIL_BCONDS_WITHOUT|grep "<$COND_NAME>"`
1945                                         if [ -n "$COND_WITHOUT" ] || [ -z "$COND_WITH" ]; then
1946                                                 COND_ARGV="wout"
1947                                         else
1948                                                 COND_ARGV="with"
1949                                         fi
1950                                 # %{with}
1951                                 elif `echo $COND_TST|grep -q 'with_'`; then
1952                                         COND_NAME=`echo $COND_NAME|sed -e s,^.*with_,,g`
1953                                         if `echo $COND_TST|grep -q !`; then
1954                                                 COND_STATE="wout"
1955                                         else
1956                                                 COND_STATE="with"
1957                                         fi
1958                                         COND_WITH=`echo $AVAIL_BCONDS_WITH|grep "<$COND_NAME>"`
1959                                         COND_WITHOUT=`echo $AVAIL_BCONDS_WITHOUT|grep "<$COND_NAME>"`
1960                                         if [ -n "$COND_WITH" ] || [ -z "$COND_WITHOUT" ]; then
1961                                                 COND_ARGV="with"
1962                                         else
1963                                                 COND_ARGV="wout"
1964                                         fi
1965                                 fi
1966                                 RESULT="${COND_STATE}-${COND_ARGV}"
1967                                 case "$RESULT" in
1968                                         "with-wout" | "wout-with" )
1969                                                 GO=""
1970                                                 ;;
1971                                         "wout-wout" | "with-with" )
1972                                                 GO="yes"
1973                                                 ;;
1974                                         * )
1975                                                 echo "Action '$RESULT' was not defined for package '$package_item'"
1976                                                 GO="yes"
1977                                                 ;;
1978                                 esac
1979                         fi
1980
1981                         if [ "$GO" = "yes" ]; then
1982                                 if [ "`rpm -q $package|sed -e "s/$package.*/$package/g"`" != "$package" ]; then
1983                                         echo "Testing if $package has subrequirements..."
1984                                         run_poldek -t -i $package --dumpn=".$package-req.txt"
1985                                         if [ -f ".$package-req.txt" ]; then
1986                                                 for package_name in `cat ".$package-req.txt"|grep -v ^#`
1987                                                 do
1988                                                         if [ "$package_name" = "$package" ]; then
1989                                                                 echo "Installing BuildRequired package:\t$package_name"
1990                                                                 update_shell_title "Installing BuildRequired package: ${package_name}"
1991                                                                 install_required_packages $package
1992                                                         else
1993                                                                 echo "Installing (sub)Required package:\t$package_name"
1994                                                                 update_shell_title "Installing (sub)Required package: ${package_name}"
1995                                                                 install_required_packages $package_name
1996                                                         fi
1997                                                         case $? in
1998                                                                 0)
1999                                                                         INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
2000                                                                         echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
2001                                                                         ;;
2002                                                                 *)
2003                                                                         echo "Attempting to run spawn sub - builder..."
2004                                                                         echo "Package installation failed:\t$package_name"
2005                                                                         run_sub_builder $package_name
2006                                                                         if [ $? -eq 0 ]; then
2007                                                                                 install_required_packages $package_name
2008                                                                                 case $? in
2009                                                                                         0)
2010                                                                                                 INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
2011                                                                                                 echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
2012                                                                                                 ;;
2013                                                                                         *)
2014                                                                                                 NOT_INSTALLED_PACKAGES="$package_name $NOT_INSTALLED_PACKAGES"
2015                                                                                                 ;;
2016                                                                                 esac
2017                                                                         fi
2018                                                                         ;;
2019                                                         esac
2020                                                 done
2021                                                 rm -f ".$package-req.txt"
2022                                         else
2023                                                 echo "Attempting to run spawn sub - builder..."
2024                                                 echo "Package installation failed:\t$package"
2025                                                 run_sub_builder $package
2026                                                 if [ $? -eq 0 ]; then
2027                                                         install_required_packages $package
2028                                                         case $? in
2029                                                                 0)
2030                                                                         INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
2031                                                                         echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
2032                                                                         ;;
2033                                                                 *)
2034                                                                         NOT_INSTALLED_PACKAGES="$package_name $NOT_INSTALLED_PACKAGES"
2035                                                                         ;;
2036                                                         esac
2037                                                 fi
2038                                         fi
2039                                 else
2040                                         echo "Package $package is already installed. BuildRequirement satisfied."
2041                                 fi
2042                         fi
2043                 done
2044                 if [ "$NOT_INSTALLED_PACKAGES" != "" ]; then
2045                         echo >&2 "Unable to install following packages and their dependencies:"
2046                         for pkg in "$NOT_INSTALLED_PACKAGES"
2047                         do
2048                                 echo $pkg
2049                         done
2050                         remove_build_requires
2051                         exit 8
2052                 fi
2053 }
2054
2055 init_rpm_dir() {
2056         local TOP_DIR=$(eval $RPM $RPMOPTS --eval '%{_topdir}')
2057         local rpmdir=$(eval $RPM $RPMOPTS --eval '%{_rpmdir}')
2058         local buildir=$(eval $RPM $RPMOPTS --eval '%{_builddir}')
2059         local srpmdir=$(eval $RPM $RPMOPTS --eval '%{_srcrpmdir}')
2060         local tmp
2061
2062         echo "Initializing rpm directories to $TOP_DIR from $GIT_SERVER"
2063         mkdir -p $TOP_DIR $rpmdir $buildir $srpmdir
2064
2065         cd "$TOP_DIR"
2066         if [ ! -e ../rpm-build-tools ]; then
2067                 git clone  ${GIT_SERVER}/${PACKAGES_DIR}/rpm-build-tools.git ../rpm-build-tools
2068         fi
2069         for a in adapter builder fetchsrc_request; do
2070                 ln -sf ../rpm-build-tools/${a}.sh $a
2071         done
2072         ln -sf ../rpm-build-tools/mirrors mirrors
2073         init_builder
2074 }
2075
2076 mr_proper() {
2077         init_builder
2078         NOCVSSPEC="yes"
2079         DONT_PRINT_REVISION="yes"
2080
2081         # remove spec and sources
2082         PATH=$CLEAN_PATH $RPMBUILD --clean --rmsource --rmspec --nodeps --define "_specdir $PACKAGE_DIR" --define "_sourcedir $PACKAGE_DIR" --define "_builddir $builddir" $PACKAGE_DIR/$SPECFILE
2083         rm -rf $PACKAGE_DIR/{.git,.gitignore}
2084         rmdir --ignore-fail-on-non-empty $PACKAGE_DIR
2085 }
2086
2087 #---------------------------------------------
2088 # main()
2089
2090 if [ $# = 0 ]; then
2091         usage
2092         exit 1
2093 fi
2094
2095 while [ $# -gt 0 ]; do
2096         case "${1}" in
2097                 -4|-6)
2098                         # NOTE: we should be fetcher specific, like fille WGET_OPTS, but
2099                         # unfortunately $GETURI is already formed
2100                         GETURI="$GETURI $1"
2101                         shift
2102                         ;;
2103                 -5 | --update-md5)
2104                         COMMAND="update_md5"
2105                         NODIST="yes"
2106                         NOCVSSPEC="yes"
2107                         shift ;;
2108                 -a5 | --add-md5 )
2109                         COMMAND="update_md5"
2110                         NODIST="yes"
2111                         NOCVSSPEC="yes"
2112                         ADD5="yes"
2113                         shift ;;
2114                 -n5 | --no-md5 )
2115                         NO5="yes"
2116                         shift ;;
2117                 -D | --debug )
2118                         DEBUG="yes"; shift ;;
2119                 -V | --version )
2120                         COMMAND="version"; shift ;;
2121                 --short-version )
2122                         COMMAND="short-version"; shift ;;
2123                 -a | --add_cvs)
2124                         COMMAND="add_cvs";
2125                         shift ;;
2126                 --all-branches )
2127                         ALL_BRANCHES="yes"
2128                         shift ;;
2129                 -b | -ba | --build )
2130                         COMMAND="build"; shift ;;
2131                 -bb | --build-binary )
2132                         COMMAND="build-binary"; shift ;;
2133                 -bc )
2134                         COMMAND="build-build"; shift ;;
2135                 -bi )
2136                         COMMAND="build-install"; shift ;;
2137                 -bl )
2138                         COMMAND="build-list"; shift ;;
2139                 -bp | --build-prep )
2140                         COMMAND="build-prep"; shift ;;
2141                 -bs | --build-source )
2142                         COMMAND="build-source"; shift ;;
2143                 -B | --branch )
2144                         COMMAND="branch"; shift; TAG="${1}"; shift;;
2145                 -c | --clean )
2146                         CLEAN="--clean"; shift ;;
2147                 -cf | --cvs-force )
2148                         CVS_FORCE="-f"; shift;;
2149                 --depth )
2150                         DEPTH="--depth=$2"
2151                         shift 2
2152                         ;;
2153                 -g | --get )
2154                         COMMAND="get"; shift ;;
2155                 -h | --help )
2156                         COMMAND="usage"; shift ;;
2157                 --ftp )
2158                         PROTOCOL="ftp"; shift ;;
2159                 --http )
2160                         PROTOCOL="http"; shift ;;
2161                 -j)
2162                         RPMOPTS="${RPMOPTS} --define \"_smp_mflags -j$2\""
2163                         shift 2
2164                         ;;
2165                 -j[0-9]*)
2166                         RPMOPTS="${RPMOPTS} --define \"_smp_mflags $1\""
2167                         shift
2168                         ;;
2169                 -p)
2170                         PARALLEL_DOWNLOADS=$2
2171                         shift 2
2172                         ;;
2173                 -p[0-9])
2174                         PARALLEL_DOWNLOADS=${1#-p}
2175                         shift
2176                         ;;
2177                 -l | --logtofile )
2178                         shift; LOGFILE="${1}"; shift ;;
2179                 -ni| --nice )
2180                         shift; DEF_NICE_LEVEL=${1}; shift ;;
2181                 -ske | --skip-existing-files)
2182                         SKIP_EXISTING_FILES="yes"; shift ;;
2183                 -m | --mr-proper )
2184                         COMMAND="mr-proper"; shift ;;
2185                 -ncs | --no-cvs-specs )
2186                         NOCVSSPEC="yes"; shift ;;
2187                 -nd | --no-distfiles )
2188                         NODIST="yes"; shift ;;
2189                 -nm | --no-mirrors )
2190                         NOMIRRORS="yes"; shift ;;
2191                 -nu | --no-urls )
2192                         NOURLS="yes"; shift ;;
2193                 -ns | --no-srcs )
2194                         NOSRCS="yes"; shift ;;
2195                 -ns0 | --no-source0 )
2196                         NOSOURCE0="yes"; shift ;;
2197                 -nn | --no-net )
2198                         NOCVSSPEC="yes"
2199                         NODIST="yes"
2200                         NOMIRRORS="yes"
2201                         NOURLS="yes"
2202                         NOSRCS="yes"
2203                         ALWAYS_CVSUP="no"
2204                         shift;;
2205                 -pm | --prefer-mirrors )
2206                         PREFMIRRORS="yes"
2207                         shift;;
2208                 --noinit | --no-init )
2209                         NOINIT="yes"
2210                         shift;;
2211                 --opts )
2212                         shift; RPMOPTS="${RPMOPTS} ${1}"; shift ;;
2213                 --nopatch | -np )
2214                         shift; RPMOPTS="${RPMOPTS} --define \"patch${1} : ignoring patch${1}; exit 1; \""; shift ;;
2215                 --skip-patch | -sp )
2216                         shift; RPMOPTS="${RPMOPTS} --define \"patch${1} : skiping patch${1}\""; shift ;;
2217                 --topdir)
2218                         RPMOPTS="${RPMOPTS} --define \"_topdir $2\""
2219                         shift 2
2220                         ;;
2221                 --with | --without )
2222                         case $GROUP_BCONDS in
2223                                 "yes")
2224                                         COND=${1}
2225                                         shift
2226                                         # XXX: broken: ./builder -bb ucspi-tcp.spec --without mysql
2227                                         while ! `echo ${1}|grep -qE '(^-|spec)'`
2228                                         do
2229                                                 BCOND="$BCOND $COND $1"
2230                                                 shift
2231                                         done;;
2232                                 "no")
2233                                         if [[ "$2" = *,* ]]; then
2234                                                 for a in $(echo "$2" | tr , ' '); do
2235                                                         BCOND="$BCOND $1 $a"
2236                                                 done
2237                                         else
2238                                                 BCOND="$BCOND $1 $2"
2239                                         fi
2240                                         shift 2 ;;
2241                         esac
2242                         ;;
2243                 --target )
2244                         shift; TARGET="${1}"; shift ;;
2245                 --target=* )
2246                         TARGET=$(echo "${1}" | sed 's/^--target=//'); shift ;;
2247                 -q | --quiet )
2248                         QUIET="--quiet"; shift ;;
2249                 --date )
2250                         CVSDATE="${2}"; shift 2
2251                         date -d "$CVSDATE" > /dev/null 2>&1 || { echo >&2 "No valid date specified"; exit 3; }
2252                         ;;
2253                 -r | --cvstag )
2254                         CVSTAG="$2"
2255                         shift 2
2256                         ;;
2257                 -A)
2258                         shift
2259                         CVSTAG="master"
2260                         ;;
2261                 -R | --fetch-build-requires)
2262                         FETCH_BUILD_REQUIRES="yes"
2263                         NOT_INSTALLED_PACKAGES=
2264                         shift ;;
2265                 -RB | --remove-build-requires)
2266                         REMOVE_BUILD_REQUIRES="nice"
2267                         shift ;;
2268                 -FRB | --force-remove-build-requires)
2269                         REMOVE_BUILD_REQUIRES="force"
2270                         shift ;;
2271                 -sc | --source-cvs)
2272                         COMMAND="list-sources-cvs"
2273                         shift ;;
2274                 -sd | --source-distfiles)
2275                         COMMAND="list-sources-distfiles"
2276                         shift ;;
2277                 -sdp | --source-distfiles-paths)
2278                         COMMAND="list-sources-distfiles-paths"
2279                         shift ;;
2280                 -sf | --source-files)
2281                         COMMAND="list-sources-files"
2282                         shift ;;
2283                 -lsp | --source-paths)
2284                         COMMAND="list-sources-local-paths"
2285                         shift ;;
2286                 -su | --source-urls)
2287                         COMMAND="list-sources-urls"
2288                         shift ;;
2289                 -Tvs | --tag-version-stable )
2290                         COMMAND="tag"
2291                         TAG="STABLE"
2292                         TAG_VERSION="yes"
2293                         shift;;
2294                 -Ts | --tag-stable )
2295                         COMMAND="tag"
2296                         TAG="STABLE"
2297                         TAG_VERSION="no"
2298                         shift;;
2299                 -Tv | --tag-version )
2300                         COMMAND="tag"
2301                         TAG=""
2302                         TAG_VERSION="yes"
2303                         shift;;
2304                 -Tp | --tag-prefix )
2305                         TAG_PREFIX="$2"
2306                         shift 2;;
2307                 -tt | --test-tag )
2308                         TEST_TAG="yes"
2309                         shift;;
2310                 -T | --tag )
2311                         COMMAND="tag"
2312                         shift
2313                         TAG="$1"
2314                         TAG_VERSION="no"
2315                         shift;;
2316                 -ir | --integer-release-only )
2317                         INTEGER_RELEASE="yes"
2318                         shift;;
2319                 -U | --update )
2320                         COMMAND="update_md5"
2321                         UPDATE="yes"
2322                         NOCVSSPEC="yes"
2323                         NODIST="yes"
2324                         shift ;;
2325                 -Upi | --update-poldek-indexes )
2326                         UPDATE_POLDEK_INDEXES="yes"
2327                         shift ;;
2328                 --init-rpm-dir)
2329                         COMMAND="init_rpm_dir"
2330                         shift ;;
2331                 -u | --try-upgrade )
2332                         TRY_UPGRADE="1"; shift ;;
2333                 --upgrade-version )
2334                         shift; UPGRADE_VERSION="$1"; shift;;
2335                 -un | --try-upgrade-with-float-version )
2336                         TRY_UPGRADE="1"; FLOAT_VERSION="1"; shift ;;
2337                 -v | --verbose )
2338                         BE_VERBOSE="1"; shift ;;
2339                 --define)
2340                         shift
2341                         MACRO="${1}"
2342                         shift
2343                         if echo "${MACRO}" | grep -q '\W'; then
2344                                 RPMOPTS="${RPMOPTS} --define \"${MACRO}\""
2345                         else
2346                                 VALUE="${1}"
2347                                 shift
2348                                 RPMOPTS="${RPMOPTS} --define \"${MACRO} ${VALUE}\""
2349                         fi
2350                         ;;
2351                 --alt_kernel)
2352                         shift
2353                         RPMOPTS="${RPMOPTS} --define \"alt_kernel $1\""
2354                         shift
2355                         ;;
2356                 --short-circuit)
2357                         RPMBUILDOPTS="${RPMBUILDOPTS} --short-circuit"
2358                         shift
2359                         ;;
2360                 --show-bconds | -show-bconds | -print-bconds | --print-bconds | -display-bconds | --display-bconds )
2361                         COMMAND="show_bconds"
2362                         shift
2363                         ;;
2364                 --show-bcond-args)
2365                         COMMAND="show_bcond_args"
2366                         shift
2367                         ;;
2368                 --show-avail-bconds)
2369                         COMMAND="show_avail_bconds"
2370                         shift
2371                         ;;
2372                 --nodeps)
2373                         shift
2374                         RPMOPTS="${RPMOPTS} --nodeps"
2375                         ;;
2376                 -debug)
2377                         RPMBUILDOPTS="${RPMBUILDOPTS} -debug"; shift
2378                         ;;
2379                 -*)
2380                         Exit_error err_invalid_cmdline "$1"
2381                         ;;
2382                 *)
2383                         SPECFILE=${1%/}; shift
2384                         # check if specname was passed as specname:cvstag
2385                         if [ "${SPECFILE##*:}" != "${SPECFILE}" ]; then
2386                                 CVSTAG="${SPECFILE##*:}"
2387                                 SPECFILE="${SPECFILE%%:*}"
2388                         fi
2389                         # always have SPECFILE ending with .spec extension
2390                         SPECFILE=${SPECFILE%%.spec}.spec
2391                         ASSUMED_NAME=$(basename ${SPECFILE%%.spec})
2392         esac
2393 done
2394
2395 if [ "$CVSTAG" ]; then
2396         # pass $CVSTAG used by builder to rpmbuild too, so specs could use it
2397         RPMOPTS="$RPMOPTS --define \"_cvstag $CVSTAG\""
2398 fi
2399
2400 if [ -n "$ALL_BRANCHES" -a -z "$DEPTH" ]; then
2401         echo >&2 "--all branches requires --depth <number>"
2402         Exit_error err_invalid_cmdline
2403 fi
2404
2405 if [ -n "$DEBUG" ]; then
2406         set -x
2407         set -v
2408 fi
2409
2410 if [ -n "$TARGET" ]; then
2411         case "$RPMBUILD" in
2412                 "rpmbuild")
2413                         TARGET_SWITCH="--target $TARGET" ;;
2414                 "rpm")
2415                         TARGET_SWITCH="--target=$TARGET" ;;
2416         esac
2417 fi
2418
2419 if [ "$SCHEDTOOL" != "no" ]; then
2420         NICE_COMMAND="$SCHEDTOOL"
2421 else
2422         NICE_COMMAND="nice -n ${DEF_NICE_LEVEL}"
2423 fi
2424
2425 update_shell_title "$COMMAND"
2426 case "$COMMAND" in
2427         "show_bconds")
2428                 init_builder
2429                 if [ -z "$SPECFILE" ]; then
2430                         Exit_error err_no_spec_in_cmdl
2431                 fi
2432                 get_spec > /dev/null
2433                 parse_spec
2434                 set_bconds_values
2435                 display_bconds
2436                 ;;
2437         "show_bcond_args")
2438                 init_builder
2439                 if [ -z "$SPECFILE" ]; then
2440                         Exit_error err_no_spec_in_cmdl
2441                 fi
2442                 get_spec > /dev/null
2443                 parse_spec
2444                 set_bconds_values
2445                 echo "$BCOND"
2446                 ;;
2447         "show_avail_bconds")
2448                 init_builder
2449                 if [ -z "$SPECFILE" ]; then
2450                         Exit_error err_no_spec_in_cmdl
2451                 fi
2452
2453                 get_spec > /dev/null
2454                 parse_spec
2455                 local bcond_avail=$(find_spec_bcond $SPECFILE)
2456                 local opt bcond bconds
2457                 for opt in $bcond_avail; do
2458                         case "$opt" in
2459                         without_*)
2460                                 bcond=${opt#without_}
2461                                 bconds="$bconds $bcond"
2462                                 ;;
2463                         with_*)
2464                                 bcond=${opt#with_}
2465                                 bconds="$bconds $bcond"
2466                                 ;;
2467                         *)
2468                                 echo >&2 "ERROR: unexpected '$opt' in show_avail_bconds"
2469                                 exit 1
2470                                 ;;
2471                         esac
2472                 done
2473                 echo $bconds
2474
2475                 ;;
2476         "build" | "build-binary" | "build-source" | "build-prep" | "build-build" | "build-install" | "build-list")
2477                 init_builder
2478                 if [ -z "$SPECFILE" ]; then
2479                         Exit_error err_no_spec_in_cmdl
2480                 fi
2481
2482                 # display SMP make flags if set
2483                 smp_mflags=$(rpm -E %{?_smp_mflags})
2484                 if [ "$smp_mflags" ]; then
2485                         echo "builder: SMP make flags are set to $smp_mflags"
2486                 fi
2487
2488                 get_spec
2489                 parse_spec
2490                 set_bconds_values
2491                 display_bconds
2492                 display_branches
2493                 if [ "$COMMAND" != "build-source" ]; then
2494                         check_buildarch
2495                 fi
2496                 fetch_build_requires
2497                 if [ "$INTEGER_RELEASE" = "yes" ]; then
2498                         echo "Checking release $PACKAGE_RELEASE..."
2499                         if echo $PACKAGE_RELEASE | grep -q '^[^.]*\.[^.]*$' 2>/dev/null ; then
2500                                 Exit_error err_fract_rel "$PACKAGE_RELEASE"
2501                         fi
2502                 fi
2503
2504                 # ./builder -bs test.spec -r AC-branch -Tp auto-ac- -tt
2505                 if [ -n "$TEST_TAG" ]; then
2506                         local TAGVER=`make_tagver`
2507                         tag_exist $TAGVER || [ $TAGVER = $CVSTAG ] || Exit_error err_tag_exists $TAGVER
2508                         # check also tags created in CVS
2509                         local TAGVER_CVS=$(echo $TAGVER | tr '[.@]' '[_#]')
2510                         local CVSTAG_CVS=$(echo $CVSTAG | tr '[.@]' '[_#]')
2511                         tag_exist $TAGVER_CVS || [ $TAGVER_CVS = $CVSTAG_CVS ] \
2512                                 || Exit_error err_tag_exists $TAGVER_CVS
2513                         # - do not allow to build from HEAD when XX-branch exists
2514                         TREE_PREFIX=$(echo "$TAG_PREFIX" | sed -e 's#^auto/\([a-zA-Z]\+\)/.*#\1#g')
2515                         if [ "$TAGVER" != "$CVSTAG" -a "$TAGVER_CVS" != "$CVSTAG" -a  "$TREE_PREFIX" != "$TAG_PREFIX" ]; then
2516                                 TAG_BRANCH="${TREE_PREFIX}-branch"
2517                                 if [ -n "$DEPTH" ]; then
2518                                         cmd_branches="git ls-remote --heads"
2519                                         ref_prefix=refs/heads
2520                                 else
2521                                         cmd_branches="git show-ref"
2522                                         ref_prefix=refs/remotes/${REMOTE_PLD}
2523                                 fi
2524                                 TAG_STATUS=$($cmd_branches | grep -i "${ref_prefix}/$TAG_BRANCH$" | cut -c'-40')
2525                                 if [ -n "$TAG_STATUS" -a "$TAG_STATUS" != $(git rev-parse "$CVSTAG") ]; then
2526                                         Exit_error err_branch_exists "$TAG_STATUS"
2527                                 fi
2528                         fi
2529
2530                 fi
2531
2532                 if [ -n "$NOSOURCE0" ] ; then
2533                         SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2534                 fi
2535                 try_upgrade
2536                 case $? in
2537                         0)
2538                                 get_files $SOURCES $PATCHES
2539                                 check_md5 $SOURCES
2540                                 ;;
2541                         *)
2542                                 NODIST="yes" get_files $SOURCES $PATCHES
2543                                 update_md5 $SOURCES $PATCHES
2544                                 ;;
2545                 esac
2546                 build_package
2547                 if [ "$UPDATE_POLDEK_INDEXES" = "yes" ] && [ "$COMMAND" = "build" -o "$COMMAND" = "build-binary" ]; then
2548                         run_poldek --sdir="${POLDEK_INDEX_DIR}" ${UPDATE_POLDEK_INDEXES_OPTS} --mkidxz
2549                 fi
2550                 remove_build_requires
2551                 ;;
2552         "branch" )
2553                 init_builder
2554                 if [ -z "$SPECFILE" ]; then
2555                         Exit_error err_no_spec_in_cmdl
2556                 fi
2557
2558                 get_spec
2559                 parse_spec
2560                 branch_files $TAG
2561                 ;;
2562         "add_cvs" )
2563                 init_builder
2564                 if [ -z "$SPECFILE" ]; then
2565                         Exit_error err_no_spec_in_cmdl
2566                 fi
2567
2568                 create_git_repo
2569                 if [ -n "$NEW_REPO" ]; then
2570                         parse_spec
2571                         local file
2572                         for file in $SOURCES $PATCHES; do
2573                                 if [ -z $(src_md5 "$file") ]; then
2574                                         git add $file || Exit_error err_no_source_in_repo $file
2575                                 else
2576                                         cvsignore_df `nourl $file`
2577                                 fi
2578                         done
2579                         git add $SPECFILE
2580                         echo "When you are ready commit your changes and run git push origin master"
2581                 else
2582                         echo "You had already git repository. Push chosen branches to remote: ${REMOTE_PLD}"
2583                 fi
2584                 ;;
2585         "get" )
2586                 init_builder
2587                 if [ -z "$SPECFILE" ]; then
2588                         Exit_error err_no_spec_in_cmdl
2589                 fi
2590
2591                 get_spec
2592                 parse_spec
2593
2594                 if [ -n "$NOSOURCE0" ] ; then
2595                         SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2596                 fi
2597                 get_files $SOURCES $PATCHES
2598                 check_md5 $SOURCES
2599                 ;;
2600         "update_md5" )
2601                 init_builder
2602                 if [ -z "$SPECFILE" ]; then
2603                         Exit_error err_no_spec_in_cmdl
2604                 fi
2605
2606                 get_spec
2607                 parse_spec
2608
2609                 if [ -n "$NOSOURCE0" ] ; then
2610                         SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
2611                 fi
2612                 update_md5 $SOURCES $PATCHES
2613                 ;;
2614         "tag" )
2615                 NOURLS=1
2616                 NODIST="yes"
2617                 init_builder
2618                 if [ -z "$SPECFILE" ]; then
2619                         Exit_error err_no_spec_in_cmdl
2620                 fi
2621
2622                 parse_spec
2623                 if  [ ! -d .git ]; then
2624                         echo "No git reposiotory" >&2
2625                         exit 101
2626                 fi
2627                 tag_files
2628                 ;;
2629         "mr-proper" )
2630                 mr_proper
2631                 ;;
2632         "list-sources-files" )
2633                 init_builder
2634                 NOCVSSPEC="yes"
2635                 DONT_PRINT_REVISION="yes"
2636                 get_spec
2637                 parse_spec
2638                 for SAP in $SOURCES $PATCHES; do
2639                         echo $SAP | awk '{gsub(/.*\//,"") ; print}'
2640                 done
2641                 ;;
2642         "list-sources-urls" )
2643                 init_builder
2644                 NOCVSSPEC="yes"
2645                 DONT_PRINT_REVISION="yes"
2646                 get_spec
2647                 parse_spec
2648                 SAPS="$SOURCES $PATCHES"
2649                 for SAP in $SAPS ; do
2650                         echo $SAP
2651                 done
2652                 ;;
2653         "list-sources-local-paths" )
2654                 init_builder
2655                 NOCVSSPEC="yes"
2656                 DONT_PRINT_REVISION="yes"
2657                 get_spec
2658                 parse_spec
2659                 for SAP in $SOURCES $PATCHES; do
2660                         echo $PACKAGE_DIR/$(echo $SAP | awk '{gsub(/.*\//,"") ; print }')
2661                 done
2662                 ;;
2663         "list-sources-distfiles-paths" )
2664                 init_builder
2665                 NOCVSSPEC="yes"
2666                 DONT_PRINT_REVISION="yes"
2667                 get_spec
2668                 parse_spec
2669                 for SAP in $SOURCES $PATCHES; do
2670                         if [ -n "$(src_md5 "$SAP")" ]; then
2671                                 distfiles_path "$SAP"
2672                         fi
2673                 done
2674                 ;;
2675         "list-sources-distfiles" )
2676                 init_builder
2677                 NOCVSSPEC="yes"
2678                 DONT_PRINT_REVISION="yes"
2679                 get_spec
2680                 parse_spec
2681                 for SAP in $SOURCES $PATCHES; do
2682                         if [ -n "$(src_md5 "$SAP")" ]; then
2683                                 distfiles_url "$SAP"
2684                         fi
2685                 done
2686                 ;;
2687         "list-sources-cvs" )
2688                 init_builder
2689 #               NOCVSSPEC="yes"
2690                 DONT_PRINT_REVISION="yes"
2691                 get_spec
2692                 parse_spec
2693                 for SAP in $SOURCES $PATCHES; do
2694                         if [ -z "$(src_md5 "$SAP")" ]; then
2695                                 echo $SAP | awk '{gsub(/.*\//,"") ; print}'
2696                         fi
2697                 done
2698                 ;;
2699         "init_rpm_dir")
2700                 init_rpm_dir
2701                 ;;
2702         "usage" )
2703                 usage
2704                 ;;
2705         "short-version" )
2706                 echo "$VERSION"
2707                 ;;
2708         "version" )
2709                 echo "$VERSIONSTRING"
2710                 ;;
2711 esac
2712 if [ -f "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES" -a "$REMOVE_BUILD_REQUIRES" != "" ]; then
2713         rm "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES"
2714 fi
2715 cd "$__PWD"
2716
2717 # vi:syntax=sh:ts=4:sw=4:noet
This page took 0.379626 seconds and 4 git commands to generate.