#!/bin/sh

set -e

DEF=/etc/cxref/cxref-cpp.defines
gcc_vers () {

    ! [ -e $DEF ] || 
    	awk '/-D__GNUC__/ {split($NF,A,"=");maj=A[2]}
  	     /-D__GNUC_MINOR__/ {split($NF,A,"=");min=A[2]}
  	     /-D__GNUC_PATCHLEVEL__/ {split($NF,A,"=");pat=A[2]}
	     END {printf("%s.%s.%s",maj,min,pat)}' $DEF
}


. /etc/cxref/config

j="$@"

GV=`gcc -v 2>&1 | grep "^gcc version" | awk '{print $3}'`
GVC=`gcc_vers`
if [ "$GV" != "$GVC" ] ; then

	echo "Warning: cxref-cpp needs to be reconfigured against your latest gcc/cpp"
	echo "   $GV != $GVC"
	echo "   Please run 'dpkg-reconfigure cxref' as root."

	if [ "$AUTOCONF" = "true" ] ; then
	    b=`tempfile -p cxref -m 777`
	    c=`tempfile -p cxref -m 777`
	    sed -e 's,/etc,/tmp,g' -e 's,cxref/cxref,cxref,g' </usr/bin/cxref-cpp-configure >$b
	    chmod 755 $b
	    $b
	    mv /tmp/cxref-cpp.defines $c
	    j=`echo $j | sed 's,\-cxref\-cpp\-defines  *[^ ]*,,g'`
	    j="$j -cxref-cpp-defines $c"
	else
	    exit 1
	fi
fi

exec /usr/bin/cxref-cpp.upstream $j
