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