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 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
16 REVISION=${RCSID#* * } REVISION=${REVISION%% *}
17 VERSION="v0.35/$REVISION"
19 Adapter adapts .spec files for PLD Linux.
20 $VERSION (C) 1999-2010 Free Penguins".
23 dir=$(d=$0; [ -L "$d" ] && d=$(readlink -f "$d"); dirname "$d")
24 adapter=$dir/adapter.awk
25 usage="Usage: $PROGRAM [FLAGS] SPECFILE
27 -s|--no-sort|--skip-sort
28 skip BuildRequires, Requires sorting
29 -m|--no-macros|--skip-macros
30 skip use_macros() substitutions
34 skip %defattr corrections
36 do not do any diffing, just dump the output
39 if [ ! -x /usr/bin/getopt ]; then
40 echo >&2 "You need to install util-linux to use adapter"
44 if [ ! -x /usr/bin/patch ]; then
45 echo >&2 "You need to install patch to use adapter"
49 [ -n "$PAGER" ] || PAGER="/usr/bin/less -r"
51 if [ -n "$CONFIG_DIR" ]; then
52 USER_CFG="$CONFIG_DIR/.adapterrc"
53 elif [ -n "$HOME_ETC" ]; then
54 USER_CFG="$HOME_ETC/.adapterrc"
59 [ -f $USER_CFG ] && . $USER_CFG
61 t=$(getopt -o hsomdaV --long help,version,sort,sort-br,no-macros,skip-macros,skip-desc,skip-defattr -n "$PROGRAM" -- "$@") || exit $?
70 -s|--no-sort|--skip-sort)
73 -m|--no-macros|--skip-macros)
94 echo >&2 "$PROGRAM: Internal error: \`$1' not recognized!"
103 # vim like diff colourization
104 LC_ALL=en_US.UTF-8 gawk ' {
105 split( $0, S, /\t/ );
107 for ( i = 2; i in S; i++ ) {
108 spaces = 7 - ( (length( $0 ) - 1) % 8 );
109 $0 = $0 "\xE2\x9E\x94";
110 for ( y = 0; y < spaces; y++ )
111 $0 = $0 "\xE2\x87\xBE";
114 gsub( /\033/, "\033[44m^[\033[49m" );
116 if ( sub( /^ /, "" ) )
118 sub( /(\xE2\x9E\x94(\xE2\x87\xBE)*| )+$/, "\033[31;41m&\033[39;49m" );
119 gsub( /\xE2\x9E\x94(\xE2\x87\xBE)*/, "\033[7m&\033[27m" );
120 gsub( /\xE2\x87\xBE/, " " );
121 # uncomment if you do not like utf-8 arrow
122 # gsub( /\xE2\x9E\x94/, ">" );
124 gsub( /\007/, "\033[44m^G\033[49m" );
125 gsub( /\r/, "\033[44m^M\033[49m" );
127 /^(Index:|diff|---|\+\+\+) / { $0 = "\033[32m" $0 }
128 /^@@ / { $0 = "\033[33m" $0 }
129 /^-/ { $0 = "\033[35m" $0 }
130 /^+/ { $0 = "\033[36m" $0 }
131 { $0 = $0 "\033[0m"; print }
137 # diff2hunks orignally by dig
143 for my $filename (@ARGV) {
146 open $fh, "<", $filename or die "$filename: open for reading: $!";
150 for my $i (0 ... $#lines) {
151 next unless $lines[$i] =~ m/^\@\@ /;
152 if ($i >= 2 and $lines[$i - 2] =~ m/^--- / and $lines[$i - 1] =~ m/^\+\+\+ /) {
153 @curheader = @lines[$i - 2 ... $i - 1];
155 next unless @curheader;
157 while ($j < @lines and $lines[$j] !~ m/^\@\@ /) {$j++}
159 if $j >= 3 and $j < @lines
160 and $lines[$j - 2] =~ m/^--- /
161 and $lines[$j - 1] =~ m/^\+\+\+ /;
163 $j-- until $lines[$j] =~ m/^[ @+-]/;
164 my $hunkfilename = $filename;
165 $hunkfilename =~ s/((\.(pat(ch)?|diff?))?)$/"-".sprintf("%03i",$counter++).$1/ei;
167 open $ofh, ">", $hunkfilename or die "$hunkfilename: open for writing: $!";
168 print $ofh @curheader, @lines[$i ... $j];
175 # import selected macros for adapter.awk
176 # you should update the list also in adapter.awk when making changes here
177 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 export ADAPTER_REVISION=$REVISION
236 eval $(rpm --eval "$(echo -e $eval_expr)")
241 workdir=$(mktemp -d ${TMPDIR:-/tmp}/adapter-XXXXXX) || exit $?
244 local tmp=$workdir/$(basename $SPECFILE) || exit $?
248 LC_ALL=en_US.UTF-8 $awk -f $adapter $SPECFILE > $tmp || exit $?
250 if [ "$outputonly" = 1 ]; then
253 elif [ "$(diff --brief $SPECFILE $tmp)" ]; then
254 diff -u $SPECFILE $tmp > $tmp.diff
256 diffcol $tmp.diff | $PAGER
258 echo -n "Accept? (Yes, No, Confirm each chunk)? "
263 echo "Ok, adapterized."
266 [cC]) # confirm each chunk
268 for t in $(ls $tmp-*.diff); do
270 echo -n "Accept? (Yes, [N]o, Quit)? "
293 echo "The SPEC is perfect ;)"
300 [ -f "$SPECFILE" ] || SPECFILE="$(basename $SPECFILE .spec).spec"
302 if [ $# -ne 1 -o ! -f "$SPECFILE" ]; then