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