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