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 VERSION="v0.35/$REVISION"
18 Adapter adapts .spec files for PLD Linux.
19 $VERSION (C) 1999-2013 Free Penguins".
22 dir=$(d=$0; [ -L "$d" ] && d=$(readlink -f "$d"); dirname "$d")
23 adapter=$dir/adapter.awk
24 usage="Usage: $PROGRAM [FLAGS] SPECFILEs
26 -s|--no-sort|--skip-sort
27 skip BuildRequires, Requires sorting
28 -m|--no-macros|--skip-macros
29 skip use_macros() substitutions
33 skip %defattr corrections
35 do not do any diffing, just dump the output
38 if [ ! -x /usr/bin/getopt ]; then
39 echo >&2 "You need to install util-linux to use adapter"
43 if [ ! -x /usr/bin/patch ]; then
44 echo >&2 "You need to install patch to use adapter"
48 [ -n "$PAGER" ] || PAGER="/usr/bin/less -r"
50 if [ -n "$CONFIG_DIR" ]; then
51 USER_CFG="$CONFIG_DIR/.adapterrc"
52 elif [ -n "$HOME_ETC" ]; then
53 USER_CFG="$HOME_ETC/.adapterrc"
58 [ -f $USER_CFG ] && . $USER_CFG
60 t=$(getopt -o hsomdaV --long help,version,sort,sort-br,no-macros,skip-macros,skip-desc,skip-defattr -n "$PROGRAM" -- "$@") || exit $?
69 -s|--no-sort|--skip-sort)
72 -m|--no-macros|--skip-macros)
93 echo >&2 "$PROGRAM: Internal error: \`$1' not recognized!"
102 # vim like diff colourization
103 LC_ALL=en_US.UTF-8 gawk ' {
104 split( $0, S, /\t/ );
106 for ( i = 2; i in S; i++ ) {
107 spaces = 7 - ( (length( $0 ) - 1) % 8 );
108 $0 = $0 "\xE2\x9E\x94";
109 for ( y = 0; y < spaces; y++ )
110 $0 = $0 "\xE2\x87\xBE";
113 gsub( /\033/, "\033[44m^[\033[49m" );
115 if ( sub( /^ /, "" ) )
117 sub( /(\xE2\x9E\x94(\xE2\x87\xBE)*| )+$/, "\033[31;41m&\033[39;49m" );
118 gsub( /\xE2\x9E\x94(\xE2\x87\xBE)*/, "\033[7m&\033[27m" );
119 gsub( /\xE2\x87\xBE/, " " );
120 # uncomment if you do not like utf-8 arrow
121 # gsub( /\xE2\x9E\x94/, ">" );
123 gsub( /\007/, "\033[44m^G\033[49m" );
124 gsub( /\r/, "\033[44m^M\033[49m" );
126 /^(Index:|diff|---|\+\+\+) / { $0 = "\033[32m" $0 }
127 /^@@ / { $0 = "\033[33m" $0 }
128 /^-/ { $0 = "\033[35m" $0 }
129 /^+/ { $0 = "\033[36m" $0 }
130 { $0 = $0 "\033[0m"; print }
136 # diff2hunks orignally by dig
142 for my $filename (@ARGV) {
145 open $fh, "<", $filename or die "$filename: open for reading: $!";
149 for my $i (0 ... $#lines) {
150 next unless $lines[$i] =~ m/^\@\@ /;
151 if ($i >= 2 and $lines[$i - 2] =~ m/^--- / and $lines[$i - 1] =~ m/^\+\+\+ /) {
152 @curheader = @lines[$i - 2 ... $i - 1];
154 next unless @curheader;
156 while ($j < @lines and $lines[$j] !~ m/^\@\@ /) {$j++}
158 if $j >= 3 and $j < @lines
159 and $lines[$j - 2] =~ m/^--- /
160 and $lines[$j - 1] =~ m/^\+\+\+ /;
162 $j-- until $lines[$j] =~ m/^[ @+-]/;
163 my $hunkfilename = $filename;
164 $hunkfilename =~ s/((\.(pat(ch)?|diff?))?)$/"-".sprintf("%03i",$counter++).$1/ei;
166 open $ofh, ">", $hunkfilename or die "$hunkfilename: open for writing: $!";
167 print $ofh @curheader, @lines[$i ... $j];
174 # import selected macros for adapter.awk
175 # you should update the list also in adapter.awk when making changes here
176 import_rpm_macros() {
238 for macro in $macros; do
239 eval_expr="$eval_expr\nexport $macro='%{$macro}'"
243 # get cvsaddress for changelog section
244 # using rpm macros as too lazy to add ~/.adapterrc parsing support.
245 eval_expr="$eval_expr
246 export _cvsmaildomain='%{?_cvsmaildomain}%{!?_cvsmaildomain:@pld-linux.org}'
247 export _cvsmailfeedback='%{?_cvsmailfeedback}%{!?_cvsmailfeedback:PLD Team <feedback@pld-linux.org>}'
250 export ADAPTER_REVISION=$REVISION
252 eval $(rpm --eval "$(echo -e $eval_expr)")
257 [ -f "$SPECFILE" ] || SPECFILE="$(basename $SPECFILE .spec).spec"
260 workdir=$(mktemp -d ${TMPDIR:-/tmp}/adapter-XXXXXX) || exit $?
263 local tmp=$workdir/$(basename $SPECFILE) || exit $?
267 LC_ALL=en_US.UTF-8 $awk -f $adapter $SPECFILE > $tmp || exit $?
269 if [ "$outputonly" = 1 ]; then
272 elif [ "$(diff --brief $SPECFILE $tmp)" ]; then
273 diff -u $SPECFILE $tmp > $tmp.diff
275 diffcol $tmp.diff | $PAGER
277 echo -n "Accept? (Yes, No, Confirm each chunk)? "
282 echo "Ok, adapterized."
285 [cC]) # confirm each chunk
287 for t in $(ls $tmp-*.diff); do
289 echo -n "Accept? (Yes, [N]o, Quit)? "
312 echo "The spec $SPECFILE is perfect ;)"
318 if [ $# -eq 0 ]; then
323 for SPECFILE in "$@"; do