#!/bin/sh
#
#  Configuration script for sgs.  This generates the site.h file
#  in the sgs/api directory, and builds the makefile.     
#

# Is this a Posix or bsd-ish shell?

if echo '\c' | grep -s c >/dev/null 2>&1
then
	ECHO_N="echo -n"
	ECHO_C=""
else
	ECHO_N="echo"
	ECHO_C='\c'
fi

echo
echo "Starting build of SGS 1.0.0."
echo 
echo
echo "Interactive configuration section"
echo

############################
# Get SGS_HOME             #
############################

$ECHO_N "	Top of install tree ? [/usr/local/sgs] $ECHO_C"
read INST_TOP
if test "$INST_TOP." = "."
then
	INST_TOP=/usr/local/sgs
fi

############################
# Get the path for make    #
############################

echo
echo "There might be many versions of the make utility installed on"
echo "your machine.  For instance, you might have the standard make"
echo "and the GNU make utilities installed in different directories."
echo "The make utility is typically installed under /bin, or /usr/bin."
echo "To have the setup script automatically detect the make utility,"
echo "simply press ENTER when asked to provide the path.  To specify"
echo "which version of make you want to use, enter the full pathname"
echo "of the make utility.  For example, /bin/make."
echo ""
echo
$ECHO_N "	Which make ? [autodetect] $ECHO_C"
read MAKE_PATH
if test "$MAKE_PATH." = "."
then
	echo
	echo "Trying to locate the make utility..."

	MAKE_PATHS="/bin/make /usr/bin/make /etc/make /usr/ccs/bin/make"
	MAKE_PATH=""
	for pathname in $MAKE_PATHS
	do
		if test -f $pathname
		then
			MAKE_PATH=${pathname}
			echo "Found the make utility in ${pathname}"
			break
		fi
	done

	if test "$MAKE_PATH." = "."
	then
		echo "Could not find the make utility. You should find out"
		echo "the location of the make utility on your machine and "
		echo "run the setup script again, this time specifying the"
		echo "pathname for the make utility."
		echo
	fi
fi

echo

if test -f ${MAKE_PATH}
then
	echo
else
	echo "The make utility pathname is invalid."
	echo
fi

###################################
# Get the path for the C compiler #
###################################

echo
echo "There might be many versions of the C compiler installed on"
echo "your machine.  For instance, you might have the standard cc"
echo "and the GNU make utilities installed in different directories."
echo "cc is typically installed under /bin, or /usr/bin. To have the "
echo "setup script automatically detect it, simply press ENTER when "
echo "asked to provide the path.  To specify which version of cc you "
echo "want to use, enter the full pathname.  For example, /usr/bin/gcc."
echo
$ECHO_N "	Which cc ? [autodetect] $ECHO_C"
read CC_PATH
if test "$CC_PATH." = "."
then
	echo
	echo "Trying to locate the make utility..."

	CC_PATHS="/usr/bin/gcc /usr/local/bin/gcc /usr/ccs/bin/cc /bin/cc /usr/bin/cc"
	CC_PATH=""
	for pathname in $CC_PATHS
	do
		if test -f $pathname
		then
			CC_PATH=${pathname}
			echo "Found cc in ${pathname}"
			break
		fi
	done

	if test "$CC_PATH." = "."
	then
		echo "Could not find the C compiler. You should find "
		echo "out its location on your machine and run the "
		echo "setup script again, this time specifying the "
		echo "pathname instead of asking for auto detection."
		echo
	fi
fi

echo

if test -f ${CC_PATH}
then
	echo
else
	echo "The C compiler pathname is invalid."
	echo
fi


##############################
# Get strip utility command  #
##############################

echo "The strip utility accepts different flags depending on the"
echo "Unix flavor that you are running.  For example, on Linux no"
echo "flag is required.  On NEXTSTEP, strip works best when invoked"
echo "with the -A and -n flags."
echo
$ECHO_N "	Flags to the strip utility ? [none] $ECHO_C"
read STRIP_FLAGS
if test "$STRIP_FLAGS." = "."
then
	STRIP_FLAGS=""
fi


##############################
# Run ranlib on libsgs-api?  #
##############################
echo
echo "Some operating systems such as Solaris 2 do not require that ranlib"
echo "be executed on objects archives, but this is the exception rather than"
echo "the rule."
echo
$ECHO_N "	Run ranlib on the libraries ? [y] $ECHO_C"
read RANLIB_YES_NO
if test "$RANLIB_YES_NO." = "."
then
	cp api/makefile.std api/makefile
else
	cp api/makefile.solaris2 api/makefile
fi

###################################
# Compile with debugging symbols? #
###################################
echo
echo "You can specify whether to compile with debugging symbols."
echo "If you elect to compile without them, SGS will be compiled "
echo "with optimizations."
echo
$ECHO_N "	Compile with debugging symbols ? [y] $ECHO_C"
read DEBUG_YES_NO
if test "$DEBUG_YES_NO." = "."
then
	CCFLAGS="-g -Wall"
else
	CCFLAGS="-O4 -Wall"
fi

#########################
# Get additional CFLAGS #
#########################
echo
echo "Specify here any additional flags that you would like to "
echo "pass the C compiler. "
echo
$ECHO_N "	Additional C flags : $ECHO_C"
read ADDITIONAL_CFLAGS

###########################
# Get additional LD FLAGS #
###########################
echo
echo "Specify here any additional flags that you would like to "
echo "see on the link line. "
echo
$ECHO_N "	Additional ld flags : $ECHO_C"
read OTHER_LDFLAGS

##############################
# Auto configuration section #
##############################
echo
echo "Automatic configuration section"
echo

LIBC_FILES="bsd/libc.h libc.h g++/libc.h unistd.h"
LIBC_FILE=""
INCLUDE_LIBC=""
for entry in $LIBC_FILES
do
	if test -f /usr/include/$entry
	then
		LIBC_FILE=${entry}
		INCLUDE_LIBC="#include <${LIBC_FILE}>"
		echo "Found libc.h in ${entry}"
		break
	fi
done

FCNTL_FILES="fcntl.h"
FCNTL_FILE=""
INCLUDE_FCNTL=""
for entry in $FCNTL_FILES
do
	if test -f /usr/include/$entry
	then
		FCNTL_FILE=${entry}
		INCLUDE_FCNTL="#include <${FCNTL_FILE}>"
		echo "Found fcntl.h"
		break
	fi
done

TIME_FILES="bsd/sys/time.h sys/time.h time.h"
TIME_FILE=""
INCLUDE_TIME=""
for entry in $TIME_FILES
do
	if test -f /usr/include/$entry
	then
		TIME_FILE=${entry}
		INCLUDE_TIME="#include <${TIME_FILE}>"
		echo "Found time.h in ${entry}"
		break
	fi
done

#
# Link to these libraries if they exist.  These are required for Solaris 2
#

EXTRA_LIBS="EXTRA_LIBS="

SOCKET_FILES="/usr/lib/libsocket.a /usr/local/lib/libsocket.a /lib/libsocket.a"
for entry in $SOCKET_FILES
do
	if test -f $entry
	then
		EXTRA_LIBS=" -lsocket "
		echo "Found ${entry}"
		break
	fi
done

NSL_FILES="/usr/lib/libnsl.a /usr/local/lib/libnsl.a /lib/libnsl.a"
for entry in $NSL_FILES
do
	if test -f $entry
	then
		EXTRA_LIBS="${EXTRA_LIBS} -lnsl "
		echo "Found ${entry}"
		break
	fi
done

UCB_FILES="/usr/lib/libucb.a /usr/local/lib/libucb.a /lib/libucb.a"
UCB_FILE=""
for entry in $UCB_FILES
do
	if test -f $entry
	then
		EXTRA_LIBS="${EXTRA_LIBS} -lucb "
		echo "Found ${UCB_FILE}"
		break
	fi
done

############################
#       Build site.h       #
############################

DEFAULT_PATH="#define SGS_DEFAULT_PATH \"${INST_TOP}\""

# Make a backup of site.h if it already exists

if test -f api/site.h
then
	mv api/site.h api/site.h~
fi

echo "/*" > api/site.h
echo " * Filename: site.h" >> api/site.h
echo " * Project:  sgs" >> api/site.h
echo " * Module:   API" >> api/site.h
echo " * Author:   Pascal Forget" >> api/site.h
echo " * Date:     October 1995" >> api/site.h
echo " *" >> api/site.h
echo " * Copyright (C) 1995 Pascal Forget" >> api/site.h
echo " *" >> api/site.h
echo " * Warning: This file is generated by the setup script." >> api/site.h
echo " * It will be overwritten each time the script is executed.">>api/site.h
echo " *" >> api/site.h
echo " */" >> api/site.h
echo "" >> api/site.h
echo "#ifndef SITE_H__" >> api/site.h
echo "#define SITE_H__" >> api/site.h
echo "" >> api/site.h
echo ${INCLUDE_LIBC} >> api/site.h
echo "" >> api/site.h
echo ${INCLUDE_FCNTL} >> api/site.h
echo "" >> api/site.h
echo ${INCLUDE_TIME} >> api/site.h
echo "" >> api/site.h
echo ${DEFAULT_PATH} >> api/site.h
echo "" >> api/site.h
echo "#endif" >> api/site.h

############################
#    Build the makefile    #
############################

# Make a backup of site.h if it already exists

if test -f makefile
then
	mv makefile makefile~
fi

echo "#" > makefile
echo "# Warning: This file is created by the setup script." >> makefile 
echo "# It will be overwritten each time the script is executed." >> makefile
echo "#" >> makefile

INSTALLROOT="INSTALLROOT=${INST_TOP}"
echo ${INSTALLROOT} >> makefile

MAKE_MACRO="MAKE=${MAKE_PATH} "
echo ${MAKE_MACRO} >> makefile

CC_MACRO="CC=${CC_PATH} "
echo ${CC_MACRO} >> makefile

CFLAGS_MACRO="CFLAGS=${CCFLAGS} ${ADDITIONAL_CFLAGS}"
echo ${CFLAGS_MACRO} >> makefile

LDFLAGS_MACRO="OTHER_LDFLAGS=${OTHER_LDFLAGS} "
echo ${LDFLAGS_MACRO} >> makefile

STRIP_MACRO="STRIP=strip ${STRIP_FLAGS} "
echo ${STRIP_MACRO} >> makefile

EXTRA_LIBS_MACRO="EXTRA_LIBS=${EXTRA_LIBS} "
echo ${EXTRA_LIBS_MACRO} >> makefile
cat makefile.ini >> makefile

############################
#  Print message and exit  #
############################

echo
echo "Ready to build SGS 1.0.0."
echo
echo "You may wish to check \"api/site.h\" although the defaults should be"
echo "fine.  When you're ready, type  \"make install\" to build the software"
echo
