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