#!/bin/sh
#
# Copyright (c) 1990 by Carrick Sean Casey.
# For copying and distribution information, see the file COPYING.
#
# Generic setup script for source configuration. This queries the user for
# configuration parameters and determines some things about the target
# system. It then writes a file called "config" that can be used by "make"
# to preset make variables.

#wd=`pwd`
homedir=${HOME}
cflags=""
pmake=""
otherlibs="-lcurses -ltermcap"
resolvlib=""
ask="support/ask"
askisthere="false"

# what if no $HOME?

if [ "${homedir}" = "" ]; then homedir=`pwd`; fi


# defaults

defsystem="bsd"
defbindir="${homedir}/bin"
defmandir="${homedir}/man"
deflibdir="${homedir}/lib"
defcompiler="cc"
defextra=""
defmakedepend="makedepend"
defdependopts="-fMake"
defhost="129.24.12.18"
defport="7326"

#
# Compile ask because echo doesn't have consistent args.
#

if [ -r "$ask" ]; then
	if $ask > /dev/null 2>&1; then
		askisthere="true"
	else
		echo "Ask appears to have been compiled on a different system."
	fi
fi

if [ "$askisthere" = "false" ]; then
	echo "Just a moment while I compile \"ask\"..."
	cc -O -o $ask support/ask.c
	echo "Done."
	echo ""
fi

#
#	Get default answers
#

if [ -r "./defaults" ]; then
	echo "Loading default answers..."
	. defaults
fi


#
#	Get the system type.
#

cat <<EOF

This script will ask you some questions. Default answers are provided
in brackets [like this], and will be used if you just type return. If
you wish to enter nothing at all, use a space.


Please enter your system type. The following systems are recognized:

bsd		Most Berkeley based Unix systems 
linux		Linux
sysv		Some System V based Unix systems.
hpux		HP-UX Hewlett-Packard Unix.
irix		Silicon Graphics Irix.
dynix		Dynix on a Sequent Symmetry.
next		NeXTStep
hcx		HCX Unix.

Bsd works with a wide range of Berkeleyish Unix systems. If
you're not sure, or your system isn't in the list, use the default.

EOF

$ask "System type? [${defsystem}] " 
read system
if [ -z "${system}" ]; then
	system="$defsystem"
fi


#
#	Make system dependent options 
#


case $system in
bsd)
	system=bsd
	sysdep="sysdep.bsd"
	lflags="-u"
	;;
linux)
	sysdep="sysdep.bsd"
	otherlibs="$otherlibs -lbsd"
	lflags="-u"
	cflags="$cflags -DVOID_SIGHANDLER=1"
	;;
sysv)
	sysdep="sysdep.sysv"
	cflags="${cflags} -DSYSV -DNOWINSIZE"
	lflags="-u"
	;;
hpux)
	sysdep="sysdep.sysv"
	cflags="${cflags} -DSYSV -D_BSD -DNEED_ALLOCA -DNOWINSIZE"
	lflags="-u"
	;;
dynix)
	pmake="&"
	otherlibs="-lseq -ltermcap"
	sysdep="sysdep.bsd"
	cflags="${cflags} -DNEED_CTYPE -DNO_LRAND48"
	lflags="-u"
	;;
irix)	sysdep="sysdep.sysv"
	cflags="${cflags} -DSYSV -DNEED_ALLOCA"
	if [ `/bin/uname -r | sed 's/\..*//'` = "4" ];then
		cflags="-cckr ${cflags}"
	fi
	otherlibs="${otherlibs} -lmalloc -lsun"
	lflags="-u"
	;;
next)
	sysdep="sysdep.bsd"
	cflags="${cflags} -bsd -DNO_LRAND48"
	lflags="-u"
	;;
hcx)
	sysdep="sysdep.bsd"
	cflags="${cflags} -DNO_WINSIZE"
	lflags="-u"
	;;
*)
	echo ""
	echo "I don't know system \"$system\". Rerun setup and try again."
	exit 1
	;;
esac


#
#	Get location for executable
#

cat <<EOF



When you type "make install" the program and a manual page will be
installed. The default will put the executable in your personal bin
directory, the database file in your personal lib directory, and the 
man page in your personal man directory.

EOF
$ask "Directory for executables? [${defbindir}] "
read bindir
if [ -z "${bindir}" ]; then
	bindir="${defbindir}"
else
	case "$bindir" in
		*/bin)
			defparent="`expr "$bindir" : '\(.*\)/bin'`"
			defmandir="$defparent/man"
			deflibdir="$defparent/lib"
			;;
		*)
			:;;
	esac
fi
$ask "Directory for manual page? [${defmandir}] "
read mandir
if [ -z "${mandir}" ]; then
	mandir="${defmandir}"
fi
$ask "Directory for support file? [${deflibdir}] "
read libdir
if [ -z "${libdir}" ]; then
	libdir="${deflibdir}"
fi

case "$bindir" in
	\~)	bindir="${homedir}";;
	\~/*)	bindir="${homedir}`expr "$bindir" : '~\(.*\)'`";;
	*) :;;
esac

case "$mandir" in
	\~)	mandir="${homedir}";;
	\~/*)	mandir="${homedir}`expr "$mandir" : '~\(.*\)'`";;
	*) :;;
esac

case "$libdir" in
	\~)	libdir="${homedir}";;
	\~/*)	libdir="${homedir}`expr "$libdir" : '~\(.*\)'`";;
	*) :;;
esac

#
#	Get compiler to use
#

cat <<EOF


The package will normally be compiled with the regular C compiler, "cc".
You may specify an alternate compiler like gcc here.

EOF
if [ -f "/bin/uname" ]; then
	if [ `/bin/uname` = "AIX" ]; then
		defcompiler="bsdcc"
	fi
fi
$ask "Compiler? [${defcompiler}] "
read compiler
if [ -z "${compiler}" ]; then
	compiler="${defcompiler}"
fi

#
#	Extra options
#

cat <<EOF


If you use the default, the package will be compiled with "-O -s" to
conserve disk space. If icb crashes and you wish to debug it, or if you
even suspect it might crash on your system, you should use "-g" instead.

If you are using gcc, and you start getting messages about failed IOCTLs,
then your gcc hasn't been properly installed. Add "-traditional" to the
compile flags, and tell your sysadmin to run "fixincludes".

EOF

if [ -z "${defextra}" -o "${compiler}" != "${defcompiler}" ]; then
	if [ "${compiler}" = "gcc" ]; then
		defextra="-O -s"
#		defextra="-g -W -Wcomment -Wswitch -Wunused -Wshadow -Wcast-qual -traditional"
	else
		defextra="-O -s"
	fi
fi
if [ -f "/bin/uname" ]; then
	if [ `/bin/uname` = "AIX" ]; then
		defextra="${defextra} -ma -w"
	fi
fi
	
$ask "Compiler options? [${defextra}] "
read extra
if [ -z "${extra}" ]; then
	extra="${defextra}"
fi


#
#	Get makefile dependency generator to use
#

cat <<EOF



If you intend to modify the source, you may wish to specify a
makefile dependency generator to be called up with "make depend".
The makefiles in the subdirectories are named "Make" so you may
also wish to specify an option to specify this file.

If you are not going to modify the source, just use the default.

EOF
$ask "Makefile dependency generator? [${defmakedepend}] "
read makedepend
if [ -z "${makedepend}" ]; then
	makedepend="${defmakedepend}"
fi
$ask "Options for dependency generator? [${defdependopts}] "
read dependopts
if [ -z "${dependopts}" ]; then
	dependopts="${defdependopts}"
fi

#
#	Get default port and host
#

cat <<EOF


This program accesses the service by connecting to a remote server.
For normal use, it will look in .icbservrc in the user's home directory
and the the icb server database file (icbserverdb in the support files
directory).  If it cannot find a legal entry in either of these places,
it will use the following default host and port.

It is usually better for efficiency and portability to use the numeric
address of the host. Legal addresses might be: crater.unm.edu,
129.24.12.18 or [129.24.12.18].
EOF
$ask "Internet host name of the server's machine? [${defhost}] "
read host
if [ -z "${host}" ]; then
	host="${defhost}"
fi
$ask "Port number of the server? [${defport}] "
read port
if [ -z "${port}" ]; then
	port="${defport}"
fi

#
#	Execute the server install script
#

if [ -r "support/ssetup" ]; then
	server="true"
	. support/ssetup
fi


cat <<EOF


Now I'm going to look for some files...

EOF

#
# find strings.h
#

if [ -r "/usr/include/strings.h" ]; then
	echo "Your strings header is <strings.h>".
	cflags="${cflags} -DSTRINGS_H"
elif [ -r "/usr/include/string.h" ]; then
	echo "Your strings header is <string.h>".
else
	echo "Couldn't find <strings.h> or <string.h>. Quitting..."
	exit 0
fi

#
# find time.h
#

if [ -r "/usr/include/time.h" ]; then
	echo "Your time header is <time.h>".
	cflags="${cflags} -DTIME_H"
elif [ -r "/usr/include/sys/time.h" ]; then
	echo "Your time header is <sys/time.h>".
else
	echo "Couldn't find <time.h> or <sys/time.h>. Quitting..."
	exit 0
fi

#
# see if libresolv is out there
#

if [ -r "/usr/lib/libresolv.a" ]; then
	echo "Found the TCP/IP resolver library."
	if [ "$system" != "dynix" ]; then
		resolvlib="-lresolv"
	fi
else
	echo "Didn't find /usr/lib/libresolv.a. Without this library, some"
	echo "systems may core dump. We'll give it a try..."
fi


#
# check for some functions
#

#if support/hasfunc strerror; then :
#else
#	cflags="${cflags} -DNOSTRERROR"
#fi

#if support/hasfunc strtol; then :
#else
#	cflags="${cflags} -DNOSTRTOL"
#fi
	
cat <<EOF

Saving default answers...
EOF

cat > defaults<<EOF
defsystem="${system}"
defbindir="${bindir}"
defmandir="${mandir}"
deflibdir="${libdir}"
defcompiler="${compiler}"
defextra="${extra}"
defmakedepend="${makedepend}"
defdependopts="${dependopts}"
defhost="${host}"
defport="${port}"
EOF

cat <<EOF

Creating "config"...
EOF

cat > config <<EOF
#
#	THIS IS AN AUTOMATICALLY GENERATED FILE
#
#	If you edit this file and later run "setup", your
#	changes will be lost. This file gets fed to "make",
#	so keep that in mind if you make changes.
#

# Installing user's home directory
HOME		= ${HOME}

# Directory where the client will be installed.
BINDIR		= ${bindir}

# Directory where the manual page will be installed.
MANDIR		= ${mandir}

# Compiler to use.
CC		= ${compiler}

# Compile options.
CFLAGS		= ${extra} ${cflags}
TCLCFLAGS	= ${extra} ${cflags}

# Lint flags
LFLAGS		= ${lflags} ${cflags}

# System dependent source file.
SYSDEP		=${sysdep}

# Other libraries to include.
OTHERLIBS	=${otherlibs}

# Resolver library
RESOLVLIB	=${resolvlib}

# Parallel makes. Set to & for Sequents.
PMAKE		=${pmake}

# Makefile dependency generator.
MAKEDEPEND	= ${makedepend}

# Makefile dependency generator.
DEPENDFLAGS	= ${dependopts} ${cflags}

# Hostname and port of remote server.
DEFAULTHOST	= ${host}
DEFAULTPORT	= ${port}

# Directory for support files
LIBDIR = ${libdir}

# location of server database
SERVERLIST = ${libdir}/icbserverdb

EOF

if [ -n "${server}" ]; then
	cat <<EOF >> config

# Directory for the server support files
LIBDIR		= ${libdir}

# Server symbolic name
SERVERNAME	= "${servername}"
EOF
fi


#
#	All done. Tell the user what to do next.
#

cat <<EOF


Setup script complete.

Now you can type:

make 		Compile the program.
make install	Compile and install program and manual pages.
make clean	Clean up the mess that compile generates.
make depend     (Hackers only) Generate makefile dependencies.

If you have problems, you may need to edit "config" by hand. See the
file INSTALLING if you're really stuck.

EOF
