]> git.pld-linux.org Git - packages/rpm-build-tools.git/blame - adapter_.sh
- some fedora extras addons
[packages/rpm-build-tools.git] / adapter_.sh
CommitLineData
fd283fd9
ER
1#!/bin/sh
2#
fd283fd9
ER
3# Authors:
4# Michał Kuratczyk <kura@pld.org.pl>
5# Sebastian Zagrodzki <s.zagrodzki@mimuw.edu.pl>
6# Tomasz Kłoczko <kloczek@rudy.mif.pg.gda.pl>
7# Artur Frysiak <wiget@pld-linux.org>
8# Michal Kochanowicz <mkochano@pld.org.pl>
9# Elan Ruusamäe <glen@pld-linux.org>
10#
11# See cvs log adapter{,.awk} for list of contributors
12#
13# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
14
77b7f1e2 15RCSID='$Id$'
41f549c5 16REVISION=${RCSID#* * } REVISION=${REVISION%% *}
17VERSION="v0.35/$REVISION"
77b7f1e2
ER
18VERSIONSTRING="\
19Adapter adapts .spec files for PLD Linux.
41f549c5 20$VERSION (C) 1999-2010 Free Penguins".
77b7f1e2
ER
21
22PROGRAM=${0##*/}
41f549c5 23dir=$(d=$0; [ -L "$d" ] && d=$(readlink -f "$d"); dirname "$d")
77b7f1e2
ER
24adapter=$dir/adapter.awk
25usage="Usage: $PROGRAM [FLAGS] SPECFILE
fd283fd9
ER
26
27-s|--no-sort|--skip-sort
28 skip BuildRequires, Requires sorting
29-m|--no-macros|--skip-macros
30 skip use_macros() substitutions
31-d|--skip-desc
32 skip desc wrapping
33-a|--skip-defattr
34 skip %defattr corrections
77b7f1e2
ER
35-o
36 do not do any diffing, just dump the output
fd283fd9
ER
37"
38
39if [ ! -x /usr/bin/getopt ]; then
77b7f1e2 40 echo >&2 "You need to install util-linux to use adapter"
fd283fd9
ER
41 exit 1
42fi
43
44if [ ! -x /usr/bin/patch ]; then
77b7f1e2 45 echo >&2 "You need to install patch to use adapter"
fd283fd9
ER
46 exit 1
47fi
48
87a659b0 49[ -n "$PAGER" ] || PAGER="/usr/bin/less -r"
50
51if [ -n "$CONFIG_DIR" ]; then
52 USER_CFG="$CONFIG_DIR/.adapterrc"
53elif [ -n "$HOME_ETC" ]; then
54 USER_CFG="$HOME_ETC/.adapterrc"
55else
56 USER_CFG=~/.adapterrc
57fi
58
59[ -f $USER_CFG ] && . $USER_CFG
60
77b7f1e2 61t=$(getopt -o hsomdaV --long help,version,sort,sort-br,no-macros,skip-macros,skip-desc,skip-defattr -n "$PROGRAM" -- "$@") || exit $?
fd283fd9
ER
62eval set -- "$t"
63
64while true; do
65 case "$1" in
66 -h|--help)
87a659b0 67 echo 2>&1 "$usage"
fd283fd9
ER
68 exit 1
69 ;;
70 -s|--no-sort|--skip-sort)
71 export SKIP_SORTBR=1
72 ;;
73 -m|--no-macros|--skip-macros)
74 export SKIP_MACROS=1
75 ;;
76 -d|--skip-desc)
77 export SKIP_DESC=1
78 ;;
79 -a|--skip-defattr)
80 export SKIP_DEFATTR=1
81 ;;
77b7f1e2
ER
82 -V|--version)
83 echo "$VERSIONSTRING"
84 exit 0
85 ;;
86 -o)
87 outputonly=1
88 ;;
fd283fd9
ER
89 --)
90 shift
db90382f 91 break
fd283fd9
ER
92 ;;
93 *)
77b7f1e2 94 echo >&2 "$PROGRAM: Internal error: \`$1' not recognized!"
fd283fd9 95 exit 1
db90382f 96 ;;
fd283fd9
ER
97 esac
98 shift
99done
100
101diffcol()
102{
87a659b0 103 # vim like diff colourization
104LC_ALL=en_US.UTF-8 gawk ' {
105 split( $0, S, /\t/ );
106 $0 = S[ 1 ];
107 for ( i = 2; i in S; i++ ) {
108 spaces = 7 - ( (length( $0 ) - 1) % 8 );
109 $0 = $0 "\xE2\x9E\x94";
110 for ( y = 0; y < spaces; y++ )
111 $0 = $0 "\xE2\x87\xBE";
112 $0 = $0 S[ i ];
113 }
114 gsub( /\033/, "\033[44m^[\033[49m" );
115 cmd = "";
116 if ( sub( /^ /, "" ) )
117 cmd = " ";
118 sub( /(\xE2\x9E\x94(\xE2\x87\xBE)*| )+$/, "\033[31;41m&\033[39;49m" );
119 gsub( /\xE2\x9E\x94(\xE2\x87\xBE)*/, "\033[7m&\033[27m" );
120 gsub( /\xE2\x87\xBE/, " " );
121 # uncomment if you do not like utf-8 arrow
122 # gsub( /\xE2\x9E\x94/, ">" );
123 $0 = cmd $0;
124 gsub( /\007/, "\033[44m^G\033[49m" );
125 gsub( /\r/, "\033[44m^M\033[49m" );
126}
127/^(Index:|diff|---|\+\+\+) / { $0 = "\033[32m" $0 }
128/^@@ / { $0 = "\033[33m" $0 }
129/^-/ { $0 = "\033[35m" $0 }
130/^+/ { $0 = "\033[36m" $0 }
131{ $0 = $0 "\033[0m"; print }
132' "$@"
fd283fd9
ER
133}
134
135diff2hunks()
136{
87a659b0 137 # diff2hunks orignally by dig
138 perl -e '
fd283fd9
ER
139#! /usr/bin/perl -w
140
141use strict;
142
143for my $filename (@ARGV) {
db90382f
ER
144 my $counter = 1;
145 my $fh;
146 open $fh, "<", $filename or die "$filename: open for reading: $!";
147 my @lines = <$fh>;
148 my @hunks;
149 my @curheader;
150 for my $i (0 ... $#lines) {
151 next unless $lines[$i] =~ m/^\@\@ /;
152 if ($i >= 2 and $lines[$i - 2] =~ m/^--- / and $lines[$i - 1] =~ m/^\+\+\+ /) {
153 @curheader = @lines[$i - 2 ... $i - 1];
154 }
155 next unless @curheader;
156 my $j = $i + 1;
157 while ($j < @lines and $lines[$j] !~ m/^\@\@ /) {$j++}
158 $j -= 2
159 if $j >= 3 and $j < @lines
160 and $lines[$j - 2] =~ m/^--- /
161 and $lines[$j - 1] =~ m/^\+\+\+ /;
162 $j--;
163 $j-- until $lines[$j] =~ m/^[ @+-]/;
164 my $hunkfilename = $filename;
165 $hunkfilename =~ s/((\.(pat(ch)?|diff?))?)$/"-".sprintf("%03i",$counter++).$1/ei;
166 my $ofh;
167 open $ofh, ">", $hunkfilename or die "$hunkfilename: open for writing: $!";
168 print $ofh @curheader, @lines[$i ... $j];
169 close $ofh;
170 }
fd283fd9
ER
171}
172' "$@"
173}
174
77b7f1e2
ER
175# import selected macros for adapter.awk
176# you should update the list also in adapter.awk when making changes here
177import_rpm_macros() {
178 macros="
179 _topdir
180 _prefix
181 _bindir
182 _sbindir
183 _libdir
184 _sysconfdir
185 _datadir
186 _includedir
187 _mandir
188 _infodir
189 _examplesdir
190 _defaultdocdir
191 _kdedocdir
192 _gtkdocdir
193 _desktopdir
194 _pixmapsdir
195 _javadir
41f549c5 196 _pkgconfigdir
77b7f1e2
ER
197
198 perl_sitearch
199 perl_archlib
200 perl_privlib
201 perl_vendorlib
202 perl_vendorarch
203 perl_sitelib
204
205 py_sitescriptdir
206 py_sitedir
207 py_scriptdir
208 py_ver
209
210 ruby_archdir
211 ruby_ridir
212 ruby_rubylibdir
213 ruby_sitearchdir
214 ruby_sitelibdir
215 ruby_rdocdir
216
217 php_pear_dir
218 php_data_dir
219 tmpdir
220"
221 eval_expr=""
222 for macro in $macros; do
223 eval_expr="$eval_expr\nexport $macro='%{$macro}'"
224 done
225
226
227 # get cvsaddress for changelog section
228 # using rpm macros as too lazy to add ~/.adapterrc parsing support.
229 eval_expr="$eval_expr
230 export _cvsmaildomain='%{?_cvsmaildomain}%{!?_cvsmaildomain:@pld-linux.org}'
231 export _cvsmailfeedback='%{?_cvsmailfeedback}%{!?_cvsmailfeedback:PLD Team <feedback@pld-linux.org>}'
232 "
233
41f549c5 234 export ADAPTER_REVISION=$REVISION
235
77b7f1e2
ER
236 eval $(rpm --eval "$(echo -e $eval_expr)")
237}
238
239adapterize() {
240 local workdir
41f549c5 241 workdir=$(mktemp -d ${TMPDIR:-/tmp}/adapter-XXXXXX) || exit $?
77b7f1e2
ER
242 awk=gawk
243
41f549c5 244 local tmp=$workdir/$(basename $SPECFILE) || exit $?
77b7f1e2
ER
245
246 import_rpm_macros
247
41f549c5 248 LC_ALL=en_US.UTF-8 $awk -f $adapter $SPECFILE > $tmp || exit $?
db90382f 249
77b7f1e2
ER
250 if [ "$outputonly" = 1 ]; then
251 cat $tmp
db90382f 252
77b7f1e2 253 elif [ "$(diff --brief $SPECFILE $tmp)" ]; then
db90382f
ER
254 diff -u $SPECFILE $tmp > $tmp.diff
255 if [ -t 1 ]; then
87a659b0 256 diffcol $tmp.diff | $PAGER
fd283fd9 257 while : ; do
db90382f
ER
258 echo -n "Accept? (Yes, No, Confirm each chunk)? "
259 read ans
260 case "$ans" in
261 [yYoO]) # y0 mama
262 mv -f $tmp $SPECFILE
263 echo "Ok, adapterized."
264 break
265 ;;
266 [cC]) # confirm each chunk
267 diff2hunks $tmp.diff
268 for t in $(ls $tmp-*.diff); do
87a659b0 269 diffcol $t | $PAGER
fd283fd9
ER
270 echo -n "Accept? (Yes, [N]o, Quit)? "
271 read ans
272 case "$ans" in
273 [yYoO]) # y0 mama
87a659b0 274 patch -p0 < $t
fd283fd9 275 ;;
db90382f 276 [Q]) # Abort
fd283fd9
ER
277 break
278 ;;
279 esac
db90382f
ER
280 done
281 break
282 ;;
283 [QqnNsS])
284 echo "Ok, exiting."
285 break
286 ;;
287 esac
fd283fd9 288 done
db90382f
ER
289 else
290 cat $tmp.diff
291 fi
292 else
293 echo "The SPEC is perfect ;)"
294 fi
295
77b7f1e2 296 rm -rf $workdir
fd283fd9
ER
297}
298
81deaf3c
ER
299SPECFILE="$1"
300[ -f "$SPECFILE" ] || SPECFILE="$(basename $SPECFILE .spec).spec"
301
302if [ $# -ne 1 -o ! -f "$SPECFILE" ]; then
fd283fd9
ER
303 echo "$usage"
304 exit 1
305fi
306
fd283fd9 307adapterize
87a659b0 308
309# vim: ts=4:sw=4
This page took 0.087298 seconds and 4 git commands to generate.