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