#!/bin/sh
#
# setup - setup chimera
#
# this is NOT the GNU autoconfig program.  this is a cheesy program
# that i wrote to make installation easier for folks.  at least
# i hope it makes installation easier.
#

#
# This part ripped off from the perl Configure program
#
# determine how to suppress newline on echo command
#   n:      for bsd style echo: -n before text
#   c:      for sysv style: \c after text
#
# some greps do not return status, grrr.
echo "grimblepritz" >contains.txt
if grep blurfldyick contains.txt >/dev/null 2>&1
then
	contains=contains
elif grep grimblepritz contains.txt >/dev/null 2>&1
then
	contains=grep
else
	contains=contains
fi
# the following should work in any shell
case "$contains" in
contains*)
    echo " "
    echo "AGH!  Grep doesn't return a status.  Attempting remedial action."
    cat >contains <<'EOSS'
grep "$1" "$2" >.greptmp && cat .greptmp && test -s .greptmp
EOSS
chmod +x contains
esac

(echo "x\c" ; echo " ") > echotmp
if $contains c echotmp >/dev/null 2>&1
then
	n='-n'
	c=''
else
	n=''
	c='\c'
fi
/bin/rm -f echotmp

necho ()
{
	echo $n $1 $c
}

echo
echo Chimera Installation
echo
echo To select the default for a question just press RETURN.
echo
echo You will now be asked for an installation directory for chimera.
echo The following subdirectories will be created if they do not already
echo exist: lib, man, bin.  The lib directory will contain support
echo files for chimera.  The man directory will contain a very brief
echo UNIX manual page.  The bin directory will contain the chimera
echo executable and some support programs.
echo
echo Common installation directories are
echo /usr/local/infosys
echo /local/infosys
echo /usr/local
echo
echo If you are installing chimera for personal use only you may want to
echo put the installation directory in your home directory.
echo
necho "Enter the name of the installation directory: "
read root

if [ "$root" = "" ]
then
	root=/usr/local/infosys
	chimeralib=$root/lib
	echo
	echo I will make /usr/local/infosys the installation directory.
	echo

elif [ "$root" = "/usr/local" ]
then
	echo
	echo Would you like a chimera subdirectory under
        necho "/usr/local/lib (y|n) ? "
	read libsubdir

	if [ "$libsubdir" = "n" ]
	then
		chimeralib=/usr/local/lib
	else
		chimeralib=/usr/local/lib/chimera
	fi
else
	chimeralib=$root/lib
fi

echo
echo Directories:
echo $root/bin will be the executable directory.
echo $chimeralib will be the configuration file directory.
echo $root/man will be the man page directory.
necho "Is this acceptable (y|n) ? "
read acceptable

if [ "$acceptable" = "n" ]
then
	exit
fi

echo
echo Now I am going to ask you the name of the default printer.
echo The default is lp.
echo
necho "Enter the name of the default printer: "
read printername

if [ "$printername" = "" ]
then
	printername=lp
	echo
	echo I am going to make lp the default printer.
	echo
fi

echo
echo One more question about files.  I will now ask you for the name of the
echo first document to display when chimera starts up.  The
echo default is file:$chimeralib/home.html which is included with
echo chimera and is automatically installed.  It is not a bad
echo choice.  If you have a local HTTP server then may want to use
echo it.  For example, the server at UNLV is www.unlv.edu
echo so I would set the home document to
echo
echo http://www.unlv.edu/
echo
necho "Enter the URL of the home document: "
read homedoc

if [ "$homedoc" = "" ]
then
	homedoc=file:$chimeralib/home.html
	echo
	echo Using the default home document $homedoc
	echo 
fi

echo
echo Now for some questions concerning options and features.
echo
echo Chimera has optional support for the term asynch socket transport.
echo This requires the term package, which is not included here.
necho "Do you wish to compile with term support (n|y) ? "
read termresp

TERMFLAG=""
if [ "$termresp" = "y" ]
then
	TERMFLAG="-DTERM"
        echo
	echo We assume your term lib is named libclient.a.
	echo What is the path to the term library, not including
        necho "the lib : "
	read termresp

	if [ "$termresp" = "" ]
	then
		echo You must give a location, exiting configuration
		exit 1
	else
		TERMLIB="-L$termresp -lclient"
	fi

	echo
	necho "Enter the path of the term header files : "
	read termresp

	if [ "$termresp" = "" ]
	then
		echo You must give a path, exiting configuration
		exit 1
	else
		TERMINC="-I$termresp"
	fi

        echo
        echo "Are you running any servers (ftp, gopher, http, ...) on"
        necho "your machine (y|n) ? "
        read servers

        if [ "$servers" = "n" ]
	then
		TERMFLAG=$TERMFLAG" -DNONETWORK"
        fi
else
	echo
	echo "If your site uses a firewall then you will probably want"
	echo "to use SOCKS."
	echo
	necho "Do you want to use SOCKS (n|y) ? "
        read socks

        if [ "$socks" = "y" ]
        then
		SOCKSFLAG=-DSOCKS
		necho "Enter the location of the socks library : "
		read megarbage

		if [ "$megarbage" = "" ]
		then
			SOCKSLIB="-lsocks"
		else
			SOCKSLIB="-L$megarbage -lsocks"
		fi
        else
                echo Leaving SOCKS out
        fi
fi

echo
echo If you have Xaw3d and would like to install it, give the path
echo up to the beginning of the lib, eg. /usr/local/lib.  Press RETURN
necho "if want to use the standard Xaw library: "
read xawresp

if [ "$xawresp" = "" ]
then
	echo Using standard Xaw
else
	XAWLIB="XAWLIB = -L$xawresp -lXaw3d"
fi

echo
echo OK.  Now I am going to configure chimera to use $root as the
echo installation directory.  When this program ends type
echo
echo xmkmf
echo make Makefiles clean all
echo
echo to compile chimera.  To install chimera after the compilation is
echo complete, type:
echo
echo make install
echo

# garabage for modifying the files follows:

# Fix Common.tmpl
mv Common.tmpl Common.tmpl.back
sed -e "s!XXXCHIMERABIN!$root/bin!" -e "s!XXXCHIMERALIB!$chimeralib!" -e "s!XXXCHIMERAMAN!$root/man!" -e "s!XXXAWLIB!$XAWLIB!" -e "s!XXXTERMFLAG!$TERMFLAG!" -e "s!XXXTERMINC!$TERMINC!" -e "s!XXXTERMLIB!$TERMLIB!" -e "s!XXXSOCKSFLAGS!$SOCKSFLAG!" -e "s!XXXSOCKSLIB!$SOCKSLIB!" Common.tmpl.dist > Common.tmpl

# Fix src/conf.h
mv src/conf.h src/conf.h.back
sed -e 's!XXXHOME_PAGE!"'$homedoc'"!' -e 's!XXXHELP_DOCUMENT!"'file:$chimeralib/help.html'"!' -e 's!XXXCONTENT_FILE!"'$chimeralib/content'"!' -e 's!XXXPRINTER_NAME!"'$printername'"!' src/conf.h.dist > src/conf.h

# copy Imakefile
mv Imakefile Imakefile.back
cp Imakefile.dist Imakefile

exit 0
# all done
