#!/bin/sh
# $Id: Configure,v 1.25 1992/08/17 22:02:19 kadhim Exp $
# Copyright (c) 1990 The Regents of the University of Colorado

tmp=conftmp$$
usage="usage: Configure [-d] [-f fn1 fn2 ...]"

develop=false
reverse=false

while test $# != 0 
do
   case $1 in
	-d) develop=true; shift;;
	-r) reverse=true; shift;;
	-f) if test $# -lt 2; then 
              echo $usage; exit 1
            else 
              shift; fn=$*; break 
            fi;;
	*) echo $usage; exit 1
   esac
done

# Get system data
if test ! -f sysdata
then
	echo 'There is no sysdata file; please create one from sysdata.sh'
	exit 1
else
	. ./sysdata
fi


# Validate system data
case $os in
SunOS)	os=sun ;;
ULTRIX) ;;
HP_UX)	os=hpux ;;
AIX)	os=_AIX ;;
*)	BAD='1'
	if test "$os" != ''
	then echo "Configure cannot handle operating system $os"
	else echo 'You have not specified an operating system'
	fi
esac

for f in sysdata $CPP
do
	if test ! -f $f
	then
		echo "File $f not found"
		BAD='1'
	fi
done

for d in . $XINC $XLIB $XVIEWINC $XVIEWLIB
do
	if test ! -d $d
	then
		echo "Directory $d not found"
		BAD='1'
	fi 
done

if test "$CPP" = ''
then
	echo 'Must specify CPP'
	BAD=1
fi

if test "$XINC" != ''
then
	if test "$XLIB" != ''
	then defines="$defines -DXINC=$XINC -DXLIB=$XLIB"
	else
		echo 'Must specify XLIB if specifying XINC'
		BAD='1'
	fi
elif test "$XLIB" != ''
then
	echo 'Must specify XINC if specifying XLIB'
	BAD='1'
fi

if test "$XVIEWINC" != ''
then
	if test "$XVIEWLIB" != ''
	then defines="$defines -DXVIEWINC=$XVIEWINC -DXVIEWLIB=$XVIEWLIB"
	else
		echo 'Must specify XVIEWLIB if specifying XVIEWINC'
		BAD='1'
	fi
elif test "$XVIEWLIB" != ''
then
	echo 'Must specify XVIEWINC if specifying XVIEWLIB'
	BAD='1'
fi

if test "$BAD" = '1'
then
	echo 'Please correct your sysdata file'
	exit 1
fi


# Determine the name of the system directory
elisys='-DELISYS='`pwd | sed 's,^/tmp_mnt,,'`

# Set configuration symbols
defines="$defines -D${os} -DCPP=$CPP $elisys"


# Configure all files that end in .cpp
# Look for the RCS versions and check them out if -d was given
echo " "
if $develop
then
        if test "$fn" != ''
        then
		echo "Checking out RCS files..."
		for i in $fn
		do
			co $i
		done
        else
		echo "Looking for configurable RCS files to be checked out..."
		for i in `find . -name '*.cpp,v' -print`
		do
			j=`echo $i | sed -e 's;RCS/;;' -e 's;,v$;;'`
			co $i $j
		done
	fi
fi

if test "x$fn" = 'x'
then
	echo " "
	if $reverse
	then
		echo "Looking for files to be removed..."
	else
		echo "Looking for files to be configured..."
	fi
	find . -name '*.cpp' -print >$tmp
fi

# Note: we need the 2>/dev/null on the cpp invocation because
# on AIX cpp still complains about funny C comments even with -C
echo " "
echo "Configuring..."
if test "x$fn" = 'x'
then
	fn=`cat $tmp`
fi
for i in $fn
do
	if test ! -f $i
	then
		echo "Configure: $i does not exist"
	else
		j=`echo $i | sed 's/.cpp$//'`
		if $reverse
		then
			echo removing $j
			rm -f $j
		else
			echo editing $i '->' $j
			sed -e 's/^#!/!!!shell/' \
               		         -e 's/^\([ \t]*\)#\([ \t]\)/\1!!!comment\2/' \
				-e 's;/\*;!!!Ccomment;' $i |
			$CPP -P -C -Iinclude $defines 2>/dev/null | \
			sed -e 's/!!!comment/#/' -e 's/^!!!shell/#!/' \
				-e 's;!!!Ccomment;/*;' >$j
		fi
	fi
done
rm -f $tmp
echo " "
if $reverse
then
	echo 'Configuration removed.'
else
	echo 'Configuration complete.  Use "make" to make the Eli system.'
fi

