]> git.pld-linux.org Git - packages/adapter.git/blob - adapter.sh
- diffcol with tab marking, helps to understand adapter whitespace changes
[packages/adapter.git] / adapter.sh
1 #!/bin/sh
2 #
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
15 RCSID='$Id$'
16 r=${RCSID#* * }
17 rev=${r%% *}
18 VERSION="v0.35/$rev"
19 VERSIONSTRING="\
20 Adapter adapts .spec files for PLD Linux.
21 $VERSION (C) 1999-2009 Free Penguins".
22
23 PROGRAM=${0##*/}
24 dir=$(d=$0; [ -L "$d" ] && d=$(readlink "$d"); dirname "$d")
25 adapter=$dir/adapter.awk
26 usage="Usage: $PROGRAM [FLAGS] SPECFILE
27
28 -s|--no-sort|--skip-sort
29         skip BuildRequires, Requires sorting
30 -m|--no-macros|--skip-macros
31         skip use_macros() substitutions
32 -d|--skip-desc
33         skip desc wrapping
34 -a|--skip-defattr
35         skip %defattr corrections
36 -o
37         do not do any diffing, just dump the output
38 "
39
40 if [ ! -x /usr/bin/getopt ]; then
41         echo >&2 "You need to install util-linux to use adapter"
42         exit 1
43 fi
44
45 if [ ! -x /usr/bin/patch ]; then
46         echo >&2 "You need to install patch to use adapter"
47         exit 1
48 fi
49
50 [ -n "$PAGER" ] || PAGER="/usr/bin/less -r"
51
52 if [ -n "$CONFIG_DIR" ]; then
53         USER_CFG="$CONFIG_DIR/.adapterrc"
54 elif [ -n "$HOME_ETC" ]; then
55         USER_CFG="$HOME_ETC/.adapterrc"
56 else
57         USER_CFG=~/.adapterrc
58 fi
59
60 [ -f $USER_CFG ] && . $USER_CFG
61
62 t=$(getopt -o hsomdaV --long help,version,sort,sort-br,no-macros,skip-macros,skip-desc,skip-defattr -n "$PROGRAM" -- "$@") || exit $?
63 eval set -- "$t"
64
65 while true; do
66         case "$1" in
67         -h|--help)
68                 echo 2>&1 "$usage"
69                 exit 1
70         ;;
71         -s|--no-sort|--skip-sort)
72                 export SKIP_SORTBR=1
73         ;;
74         -m|--no-macros|--skip-macros)
75                 export SKIP_MACROS=1
76         ;;
77         -d|--skip-desc)
78                 export SKIP_DESC=1
79         ;;
80         -a|--skip-defattr)
81                 export SKIP_DEFATTR=1
82         ;;
83         -V|--version)
84                 echo "$VERSIONSTRING"
85                 exit 0
86                 ;;
87         -o)
88                 outputonly=1
89         ;;
90         --)
91                 shift
92                 break
93         ;;
94         *)
95                 echo >&2 "$PROGRAM: Internal error: \`$1' not recognized!"
96                 exit 1
97                 ;;
98         esac
99         shift
100 done
101
102 diffcol()
103 {
104         # vim like diff colourization
105 LC_ALL=en_US.UTF-8 gawk ' {
106         split( $0, S, /\t/ );
107         $0 = S[ 1 ];
108         for ( i = 2; i in S; i++ ) {
109                 spaces = 7 - ( (length( $0 ) - 1) % 8 );
110                 $0 = $0 "\xE2\x9E\x94";
111                 for ( y = 0; y < spaces; y++ )
112                         $0 = $0 "\xE2\x87\xBE";
113                 $0 = $0 S[ i ];
114         }
115         gsub( /\033/, "\033[44m^[\033[49m" );
116         cmd = "";
117         if ( sub( /^ /, "" ) )
118                 cmd = " ";
119         sub( /(\xE2\x9E\x94(\xE2\x87\xBE)*| )+$/, "\033[31;41m&\033[39;49m" );
120         gsub( /\xE2\x9E\x94(\xE2\x87\xBE)*/, "\033[7m&\033[27m" );
121         gsub( /\xE2\x87\xBE/, " " );
122         # uncomment if you do not like utf-8 arrow
123         # gsub( /\xE2\x9E\x94/, ">" );
124         $0 = cmd $0;
125         gsub( /\007/, "\033[44m^G\033[49m" );
126         gsub( /\r/, "\033[44m^M\033[49m" );
127 }
128 /^(Index:|diff|---|\+\+\+) / { $0 = "\033[32m" $0 }
129 /^@@ / { $0 = "\033[33m" $0 }
130 /^-/ { $0 = "\033[35m" $0 }
131 /^+/ { $0 = "\033[36m" $0 }
132 { $0 = $0 "\033[0m"; print }
133 ' "$@"
134 }
135
136 diff2hunks()
137 {
138         # diff2hunks orignally by dig
139         perl -e '
140 #! /usr/bin/perl -w
141
142 use strict;
143
144 for my $filename (@ARGV) {
145         my $counter = 1;
146         my $fh;
147         open $fh, "<", $filename or die "$filename: open for reading: $!";
148         my @lines = <$fh>;
149         my @hunks;
150         my @curheader;
151         for my $i (0 ... $#lines) {
152                 next unless $lines[$i] =~ m/^\@\@ /;
153                 if ($i >= 2 and $lines[$i - 2] =~ m/^--- / and $lines[$i - 1] =~ m/^\+\+\+ /) {
154                         @curheader = @lines[$i - 2 ... $i - 1];
155                 }
156                 next unless @curheader;
157                 my $j = $i + 1;
158                 while ($j < @lines and $lines[$j] !~ m/^\@\@ /) {$j++}
159                 $j -= 2
160                         if $j >= 3 and $j < @lines
161                                 and $lines[$j - 2] =~ m/^--- /
162                                 and $lines[$j - 1] =~ m/^\+\+\+ /;
163                 $j--;
164                 $j-- until $lines[$j] =~ m/^[ @+-]/;
165                 my $hunkfilename = $filename;
166                 $hunkfilename =~ s/((\.(pat(ch)?|diff?))?)$/"-".sprintf("%03i",$counter++).$1/ei;
167                 my $ofh;
168                 open $ofh, ">", $hunkfilename or die "$hunkfilename: open for writing: $!";
169                 print $ofh @curheader, @lines[$i ... $j];
170                 close $ofh;
171         }
172 }
173 ' "$@"
174 }
175
176 # import selected macros for adapter.awk
177 # you should update the list also in adapter.awk when making changes here
178 import_rpm_macros() {
179         macros="
180         _topdir
181         _prefix
182         _bindir
183         _sbindir
184         _libdir
185         _sysconfdir
186         _datadir
187         _includedir
188         _mandir
189         _infodir
190         _examplesdir
191         _defaultdocdir
192         _kdedocdir
193         _gtkdocdir
194         _desktopdir
195         _pixmapsdir
196         _javadir
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
234         eval $(rpm --eval "$(echo -e $eval_expr)")
235 }
236
237 adapterize() {
238         local workdir
239         workdir=$(mktemp -d ${TMPDIR:-/tmp}/adapter-XXXXXX) || exit
240         awk=gawk
241
242         local tmp=$workdir/$(basename $SPECFILE) || exit
243
244         import_rpm_macros
245
246         LC_ALL=en_US.UTF-8 $awk -f $adapter $SPECFILE > $tmp || exit
247
248         if [ "$outputonly" = 1 ]; then
249                 cat $tmp
250
251         elif [ "$(diff --brief $SPECFILE $tmp)" ]; then
252                 diff -u $SPECFILE $tmp > $tmp.diff
253                 if [ -t 1 ]; then
254                                 diffcol $tmp.diff | $PAGER
255                                 while : ; do
256                                         echo -n "Accept? (Yes, No, Confirm each chunk)? "
257                                         read ans
258                                         case "$ans" in
259                                         [yYoO]) # y0 mama
260                                                 mv -f $tmp $SPECFILE
261                                                 echo "Ok, adapterized."
262                                                 break
263                                         ;;
264                                         [cC]) # confirm each chunk
265                                                 diff2hunks $tmp.diff
266                                                 for t in $(ls $tmp-*.diff); do
267                                                                 diffcol $t | $PAGER
268                                                                 echo -n "Accept? (Yes, [N]o, Quit)? "
269                                                                 read ans
270                                                                 case "$ans" in
271                                                                 [yYoO]) # y0 mama
272                                                                         patch -p0 < $t
273                                                                         ;;
274                                                                 [Q]) # Abort
275                                                                         break
276                                                                         ;;
277                                                                 esac
278                                                 done
279                                                 break
280                                         ;;
281                                         [QqnNsS])
282                                                 echo "Ok, exiting."
283                                                 break
284                                         ;;
285                                         esac
286                                 done
287                 else
288                                 cat $tmp.diff
289                 fi
290         else
291                 echo "The SPEC is perfect ;)"
292         fi
293
294         rm -rf $workdir
295 }
296
297 SPECFILE="$1"
298 [ -f "$SPECFILE" ] || SPECFILE="$(basename $SPECFILE .spec).spec"
299
300 if [ $# -ne 1 -o ! -f "$SPECFILE" ]; then
301         echo "$usage"
302         exit 1
303 fi
304
305 adapterize
306
307 # vim: ts=4:sw=4
This page took 0.073633 seconds and 4 git commands to generate.