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>
11 # See cvs log adapter{,.awk} for list of contributors
13 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
20 Adapter adapts .spec files for PLD Linux.
21 $VERSION (C) 1999-2009 Free Penguins".
24 dir=$(d=$0; [ -L "$d" ] && d=$(readlink "$d"); dirname "$d")
25 adapter=$dir/adapter.awk
26 usage="Usage: $PROGRAM [FLAGS] SPECFILE
28 -s|--no-sort|--skip-sort
29 skip BuildRequires, Requires sorting
30 -m|--no-macros|--skip-macros
31 skip use_macros() substitutions
35 skip %defattr corrections
37 do not do any diffing, just dump the output
40 if [ ! -x /usr/bin/getopt ]; then
41 echo >&2 "You need to install util-linux to use adapter"
45 if [ ! -x /usr/bin/patch ]; then
46 echo >&2 "You need to install patch to use adapter"
50 [ -n "$PAGER" ] || PAGER="/usr/bin/less -r"
52 if [ -n "$CONFIG_DIR" ]; then
53 USER_CFG="$CONFIG_DIR/.adapterrc"
54 elif [ -n "$HOME_ETC" ]; then
55 USER_CFG="$HOME_ETC/.adapterrc"
60 [ -f $USER_CFG ] && . $USER_CFG
62 t=$(getopt -o hsomdaV --long help,version,sort,sort-br,no-macros,skip-macros,skip-desc,skip-defattr -n "$PROGRAM" -- "$@") || exit $?
71 -s|--no-sort|--skip-sort)
74 -m|--no-macros|--skip-macros)
95 echo >&2 "$PROGRAM: Internal error: \`$1' not recognized!"
104 # vim like diff colourization
105 LC_ALL=en_US.UTF-8 gawk ' {
106 split( $0, S, /\t/ );
108 for ( i = 2; i in S; i++ ) {
109 spaces = 7 - ( (length( $0 ) - 1) % 8 );
110 $0 = $0 "\xE2\x9E\x94";
111 for ( y = 0; y < spaces; y++ )
112 $0 = $0 "\xE2\x87\xBE";
115 gsub( /\033/, "\033[44m^[\033[49m" );
117 if ( sub( /^ /, "" ) )
119 sub( /(\xE2\x9E\x94(\xE2\x87\xBE)*| )+$/, "\033[31;41m&\033[39;49m" );
120 gsub( /\xE2\x9E\x94(\xE2\x87\xBE)*/, "\033[7m&\033[27m" );
121 gsub( /\xE2\x87\xBE/, " " );
122 # uncomment if you do not like utf-8 arrow
123 # gsub( /\xE2\x9E\x94/, ">" );
125 gsub( /\007/, "\033[44m^G\033[49m" );
126 gsub( /\r/, "\033[44m^M\033[49m" );
128 /^(Index:|diff|---|\+\+\+) / { $0 = "\033[32m" $0 }
129 /^@@ / { $0 = "\033[33m" $0 }
130 /^-/ { $0 = "\033[35m" $0 }
131 /^+/ { $0 = "\033[36m" $0 }
132 { $0 = $0 "\033[0m"; print }
138 # diff2hunks orignally by dig
144 for my $filename (@ARGV) {
147 open $fh, "<", $filename or die "$filename: open for reading: $!";
151 for my $i (0 ... $#lines) {
152 next unless $lines[$i] =~ m/^\@\@ /;
153 if ($i >= 2 and $lines[$i - 2] =~ m/^--- / and $lines[$i - 1] =~ m/^\+\+\+ /) {
154 @curheader = @lines[$i - 2 ... $i - 1];
156 next unless @curheader;
158 while ($j < @lines and $lines[$j] !~ m/^\@\@ /) {$j++}
160 if $j >= 3 and $j < @lines
161 and $lines[$j - 2] =~ m/^--- /
162 and $lines[$j - 1] =~ m/^\+\+\+ /;
164 $j-- until $lines[$j] =~ m/^[ @+-]/;
165 my $hunkfilename = $filename;
166 $hunkfilename =~ s/((\.(pat(ch)?|diff?))?)$/"-".sprintf("%03i",$counter++).$1/ei;
168 open $ofh, ">", $hunkfilename or die "$hunkfilename: open for writing: $!";
169 print $ofh @curheader, @lines[$i ... $j];
176 # import selected macros for adapter.awk
177 # you should update the list also in adapter.awk when making changes here
178 import_rpm_macros() {
222 for macro in $macros; do
223 eval_expr="$eval_expr\nexport $macro='%{$macro}'"
227 # get cvsaddress for changelog section
228 # using rpm macros as too lazy to add ~/.adapterrc parsing support.
229 eval_expr="$eval_expr
230 export _cvsmaildomain='%{?_cvsmaildomain}%{!?_cvsmaildomain:@pld-linux.org}'
231 export _cvsmailfeedback='%{?_cvsmailfeedback}%{!?_cvsmailfeedback:PLD Team <feedback@pld-linux.org>}'
234 eval $(rpm --eval "$(echo -e $eval_expr)")
239 workdir=$(mktemp -d ${TMPDIR:-/tmp}/adapter-XXXXXX) || exit
242 local tmp=$workdir/$(basename $SPECFILE) || exit
246 LC_ALL=en_US.UTF-8 $awk -f $adapter $SPECFILE > $tmp || exit
248 if [ "$outputonly" = 1 ]; then
251 elif [ "$(diff --brief $SPECFILE $tmp)" ]; then
252 diff -u $SPECFILE $tmp > $tmp.diff
254 diffcol $tmp.diff | $PAGER
256 echo -n "Accept? (Yes, No, Confirm each chunk)? "
261 echo "Ok, adapterized."
264 [cC]) # confirm each chunk
266 for t in $(ls $tmp-*.diff); do
268 echo -n "Accept? (Yes, [N]o, Quit)? "
291 echo "The SPEC is perfect ;)"
298 [ -f "$SPECFILE" ] || SPECFILE="$(basename $SPECFILE .spec).spec"
300 if [ $# -ne 1 -o ! -f "$SPECFILE" ]; then