#!/bin/sh
#
#	Copyright (C) John Cherry <cherry@osdl.org>
#       2004 Open Source Developement Lab
#
# AUTHOR:
#	Original script by John Cherry <cherry@osdl.org>
#
# compdiff - compare two different compregress.sh builds and display
#            new warnings
#
#   usage: compdiff <dir1> <dir2>
#          dir1 - old build directory
#          dir2 - new build directory
#
#	1.0.0		Original deployment
#       1.0.1           Fixed basename problem, so now it does not need
#                       to run in the directory directly above the
#                       compared directories.
# 	1.0.2		Fixed bug with editing the non-numbers files.
#
usage () {
	echo $@
	echo "   Usage: compdiff <dir1> <dir2>"
	exit 0
}

#
# Get kernel version from the top level Makefile
#
get_version () {
	TMP=`sed -n '1p' < Makefile`
	set $TMP
	VERSION=$3
	TMP=`sed -n '2p' < Makefile`
	set $TMP
	VERSION=$VERSION.$3
	BASE_VERSION=$VERSION
	TMP=`sed -n '3p' < Makefile`
	set $TMP
	VERSION=$VERSION.$3
	TMP=`sed -n '4p' < Makefile`
	set $TMP
	VERSION=$VERSION$3
}

GROCKDIFF="grockdiff"

#
# Make sure command line arguments are reasonable
#

if [ $# != 2 ]; then
	usage "Check number of arguments:"
else
	DIR1=$1
	DIR2=$2
	if [ ! -d $DIR1 ]; then
		usage "$DIR1 is not a valid directory."
	fi
	if [ ! -d $DIR2 ]; then
		usage "$DIR2 is not a valid directory."
	fi
	DIR1_BASE=`basename $DIR1`
	DIR2_BASE=`basename $DIR2`
fi

#
# Havest all possible log files in both directories
#
pushd $DIR1 > /dev/null
get_version 
VERSION1=$VERSION
popd > /dev/null
pushd $DIR2 > /dev/null
get_version 
VERSION2=$VERSION
popd > /dev/null

WARN1_NUMS=`mktemp tmplog.XXXXXX`
WARN2_NUMS=`mktemp tmplog.XXXXXX`
sort -u $DIR1/*warning.list > $WARN1_NUMS
sort -u $DIR2/*warning.list > $WARN2_NUMS
ERR1_NUMS=`mktemp tmplog.XXXXXX`
ERR2_NUMS=`mktemp tmplog.XXXXXX`
sort -u $DIR1/*error.list > $ERR1_NUMS
sort -u $DIR2/*error.list > $ERR2_NUMS

WARN1_NONUMS=`mktemp tmplog.XXXXXX`
WARN2_NONUMS=`mktemp tmplog.XXXXXX`
cat $WARN1_NUMS | sed -e 's/c:[0-9]*/c/' -e 's/h:[0-9]*/h/' > $WARN1_NONUMS
cat $WARN2_NUMS | sed -e 's/c:[0-9]*/c/' -e 's/h:[0-9]*/h/' > $WARN2_NONUMS
ERR1_NONUMS=`mktemp tmplog.XXXXXX`
ERR2_NONUMS=`mktemp tmplog.XXXXXX`
cat $ERR1_NUMS | sed -e 's/c:[0-9]*/c/' -e 's/h:[0-9]*/h/' > $ERR1_NONUMS
cat $ERR2_NUMS | sed -e 's/c:[0-9]*/c/' -e 's/h:[0-9]*/h/' > $ERR2_NONUMS

WARN_CHANGES=`mktemp tmplog.XXXXXX`
diff $WARN1_NONUMS $WARN2_NONUMS | $GROCKDIFF $WARN1_NUMS $WARN2_NUMS > $WARN_CHANGES
ERR_CHANGES=`mktemp tmplog.XXXXXX`
diff $ERR1_NONUMS $ERR2_NONUMS | $GROCKDIFF $ERR1_NUMS $ERR2_NUMS > $ERR_CHANGES

NEW_WARNINGS="new_warnings.$DIR2_BASE"
FIXED_WARNINGS="fixed_warnings.$DIR1_BASE"
grep "^>" $WARN_CHANGES | sed 's/^> //' > $NEW_WARNINGS
grep "^<" $WARN_CHANGES | sed 's/^< //' > $FIXED_WARNINGS
NEW_WARN_CNT=`cat $NEW_WARNINGS | wc -l`
FIXED_WARN_CNT=`cat $FIXED_WARNINGS | wc -l`
NEW_ERRORS="new_errors.$DIR2_BASE"
FIXED_ERRORS="fixed_errors.$DIR1_BASE"
grep "^>" $ERR_CHANGES | sed 's/^> //' > $NEW_ERRORS
grep "^<" $ERR_CHANGES | sed 's/^< //' > $FIXED_ERRORS
NEW_ERR_CNT=`cat $NEW_ERRORS | wc -l`
FIXED_ERR_CNT=`cat $FIXED_ERRORS | wc -l`

printf "\nSummary:\n   New warnings = %d\n   New errors = %d\n" $NEW_WARN_CNT $NEW_ERR_CNT
printf "   Fixed warnings = %d\n   Fixed errors = %d\n\n" $FIXED_WARN_CNT $FIXED_ERR_CNT

printf "New warnings:\n-------------\n"
cat $NEW_WARNINGS
printf "\nNew errors:\n-----------\n"
cat $NEW_ERRORS
printf "\nFixed warnings:\n---------------\n"
cat $FIXED_WARNINGS
printf "\nFixed errors:\n--------------\n"
cat $FIXED_ERRORS
echo

rm $WARN1_NUMS $WARN2_NUMS $WARN1_NONUMS $WARN2_NONUMS $WARN_CHANGES
rm $ERR1_NUMS $ERR2_NUMS $ERR1_NONUMS $ERR2_NONUMS $ERR_CHANGES

