1#!/bin/bash -e
2
3usage()
4{
5    echo "usage:  $0 <patch file>"
6    exit 1
7}
8
9if [ "$1" = "" ] ; then
10    usage
11fi
12
13if [ "$1" = "--compile" ] ; then
14    compile=true
15    shift
16fi
17
18SCRIPT_DIR=$(dirname $0)
19if [ -e $SCRIPT_DIR/kchecker ] ; then
20    KCHECKER=$SCRIPT_DIR/kchecker
21    STRIP=$SCRIPT_DIR/strip_whitespace.pl
22elif which kchecker | grep kchecker > /dev/null ; then
23    KCHECKER=kchecker
24    STRIP=strip_whitespace.pl
25else
26    echo "$SCRIPT_DIR"
27    echo "kchecker script not found."
28    exit 1
29fi
30
31PATCH=$1
32
33files=$(grep ^+++ $PATCH | cut -f 1 | cut -b 5-)
34if [ "$files" = "" ] ; then
35    usage
36fi
37
38if ! cat $PATCH | patch -p1 --dry-run > /dev/null ; then
39    echo "Couldn't apply patch"
40    exit 1
41fi
42
43before=$(mktemp /tmp/before.XXXXXXXXXX)
44after=$(mktemp /tmp/after.XXXXXXXXXX)
45tmpfile=$(mktemp)
46
47for file in $files ; do
48    file=${file#*/}
49
50    $STRIP $file > $before
51    if [ "$compile" = "true" ] ; then
52	if ! $KCHECKER --test-parsing --outfile=$before $file ; then
53		echo "warning: compile failed."
54	fi
55	mv $before $tmpfile
56	$STRIP $file > $before
57	cat $tmpfile >> $before
58    fi
59    cat $PATCH | patch -p1
60    $STRIP $file > $after
61    if [ "$compile" = "true" ] ; then
62	if ! $KCHECKER --test-parsing --outfile=$after $file ; then
63		echo "warning: compile failed.  *again*"
64	fi
65	mv $after $tmpfile
66	$STRIP $file > $after
67	cat $tmpfile >> $after
68    fi
69    cat $PATCH | patch -p1 -R
70
71    if [ ! -s $before ] ; then
72	echo "Error:  No result"
73	exit 1
74    fi
75
76    if diff $before $after > /dev/null ; then
77	echo
78	echo Only white space changed
79	echo
80    else
81	echo '!!#$%@$%@^@#$^@#%@$%@$%@#%$@#%!!'
82	echo '!!                            !!'
83	echo '!!  This patch changes stuff  !!'
84	echo '!!                            !!'
85	echo '!!#$%@$%@^@#$^@#%@$%@$%@#%$@#%!!'
86
87	diff -u $before $after 
88    fi
89    rm -f $before $after $tmpfile
90done
91
92