#!/bin/sh

NAME=$(basename $0)
MONO_API_INFO1="/usr/bin/mono /usr/lib/mono/1.0/mono-api-info.exe"
MONO_API_INFO2="/usr/bin/mono /usr/lib/mono/2.0/mono-api-info.exe"
MONO_API_DIFF="/usr/bin/mono /usr/lib/mono/1.0/mono-api-diff.exe"

if [ ! -e "$1" -a ! -e "$2" ]
then
	echo "usage: $NAME [-2] old.dll new.dll"
	exit 1
fi

API_OLD=$(tempfile)
API_NEW=$(tempfile)
API_DIFF=$(tempfile)

if [ "$1" = "-2" ]; then
	if [ ! -x $MONO_API_INFO2 ]; then
		echo "Error: $MONO_API_INFO2 does not exist, you need to install the mono-gmcs package"
		exit 1
	fi
	MONO_API_INFO=$MONO_API_INFO2
	shift
else
	MONO_API_INFO=$MONO_API_INFO1
fi

${MONO_API_INFO} "$1" > ${API_OLD}
${MONO_API_INFO} "$2" > ${API_NEW}
${MONO_API_DIFF} ${API_OLD} ${API_NEW} > ${API_DIFF}

grep -q 'Assembly version not equal: ' ${API_DIFF}
version_changed=$?

name=$(head -n3 ${API_DIFF} | tail -n1 | sed 's;\ ;\n;g' | grep ^name | cut -d\= -f2 | sed 's;\";;g')
missing_total=$(head -n3 ${API_DIFF} | tail -n1 | sed 's;\ ;\n;g' | grep ^missing_total | cut -d\= -f2 | sed 's;\";;g')
extra_total=$(head -n3 ${API_DIFF} | tail -n1 | sed 's;\ ;\n;g' | grep ^extra_total | cut -d\= -f2 | sed 's;\";;g')

if [ -z $missing_total ]; then
	missing_total=0
fi
if [ -z $extra_total ]; then
	extra_total=0
fi

echo "CLI API Check"
echo -e "Assembly Name:\t\t$name"
echo -e "Missing Interfaces:\t$missing_total"
echo -e "Additional Interfaces:\t$extra_total"

if [ $missing_total ]
then
	if [ $missing_total -gt 0 ]
	then
		echo 
		echo "The two assemblies you compared are NOT API compatible!"
		echo "You must use a new package name!"
	fi
fi

if [ $extra_total ]
then
	if [ $extra_total -gt 0 ]
	then
		echo
		echo "The new assembly has additional interfaces. You must raise"
		echo "the minimal version in clilibs!"
	fi
fi

if [ $version_changed ]; then
	echo
	echo "The assembly versions do NOT MATCH!"
	echo "If they are API compatible you MUST generate and install a GAC policy file!"
fi

rm -f ${API_OLD} ${API_NEW} ${API_DIFF}
