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