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