]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - builder.sh
- sed split was buggy, use diff2hunks code by dig
[packages/rpm-build-tools.git] / builder.sh
1 #!/bin/sh
2 # -----------
3 # $Id$
4 # Exit codes:
5 #         0 - succesful
6 #         1 - help displayed
7 #         2 - no spec file name in cmdl parameters
8 #         3 - spec file not stored in repo
9 #         4 - some source, patch or icon files not stored in repo
10 #         5 - package build failed
11 #         6 - spec file with errors
12 #         7 - wrong source in /etc/poldek.conf
13 #         8 - Failed installing buildrequirements and subrequirements
14 #         9 - Requested tag already exist
15 #        10 - Refused to build fractional release
16 #       100 - Unknown error (should not happen)
17
18 # Notes (todo):
19 #       - builder -u fetches current version first (well that's okay, how you compare versions if you have no old spec?)
20 #       - when Icon: field is present, -5 and -a5 doesn't work
21 #       - builder -R skips installing BR if spec is not present before builder invocation (need to run builder twice)
22
23 VERSION="\
24 Build package utility from PLD Linux CVS repository
25 V 0.13 (C) 1999-2005 Free Penguins".
26 PATH="/bin:/usr/bin:/usr/sbin:/sbin:/usr/X11R6/bin"
27
28 COMMAND="build"
29 TARGET=""
30
31 SPECFILE=""
32 BE_VERBOSE=""
33 QUIET=""
34 CLEAN=""
35 DEBUG=""
36 NOURLS=""
37 NOCVS=""
38 NOCVSSPEC=""
39 NODIST=""
40 UPDATE=""
41 UPDATE5=""
42 ADD5=""
43 NO5=""
44 ADAPTERIZE=""
45 ALWAYS_CVSUP=${ALWAYS_CVSUP:-"yes"}
46 CVSROOT=""
47
48 # It can be used i.e. in log file naming.
49 # See LOGFILE example.
50 DATE=`date +%Y-%m-%d_%H-%M-%S`
51
52 # Example: LOGFILE='../log.$PACKAGE_NAME'
53 # Example: LOGFILE='../LOGS/log.$PACKAGE_NAME.$DATE'
54 # Yes, you can use variable name! Note _single_ quotes!
55 LOGFILE=''
56
57 LOGDIR=""
58 LOGDIROK=""
59 LOGDIRFAIL=""
60 LASTLOG_FILE=""
61
62 CHMOD="no"
63 CHMOD_MODE="0444"
64 RPMOPTS=""
65 BCOND=""
66 GROUP_BCONDS="no"
67 CVSIGNORE_DF="no"
68
69 PATCHES=""
70 SOURCES=""
71 ICONS=""
72 PACKAGE_RELEASE=""
73 PACKAGE_VERSION=""
74 PACKAGE_NAME=""
75 PROTOCOL="ftp"
76 WGET_RETRIES=${MAX_WGET_RETRIES:-0}
77 CVS_RETRIES=${MAX_CVS_RETRIES:-1000}
78
79 CVSTAG=""
80 RES_FILE=""
81
82 CVS_SERVER="cvs.pld-linux.org"
83 DISTFILES_SERVER="://distfiles.pld-linux.org"
84 ATTICDISTFILES_SERVER="://attic-distfiles.pld-linux.org"
85
86 DEF_NICE_LEVEL=19
87
88 FAIL_IF_NO_SOURCES="yes"
89
90 if [ -x /usr/bin/rpm-getdeps ]; then
91          FETCH_BUILD_REQUIRES_RPMGETDEPS="yes"
92 else
93          FETCH_BUILD_REQUIRES_RPMGETDEPS="no"
94 fi
95
96 # Here we load saved user environment used to
97 # predefine options set above, or passed to builder
98 # in command line.
99 # This one reads global system environment settings:
100 if [ -f ~/etc/builderrc ]; then
101         . ~/etc/builderrc
102 fi
103 # And this one cascades settings using user personal
104 # builder settings.
105 # Example of ~/.builderrc:
106 #
107 #UPDATE_POLDEK_INDEXES="yes"
108 #FETCH_BUILD_REQUIRES="yes"
109 #REMOVE_BUILD_REQUIRES="force"
110 #GROUP_BCONDS="yes"
111 #LOGFILE='../LOGS/log.$PACKAGE_NAME.$DATE'
112 #
113 SU_SUDO=""
114 if [ -n "$HOME_ETC" ]; then
115         USER_CFG="$HOME_ETC/.builderrc"
116 else
117         USER_CFG=~/.builderrc
118 fi
119
120 [ -f "$USER_CFG" ] && . "$USER_CFG"
121
122 wget --help 2>&1 | grep -q ' \-\-no-check\-certificate ' && WGET_OPTS="$WGET_OPTS --no-check-certificate"
123
124 if [ -n "$USE_PROZILLA" ]; then
125         GETURI="proz --no-getch -r -P ./ -t$WGET_RETRIES $PROZILLA_OPTS"
126         GETURI2="$GETURI"
127         OUTFILEOPT="-O"
128 elif [ -n "$USE_AXEL" ]; then
129         GETURI="axel -a $AXEL_OPTS"
130         GETURI2="$GETURI"
131         OUTFILEOPT="-o"
132 else
133         wget --help 2>&1 | grep -q ' \-\-inet ' && WGET_OPTS="$WGET_OPTS --inet"
134         wget --help 2>&1 | grep -q ' \-\-retry\-connrefused ' && WGET_OPTS="$WGET_OPTS --retry-connrefused"
135
136         GETURI="wget --passive-ftp -c -nd -t$WGET_RETRIES $WGET_OPTS"
137         GETURI2="wget -c -nd -t$WGET_RETRIES $WGET_OPTS"
138         OUTFILEOPT="-O"
139 fi
140
141 GETLOCAL="cp -a"
142
143 if (rpm --version 2>&1 | grep -q '4.0.[0-2]'); then
144         RPM="rpm"
145         RPMBUILD="rpm"
146 else
147         RPM="rpm"
148         RPMBUILD="rpmbuild"
149 fi
150
151 POLDEK_INDEX_DIR="`$RPM --eval %_rpmdir`/"
152 POLDEK_CMD="$SU_SUDO /usr/bin/poldek --noask"
153
154 run_poldek()
155 {
156         RES_FILE=~/tmp/poldek-exit-status.$RANDOM
157         if [ -n "$LOGFILE" ]; then
158                 LOG=`eval echo $LOGFILE`
159                 if [ -n "$LASTLOG_FILE" ]; then
160                         echo "LASTLOG=$LOG" > $LASTLOG_FILE
161                 fi
162                 (nice -n ${DEF_NICE_LEVEL} ${POLDEK_CMD} `while test $# -gt 0; do echo "$1 ";shift;done` ; echo $? > ${RES_FILE})|tee -a $LOG
163                 return $exit_pldk
164         else
165                 (nice -n ${DEF_NICE_LEVEL} ${POLDEK_CMD} `while test $# -gt 0; do echo "$1 ";shift;done` ; echo $? > ${RES_FILE}) 1>&2 >/dev/null
166                 return `cat ${RES_FILE}`
167                 rm -rf ${RES_FILE}
168         fi
169 }
170
171 #---------------------------------------------
172 # functions
173
174 usage()
175 {
176         if [ -n "$DEBUG" ]; then set -xv; fi
177         echo "\
178 Usage: builder [-D|--debug] [-V|--version] [-a|--as_anon] [-b|-ba|--build]
179
180 [-bb|--build-binary] [-bs|--build-source] [-u|--try-upgrade]
181 [{-B|--branch} <branch>] [{-d|--cvsroot} <cvsroot>] [-g|--get]
182 [-h|--help] [--http] [{-l,--logtofile} <logfile>] [-m|--mr-proper]
183 [-q|--quiet] [--date <yyyy-mm-dd> [-r <cvstag>] [{-T--tag <cvstag>]
184 [-Tvs|--tag-version-stable] [-Tvn|--tag-version-nest]
185 [-Ts|--tag-stable] [-Tn|--tag-nest] [-Tv|--tag-version]
186 [{-Tp|--tag-prefix} <prefix>] [{-tt|--test-tag}]
187 [-nu|--no-urls] [-v|--verbose] [--opts <rpm opts>] [--show-bconds]
188 [--with/--without <feature>] [--define <macro> <value>] <package>[.spec]
189
190 -5, --update-md5    - update md5 comments in spec, implies -nd -ncs
191 --adapter[ize]      - run adapter.awk on SPECFILE
192 -a5, --add-md5      - add md5 comments to URL sources, implies -nc -nd -ncs
193 -n5, --no-md5       - ignore md5 comments in spec
194 -D, --debug         - enable builder script debugging mode,
195 -debug              - produce rpm debug package (same as --opts -debug)
196 -V, --version       - output builder version
197 -a, --as_anon       - get files via pserver as cvs@$CVS_SERVER,
198 -b, -ba, --build    - get all files from CVS repo or HTTP/FTP and build package
199                       from <package>.spec,
200 -bb, --build-binary - get all files from CVS repo or HTTP/FTP and build binary
201                       only package from <package>.spec,
202 -bs, --build-source - get all files from CVS repo or HTTP/FTP and only pack
203                       them into src.rpm,
204 -bp, --build-prep   - execute the %prep phase of <package>.spec,
205 -B, --branch        - add branch
206 -c, --clean         - clean all temporarily created files (in BUILD, SOURCES,
207                       SPECS and \$RPM_BUILD_ROOT),
208 -d <cvsroot>, --cvsroot <cvsroot>
209                     - setup \$CVSROOT,
210 --define <macro> <value>
211                     - define a macro <macro> with value <value>,
212 --nodeps            - rpm won't check any dependences
213 -g, --get           - get <package>.spec and all related files from CVS repo
214                       or HTTP/FTP,
215 -h, --help          - this message,
216 --http              - use http instead of ftp,
217 -l <logfile>, --logtofile <logfile>
218                     - log all to file,
219 -m, --mr-proper     - only remove all files related to spec file and all work
220                       resources,
221 -nc, --no-cvs       - don't download sources from CVS, if source URL is given,
222 -ncs, --no-cvs-specs
223                     - don't check specs in CVS
224 -nd, --no-distfiles - don't download from distfiles
225 -nm, --no-mirrors   - don't download from mirror, if source URL is given,
226 -nu, --no-urls      - don't try to download from FTP/HTTP location,
227 -ns, --no-srcs      - don't download Sources
228 -ns0, --no-source0  - don't download Source0
229 -nn, --no-net       - don't download anything from the net
230 --opts <rpm opts>   - additional options for rpm
231 -q, --quiet         - be quiet,
232 --date yyyy-mm-dd   - build package using resources from specified CVS date,
233 -r <cvstag>, --cvstag <cvstag>
234                     - build package using resources from specified CVS tag,
235 -R, --fetch-build-requires
236                     - fetch what is BuildRequired,
237 -RB, --remove-build-requires
238                     - remove all you fetched with -R or --fetch-build-requires
239                       remember, this option requires confirmation,
240 -FRB, --force-remove-build-requires
241                     - remove all you fetched with -R or --fetch-build-requires
242                       remember, this option works without confirmation,
243 -sf, --source-files - list sources - bare filenames (intended for offline
244                       operations; does not work when Icon field is present
245                                                          but icon file is absent),
246 -sp, --source-paths - list sources - filenames with full local paths (intended for
247                       offline operations; does not work when Icon field is present
248                                                          but icon file is absent),
249 -T <cvstag> , --tag <cvstag>
250                     - add cvs tag <cvstag> for files,
251 -Tvs, --tag-version-stable
252                     - add cvs tags STABLE and NAME-VERSION-RELEASE for files,
253 -Tvn, --tag-version-nest
254                     - add cvs tags NEST and NAME-VERSION-RELEASE for files,
255 -Ts, --tag-stable
256                     - add cvs tag STABLE for files,
257 -Tn, --tag-nest
258                     - add cvs tag NEST for files,
259 -Tv, --tag-version
260                     - add cvs tag NAME-VERSION-RELEASE for files,
261 -Tp, --tag-prefix <prefix>
262                     - add <prefix> to NAME-VERSION-RELEASE tags,
263 -tt, --test-tag <prefix>
264                     - fail if tag is already present,
265 -ir, --integer-release-only
266                     - allow only integer and snapshot releases
267 -v, --verbose       - be verbose,
268 -u, --try-upgrade   - check version, and try to upgrade package
269 -un, --try-upgrade-with-float-version
270                     - as above, but allow float version
271 -U, --update        - refetch sources, don't use distfiles, and update md5 comments
272 -Upi, --update-poldek-indexes
273                     - refresh or make poldek package index files.
274 -np, --nopatch <patchnumber>
275                     - don't apply <patchnumber>
276 --show-bconds       - show available conditional builds, which can be used
277                     - with --with and/or --without switches.
278 --with/--without <feature>
279                     - conditional build package depending on %_with_<feature>/
280                       %_without_<feature> macro switch.  You may now use
281                       --with feat1 feat2 feat3 --without feat4 feat5 --with feat6
282                       constructions. Set GROUP_BCONDS to yes to make use of it.
283 --target <platform>, --target=<platform>
284                     - build for platform <platform>.
285 "
286 }
287
288 update_shell_title() {
289         [ -t 1 ] || return
290         local msg="builder[$SPECFILE] $*"
291         case "$TERM" in
292                 cygwin|xterm*)
293                 echo -ne "\033]1;$msg\007\033]2;$msg\007"
294         ;;
295                 screen*)
296                 echo -ne "\033]0;$msg\007"
297         ;;
298         esac
299 }
300
301 # set TARGET from BuildArch: from SPECFILE
302 set_spec_target() {
303          if [ -n "$SPECFILE" ] && [ -z "$TARGET" ]; then
304                   tmp=$(awk '/^BuildArch:/ { print $NF}' $SPECFILE)
305                   if [ "$tmp" ]; then
306                                 TARGET="$tmp"
307                                 case "$RPMBUILD" in
308                                 "rpmbuild")
309                                          TARGET_SWITCH="--target $TARGET" ;;
310                                 "rpm")
311                                          TARGET_SWITCH="--target=$TARGET" ;;
312                                 esac
313                   fi
314          fi
315 }
316
317 cache_rpm_dump () {
318          if [ -n "$DEBUG" ]; then
319                   set -x;
320                   set -v;
321          fi
322
323 rpm_dump_cache=`
324         case "$RPMBUILD" in
325                 rpm )
326                         rpm -bp --nodeps --define 'prep %dump' $BCOND $TARGET_SWITCH $SPECFILE 2>&1
327                         ;;
328                 rpmbuild )
329                         rpmbuild --nodigest --nosignature --define 'prep %dump' $BCOND $TARGET_SWITCH $SPECFILE 2>&1
330                         ;;
331         esac`
332 }
333
334 rpm_dump () {
335         if [ -z "$rpm_dump_cache" ] ; then
336                 echo "internal error: cache_rpm_dump not called!" 1>&2
337         fi
338         echo "$rpm_dump_cache"
339 }
340
341 parse_spec()
342 {
343         update_shell_title "parse_spec"
344         if [ -n "$DEBUG" ]; then
345                 set -x;
346                 set -v;
347         fi
348
349         cd $SPECS_DIR
350
351         cache_rpm_dump
352
353         if [ "$NOSRCS" != "yes" ]; then
354                 SOURCES="`rpm_dump | awk '/SOURCEURL[0-9]+/ {print $3}'`"
355         fi
356         if (rpm_dump | grep -qEi ":.*nosource.*1"); then
357                 FAIL_IF_NO_SOURCES="no"
358         fi
359
360         PATCHES="`rpm_dump | awk '/PATCHURL[0-9]+/ {print $3}'`"
361         ICONS="`awk '/^Icon:/ {print $2}' ${SPECFILE}`"
362         PACKAGE_NAME="`$RPM -q --qf '%{NAME}\n' --specfile ${SPECFILE} 2> /dev/null | head -n 1`"
363         PACKAGE_VERSION="`$RPM -q --qf '%{VERSION}\n' --specfile ${SPECFILE} 2> /dev/null| head -n 1`"
364         PACKAGE_RELEASE="`$RPM -q --qf '%{RELEASE}\n' --specfile ${SPECFILE} 2> /dev/null | head -n 1`"
365
366 # These variables may be unset after first cache_rpm_dump call
367 # (because of not-yet-retrieved icon file)
368 #
369 #       if [ -z "$PACKAGE_NAME" -o -z "$PACKAGE_VERSION" -o -z "$PACKAGE_RELEASE" ]; then
370 #                Exit_error err_no_package_data;
371 #       fi
372
373         if [ -n "$BE_VERBOSE" ]; then
374                 echo "- Sources :  `nourl $SOURCES`"
375                 if [ -n "$PATCHES" ]; then
376                         echo "- Patches :  `nourl $PATCHES`"
377                 else
378                         echo "- Patches :  *no patches needed*"
379                 fi
380                 if [ -n "$ICONS" ]; then
381                         echo "- Icon    :  `nourl $ICONS`"
382                 else
383                         echo "- Icon    :  *no package icon*"
384                 fi
385                 echo "- Name    : $PACKAGE_NAME"
386                 echo "- Version : $PACKAGE_VERSION"
387                 echo "- Release : $PACKAGE_RELEASE"
388         fi
389 }
390
391 Exit_error()
392 {
393         if [ -n "$DEBUG" ]; then
394                 set -x;
395                 set -v;
396         fi
397
398         cd "$__PWD"
399
400         case "$1" in
401                 "err_no_spec_in_cmdl" )
402                         remove_build_requires
403                         echo "ERROR: spec file name not specified.";
404                         exit 2 ;;
405                 "err_no_spec_in_repo" )
406                         remove_build_requires
407                         echo "Error: spec file not stored in CVS repo.";
408                         exit 3 ;;
409                 "err_no_source_in_repo" )
410                         remove_build_requires
411                         echo "Error: some source, patch or icon files not stored in CVS repo. ($2)";
412                         exit 4 ;;
413                 "err_build_fail" )
414                         remove_build_requires
415                         echo "Error: package build failed. (${2:-no more info})";
416                         exit 5 ;;
417                 "err_no_package_data" )
418                         remove_build_requires
419                         echo "Error: couldn't get out package name/version/release from spec file."
420                         exit 6 ;;
421            "err_tag_exists" )
422                         remove_build_requires
423                         echo "Tag ${2} already exists (spec release: ${3}).";
424                         exit 9 ;;
425            "err_fract_rel" )
426                         remove_build_requires
427                         echo "Release ${2} not integer and not a snapshot.";
428                         exit 10 ;;
429                 "err_branch_exists" )
430                         remove_build_requires
431                         echo "Tree branch already exists (${2}).";
432                         exit 11 ;;
433
434         esac
435    echo "Unknown error."
436    exit 100
437 }
438
439 init_builder()
440 {
441         if [ -n "$DEBUG" ]; then
442                 set -x;
443                 set -v;
444         fi
445
446         SOURCE_DIR="`$RPM --eval '%{_sourcedir}'`"
447         SPECS_DIR="`$RPM --eval '%{_specdir}'`"
448
449         __PWD="`pwd`"
450 }
451
452 get_spec()
453 {
454
455         update_shell_title "get_spec"
456
457         if [ -n "$DEBUG" ]; then
458                 set -x;
459                 set -v;
460         fi
461
462         cd "$SPECS_DIR"
463         if [ \! -f "$SPECFILE" ]; then
464                 SPECFILE="`basename $SPECFILE .spec`.spec";
465         fi
466         if [ "$NOCVSSPEC" != "yes" ]; then
467                 OPTIONS="up "
468
469                 if [ -n "$CVSROOT" ]; then
470                         OPTIONS="-d $CVSROOT $OPTIONS"
471                 else
472                         if [ ! -s CVS/Root -a "$NOCVSSPEC" != "yes" ]; then
473                                 echo "warning: No cvs access defined - using local .spec file"
474                                 NOCVSSPEC="yes"
475                         fi
476                 fi
477
478                 if [ -z "$CVSDATE" -a -z "$CVSTAG" ]; then
479                         OPTIONS="$OPTIONS -A"
480                 else
481                         if [ -n "$CVSDATE" ]; then
482                                 OPTIONS="$OPTIONS -D $CVSDATE"
483                         fi
484                         if [ -n "$CVSTAG" ]; then
485                                 OPTIONS="$OPTIONS -r $CVSTAG"
486                         fi
487                 fi
488
489                 result=1
490                 retries_counter=0
491                 while [ "$result" != "0" -a "$retries_counter" -le "$CVS_RETRIES" ]
492                 do
493                         retries_counter=$(( $retries_counter + 1 ))
494                         output=$(LC_ALL=C cvs $OPTIONS $SPECFILE 2>&1)
495                         result=$?
496                         [ -n "$output" ] && echo "$output"
497                         if [ "$result" -ne "0" ]; then
498                                 if (echo "$output" | grep -qE "(Cannot connect to|connect to .* failed|Connection reset by peer|Connection timed out|Unknown host)") && [ "$retries_counter" -le "$CVS_RETRIES" ]; then
499                                         echo "Trying again [$SPECFILE]... ($retries_counter)"
500                                         sleep 2
501                                         continue
502                                 fi
503                                 Exit_error err_no_spec_in_repo;
504                         fi
505                 done
506         fi
507         if [ ! -f "$SPECFILE" ]; then
508                 Exit_error err_no_spec_in_repo;
509         fi
510
511         if [ "$CHMOD" = "yes" -a -n "$SPECFILE" ]; then
512                 chmod $CHMOD_MODE $SPECFILE
513         fi
514         unset OPTIONS
515         [ -n "$DONT_PRINT_REVISION" ] || grep -E -m 1 "^#.*Revision:.*Date" $SPECFILE
516
517          set_spec_target
518 }
519
520 find_mirror()
521 {
522         cd "$SPECS_DIR"
523         url="$1"
524         if [ ! -f "mirrors" -a "$NOCVSSPEC" != "yes" ] ; then
525                 cvs update mirrors >&2
526         fi
527
528         IFS="|"
529         while read origin mirror name rest
530         do
531                 ol=`echo -n "$origin"|wc -c`
532                 prefix="`echo -n "$url" | head -c $ol`"
533                 if [ "$prefix" = "$origin" ] ; then
534                         suffix="`echo "$url"|cut -b $ol-`"
535                         echo -n "$mirror$suffix"
536                         return 0
537                 fi
538         done < mirrors
539         echo "$url"
540 }
541
542 src_no ()
543 {
544         cd $SPECS_DIR
545         rpm_dump | \
546         grep "SOURCEURL[0-9]*[  ]*$1""[         ]*$" | \
547         sed -e 's/.*SOURCEURL\([0-9][0-9]*\).*/\1/' | \
548         head -n 1 | xargs
549 }
550
551 src_md5 ()
552 {
553         [ X"$NO5" = X"yes" ] && return
554         no=$(src_no "$1")
555         [ -z "$no" ] && return
556         cd $SPECS_DIR
557         spec_rev=$(grep $SPECFILE CVS/Entries 2>/dev/null | sed -e s:/$SPECFILE/:: -e s:/.*::)
558         if [ -z "$spec_rev" ]; then
559                 spec_rev="$(head -n 1 $SPECFILE | sed -e 's/.*\$Revision: \([0-9.]*\).*/\1/')"
560         fi
561         spec="$SPECFILE[0-9.,]*,$(echo $spec_rev | sed 's/\./\\./g')"
562         md5=$(grep -s -v '^#' additional-md5sums | \
563         grep -E "[      ]$(basename "$1")[      ]+${spec}([     ,]|\$)" | \
564         sed -e 's/^\([0-9a-f]\{32\}\).*/\1/' | \
565         grep -E '^[0-9a-f]{32}$')
566         if [ X"$md5" = X"" ] ; then
567                 source_md5=`grep -i "#[         ]*Source$no-md5[        ]*:" $SPECFILE | sed -e 's/.*://'`
568                 if [ ! -z "$source_md5" ] ; then
569                         echo $source_md5;
570                 else
571                         # we have empty SourceX-md5, but it is still possible
572                         # that we have NoSourceX-md5 AND NoSource: X
573                         nosource_md5=`grep -i "#[        ]*NoSource$no-md5[      ]*:" $SPECFILE | sed -e 's/.*://'`
574                         if [ ! -z "$nosource_md5" -a ! X"`grep -i "^NoSource:[   ]*$no$" $SPECFILE`" = X"" ] ; then
575                                 echo $nosource_md5;
576                         fi;
577                 fi;
578         else
579                 if [ $(echo "$md5" | wc -l) != 1 ] ; then
580                         echo "$SPECFILE: more then one entry in additional-md5sums for $1" 1>&2
581                 fi
582                 echo "$md5" | tail -n 1
583         fi
584 }
585
586 distfiles_url ()
587 {
588         echo "$PROTOCOL$DISTFILES_SERVER/distfiles/by-md5/$(src_md5 "$1" | sed -e 's|^\(.\)\(.\)|\1/\2/&|')/$(basename "$1")"
589 }
590
591 distfiles_attic_url ()
592 {
593         echo "$PROTOCOL$ATTICDISTFILES_SERVER/distfiles/Attic/by-md5/$(src_md5 "$1" | sed -e 's|^\(.\)\(.\)|\1/\2/&|')/$(basename "$1")"
594 }
595
596 good_md5 ()
597 {
598         md5=$(src_md5 "$1")
599         [ "$md5" = "" ] || \
600         [ "$md5" = "$(md5sum $(nourl "$1") 2> /dev/null | sed -e 's/ .*//')" ]
601 }
602
603 good_size ()
604 {
605          size="$(find $(nourl "$1") -printf "%s" 2>/dev/null)"
606          [ -n "$size" -a "$size" -gt 0 ]
607 }
608
609 cvsignore_df ()
610 {
611         if [ "$CVSIGNORE_DF" != "yes" ]; then
612                 return
613         fi
614         cvsignore=${SOURCE_DIR}/.cvsignore
615         if ! grep -q "^$1\$" $cvsignore 2> /dev/null; then
616                 echo "$1" >> $cvsignore
617         fi
618 }
619
620 get_files()
621 {
622         GET_FILES="$@"
623         update_shell_title "get_files"
624
625         if [ -n "$DEBUG" ]; then
626                 set -x;
627                 set -v;
628         fi
629
630         if [ -n "$1$2$3$4$5$6$7$8$9${10}" ]; then
631                 cd "$SOURCE_DIR"
632
633                 OPTIONS="up "
634                 if [ -n "$CVSROOT" ]; then
635                         OPTIONS="-d $CVSROOT $OPTIONS"
636                 else
637                         if [ ! -s CVS/Root -a "$NOCVS" != "yes" ]; then
638                                 echo "warning: No cvs access defined for SOURCES"
639                                 NOCVS="yes"
640                         fi
641                 fi
642                 if [ -z "$CVSDATE" -a -z "$CVSTAG" ]; then
643                         OPTIONS="$OPTIONS -A"
644                 else
645                         if [ -n "$CVSDATE" ]; then
646                                 OPTIONS="$OPTIONS -D $CVSDATE"
647                         fi
648                         if [ -n "$CVSTAG" ]; then
649                                 OPTIONS="$OPTIONS -r $CVSTAG"
650                         fi
651                 fi
652                 for i in $GET_FILES
653                 do
654                         if [ -n "$UPDATE5" ]; then
655                                 if [ -n "$ADD5" ]; then
656                                         [ `nourl $i` = "$i" ] && continue
657                                         grep -qiE '^#[  ]*Source'$(src_no $i)'-md5[     ]*:' $SPECS_DIR/$SPECFILE && continue
658                                 else
659                                         grep -qiE '^#[  ]*Source'$(src_no $i)'-md5[     ]*:' $SPECS_DIR/$SPECFILE || continue
660                                 fi
661                         fi
662                         FROM_DISTFILES=0
663                         if [ ! -f `nourl $i` ] || [ $ALWAYS_CVSUP = "yes" ]; then
664                                 if echo $i | grep -vE '(http|ftp|https|cvs|svn)://' | grep -qE '\.(gz|bz2)$']; then
665                                         echo "Warning: no URL given for $i"
666                                 fi
667
668                                 if [ -n "$(src_md5 "$i")" ] && [ -z "$NODIST" ]; then
669                                         if good_md5 "$i" && good_size "$i"; then
670                                                 echo "$(nourl "$i") having proper md5sum already exists"
671                                                 continue
672                                         fi
673                                         target=$(nourl "$i")
674                                         url=$(distfiles_url "$i")
675                                         url_attic=$(distfiles_attic_url "$i")
676                                         FROM_DISTFILES=1
677                                         if [ "`echo $url | grep -E '^(\.|/)'`" ]; then
678                                                 update_shell_title "get_files: $url"
679                                                 ${GETLOCAL} $url $target
680                                         else
681                                                 if [ -z "$NOMIRRORS" ]; then
682                                                         url="`find_mirror "$url"`"
683                                                 fi
684                                                 update_shell_title "get_files: $url"
685                                                 ${GETURI} ${OUTFILEOPT} "$target" "$url" || \
686                                                 if [ "`echo $url | grep -E 'ftp://'`" ]; then
687                                                         update_shell_title "get_files: $url"
688                                                         ${GETURI2} ${OUTFILEOPT} "$target" "$url"
689                                                 fi
690                                         fi
691                                         if ! test -s "$target"; then
692                                                 rm -f "$target"
693                                                 if [ `echo $url_attic | grep -E '^(\.|/)'` ]; then
694                                                         update_shell_title "get_files: $url_attic"
695                                                         ${GETLOCAL} $url_attic $target
696                                                 else
697                                                         if [ -z "$NOMIRRORS" ]; then
698                                                                 url_attic="`find_mirror "$url_attic"`"
699                                                         fi
700                                                         update_shell_title "get_files: $url_attic"
701                                                         ${GETURI} ${OUTFILEOPT} "$target" "$url_attic" || \
702                                                         if [ "`echo $url_attic | grep -E 'ftp://'`" ]; then
703                                                                  update_shell_title "get_files: $url_attic"
704                                                                 ${GETURI2} ${OUTFILEOPT} "$target" "$url_attic"
705                                                         fi
706                                                 fi
707                                         fi
708                                         if test -s "$target"; then
709                                                 cvsignore_df $target
710                                         else
711                                                 rm -f "$target"
712                                                 FROM_DISTFILES=0
713                                         fi
714                                 elif [ -z "$(src_md5 "$i")" -a "$NOCVS" != "yes" ]; then
715                                         # ( echo $i | grep -qvE '(ftp|http|https)://' ); -- if CVS should be used, but URLs preferred
716                                         result=1
717                                         retries_counter=0
718                                         while [ "$result" != "0" -a "$retries_counter" -le "$CVS_RETRIES" ]
719                                         do
720                                                 retries_counter=$(( $retries_counter + 1 ))
721                                                 output=$(LC_ALL=C cvs $OPTIONS `nourl $i` 2>&1)
722                                                 result=$?
723                                                 [ -n "$output" ] && echo "$output"
724                                                 if (echo "$output" | grep -qE "(Cannot connect to|connect to .* failed|Connection reset by peer|Connection timed out|Unknown host)") && [ "$result" -ne "0" -a "$retries_counter" -le "$CVS_RETRIES" ]; then
725                                                         echo "Trying again [`nourl $i`]... ($retries_counter)"
726                                                         sleep 2
727                                                         continue
728                                                 else
729                                                         break
730                                                 fi
731                                         done
732                                 fi
733
734                                 if [ -z "$NOURLS" ] && [ ! -f "`nourl $i`" -o -n "$UPDATE" ] && [ "`echo $i | grep -E 'ftp://|http://|https://'`" ]; then
735                                         if [ -z "$NOMIRRORS" ]; then
736                                                 im="`find_mirror "$i"`"
737                                         else
738                                                 im="$i"
739                                         fi
740                                         update_shell_title "get_files: $im"
741                                         ${GETURI} "$im" || \
742                                         if [ "`echo $im | grep -E 'ftp://'`" ]; then
743                                                  update_shell_title "get_files: $im"
744                                                 ${GETURI2} "$im"
745                                         fi
746                                 fi
747
748                         fi
749                         srcno=$(src_no $i)
750                         if [ ! -f "`nourl $i`" -a "$FAIL_IF_NO_SOURCES" != "no" ]; then
751                                 Exit_error err_no_source_in_repo $i;
752                         elif [ -n "$UPDATE5" ] && \
753                                 ( ( [ -n "$ADD5" ] && echo $i | grep -q -E 'ftp://|http://|https://' && \
754                                 [ -z "$(grep -E -i '^NoSource[  ]*:[    ]*'$i'([        ]|$)' $SPECS_DIR/$SPECFILE)" ] ) || \
755                                 grep -q -i -E '^#[      ]*source'$(src_no $i)'-md5[     ]*:' $SPECS_DIR/$SPECFILE )
756                         then
757                                 echo "Updating source-$srcno md5."
758                                 md5=$(md5sum `nourl $i` | cut -f1 -d' ')
759                                 perl -i -ne '
760                                 print unless /^\s*#\s*Source'$srcno'-md5\s*:/i;
761                                 print "# Source'$srcno'-md5:\t'$md5'\n"
762                                 if /^Source'$srcno'\s*:\s+/;
763                                 ' \
764                                 $SPECS_DIR/$SPECFILE
765                         fi
766
767                         if good_md5 "$i" && good_size "$i"; then
768                                 :
769                         elif [ "$FROM_DISTFILES" = 1 ]; then
770                                 # wrong md5 from distfiles: remove the file and try again
771                                 # but only once ...
772                                 echo "MD5 sum mismatch. Trying full fetch."
773                                 FROM_DISTFILES=2
774                                 rm -f $target
775                                 update_shell_title "get_files: $url"
776                                 ${GETURI} ${OUTFILEOPT} "$target" "$url" || \
777                                 if [ "`echo $url | grep -E 'ftp://'`" ]; then
778                                          update_shell_title "get_files: $url"
779                                         ${GETURI2} ${OUTFILEOPT} "$target" "$url"
780                                 fi
781                                 if ! test -s "$target"; then
782                                         rm -f "$target"
783                                         update_shell_title "get_files: $url_attic"
784                                         ${GETURI} ${OUTFILEOPT} "$target" "$url_attic" || \
785                                         if [ "`echo $url_attic | grep -E 'ftp://'`" ]; then
786                                                  update_shell_title "get_files: $url_attic"
787                                                 ${GETURI2} ${OUTFILEOPT} "$target" "$url_attic"
788                                         fi
789                                 fi
790                                 test -s "$target" || rm -f "$target"
791                         fi
792
793                         if good_md5 "$i" && good_size "$i" ; then
794                                 :
795                         else
796                                 echo "MD5 sum mismatch or 0 size.  Use -U to refetch sources,"
797                                 echo "or -5 to update md5 sums, if you're sure files are correct."
798                                 Exit_error err_no_source_in_repo $i
799                         fi
800                 done
801
802                 if [ "$CHMOD" = "yes" ]; then
803                         CHMOD_FILES="`nourl $GET_FILES`"
804                         if [ -n "$CHMOD_FILES" ]; then
805                                 chmod $CHMOD_MODE $CHMOD_FILES
806                         fi
807                 fi
808                 unset OPTIONS
809         fi
810 }
811
812 make_tagver() {
813
814                 if [ -n "$DEBUG" ]; then
815                          set -x;
816                          set -v;
817                 fi
818
819                 # Check whether first character of PACKAGE_NAME is legal for tag name
820                 if [ -z "${PACKAGE_NAME##[_0-9]*}" -a -z "$TAG_PREFIX" ]; then
821                         TAG_PREFIX=tag_
822                 fi
823                 TAGVER=$TAG_PREFIX$PACKAGE_NAME-`echo $PACKAGE_VERSION | sed -e "s/\./\_/g" -e "s/@/#/g"`-`echo $PACKAGE_RELEASE | sed -e "s/\./\_/g" -e "s/@/#/g"`
824                 # Remove #kernel.version_release from TAGVER because tagging sources
825                 # could occur with different kernel-headers than kernel-headers used at build time.
826                 TAGVER=$(echo "$TAGVER" | sed -e 's/#.*//g')
827                 echo -n "$TAGVER"
828 }
829
830 tag_files()
831 {
832         TAG_FILES="$@"
833
834         if [ -n "$DEBUG" ]; then
835                 set -x;
836                 set -v;
837         fi
838
839         if [ -n "$1$2$3$4$5$6$7$8$9${10}" ]; then
840                 echo "Version: $PACKAGE_VERSION"
841                 echo "Release: $PACKAGE_RELEASE"
842
843                 TAGVER=`make_tagver`
844
845                 if [ "$TAG_VERSION" = "yes" ]; then
846                         echo "CVS tag: $TAGVER"
847                 fi
848                 if [ -n "$TAG" ]; then
849                         echo "CVS tag: $TAG"
850                 fi
851
852                 OPTIONS="tag -F"
853                 if [ -n "$CVSROOT" ]; then
854                         OPTIONS="-d $CVSROOT $OPTIONS"
855                 fi
856
857                 cd "$SOURCE_DIR"
858                 for i in $TAG_FILES
859                 do
860                         # don't tag files stored on distfiles
861                         [ -n "`src_md5 $i`" ] && continue
862                         if [ -f "`nourl $i`" ]; then
863                                 if [ "$TAG_VERSION" = "yes" ]; then
864                                         cvs $OPTIONS $TAGVER `nourl $i`
865                                 fi
866                                 if [ -n "$TAG" ]; then
867                                         cvs $OPTIONS $TAG `nourl $i`
868                                 fi
869                         else
870                                 Exit_error err_no_source_in_repo $i
871                         fi
872                 done
873
874                 cd "$SPECS_DIR"
875                 if [ "$TAG_VERSION" = "yes" ]; then
876                         cvs $OPTIONS $TAGVER $SPECFILE
877                 fi
878                 if [ -n "$TAG" ]; then
879                         cvs $OPTIONS $TAG $SPECFILE
880                 fi
881
882                 unset OPTIONS
883         fi
884 }
885
886 branch_files()
887 {
888         TAG=$1
889         echo "CVS branch tag: $TAG"
890         shift;
891
892         TAG_FILES="$@"
893
894         if [ -n "$DEBUG" ]; then
895                 set -x;
896                 set -v;
897         fi
898
899         if [ -n "$1$2$3$4$5$6$7$8$9${10}" ]; then
900
901                 OPTIONS="tag -b"
902                 if [ -n "$CVSROOT" ]; then
903                         OPTIONS="-d $CVSROOT $OPTIONS"
904                 fi
905                 cd "$SOURCE_DIR"
906                 for i in $TAG_FILES
907                 do
908                         if [ -f `nourl $i` ]; then
909                                 cvs $OPTIONS $TAG `nourl $i`
910                         else
911                                 Exit_error err_no_source_in_repo $i
912                         fi
913                 done
914                 cd "$SPECS_DIR"
915                 cvs $OPTIONS $TAG $SPECFILE
916
917                 unset OPTIONS
918         fi
919 }
920
921
922
923 build_package()
924 {
925         update_shell_title "build_package"
926         if [ -n "$DEBUG" ]; then
927                 set -x;
928                 set -v;
929         fi
930
931         cd "$SPECS_DIR"
932
933         if [ -n "$TRY_UPGRADE" ]; then
934                   update_shell_title "build_package: try_upgrade"
935                 if [ -n "$FLOAT_VERSION" ]; then
936                         TNOTIFY=`./pldnotify.awk $SPECFILE -n` || exit 1
937                 else
938                         TNOTIFY=`./pldnotify.awk $SPECFILE` || exit 1
939                 fi
940
941                 TNEWVER=`echo $TNOTIFY | awk '{ match($4,/\[NEW\]/); print $5 }'`
942
943                 if [ -n "$TNEWVER" ]; then
944                         TOLDVER=`echo $TNOTIFY | awk '{ print $3; }'`
945                         echo "New version found, updating spec file to version " $TNEWVER
946                         cp -f $SPECFILE $SPECFILE.bak
947                         chmod +w $SPECFILE
948                         eval "perl -pi -e 's/Version:\t"$TOLDVER"/Version:\t"$TNEWVER"/gs' $SPECFILE"
949                         eval "perl -pi -e 's/Release:\t[1-9]{0,4}/Release:\t0.1/' $SPECFILE"
950                         parse_spec;
951                         if [ -n "$ICONS" ]; then
952                                 get_files $ICONS;
953                                 parse_spec;
954                         fi
955                         NODIST="yes" UPDATE5="yes" get_files "$SOURCES $PATCHES";
956                         unset TOLDVER TNEWVER TNOTIFY
957                 fi
958         fi
959         cd "$SPECS_DIR"
960
961         case "$COMMAND" in
962                 build )
963                         BUILD_SWITCH="-ba" ;;
964                 build-binary )
965                         BUILD_SWITCH="-bb" ;;
966                 build-source )
967                         BUILD_SWITCH="-bs --nodeps" ;;
968                 build-prep )
969                         BUILD_SWITCH="-bp --nodeps" ;;
970         esac
971
972         update_shell_title "build_package: $COMMAND"
973         if [ -n "$LOGFILE" ]; then
974                 LOG=`eval echo $LOGFILE`
975                 if [ -d "$LOG" ]; then
976                         echo "Log file $LOG is a directory."
977                         echo "Parse error in the spec?"
978                         Exit_error err_build_fail;
979                 fi
980                 if [ -n "$LASTLOG_FILE" ]; then
981                         echo "LASTLOG=$LOG" > $LASTLOG_FILE
982                 fi
983                 RES_FILE=~/tmp/$RPMBUILD-exit-status.$RANDOM
984                 (time eval nice -n ${DEF_NICE_LEVEL} $RPMBUILD $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $BCOND $TARGET_SWITCH $SPECFILE; echo $? > $RES_FILE) 2>&1 |tee $LOG
985                 RETVAL=`cat $RES_FILE`
986                 rm $RES_FILE
987                 if [ -n "$LOGDIROK" ] && [ -n "$LOGDIRFAIL" ]; then
988                         if [ "$RETVAL" -eq "0" ]; then
989                                 mv $LOG $LOGDIROK
990                         else
991                                 mv $LOG $LOGDIRFAIL
992                         fi
993                 fi
994         else
995                 eval nice -n ${DEF_NICE_LEVEL} $RPMBUILD $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $BCOND $TARGET_SWITCH $SPECFILE
996                 RETVAL=$?
997         fi
998         if [ "$RETVAL" -ne "0" ]; then
999                 if [ -n "$TRY_UPGRADE" ]; then
1000                         echo "\n!!! Package with new version cannot be build automagically\n"
1001                         mv -f $SPECFILE.bak $SPECFILE
1002                 fi
1003                 Exit_error err_build_fail;
1004         fi
1005         unset BUILD_SWITCH
1006 }
1007
1008 nourl()
1009 {
1010         echo "$@" | sed 's#\<\(ftp\|http\|https\|cvs\|svn\)://[^ ]*/##g'
1011 }
1012
1013 install_required_packages()
1014 {
1015         run_poldek -vi $1
1016         return $?
1017 }
1018
1019 set_bconds_values()
1020 {
1021         update_shell_title "set_bconds_values"
1022
1023         AVAIL_BCONDS_WITHOUT=""
1024         AVAIL_BCONDS_WITH=""
1025         if `grep -q ^%bcond ${SPECFILE}`; then
1026                 BCOND_VERSION="NEW"
1027         elif `egrep -q ^#\ *_with ${SPECFILE}`; then
1028                 BCOND_VERSION="OLD"
1029         else
1030                 BCOND_VERSION="NONE"
1031         fi
1032
1033         # expand bconds from ~/.bcondrc
1034         # The file structure is like gentoo's package.use:
1035         # ---
1036         # * -selinux
1037         # samba -mysql -pgsql
1038         # w32codec-installer license_agreement
1039         # php +mysqli
1040         # ---
1041         if [ "${BCOND_VERSION}" != "NONE" ] && ( [ -f $HOME/.bcondrc ] || ( [ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ] ) ) ; then
1042                 # This takes package name, first defined in spec.
1043                 # so consider that when defining flags for package.
1044                 PN=`$RPM -q --qf '%{NAME}\n' --specfile $SPECFILE | head -n 1`
1045                 SN=${SPECFILE%%\.spec}
1046                 AVAIL=`$RPMBUILD --bcond $SPECFILE`
1047
1048                 BCONDRC=$HOME/.bcondrc
1049                 [ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ] && BCONDRC=$HOME_ETC/.bcondrc
1050
1051                 while read pkg flags; do
1052                         # ignore comments
1053                         [[ "$pkg" == \#* ]] && continue
1054
1055                         # any package or current package?
1056                         if [ "$pkg" = "*" ] || [ "$pkg" = "$PN" ] || [ "$pkg" = "$SN" ]; then
1057                                 for flag in $flags; do
1058                                         opt=${flag#[+-]}
1059
1060                                         # use only flags which are in this package.
1061                                         if [[ $AVAIL = *${opt}* ]]; then
1062                                                 if [[ $flag = -* ]]; then
1063                                                         BCOND="$BCOND --without $opt"
1064                                                 else
1065                                                         BCOND="$BCOND --with $opt"
1066                                                 fi
1067                                         fi
1068                                 done
1069                         fi
1070                 done < $BCONDRC
1071         fi
1072
1073         case "${BCOND_VERSION}" in
1074                  NONE)
1075                         :
1076                         ;;
1077                 OLD)
1078                         echo "Warning: This spec has old style bconds. Fix it || die."
1079                         for opt in `$RPMBUILD --bcond $SPECFILE |grep ^_without_`
1080                         do
1081                                 AVAIL_BCOND_WITHOUT=`echo $opt|sed -e "s/^_without_//g"`
1082                                 if `echo $BCOND|grep -q -- "--without $AVAIL_BCOND_WITHOUT"`;then
1083                                         AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT <$AVAIL_BCOND_WITHOUT>"
1084                                 else
1085                                         AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT $AVAIL_BCOND_WITHOUT"
1086                                 fi
1087                         done
1088
1089                         for opt in `$RPMBUILD --bcond $SPECFILE |grep ^_with_`
1090                         do
1091                                 AVAIL_BCOND_WITH=`echo $opt|sed -e "s/^_with_//g"`
1092                                 if `echo $BCOND|grep -q -- "--with $AVAIL_BCOND_WITH"`;then
1093                                         AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH <$AVAIL_BCOND_WITH>"
1094                                 else
1095                                         AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH $AVAIL_BCOND_WITH"
1096                                 fi
1097                         done
1098                         ;;
1099                 NEW)
1100                         cond_type="" # with || without
1101                         for opt in `$RPMBUILD --bcond $SPECFILE`
1102                         do
1103                                 case "$opt" in
1104                                         _without)
1105                                                 cond_type="without"
1106                                                 ;;
1107                                         _with)
1108                                                 cond_type="with"
1109                                                 ;;
1110                                         _without_*)
1111                                                 AVAIL_BCOND_WITHOUT="`echo $opt | sed 's/^_without_//g'`"
1112                                                 if `echo $BCOND|grep -q -- "--without $AVAIL_BCOND_WITHOUT"`;then
1113                                                         AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT <$AVAIL_BCOND_WITHOUT>"
1114                                                 else
1115                                                         AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT $AVAIL_BCOND_WITHOUT"
1116                                                 fi
1117                                                 ;;
1118                                         _with_*)
1119                                                 AVAIL_BCOND_WITH="`echo $opt | sed 's/^_with_//g'`"
1120                                                 if `echo $BCOND|grep -q -- "--with $AVAIL_BCOND_WITH"`;then
1121                                                         AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH <$AVAIL_BCOND_WITH>"
1122                                                 else
1123                                                         AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH $AVAIL_BCOND_WITH"
1124                                                 fi
1125                                                 ;;
1126                                         *)
1127                                                 case "$cond_type" in
1128                                                         with)
1129                                                                 cond_type=''
1130                                                                 AVAIL_BCOND_WITH="$opt"
1131                                                                 if `echo $BCOND|grep -q -- "--with $AVAIL_BCOND_WITH"`;then
1132                                                                         AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH <$AVAIL_BCOND_WITH>"
1133                                                                 else
1134                                                                         AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH $AVAIL_BCOND_WITH"
1135                                                                 fi
1136                                                                 ;;
1137                                                         without)
1138                                                                 cond_type=''
1139                                                                 AVAIL_BCOND_WITHOUT="$opt"
1140                                                                 if `echo $BCOND|grep -q -- "--without $AVAIL_BCOND_WITHOUT"`;then
1141                                                                         AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT <$AVAIL_BCOND_WITHOUT>"
1142                                                                 else
1143                                                                         AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT $AVAIL_BCOND_WITHOUT"
1144                                                                 fi
1145                                                                 ;;
1146                                                 esac
1147                                                 ;;
1148                                 esac
1149                         done
1150                         ;;
1151         esac
1152 }
1153
1154 run_sub_builder()
1155 {
1156         package_name="${1}"
1157         echo -ne "Package installation failed:\t$package_name\n"
1158         #
1159         # No i tutaj bym chcia³ zrobiæ sztuczn± inteligencjê, która spróbuje tego
1160         # pakieta zbudowaæ. Aktualnie niewiele dziala, bo generalnie nie widze do
1161         # konca algorytmu... Ale damy rade. :) Na razie po prostu sie wyjebie tak samo
1162         # jakby nie bylo tego kawalka kodu.
1163         #
1164         # Update: Poprawi³em parê rzeczy i zaczê³o generowaæ pakiety spoza zadanej listy.
1165         #         Jednym s³owem budowanie niespoldkowanych zale¿no¶ci dzia³a w paru przypadkach.
1166         #
1167         #
1168         # y0shi.
1169
1170         parent_spec_name=''
1171
1172         # Istnieje taki spec? ${package}.spec
1173         if [ -f "${SPECS_DIR}${package}.spec" ]; then
1174                 parent_spec_name=${package}.spec
1175         elif [ -f "${SPECS_DIR}`echo ${package_name}|sed -e s,-devel.*,,g -e s,-static,,g`.spec" ]; then
1176                 parent_spec_name="`echo ${package_name}|sed -e s,-devel.*,,g -e s,-static,,g`.spec"
1177         else
1178                 for provides_line in `grep ^Provides:.*$package  ${SPECS_DIR} -R`
1179                 do
1180                         echo $provides_line
1181                 done
1182         fi
1183
1184         if [ "${parent_spec_name}" != "" ]; then
1185                 sub_builder_opts=''
1186                 if [ "${FETCH_BUILD_REQUIRES}" == "yes" ]; then
1187                         sub_builder_opts="${sub_builder_opts} -R"
1188                 fi
1189                 if [ "${REMOVE_BUILD_REQUIRES}" == "nice" ]; then
1190                         sub_builder_opts="${sub_builder_opts} -RB"
1191                 elif [ "${REMOVE_BUILD_REQUIRES}" == "force" ]; then
1192                         sub_builder_opts="${sub_builder_opts} -FRB"
1193                 fi
1194                 if [ "${UPDATE_POLDEK_INDEXES}" == "yes" ]; then
1195                         sub_builder_opts="${sub_builder_opts} -Upi"
1196                 fi
1197                 cd "${SPECS_DIR}"
1198                 ./builder ${sub_builder_opts} ${parent_spec_name}
1199         fi
1200         NOT_INSTALLED_PACKAGES="$NOT_INSTALLED_PACKAGES $package_name"
1201 }
1202
1203 remove_build_requires()
1204 {
1205         if [ "$INSTALLED_PACKAGES" != "" ]; then
1206                 case "$REMOVE_BUILD_REQUIRES" in
1207                         "force")
1208                                 run_poldek --noask -ve $INSTALLED_PACKAGES
1209                                 ;;
1210                         "nice")
1211                                 run_poldek --ask -ve $INSTALLED_PACKAGES
1212                                 ;;
1213                         *)
1214                                 echo You may want to manually remove following BuildRequires fetched:
1215                                 echo $INSTALLED_PACKAGES
1216                                 echo Try poldek -e \`cat `pwd`/.${SPECFILE}_INSTALLED_PACKAGES\`
1217                                 ;;
1218                 esac
1219         fi
1220 }
1221
1222 display_bconds()
1223 {
1224         if [ "$AVAIL_BCONDS_WITH" != "" ] || [ "$AVAIL_BCONDS_WITHOUT" != "" ]; then
1225                 if [ "$BCOND" != "" ]; then
1226                         echo -ne "\nBuilding $SPECFILE with the following conditional flags:\n"
1227                         echo -ne "$BCOND"
1228                 else
1229                         echo -ne "\nNo conditional flags passed"
1230                 fi
1231                 echo -ne "\n\nfrom available:\n"
1232                 echo -ne "--with   :\t$AVAIL_BCONDS_WITH\n--without:\t$AVAIL_BCONDS_WITHOUT\n\n"
1233         fi
1234 }
1235
1236 display_branches()
1237 {
1238          if [ "$NOCVSSPEC" != "yes" ]; then
1239                   echo -ne "Available branches: "
1240                   cvs status -v "${SPECFILE}" | awk '!/Sticky Tag:/ && /\(branch:/ { print $1 } ' | xargs
1241          fi
1242 }
1243
1244 # checks a given list of packages/files/provides agains current rpmdb.
1245 # outputs all dependencies whcih current rpmdb doesn't satisfy.
1246 # input can be either STDIN or parameters
1247 _rpm_prov_check()
1248 {
1249          local DEPS
1250
1251          if [ "$#" -gt 0 ]; then
1252                   DEPS="$@"
1253          else
1254                   DEPS=$(cat)
1255          fi
1256
1257          DEPS=$(rpm -q --whatprovides $DEPS 2>&1 | awk '/^(error:|no package provides)/ { print }')
1258
1259          # packages
1260          echo "$DEPS" | awk '/^no package provides/ { print $NF }'
1261
1262          # other deps (files)
1263          echo "$DEPS" | awk -F: '/^error:.*No such file/{o = $2; gsub("^ file ", "", o); print o}'
1264 }
1265
1266 # checks if given package/files/provides exists in rpmdb.
1267 # inout can be either stdin or parameters
1268 # returns packages wchi hare present in the rpmdb
1269 _rpm_cnfl_check()
1270 {
1271          local DEPS
1272
1273          if [ "$#" -gt 0 ]; then
1274                   DEPS="$@"
1275          else
1276                   DEPS=$(cat)
1277          fi
1278
1279          rpm -q --whatprovides $DEPS 2>/dev/null | awk '!/no package provides/ { print }'
1280 }
1281
1282 fetch_build_requires()
1283 {
1284         if [ "${FETCH_BUILD_REQUIRES}" = "yes" ]; then
1285                 update_shell_title "fetch_build_requires"
1286                 if [ "$FETCH_BUILD_REQUIRES_RPMGETDEPS" = "yes" ]; then
1287                         CONF=$(rpm-getdeps $BCOND $SPECFILE 2> /dev/null | awk '/^\-/ { print $3 } ' | _rpm_cnfl_check | xargs)
1288                         DEPS=$(rpm-getdeps $BCOND $SPECFILE 2> /dev/null | awk '/^\+/ { print $3 } ' | _rpm_prov_check | xargs)
1289
1290                         update_shell_title "fetch_build_requires: update indexes"
1291                         if [ -n "$CONF" ] || [ -n "$DEPS" ]; then
1292                                 $SU_SUDO /usr/bin/poldek --update || $SU_SUDO /usr/bin/poldek --upa
1293                         fi
1294                         if [ -n "$CONF" ]; then
1295                                  update_shell_title "fetch_build_requires: uninstall conflicting packages"
1296                                 echo "Trying to uninstall conflicting packages ($CONF):"
1297                                 $SU_SUDO /usr/bin/poldek --noask --nofollow -ev $CONF
1298                         fi
1299                         if [ -n "$DEPS" ]; then
1300                                 update_shell_title "fetch_build_requires: install deps ($DEPS)"
1301                                 echo "Trying to install dependencies ($DEPS):"
1302                                 $SU_SUDO /usr/bin/poldek --caplookup -uGv $DEPS
1303                         fi
1304                         return
1305                 fi
1306
1307                 echo -ne "\nAll packages installed by fetch_build_requires() are written to:\n"
1308                 echo -ne "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES\n"
1309                 echo -ne "\nIf anything fails, you may get rid of them by executing:\n"
1310                 echo "poldek -e \`cat `pwd`/.${SPECFILE}_INSTALLED_PACKAGES\`\n\n"
1311                 echo > `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1312                 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`
1313                 do
1314                         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`"
1315                         GO="yes"
1316                         package=`basename "$package_item"|sed -e "s/}$//g"`
1317                         COND_ARCH_TST="`cat $SPECFILE|grep -B1 BuildRequires|grep -B1 $package|grep ifarch|sed -e "s/^.*ifarch//g"`"
1318                         mach=`uname -m`
1319
1320                         COND_TST=`cat $SPECFILE|grep BuildRequires|grep "$package"`
1321                         if `echo $COND_TST|grep -q '^BuildRequires:'`; then
1322                                 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
1323                                         GO="yes"
1324                                 fi
1325                         # bcond:
1326                         else
1327                                 COND_NAME=`echo $COND_TST|sed -e s,:BuildRequires:.*$,,g`
1328                                 GO=""
1329                                 # %{without}
1330                                 if `echo $COND_TST|grep -q 'without_'`; then
1331                                         COND_NAME=`echo $COND_NAME|sed -e s,^.*without_,,g`
1332                                         if `echo $COND_TST|grep -q !`; then
1333                                                 COND_STATE="with"
1334                                         else
1335                                                 COND_STATE="wout"
1336                                         fi
1337                                         COND_WITH=`echo $AVAIL_BCONDS_WITH|grep "<$COND_NAME>"`
1338                                         COND_WITHOUT=`echo $AVAIL_BCONDS_WITHOUT|grep "<$COND_NAME>"`
1339                                         if [ -n "$COND_WITHOUT" ] || [ -z "$COND_WITH" ]; then
1340                                                 COND_ARGV="wout"
1341                                         else
1342                                                 COND_ARGV="with"
1343                                         fi
1344                                 # %{with}
1345                                 elif `echo $COND_TST|grep -q 'with_'`; then
1346                                         COND_NAME=`echo $COND_NAME|sed -e s,^.*with_,,g`
1347                                         if `echo $COND_TST|grep -q !`; then
1348                                                 COND_STATE="wout"
1349                                         else
1350                                                 COND_STATE="with"
1351                                         fi
1352                                         COND_WITH=`echo $AVAIL_BCONDS_WITH|grep "<$COND_NAME>"`
1353                                         COND_WITHOUT=`echo $AVAIL_BCONDS_WITHOUT|grep "<$COND_NAME>"`
1354                                         if [ -n "$COND_WITH" ] || [ -z "$COND_WITHOUT" ]; then
1355                                                 COND_ARGV="with"
1356                                         else
1357                                                 COND_ARGV="wout"
1358                                         fi
1359                                 fi
1360                                 RESULT="${COND_STATE}-${COND_ARGV}"
1361                                 case "$RESULT" in
1362                                         "with-wout" | "wout-with" )
1363                                                 GO=""
1364                                                 ;;
1365                                         "wout-wout" | "with-with" )
1366                                                 GO="yes"
1367                                                 ;;
1368                                         * )
1369                                                 echo "Action '$RESULT' was not defined for package '$package_item'"
1370                                                 GO="yes"
1371                                                 ;;
1372                                 esac
1373                         fi
1374
1375                         if [ "$GO" = "yes" ]; then
1376                                 if [ "`rpm -q $package|sed -e "s/$package.*/$package/g"`" != "$package" ]; then
1377                                         echo "Testing if $package has subrequirements..."
1378                                         run_poldek -t -i $package --dumpn=".$package-req.txt"
1379                                         if [ -f ".$package-req.txt" ]; then
1380                                                 for package_name in `cat ".$package-req.txt"|grep -v ^#`
1381                                                 do
1382                                                         if [ "$package_name" = "$package" ]; then
1383                                                                 echo -ne "Installing BuildRequired package:\t$package_name\n"
1384                                                                 export PROMPT_COMMAND=`echo -ne "\033]0;${SPECFILE}: Installing BuildRequired package: ${package_name}\007"`
1385                                                                 install_required_packages $package;
1386                                                         else
1387                                                                 echo -ne "Installing (sub)Required package:\t$package_name\n"
1388                                                                 export PROMPT_COMMAND=`echo -ne "\033]0;${SPECFILE}: Installing (sub)Required package: ${package_name}\007"`
1389                                                                 install_required_packages $package_name;
1390                                                         fi
1391                                                         case $? in
1392                                                                 0)
1393                                                                         INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
1394                                                                         echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1395                                                                         ;;
1396                                                                 *)
1397                                                                         echo "Attempting to run spawn sub - builder..."
1398                                                                         run_sub_builder $package_name
1399                                                                         if [ $? -eq 0 ]; then
1400                                                                                 install_required_packages $package_name;
1401                                                                                 case $? in
1402                                                                                         0)
1403                                                                                                 INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
1404                                                                                                 echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1405                                                                                                 ;;
1406                                                                                         *)
1407                                                                                                 NOT_INSTALLED_PACKAGES="$package_name $NOT_INSTALLED_PACKAGES"
1408                                                                                                 ;;
1409                                                                                 esac
1410                                                                         fi
1411                                                                         ;;
1412                                                         esac
1413                                                 done
1414                                                 rm -f ".$package-req.txt"
1415                                         else
1416                                                 echo "Attempting to run spawn sub - builder..."
1417                                                 run_sub_builder $package
1418                                                 if [ $? -eq 0 ]; then
1419                                                         install_required_packages $package;
1420                                                         case $? in
1421                                                                 0)
1422                                                                         INSTALLED_PACKAGES="$package_name $INSTALLED_PACKAGES"
1423                                                                         echo $package_name >> `pwd`/.${SPECFILE}_INSTALLED_PACKAGES
1424                                                                         ;;
1425                                                                 *)
1426                                                                         NOT_INSTALLED_PACKAGES="$package_name $NOT_INSTALLED_PACKAGES"
1427                                                                         ;;
1428                                                         esac
1429                                                 fi
1430                                         fi
1431                                 else
1432                                         echo "Package $package is already installed. BuildRequirement satisfied."
1433                                 fi
1434                         fi
1435                 done
1436                 export PROMPT_COMMAND=`echo -ne "\033]0;${SPECFILE}\007"`
1437                 if [ "$NOT_INSTALLED_PACKAGES" != "" ]; then
1438                         echo "Unable to install following packages and their dependencies:"
1439                         for pkg in "$NOT_INSTALLED_PACKAGES"
1440                         do
1441                                 echo $pkg
1442                         done
1443                         remove_build_requires
1444                         exit 8
1445                 fi
1446         fi
1447 }
1448
1449 diffcol()
1450 {
1451          # vim like diff colourization, glen@pld-linux.org
1452          sed -e '
1453          s,\e,\e[44m^[\e[49m,g;
1454          s,\a,\e[44m^G\e[49m,g;
1455          s,^\(Index:\|diff\|---\|+++\) .*$,\e[32m&,;
1456          s,^@@ ,\e[33m&,g;
1457          s,^-,\e[35m&,;
1458          s,^+,\e[36m&,;
1459          s,\r,\e[44m^M\e[49m,g;
1460          s,     ,    ,g;
1461          s,\([^[:space:]]\)\([[:space:]]\+\)$,\1\e[41m\2\e[49m,g;
1462          s,$,\e[0m,
1463          ' "$@"
1464 }
1465
1466
1467 diff2hunks()
1468 {
1469          # diff2hunks orignally by dig
1470          perl -e '
1471 #! /usr/bin/perl -w
1472
1473 use strict;
1474
1475 for my $filename (@ARGV) {
1476     my $counter = 1;
1477     my $fh;
1478     open $fh, "<", $filename or die "$filename: open for reading: $!";
1479     my @lines = <$fh>;
1480     my @hunks;
1481     my @curheader;
1482     for my $i (0 ... $#lines) {
1483         next unless $lines[$i] =~ m/^\@\@ /;
1484         if ($i >= 2 and $lines[$i - 2] =~ m/^--- / and $lines[$i - 1] =~ m/^\+\+\+ /) {
1485             @curheader = @lines[$i - 2 ... $i - 1];
1486         }
1487         next unless @curheader;
1488         my $j = $i + 1;
1489         while ($j < @lines and $lines[$j] !~ m/^\@\@ /) {$j++}
1490         $j -= 2
1491             if $j >= 3 and $j < @lines
1492                 and $lines[$j - 2] =~ m/^--- /
1493                 and $lines[$j - 1] =~ m/^\+\+\+ /;
1494         $j--;
1495         $j-- until $lines[$j] =~ m/^[ @+-]/;
1496         my $hunkfilename = $filename;
1497         $hunkfilename =~ s/((\.(pat(ch)?|diff?))?)$/"-".sprintf("%03i",$counter++).$1/ei;
1498         my $ofh;
1499         open $ofh, ">", $hunkfilename or die "$hunkfilename: open for writing: $!";
1500         print $ofh @curheader, @lines[$i ... $j];
1501         close $ofh;
1502     }
1503 }
1504 ' "$@"
1505 }
1506
1507 adapterize()
1508 {
1509          cd "$SPECS_DIR"
1510          local tmpdir
1511          tmpdir=$(mktemp -d ${TMPDIR:-/tmp}/adapter-XXXXXX) || exit
1512          awk -f adapter.awk $SPECFILE > $tmpdir/$SPECFILE || exit
1513
1514          if [ "`diff --brief $SPECFILE $tmpdir/$SPECFILE`" ] ; then
1515                   diff -u $SPECFILE $tmpdir/$SPECFILE > $tmpdir/$SPECFILE.diff
1516                   if [ -t 1 ]; then
1517                                 diffcol $tmpdir/$SPECFILE.diff | less -r
1518                                 while : ; do
1519                                          echo -n "Accept? (Yes, No, Confirm)? "
1520                                          read ans
1521                                          case "$ans" in
1522                                          [yYoO]) # y0 mama
1523                                                   mv -f $tmpdir/$SPECFILE $SPECFILE
1524                                                   echo "Ok, adapterized."
1525                                                   break
1526                                          ;;
1527                                          [cC]) # confirm each chunk
1528                                                   diff2hunks $tmpdir/$SPECFILE.diff
1529                                                   for t in $(ls $tmpdir/$SPECFILE-*.diff); do
1530                                                                 diffcol $t | less -r
1531                                                                 echo -n "Accept? (Yes, [N]o)? "
1532                                                                 read ans
1533                                                                 case "$ans" in
1534                                                                 [yYoO]) # y0 mama
1535                                                                         patch < $t
1536                                                                         ;;
1537                                                                 esac
1538                                                   done
1539                                                   break
1540                                          ;;
1541                                          [nNsS])
1542                                                   echo "Ok, exiting."
1543                                                   break
1544                                          ;;
1545                                          esac
1546                                 done
1547                   else
1548                                 cat $tmpdir/$SPECFILE.diff
1549                   fi
1550          else
1551                   echo "The SPEC is perfect ;)"
1552          fi
1553
1554          rm -rf $tmpdir
1555 }
1556
1557 #---------------------------------------------
1558 # main()
1559
1560 if [ "$#" = 0 ]; then
1561         usage;
1562         exit 1
1563 fi
1564
1565 while test $# -gt 0
1566 do
1567         case "${1}" in
1568                 -5 | --update-md5 )
1569                         COMMAND="get";
1570                         NODIST="yes"
1571                         NOCVSSPEC="yes"
1572                         UPDATE5="yes"
1573                         shift ;;
1574                 --adapter | --adapterize )
1575                         COMMAND="adapterize";
1576                         NODIST="yes"
1577                         NOCVSSPEC="yes"
1578                         ADAPTERIZE="yes"
1579                         shift ;;
1580                 -a5 | --add-md5 )
1581                         COMMAND="get";
1582                         NODIST="yes"
1583                         NOCVS="yes"
1584                         NOCVSSPEC="yes"
1585                         UPDATE5="yes"
1586                         ADD5="yes"
1587                         shift ;;
1588                 -n5 | --no-md5 )
1589                         NO5="yes"
1590                         shift ;;
1591                 -D | --debug )
1592                         DEBUG="yes"; shift ;;
1593                 -V | --version )
1594                         COMMAND="version"; shift ;;
1595                 -a | --as_anon )
1596                         CVSROOT=":pserver:cvs@$CVS_SERVER:/cvsroot"; shift ;;
1597                 -b | -ba | --build )
1598                         COMMAND="build"; shift ;;
1599                 -bb | --build-binary )
1600                         COMMAND="build-binary"; shift ;;
1601                 -bs | --build-source )
1602                         COMMAND="build-source"; shift ;;
1603                 -bp | --build-prep )
1604                         COMMAND="build-prep"; shift ;;
1605                 -B | --branch )
1606                         COMMAND="branch"; shift; TAG="${1}"; shift;;
1607                 -c | --clean )
1608                         CLEAN="--clean --rmspec --rmsource"; shift ;;
1609                 -d | --cvsroot )
1610                         shift; CVSROOT="${1}"; shift ;;
1611                 -g | --get )
1612                         COMMAND="get"; shift ;;
1613                 -h | --help )
1614                         COMMAND="usage"; shift ;;
1615                 --http )
1616                         PROTOCOL="http"; shift ;;
1617                 -l | --logtofile )
1618                         shift; LOGFILE="${1}"; shift ;;
1619                 -ni| --nice )
1620                         shift; DEF_NICE_LEVEL=${1}; shift ;;
1621                 -m | --mr-proper )
1622                         COMMAND="mr-proper"; shift ;;
1623                 -nc | --no-cvs )
1624                         NOCVS="yes"; shift ;;
1625                 -ncs | --no-cvs-specs )
1626                         NOCVSSPEC="yes"; shift ;;
1627                 -nd | --no-distfiles )
1628                         NODIST="yes"; shift ;;
1629                 -nm | --no-mirrors )
1630                         NOMIRRORS="yes"; shift ;;
1631                 -nu | --no-urls )
1632                         NOURLS="yes"; shift ;;
1633                 -ns | --no-srcs )
1634                         NOSRCS="yes"; shift ;;
1635                 -ns0 | --no-source0 )
1636                         NOSOURCE0="yes"; shift ;;
1637                 -nn | --no-net )
1638                         NOCVS="yes"
1639                         NOCVSSPEC="yes"
1640                         NODIST="yes"
1641                         NOMIRRORS="yes"
1642                         NOURLS="yes"
1643                         NOSRCS="yes"
1644                         shift;;
1645                 --opts )
1646                         shift; RPMOPTS="$RPM_OPTS ${1}"; shift ;;
1647                 --nopatch | -np )
1648                         shift; RPMOPTS="${RPMOPTS} --define \"patch${1} echo ignoring patch${1} \""; shift ;;
1649                 --with | --without )
1650                         case $GROUP_BCONDS in
1651                                 "yes")
1652                                         COND=${1}
1653                                         shift
1654                                         while ! `echo ${1}|grep -qE '(^-|spec)'`
1655                                         do
1656                                                 BCOND="$BCOND $COND $1"
1657                                                 shift
1658                                         done;;
1659                                 "no")
1660                                         if [[ "$2" = *,* ]]; then
1661                                                  for a in $(echo "$2" | tr , ' '); do
1662                                                           BCOND="$BCOND $1 $a"
1663                                                  done
1664                                         else
1665                                                  BCOND="$BCOND $1 $2"
1666                                         fi
1667                                         shift 2 ;;
1668                         esac
1669                         ;;
1670                 --target )
1671                         shift; TARGET="${1}"; shift ;;
1672                 --target=* )
1673                         TARGET=$(echo "${1}" | sed 's/^--target=//'); shift ;;
1674                 -q | --quiet )
1675                         QUIET="--quiet"; shift ;;
1676                 --date )
1677                         CVSDATE="${2}"; shift 2 ;;
1678                 -r | --cvstag )
1679                         shift; CVSTAG="${1}"; shift ;;
1680                 -R | --fetch-build-requires)
1681                         FETCH_BUILD_REQUIRES="yes"
1682                         NOT_INSTALLED_PACKAGES=
1683                         shift ;;
1684                 -RB | --remove-build-requires)
1685                         REMOVE_BUILD_REQUIRES="nice"
1686                         shift ;;
1687                 -FRB | --force-remove-build-requires)
1688                         REMOVE_BUILD_REQUIRES="force"
1689                         shift ;;
1690                 -sf | --sources-files)
1691                         COMMAND="list-sources-files"
1692                         shift ;;
1693                 -sp | --sources-paths)
1694                         COMMAND="list-sources-local-paths"
1695                         shift ;;
1696                 -Tvs | --tag-version-stable )
1697                         COMMAND="tag";
1698                         TAG="STABLE"
1699                         TAG_VERSION="yes"
1700                         shift;;
1701                 -Tvn | --tag-version-nest )
1702                         COMMAND="tag";
1703                         TAG="NEST"
1704                         TAG_VERSION="yes"
1705                         shift;;
1706                 -Ts | --tag-stable )
1707                         COMMAND="tag";
1708                         TAG="STABLE"
1709                         TAG_VERSION="no"
1710                         shift;;
1711                 -Tn | --tag-nest )
1712                         COMMAND="tag";
1713                         TAG="NEST"
1714                         TAG_VERSION="no"
1715                         shift;;
1716                 -Tv | --tag-version )
1717                         COMMAND="tag";
1718                         TAG=""
1719                         TAG_VERSION="yes"
1720                         shift;;
1721                 -Tp | --tag-prefix )
1722                         TAG_PREFIX="$2"
1723                         shift 2;;
1724                 -tt | --test-tag )
1725                         TEST_TAG="yes"
1726                         shift;;
1727                 -T | --tag )
1728                         COMMAND="tag";
1729                         shift
1730                         TAG="$1"
1731                         TAG_VERSION="no"
1732                         shift;;
1733                 -ir | --integer-release-only )
1734                         INTEGER_RELEASE="yes"
1735                         shift;;
1736                 -U | --update )
1737                         COMMAND="get"
1738                         UPDATE="yes"
1739                         NOCVSSPEC="yes"
1740                         NODIST="yes"
1741                         UPDATE5="yes"
1742                         shift ;;
1743                 -Upi | --update-poldek-indexes )
1744                         UPDATE_POLDEK_INDEXES="yes"
1745                         shift ;;
1746                 -u | --try-upgrade )
1747                         TRY_UPGRADE="1"; shift ;;
1748                 -un | --try-upgrade-with-float-version )
1749                         TRY_UPGRADE="1"; FLOAT_VERSION="1"; shift ;;
1750                 -v | --verbose )
1751                         BE_VERBOSE="1"; shift ;;
1752                 --define)
1753                         shift
1754                         MACRO="${1}"
1755                         VALUE="${2}"
1756                         shift 2
1757                         RPMOPTS="${RPMOPTS} --define \"${MACRO} ${VALUE}\""
1758                         ;;
1759                 --show-bconds | -show-bconds | -print-bconds | --print-bconds | -display-bconds | --display-bconds )
1760                         SHOW_BCONDS="yes"
1761                         shift
1762                         ;;
1763                 --nodeps)
1764                         shift
1765                         RPMOPTS="${RPMOPTS} --nodeps"
1766                         ;;
1767                 -debug)
1768                         RPMOPTS="${RPMOPTS} -debug"; shift ;;
1769                 * )
1770                         SPECFILE="${1}"
1771                         export PROMPT_COMMAND=`echo -ne "\033]0;${SPECFILE}\007"`
1772                         shift ;;
1773         esac
1774 done
1775
1776 if [ -n "$DEBUG" ]; then
1777         set -x;
1778         set -v;
1779 fi
1780
1781 if [ -n "$TARGET" ]; then
1782         case "$RPMBUILD" in
1783                 "rpmbuild")
1784                         TARGET_SWITCH="--target $TARGET" ;;
1785                 "rpm")
1786                         TARGET_SWITCH="--target=$TARGET" ;;
1787         esac
1788 fi
1789
1790 update_shell_title "$COMMAND"
1791 case "$COMMAND" in
1792         "build" | "build-binary" | "build-source" | "build-prep" )
1793                 init_builder;
1794                 if [ -n "$SPECFILE" ]; then
1795                         get_spec;
1796                         set_bconds_values;
1797                         display_bconds;
1798                         display_branches;
1799                         [ X"$SHOW_BCONDS" = X"yes" ] && exit 0
1800                         fetch_build_requires;
1801                         parse_spec;
1802                         if [ "$INTEGER_RELEASE" = "yes" ]; then
1803                                 echo "Checking release $PACKAGE_RELEASE..."
1804                                 if echo $PACKAGE_RELEASE | grep -q '^[^.]*\.[^.]*$' 2>/dev/null ; then
1805                                         Exit_error err_fract_rel "$PACKAGE_RELEASE"
1806                                 fi
1807                         fi
1808
1809                         if [ -n "$TEST_TAG" ]; then
1810                                 TAGVER=`make_tagver`
1811                                 echo "Searching for tag $TAGVER..."
1812                                 TAGREL=$(cvs status -v $SPECFILE | grep -E "^[[:space:]]*${TAGVER}[[[:space:]]" | sed -e 's#.*(revision: ##g' -e 's#).*##g')
1813                                 if [ -n "$TAGREL" ]; then
1814                                         Exit_error err_tag_exists "$TAGVER" "$TAGREL"
1815                                 fi
1816
1817                                 # - do not allow to build from HEAD when XX-branch exists
1818                                 TREE_PREFIX=$(echo "$TAG_PREFIX" | sed -e 's#^auto-\([a-zA-Z]\+\)-.*#\1#g')
1819                                 if [ "$TREE_PREFIX" != "$TAG_PREFIX" ]; then
1820                                          TAG_BRANCH="${TREE_PREFIX}-branch"
1821                                          TAG_STATUS=$(cvs status -v $SPECFILE | grep -Ei "${TAG_BRANCH}.+(branch: [0-9.]+)")
1822                                          if [ -n "$TAG_STATUS" -a "$CVSTAG" = "HEAD" ]; then
1823                                                   Exit_error err_branch_exists "$TAG_STATUS"
1824                                          fi
1825
1826                                 fi
1827                         fi
1828
1829                         if [ -n "$ICONS" ]; then
1830                                 get_files $ICONS;
1831                                 parse_spec;
1832                         fi
1833                         if [ -n "$NOSOURCE0" ] ; then
1834                                 SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
1835                         fi
1836                         get_files "$SOURCES $PATCHES";
1837                         build_package;
1838                         if [ "$UPDATE_POLDEK_INDEXES" = "yes" -a "$COMMAND" != "build-prep" ]; then
1839                                 run_poldek --sdir="${POLDEK_INDEX_DIR}" --mkidxz
1840                         fi
1841                         remove_build_requires;
1842                 else
1843                         Exit_error err_no_spec_in_cmdl;
1844                 fi
1845                 ;;
1846         "branch" )
1847                 init_builder;
1848                 if [ -n "$SPECFILE" ]; then
1849                         get_spec;
1850                         parse_spec;
1851                         if [ -n "$ICONS" ]; then
1852                                 get_files $ICONS
1853                                 parse_spec;
1854                         fi
1855                         get_files $SOURCES $PATCHES;
1856                         branch_files $TAG "$SOURCES $PATCHES $ICONS";
1857                 else
1858                         Exit_error err_no_spec_in_cmdl;
1859                 fi
1860                 ;;
1861         "get" )
1862                 init_builder;
1863                 if [ -n "$SPECFILE" ]; then
1864                         get_spec;
1865                         parse_spec;
1866                         if [ -n "$ICONS" ]; then
1867                                 OLD_UPDATE5=$UPDATE5
1868                                 UPDATE5= get_files $ICONS
1869                                 UPDATE5=$OLD_UPDATE5
1870                                 parse_spec;
1871                         fi
1872                         if [ -n "$NOSOURCE0" ] ; then
1873                                 SOURCES=`echo $SOURCES | xargs | sed -e 's/[^ ]*//'`
1874                         fi
1875                         get_files $SOURCES $PATCHES
1876                 else
1877                         Exit_error err_no_spec_in_cmdl;
1878                 fi
1879                 ;;
1880         "tag" )
1881                 NOURLS=1
1882                 NODIST="yes"
1883                 init_builder;
1884                 if [ -n "$SPECFILE" ]; then
1885                         get_spec;
1886                         parse_spec;
1887                         if [ -n "$ICONS" ]; then
1888                                 get_files $ICONS
1889                                 parse_spec;
1890                         fi
1891                         # don't fetch sources from remote locations
1892                         new_SOURCES=""
1893                         for file in $SOURCES
1894                         do
1895                                 [ -n "`src_md5 $file`" ] && continue
1896                                 new_SOURCES="$new_SOURCES $file"
1897                         done
1898                         SOURCES="$new_SOURCES"
1899                         get_files $SOURCES $PATCHES;
1900                         tag_files "$SOURCES $PATCHES $ICONS";
1901                 else
1902                         Exit_error err_no_spec_in_cmdl;
1903                 fi
1904                 ;;
1905         "mr-proper" )
1906                 $RPM --clean --rmsource --rmspec --force --nodeps $SPECFILE
1907                 ;;
1908         "list-sources-files" )
1909                 init_builder
1910                 NOCVSSPEC="yes"
1911                 DONT_PRINT_REVISION="yes"
1912                 get_spec
1913                 parse_spec
1914                 SAPS="$SOURCES $PATCHES"
1915                 for SAP in $SAPS ; do
1916                          echo $SAP | awk '{gsub(/.*\//,"") ; print}'
1917                 done
1918                 ;;
1919         "list-sources-local-paths" )
1920                 init_builder
1921                 NOCVSSPEC="yes"
1922                 DONT_PRINT_REVISION="yes"
1923                 get_spec
1924                 parse_spec
1925                 SAPS="$SOURCES $PATCHES"
1926                 for SAP in $SAPS ; do
1927                          echo $SOURCE_DIR/$(echo $SAP | awk '{gsub(/.*\//,"") ; print }')
1928                 done
1929                 ;;
1930         "adapterize" )
1931                 adapterize
1932                 ;;
1933         "usage" )
1934                 usage;;
1935         "version" )
1936                 echo "$VERSION";;
1937 esac
1938 if [ -f "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES" -a "$REMOVE_BUILD_REQUIRES" != "" ]; then
1939         rm "`pwd`/.${SPECFILE}_INSTALLED_PACKAGES"
1940 fi
1941 cd "$__PWD"
1942
1943 # vi:syntax=sh:ts=3:sw=4
This page took 0.192176 seconds and 4 git commands to generate.