]> git.pld-linux.org Git - packages/adapter.git/blob - adapter.sh
- use less only if terminal is to short
[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 t=$(getopt -o hsomdaV --long help,version,sort,sort-br,no-macros,skip-macros,skip-desc,skip-defattr -n "$PROGRAM" -- "$@") || exit $?
51 eval set -- "$t"
52
53 while true; do
54         case "$1" in
55         -h|--help)
56                 echo 2>&1 "$usage"
57                 exit 1
58         ;;
59         -s|--no-sort|--skip-sort)
60                 export SKIP_SORTBR=1
61         ;;
62         -m|--no-macros|--skip-macros)
63                 export SKIP_MACROS=1
64         ;;
65         -d|--skip-desc)
66                 export SKIP_DESC=1
67         ;;
68         -a|--skip-defattr)
69                 export SKIP_DEFATTR=1
70         ;;
71         -V|--version)
72                 echo "$VERSIONSTRING"
73                 exit 0
74                 ;;
75         -o)
76                 outputonly=1
77         ;;
78         --)
79                 shift
80                 break
81         ;;
82         *)
83                 echo >&2 "$PROGRAM: Internal error: \`$1' not recognized!"
84                 exit 1
85                 ;;
86         esac
87         shift
88 done
89
90 diffcol()
91 {
92          # vim like diff colourization
93          sed -e '
94          s,\e,\e[44m^[\e[49m,g;
95          s,\a,\e[44m^G\e[49m,g;
96          s,^\(Index:\|diff\|---\|+++\) .*$,\e[32m&,;
97          s,^@@ ,\e[33m&,g;
98          s,^-,\e[35m&,;
99          s,^+,\e[36m&,;
100          s,\r,\e[44m^M\e[49m,g;
101          s,     ,    ,g;
102          s,\([^[:space:]]\)\([[:space:]]\+\)$,\1\e[41m\2\e[49m,g;
103          s,$,\e[0m,
104          ' "$@"
105 }
106
107 showdiff()
108 {
109         l=$(cat $1 | wc -l)
110         eval $(resize) # get terminal size
111         if [ $l -gt $LINES ]; then
112                 diffcol $1 | less -r
113         else
114                 diffcol $1
115         fi
116 }
117
118 diff2hunks()
119 {
120          # diff2hunks orignally by dig
121          perl -e '
122 #! /usr/bin/perl -w
123
124 use strict;
125
126 for my $filename (@ARGV) {
127         my $counter = 1;
128         my $fh;
129         open $fh, "<", $filename or die "$filename: open for reading: $!";
130         my @lines = <$fh>;
131         my @hunks;
132         my @curheader;
133         for my $i (0 ... $#lines) {
134                 next unless $lines[$i] =~ m/^\@\@ /;
135                 if ($i >= 2 and $lines[$i - 2] =~ m/^--- / and $lines[$i - 1] =~ m/^\+\+\+ /) {
136                         @curheader = @lines[$i - 2 ... $i - 1];
137                 }
138                 next unless @curheader;
139                 my $j = $i + 1;
140                 while ($j < @lines and $lines[$j] !~ m/^\@\@ /) {$j++}
141                 $j -= 2
142                         if $j >= 3 and $j < @lines
143                                 and $lines[$j - 2] =~ m/^--- /
144                                 and $lines[$j - 1] =~ m/^\+\+\+ /;
145                 $j--;
146                 $j-- until $lines[$j] =~ m/^[ @+-]/;
147                 my $hunkfilename = $filename;
148                 $hunkfilename =~ s/((\.(pat(ch)?|diff?))?)$/"-".sprintf("%03i",$counter++).$1/ei;
149                 my $ofh;
150                 open $ofh, ">", $hunkfilename or die "$hunkfilename: open for writing: $!";
151                 print $ofh @curheader, @lines[$i ... $j];
152                 close $ofh;
153         }
154 }
155 ' "$@"
156 }
157
158 # import selected macros for adapter.awk
159 # you should update the list also in adapter.awk when making changes here
160 import_rpm_macros() {
161         macros="
162         _topdir
163         _prefix
164         _bindir
165         _sbindir
166         _libdir
167         _sysconfdir
168         _datadir
169         _includedir
170         _mandir
171         _infodir
172         _examplesdir
173         _defaultdocdir
174         _kdedocdir
175         _gtkdocdir
176         _desktopdir
177         _pixmapsdir
178         _javadir
179
180         perl_sitearch
181         perl_archlib
182         perl_privlib
183         perl_vendorlib
184         perl_vendorarch
185         perl_sitelib
186
187         py_sitescriptdir
188         py_sitedir
189         py_scriptdir
190         py_ver
191
192         ruby_archdir
193         ruby_ridir
194         ruby_rubylibdir
195         ruby_sitearchdir
196         ruby_sitelibdir
197         ruby_rdocdir
198
199         php_pear_dir
200         php_data_dir
201         tmpdir
202 "
203         eval_expr=""
204         for macro in $macros; do
205                 eval_expr="$eval_expr\nexport $macro='%{$macro}'"
206         done
207
208
209         # get cvsaddress for changelog section
210         # using rpm macros as too lazy to add ~/.adapterrc parsing support.
211         eval_expr="$eval_expr
212         export _cvsmaildomain='%{?_cvsmaildomain}%{!?_cvsmaildomain:@pld-linux.org}'
213         export _cvsmailfeedback='%{?_cvsmailfeedback}%{!?_cvsmailfeedback:PLD Team <feedback@pld-linux.org>}'
214         "
215
216         eval $(rpm --eval "$(echo -e $eval_expr)")
217 }
218
219 adapterize() {
220         local workdir
221         workdir=$(mktemp -d ${TMPDIR:-/tmp}/adapter-XXXXXX) || exit
222         awk=gawk
223
224         local tmp=$workdir/$(basename $SPECFILE) || exit
225
226         import_rpm_macros
227
228         LC_ALL=en_US.UTF-8 $awk -f $adapter $SPECFILE > $tmp || exit
229
230         if [ "$outputonly" = 1 ]; then
231                 cat $tmp
232
233         elif [ "$(diff --brief $SPECFILE $tmp)" ]; then
234                 diff -u $SPECFILE $tmp > $tmp.diff
235                 if [ -t 1 ]; then
236                                 showdiff $tmp.diff
237                                 while : ; do
238                                         echo -n "Accept? (Yes, No, Confirm each chunk)? "
239                                         read ans
240                                         case "$ans" in
241                                         [yYoO]) # y0 mama
242                                                 mv -f $tmp $SPECFILE
243                                                 echo "Ok, adapterized."
244                                                 break
245                                         ;;
246                                         [cC]) # confirm each chunk
247                                                 diff2hunks $tmp.diff
248                                                 for t in $(ls $tmp-*.diff); do
249                                                                 showdiff $t
250                                                                 echo -n "Accept? (Yes, [N]o, Quit)? "
251                                                                 read ans
252                                                                 case "$ans" in
253                                                                 [yYoO]) # y0 mama
254                                                                         patch < $t
255                                                                         ;;
256                                                                 [Q]) # Abort
257                                                                         break
258                                                                         ;;
259                                                                 esac
260                                                 done
261                                                 break
262                                         ;;
263                                         [QqnNsS])
264                                                 echo "Ok, exiting."
265                                                 break
266                                         ;;
267                                         esac
268                                 done
269                 else
270                                 cat $tmp.diff
271                 fi
272         else
273                 echo "The SPEC is perfect ;)"
274         fi
275
276         rm -rf $workdir
277 }
278
279 SPECFILE="$1"
280 [ -f "$SPECFILE" ] || SPECFILE="$(basename $SPECFILE .spec).spec"
281
282 if [ $# -ne 1 -o ! -f "$SPECFILE" ]; then
283         echo "$usage"
284         exit 1
285 fi
286
287 adapterize
This page took 0.082101 seconds and 4 git commands to generate.