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