]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - adapter.sh
- adapter 0.29
[packages/rpm-build-tools.git] / adapter.sh
1 #!/bin/sh
2 #
3 # This is adapter v0.29. Adapter adapts .spec files for PLD Linux.
4 #
5 # Copyright (C) 1999-2003 PLD-Team <feedback@pld-linux.org>
6 # Authors:
7 #       Michał Kuratczyk <kura@pld.org.pl>
8 #       Sebastian Zagrodzki <s.zagrodzki@mimuw.edu.pl>
9 #       Tomasz Kłoczko <kloczek@rudy.mif.pg.gda.pl>
10 #       Artur Frysiak <wiget@pld-linux.org>
11 #       Michal Kochanowicz <mkochano@pld.org.pl>
12 #       Elan Ruusamäe <glen@pld-linux.org>
13 #
14 # See cvs log adapter{,.awk} for list of contributors
15 #
16 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
17
18 self=$(basename "$0")
19 adapter=$(dirname "$0")/adapter.awk
20 usage="Usage: $self [FLAGS] SPECFILE
21
22 -s|--no-sort|--skip-sort
23         skip BuildRequires, Requires sorting
24 -m|--no-macros|--skip-macros
25         skip use_macros() substitutions
26 -d|--skip-desc
27         skip desc wrapping
28 -a|--skip-defattr
29         skip %defattr corrections
30
31 "
32
33 if [ ! -x /usr/bin/getopt ]; then
34         echo >&1 "You need to install util-linux to use adapter"
35         exit 1
36 fi
37
38 if [ ! -x /usr/bin/patch ]; then
39         echo >&1 "You need to install patch to use adapter"
40         exit 1
41 fi
42
43 t=`getopt -o hsmda --long help,sort,sort-br,no-macros,skip-macros,skip-desc,skip-defattr -n "$self" -- "$@"` || exit $?
44 eval set -- "$t"
45
46 while true; do
47         case "$1" in
48         -h|--help)
49                 echo 2>&1 "$usage"
50                 exit 1
51         ;;
52         -s|--no-sort|--skip-sort)
53                 export SKIP_SORTBR=1
54         ;;
55         -m|--no-macros|--skip-macros)
56                 export SKIP_MACROS=1
57         ;;
58         -d|--skip-desc)
59                 export SKIP_DESC=1
60         ;;
61         -a|--skip-defattr)
62                 export SKIP_DEFATTR=1
63         ;;
64         --)
65                 shift
66                 break
67         ;;
68         *)
69                 echo 2>&1 "$self: Internal error: [$1] not recognized!"
70                 exit 1
71                 ;;
72         esac
73         shift
74 done
75
76 diffcol()
77 {
78          # vim like diff colourization
79          sed -e '
80          s,\e,\e[44m^[\e[49m,g;
81          s,\a,\e[44m^G\e[49m,g;
82          s,^\(Index:\|diff\|---\|+++\) .*$,\e[32m&,;
83          s,^@@ ,\e[33m&,g;
84          s,^-,\e[35m&,;
85          s,^+,\e[36m&,;
86          s,\r,\e[44m^M\e[49m,g;
87          s,     ,    ,g;
88          s,\([^[:space:]]\)\([[:space:]]\+\)$,\1\e[41m\2\e[49m,g;
89          s,$,\e[0m,
90          ' "$@"
91 }
92
93 diff2hunks()
94 {
95          # diff2hunks orignally by dig
96          perl -e '
97 #! /usr/bin/perl -w
98
99 use strict;
100
101 for my $filename (@ARGV) {
102         my $counter = 1;
103         my $fh;
104         open $fh, "<", $filename or die "$filename: open for reading: $!";
105         my @lines = <$fh>;
106         my @hunks;
107         my @curheader;
108         for my $i (0 ... $#lines) {
109                 next unless $lines[$i] =~ m/^\@\@ /;
110                 if ($i >= 2 and $lines[$i - 2] =~ m/^--- / and $lines[$i - 1] =~ m/^\+\+\+ /) {
111                         @curheader = @lines[$i - 2 ... $i - 1];
112                 }
113                 next unless @curheader;
114                 my $j = $i + 1;
115                 while ($j < @lines and $lines[$j] !~ m/^\@\@ /) {$j++}
116                 $j -= 2
117                         if $j >= 3 and $j < @lines
118                                 and $lines[$j - 2] =~ m/^--- /
119                                 and $lines[$j - 1] =~ m/^\+\+\+ /;
120                 $j--;
121                 $j-- until $lines[$j] =~ m/^[ @+-]/;
122                 my $hunkfilename = $filename;
123                 $hunkfilename =~ s/((\.(pat(ch)?|diff?))?)$/"-".sprintf("%03i",$counter++).$1/ei;
124                 my $ofh;
125                 open $ofh, ">", $hunkfilename or die "$hunkfilename: open for writing: $!";
126                 print $ofh @curheader, @lines[$i ... $j];
127                 close $ofh;
128         }
129 }
130 ' "$@"
131 }
132
133 adapterize()
134 {
135         local tmpdir
136         tmpdir=$(mktemp -d ${TMPDIR:-/tmp}/adapter-XXXXXX) || exit
137         if grep -q '\.UTF-8' $SPECFILE; then
138                 awk=gawk
139         else
140                 awk=awk
141         fi
142
143         local tmp=$tmpdir/$(basename $SPECFILE) || exit
144         $awk -f $adapter $SPECFILE > $tmp || exit
145
146         if [ "$(diff --brief $SPECFILE $tmp)" ]; then
147                 diff -u $SPECFILE $tmp > $tmp.diff
148                 if [ -t 1 ]; then
149                                 diffcol $tmp.diff | less -r
150                                 while : ; do
151                                         echo -n "Accept? (Yes, No, Confirm each chunk)? "
152                                         read ans
153                                         case "$ans" in
154                                         [yYoO]) # y0 mama
155                                                 mv -f $tmp $SPECFILE
156                                                 echo "Ok, adapterized."
157                                                 break
158                                         ;;
159                                         [cC]) # confirm each chunk
160                                                 diff2hunks $tmp.diff
161                                                 for t in $(ls $tmp-*.diff); do
162                                                                 diffcol $t | less -r
163                                                                 echo -n "Accept? (Yes, [N]o, Quit)? "
164                                                                 read ans
165                                                                 case "$ans" in
166                                                                 [yYoO]) # y0 mama
167                                                                         patch < $t
168                                                                         ;;
169                                                                 [Q]) # Abort
170                                                                         break
171                                                                         ;;
172                                                                 esac
173                                                 done
174                                                 break
175                                         ;;
176                                         [QqnNsS])
177                                                 echo "Ok, exiting."
178                                                 break
179                                         ;;
180                                         esac
181                                 done
182                 else
183                                 cat $tmp.diff
184                 fi
185         else
186                 echo "The SPEC is perfect ;)"
187         fi
188
189         rm -rf $tmpdir
190 }
191
192 if [ $# -ne 1 -o ! -f "$1" ]; then
193         echo "$usage"
194         exit 1
195 fi
196
197 SPECFILE="$1"
198 adapterize
This page took 0.659926 seconds and 4 git commands to generate.