#!/bin/sh

# $Header: /libra/secude/src/RCS/configure,v 1.32 1995/03/28 08:57:44 schneiw Exp $
#
# $Log: configure,v $

#
#    Copyright (C) 1990-1994, GMD Darmstadt.
#
#                         NOTICE
#
#    Acquisition, use, and distribution of this module 
#    and related materials are subject to restrictions 
#    mentioned in each volume of the documentation.
#
#
# Configuration script
#


date=`date '+%h %d, %H:%M:%S'`
host=`uname -n`
sys=`uname -rs`
cpu=`uname -m`

echo "\"configure $*\" on $host for $sys $date"
echo "SecuDE $* on $host for $sys $date" >configuration

cm=config/CONFIG.make
ch=include/CONFIG.h
echo "# `basename $cm` on $host for $sys $date" > $cm
echo "/* `basename $ch` on $host for $sys $date */" > $ch

for par do case $par in
	new)
		rm -f config/*.save
		;;
esac done

library=shared
verbose=no
std=no

# SecuDE top level directory:
if [ ! -f config/TOP.save ]
then
	src=$PWD
	if [ "$src" = "" ]
	then
		src=`pwd`
	fi
	secude=`dirname $src`
	echo $secude > config/TOP.save
	source="saved in"
else	
	secude=`cat config/TOP.save`
	source=from
fi
echo "TOP = $secude" >> $cm
echo "SecuDE top level directory is $secude ($source config/TOP.save)"
# Pathname of the AFDB directory
if [ ! -f config/AFDB.save ]
then
	afdb=$secude/etc/.af-db
	echo $afdb > config/AFDB.save
	source="saved in"
else
	afdb=`cat config/AFDB.save`
	source=from
fi
echo "#define AFDBFILE \"$afdb\"" >> $ch
echo ".af-db directory is $afdb ($source config/AFDB.save)"

# Pathname of directory which contains system alias file
if [ ! -f config/ALIASDIR.save ]
then
	aliasdir=$secude/etc
	echo $aliasdir > config/ALIASDIR.save
	source="saved in"
else
	aliasdir=`cat config/ALIASDIR.save`
	source=from
fi
echo "#define ALIASDIR \"$aliasdir\"" >> $ch
echo "System alias file .af-alias is in $aliasdir ($source config/ALIASDIR.save)"

# Pathname of PSE .testkeys
echo "#define TESTKEYS_PSE \"$secude/etc/.testkeys\"" >> $ch

# Check configuration keywords
x500="no"
strong="no"
sca="no"
motif="no"
secxlock="no"
icr="1"
for par do case $par in
	X500)
		x500="yes"
		;;
	ICR1)
		icr="1"
		;;
	ICR2)
		icr="2"
		;;
	STRONG)
		strong="yes"
		;;
        SCA)
		sca="yes"
		echo "DSCA = SCA" >> $cm
		echo "#define SCA" >> $ch
		echo "#define INITDEVSET" >> $ch
		echo "#define SCCONF \"$secude/etc/.starcosrc\"" >> $ch
		echo "#define SCTINSTFILE \"$secude/etc/.scttya\"" >> $ch
		echo "#define SCMANDIR \"$secude/etc/\"" >> $ch
		;;
        SCT17)
		echo "#define SCT17" >> $ch
		;;
	NEW_SHS)
		echo "#define NEW_SHS" >> $ch
		;;
	XPIPE)
		echo "XPIPE = XPIPE" >> $cm
		;;
	SECXLOCK)
		echo "SECXLOCK = SECXLOCK" >> $cm
		secxlock="yes"
		;;
	XMST)
		echo "MOTIFSECTOOL = MOTIFSECTOOL" >> $cm
		motif="yes"
		;;
	cc)
		compiler=cc
		rm -f config/CC.save
		;;
	gcc)
		compiler=gcc
		rm -f config/CC.save
		;;
	acc)
		compiler=acc
		rm -f config/CC.save
		;;
	opt)
		echo "OPT = -O" >> $cm
		;;
	debug)
		echo "OPT = -g" >> $cm
		;;
	std)
		std=yes
		;;
	static)
		library=static
		;;
	new)
		continue
		;;
	verbose)
		verbose=yes
		;;
	rsaref)
		if [ ! -f config/RSAREF.save ]
		then
			echo "You selected RSAREF which requires an RSAREF-installation"
			echo "I need the absolute pathname of the top level directory of your RSAREF installation"
			echo "Type <pathname><CR>:"
			config/get_pathname.sh RSAREF $cm
			source="saved in"
		else
			source="from"
		fi
		rsaref=`cat config/RSAREF.save`
		echo "RSAREF = $rsaref" >> $cm
		cat config/RSAREF.mk >> $cm
		echo "#define RSAREF" >> $ch
		echo "RSAREF top level directory is $rsaref (saved in config/RSAREF.save)"
		;;
	*)
		echo "configure: Unknown keyword: $par"
		echo "\"configure\" not done."
		exit 1
		;;
esac done

# Isode Relaese

# ICR1

if [ $icr = "1" ]
then
	# echo "ISODE Version ICR1"
	rm -f ./isode ./include/isode 
	ln -s ./isode.ic1 isode
	ln -s include.ic1 ./include/isode
	if [ -f config/ICR1-LIB.save ]
	then
		rm -f config/ISODE-LIB.save config/ISODE-INCLUDE.save
		ln -s ICR1-LIB.save config/ISODE-LIB.save
		ln -s ICR1-INCLUDE.save config/ISODE-INCLUDE.save
	fi
	echo "#define ICR1" >> $ch
	echo "Selected ISODE Version is ICR1"
fi

# ICR2

if [ $icr = "2" ]
then
	# echo "ISODE Version ICR2"
	rm -f ./isode ./include/isode 
	ln -s ./isode.ic2 isode
	ln -s include.ic2 ./include/isode
	if [ -f config/ICR2-LIB.save ]
	then
		rm -f config/ISODE-LIB.save config/ISODE-INCLUDE.save
		ln -s ICR2-LIB.save config/ISODE-LIB.save
		ln -s ICR2-INCLUDE.save config/ISODE-INCLUDE.save
	fi
	echo "#define ICR1" >> $ch
	echo "#define ICR2" >> $ch
	cat config/icr2.mk >> $cm
	echo "Selected ISODE Version is ICR2"
fi

# Try to find pathname of the make utility
if [ ! -f config/MAKE.save ]
then
	if [ "$MAKE" ]
	then
		echo "$MAKE" > config/MAKE.save
		echo "... make is $MAKE (from environment variable MAKE)"
	else
		echo "Try to find pathname of the make utility ..."
		mv make make.sv
		answ=`type make`
		mv make.sv make
		case $answ in
			make\ not\ found)
				echo "... Can't find your system's make utility !!"
				echo "Set it's pathname in environment variable MAKE."
				;;
			make\ is\ *)
				MAKE=`echo $answ | (read w1 w2 w3;echo $w3)`
				echo "$MAKE" > config/MAKE.save
				echo "... make is $MAKE (saved in config/MAKE.save)"
				;;
		esac
	fi
else
	MAKE=`cat config/MAKE.save`
	echo "make is \"$MAKE\" (from config/MAKE.save)"
fi
		
# Try to find compiler
if [ ! -f config/CC.save ]
then
	echo "Try to find compiler ..."
	c=${compiler:="cc"}
	answ=`type $c`
	case $answ in
		$c\ not\ found)
			if [ "$c" = "cc" ]
			then
				answ=`type gcc`
				case $answ in
					gcc\ not\ found)
						echo "... Warning: neither cc nor gcc found"
						;;
					gcc\ is\ *)
						compiler=gcc
						ccpath=`echo $answ | (read w1 w2 w3;echo $w3)`
						echo "... Compiler is $compiler ($ccpath) (saved in config/CC.save)"
						;;
				esac
			else
				echo "... Warning: $c not found"
			fi
			;;
		$c\ is\ *)
			compiler=$c
			ccpath=`echo $answ | (read w1 w2 w3;echo $w3)`
			echo "... Compiler is $compiler ($ccpath) (saved in config/CC.save)"
			;;
	esac
	echo "$compiler" >config/CC.save
	echo "CC = $compiler" >> $cm
else
	answ=`cat config/CC.save`
	compiler=`echo $answ | (read w1 w2;echo $w1)`
	ccpath=`dirname $compiler`
	if [ $ccpath = "." ]
	then
		answ1=`type $compiler`
		ccpath=`echo $answ1 | (read w1 w2 w3;echo $w3)`
	fi
	echo "Compiler is \"$answ\" ($ccpath) (from config/CC.save)"
	echo "CC = $answ" >> $cm
fi

# Check whether X500 is selected (requires ICR1 or ICR2 installation)
if [ $x500 = "yes" ]
then
	config/find_top_level.sh ISODE $cm $verbose
	if [ $? = "1" ]
	then
		echo "\"configure $*\" not done."
		echo "Rerun \"configure $* verbose\""
		exit 1
	fi

	if [ $icr = "1" ]
	then
		if [ ! -f config/ISODE-LIB.save ]
		then
			rm -f config/ICR1-INCLUDE.save
			cp config/ISODE-LIB.save config/ICR1-LIB.save
			cp config/ISODE-INCLUDE.save config/ICR1-INCLUDE.save
		fi
	fi

	if [ $icr = "2" ]
	then
		if [ ! -f config/ISODE-LIB.save ]
		then
			rm -f config/ICR2-INCLUDE.save
			cp config/ISODE-LIB.save config/ICR2-LIB.save
			cp config/ISODE-INCLUDE.save config/ICR2-INCLUDE.save
		fi
	fi

	cat config/withX500.mk >> $cm
	echo "#define X500" >> $ch
	echo "#define PEPYPATH" >> $ch
	if [ $strong = "yes" ]
	then
		echo "#define STRONG" >> $ch
	fi
else
	cat config/withoutX500.mk >> $cm
	echo "#define PEPYPATH" >> $ch
fi

# Check whether XMST is selected (requires MOTIF installation)
if [ $motif = "yes" ]
then
	config/find_top_level.sh MOTIF $cm $verbose
	if [ $? = "1" ]
	then
		echo "\"configure $*\" not done."
		echo "Rerun \"configure $* verbose\""
		exit 1
	fi
fi

# Try to recognize system platform

case $sys in

	SunOS\ 5*)
		echo "LD = ld" >>$cm
		if [ `basename $compiler` = "gcc" ]
		then
			echo "PICCFLAGS = -fPIC" >> $cm
			echo "ANSICFLAGS = -ansi" >> $cm
		else
			if [ $ccpath = "/usr/ucb/cc" ]
			then
				echo "PICCFLAGS = -PIC" >> $cm
			else
				echo "PICCFLAGS = -K PIC" >> $cm
				echo "ANSICFLAGS = -Xc" >> $cm
			fi
		fi
		echo "LIBADD = -lsocket -lnsl -lm" >> $cm
		if [ `/usr/bin/uname -p` = "sparc" -a $std = "no" ] 
		then
			echo "ARCH = sparc" >> $cm
			echo "AS = as -K PIC -P" >> $cm
		else
			echo "ARCH = std" >> $cm
		fi
		echo "LIBSECUDESO = $library" >> $cm
		if [ $library = "shared" ]
		then
			echo "SL_OPTIONS = -G" >> $cm
			echo "SL_APPENDIX = .so" >> $cm
		fi
		echo "RSECUDE = -R$(TOP)/lib" >> $cm
		if [ $motif = "yes" ]
		then
			echo "#define MOTIF122" >> $ch
			echo "DMOTIFINCLUDE = -I$(OPENWINHOME)/include -I$(MOTIFINCLUDE)" >> $cm
			echo "DMOTIFLIB = -L$(OPENWINHOME)/lib -L$(MOTIFLIB) -lXm -lMrm -lXt -lgen -lX11" >> $cm
			echo "UIL = uil" >> $cm
		fi
		if [ $secxlock = "yes" ]
		then
			echo "SECXLOCK_UCBLIB = -L/usr/ucblib -lucb" >> $cm
		fi
		if [ $sca = "yes" ]
		then
			echo "#define _BSD42_" >> $ch
		fi
		if [ $x500 = "no" ]
		then
			echo "#define SYS5" >> $ch
			echo "#define SVR4" >> $ch
			echo "#define SVR4_UCB" >> $ch
		fi
		;;
	SunOS\ 4*)
		echo "LD = ld" >> $cm
		if [ `basename $compiler` = "gcc" ]
		then
			echo "PICCFLAGS = -fPIC" >> $cm
			echo "ANSICFLAGS = -ansi" >> $cm
		else
			echo "PICCFLAGS = -PIC" >> $cm
		fi
		echo "LIBADD = -lm" >>$cm
		echo "RANLIB = ranlib libsecude.a" >> $cm
		if [ `mach` = "sparc" -a $std = "no" ] 
		then
			echo "ARCH = sparc" >> $cm
			echo "AS = as -k -P" >> $cm
		else
			echo "ARCH = std" >> $cm
		fi
		echo "LIBSECUDESO = $library" >> $cm
		if [ $library = "shared" ]
		then
			echo "SL_OPTIONS = -assert pure-text" >> $cm
			echo "SL_APPENDIX = .so" >> $cm
		fi
		if [ $motif = "yes" ]
		then
			echo "#define MOTIF121" >> $ch
			echo "DMOTIFINCLUDE = -I$(MOTIFINCLUDE)" >> $cm
			echo "DMOTIFLIB = -L$(MOTIFLIB) -lXm -lMrm -lXt -lX11" >> $cm
			echo "UIL = uil" >> $cm
		fi
		if [ $sca = "yes" ]
		then
			echo "#define _BSD42_" >> $ch
		fi
		if [ $x500 = "no" ]
		then
			echo "#define BSD42" >> $ch
			echo "#define BSD43" >> $ch
			echo "#define SUNOS4" >> $ch
			echo "#define SUNOS41" >> $ch
		fi
		;;
	Linux*)
		echo "LD = ld" >>$cm
		echo "ANSICFLAGS = -ansi" >> $cm
		echo "LIBADD = -ldbm -lm" >>$cm
		echo "RANLIB = ranlib libsecude.a" >> $cm
		echo "ARCH = std" >> $cm
		echo "LIBSECUDESO = static" >> $cm
		echo "#define LITTLE_ENDIAN" >> $ch
		echo "#define __MALLOC_0_RETURNS_NULL" >> $ch
		if [ $x500 = "no" ]
		then
			echo "#define LINUX" >> $ch
			echo "#define BSD42" >> $ch
			echo "#define BSD43" >> $ch
		fi
		;;
	HP-UX*)
		echo "CC = $compiler" >>$cm
		echo "LD = ld" >>$cm
		echo "PICCFLAGS = +Z" >> $cm
		echo "OTHERCFLAGS = -Wp,-H,384000" >> $cm
		if [ $x500 = "yes" ]
		then
			echo "LIBADD = -lsecude -ldbm -lm" >> $cm
		else
			echo "LIBADD = -ldbm -lm" >> $cm
		fi
		echo "RANLIB = ranlib libsecude.a" >> $cm
		if [ $std = "no" ] 
		then
			echo "ARCH = hp" >> $cm
			echo "AS = as -k -P" >> $cm
		else
			echo "ARCH = std" >> $cm
		fi
		echo "LIBSECUDESO = $library" >> $cm
		if [ $library = "shared" ]
		then
			echo "SL_OPTIONS = -b" >> $cm
			echo "SL_APPENDIX = .sl" >> $cm
		fi
		echo "#define HPUX" >> $ch
		if [ $sca = "yes" ]
		then
			echo "#define _POSIX_" >> $ch
		fi
#		if [ $x500 = "no" ]
#		then
#			echo "#define SYS5" >> $ch
#		fi
		;;
	OSF1\ *)
		echo "LD = ld" >>$cm
		if [ `basename $compiler` = "gcc" ]
		then
			echo "PICCFLAGS = -fPIC" >> $cm
			echo "ANSICFLAGS = -ansi" >> $cm
		else
			echo "ANSICFLAGS = -std1" >> $cm
		fi
		echo "LIBADD = -lm" >> $cm
		echo "RELINK_PGMS_SHARED = cd ../src/util; make cleanpgm" >> $cm
		echo "ARCH = std" >> $cm
		if [ $cpu = "alpha" ]
		then
			echo "#define SIZEOFLONG 64" >> $ch
		fi
		echo "LIBSECUDESO = $library" >> $cm
		if [ $library = "shared" ]
		then
			echo "SL_OPTIONS = -shared -expect_unresolved \*" >> $cm
			echo "SL_APPENDIX = .so" >> $cm
		fi
		if [ $motif = "yes" ]
		then
			echo "#define MOTIF122" >> $ch
			echo "DMOTIFINCLUDE = -I$(OPENWINHOME)/include -I$(MOTIFINCLUDE)" >> $cm
			echo "DMOTIFLIB = -L$(OPENWINHOME)/lib -L$(MOTIFLIB) -lXm -lMrm -lXt -lgen -lX11" >> $cm
			echo "UIL = uil" >> $cm
		fi
		echo "#define LITTLE_ENDIAN" >> $ch
		echo "#define OSF1" >> $ch
		if [ $sca = "yes" ]
		then
			echo "#define _BSD42_" >> $ch
			echo "#define BSDSTRS" >> $ch
		fi
		if [ $x500 = "no" ]
		then
			echo "#define BSDSTRS" >> $ch
		fi
		;;
	IRIX\ 5*)
		echo "LD = ld" >>$cm
		if [ `basename $compiler` = "gcc" ]
		then
			echo "PICCFLAGS = -fPIC" >> $cm
			echo "ANSICFLAGS = -ansi" >> $cm
		else
			if [ $ccpath = "/usr/ucb/cc" ]
			then
				echo "PICCFLAGS = -PIC" >> $cm
			else
				echo "ANSICFLAGS = -ansi" >> $cm
			fi
		fi
		echo "LIBADD = -lm" >> $cm
		echo "ARCH = std" >> $cm
		echo "LIBSECUDESO = $library" >> $cm
		if [ $library = "shared" ]
		then
			echo "SL_OPTIONS = -shared" >> $cm
			echo "SL_APPENDIX = .so" >> $cm
		fi
		if [ $motif = "yes" ]
		then
			echo "#define MOTIF122" >> $ch
			echo "DMOTIFINCLUDE = -I$(OPENWINHOME)/include -I$(MOTIFINCLUDE)" >> $cm
			echo "DMOTIFLIB = -L$(OPENWINHOME)/lib -L$(MOTIFLIB) -lXm -lMrm -lXt -lgen -lX11" >> $cm
			echo "UIL = uil" >> $cm
		fi
		if [ $sca = "yes" ]
		then
			echo "#define _BSD42_" >> $ch
		fi
		if [ $x500 = "no" ]
		then
			echo "#define SYS5" >> $ch
			echo "#define SVR4" >> $ch
			echo "#define SVR4_UCB" >> $ch
		fi
		;;
	AIX)
		echo "CC = cc" >> $cm
		echo "LD = ld" >> $cm
		echo "LIBADD = -lm" >> $cm
		echo "RANLIB = ranlib libsecude.a" >> $cm
		echo "ARCH = std" >> $cm
		echo "LIBSECUDESO = shared" >> $cm
		if [ $library = "shared" ]
		then
			echo "SL_OPTIONS = -BE:libsecude.exp" >> $cm
			echo "SL_APPENDIX = .o" >> $cm
		fi
		echo "#define SYS5" >> $ch
		echo "#define SVR4" >> $ch
		if [ $sca = "yes" ]
		then
			echo "#define _POSIX_" >> $ch
		fi
		;;
	*)
		echo "Don't know system platform $sys !!!"
		echo "Edit"
		echo "     src/$cm and"
		echo "     src/$ch   for appropriate values"
		echo "CC = cc" >> $cm
		echo "LD = ld" >> $cm
		echo "PICCFLAGS = -PIC" >> $cm
		echo "LIBADD = -lm" >> $cm
		echo "RANLIB = ranlib libsecude.a" >> $cm
		if [ `uname -p` = "sparc" ] 
		then
			echo "ARCH = sparc" >> $cm
			echo "AS = as -k -P" >> $cm
		else
			echo "ARCH = std" >> $cm
		fi
		echo "LIBSECUDESO = shared" >> $cm
		if [ $library = "shared" ]
		then
			echo "SL_OPTIONS = -assert pure-text" >> $cm
			echo "SL_APPENDIX = .so" >> $cm
		fi
		if [ $sca = "yes" ]
		then
			echo "#define _POSIX_" >> $ch
		fi
		;;
esac
cat config/CONFIG.mk >> $cm
echo "\"configure $*\" done."
echo ""
echo "If you don't like one or more of the above pathnames, change"
echo "the according .save file and rerun your configure command"

