#! /bin/sh
# This is a relatively simple configure script for HyperNeWS installation.
# "@(#)Config 3.1 7/31/90 Jim Rudolf (The Turing Institute)"

# Fairly common defaults if Config hasn't been run before
# A few more are defined after HNHOME is input
HNSRC=`pwd`
HNHOME=`echo $HNSRC | sed "s?HyperNeWS1.4/src?HyperNeWS1.4?"`
OPENWINHOME=/usr/src/openwin/sun4
TEXFONTDIR=/usr/local/lib/fonts/gf/gf300
SPOOLDIR=/usr/spool/lw
HNBINDIR=
HNLIBDIR=
HNFONTDIR=
PROLOGDIR=
LISPDIR=

# Simple data acquisition routine
cat << EOF > .myprompt
echo ; echo -n "\$msg [\$dflt] "
read ans
if [ ! "\$ans" ]; then
	ans=\$dflt
fi
if [ "\$check" = "y" ]; then
	if [ ! -d \$ans ]; then
		echo "That directory doesn't exist"
		. .myprompt
	fi
else
	check=y
fi
EOF

check=y

cat << EOF

This is the HyperNeWS configuration script.  It will ask you a few
questions about your system and where HyperNeWS files should be placed.
If you have run the script before, your old answers will be included
in square brackets as the default.  If you haven't run the script before,
suggested defaults will be used.  To accept the default, simply press
the <return> key.
EOF

# Has Config been run before?
if [ -f Config.param ]; then
	echo ; echo "You have run Config before.  I'll load your defaults."
	. Config.param
fi

##### Start collecting info for installation #####

echo
echo "The current directory (\$HNSRC) is $HNSRC"

msg="Where should HyperNeWS (\$HNHOME) be installed?"
dflt=$HNHOME
check=n
. .myprompt
HNHOME=$ans

# Set these defaults once $HNHOME has been set
if [ "$HNBINDIR" = "" ]; then
	HNBINDIR=$HNHOME/bin
fi
if [ "$HNLIBDIR" = "" ]; then
	HNLIBDIR=$HNHOME/lib
fi
if [ "$HNFONTDIR" = "" ]; then
	HNFONTDIR=$HNLIBDIR/fonts
fi

msg="Where should the binaries go?"
dflt=$HNBINDIR
check=n
. .myprompt
HNBINDIR=$ans

msg="Where should the libraries go?"
dflt=$HNLIBDIR
check=n
. .myprompt
HNLIBDIR=$ans

msg="Where should the fonts go?"
dflt=$HNFONTDIR
check=n
. .myprompt
HNFONTDIR=$ans

msg="Where is OpenWindows installed?"
dflt=$OPENWINHOME
. .myprompt
OPENWINHOME=$ans

msg="Do you have TeX on this system?"
if [ "$TEXFONTDIR" = "" ]; then
	dflt=n
else
	dflt=y
fi
check=n
. .myprompt
if [ "$ans" = "y" -o "$ans" = "Y" ]; then
	msg="Where are the TeX gf fonts installed?"
	dflt=$TEXFONTDIR
	. .myprompt
	TEXFONTDIR=$ans
	msg="Where is the printer's spool directory?"
	dflt=$SPOOLDIR
	. .myprompt
	SPOOLDIR=$ans
else
	TEXFONTDIR=
fi

msg="Do you have Quintus Prolog on this system?"
if [ "$PROLOGDIR" = "" ]; then
	dflt=n
else
	dflt=y
fi
check=n
. .myprompt
if [ "$ans" = "y" -o "$ans" = "Y" ]; then
	msg="Where is the Prolog executable installed?"
	dflt=$PROLOGDIR
	. .myprompt
	PROLOGDIR=$ans
else
	PROLOGDIR=
fi

msg="Do you have CommonLisp on this system?"
if [ "$LISPDIR" = "" ]; then
	dflt=n
else
	dflt=y
fi
check=n
. .myprompt
if [ "$ans" = "y" -o "$ans" = "Y" ]; then
	msg="Where is CommonLisp installed?"
	dflt=$LISPDIR
	. .myprompt
	LISPDIR=$ans
else
	LISPDIR=
fi

##### Done asking for config info #####

cat << EOF

All necessary variables have been collected.  The next steps are to
build the new Makefile.param and save your new Config options in
Config.param.  You can choose to abort the Config script if you wish.
EOF

msg="Do you want to continue?"
dflt=y
check=n
. .myprompt
if [ "$ans" != "y" -a "$ans" != "Y" ]; then
	echo ; echo "HyperNeWS configuration aborted." ; echo
	exit
fi

echo ; echo "Saving new values to Config.param."
cat << EOF > Config.param
HNHOME=$HNHOME
HNSRC=$HNSRC
HNBINDIR=$HNBINDIR
HNLIBDIR=$HNLIBDIR
HNFONTDIR=$HNFONTDIR
OPENWINHOME=$OPENWINHOME
TEXFONTDIR=$TEXFONTDIR
PROLOGDIR=$PROLOGDIR
LISPDIR=$LISPDIR
SPOOLDIR=$SPOOLDIR
EOF

if [ -f Makefile.param ]; then
	echo "Moving Makefile.param to Makefile.param=."
	mv Makefile.param Makefile.param=
fi
echo "Creating new Makefile.param based on template in Makefile.param.t"
sed "/SEDFLAG/r Config.param" < Makefile.param.t > Makefile.param

# Check to see if their OLUI.ps is the same as ours
echo ; echo "Checking to see if you have the right OLUI.ps file..."
hnOLUI=$HNSRC/install/OLUI.ps
owOLUI=$OPENWINHOME/etc/NeWS/NDE/OLUI.ps
if [ ! -f $owOLUI ]; then
	cat << EOF

I can't find the OpenWindows OLUI.ps file!  Please double-check your
\$OPENWINHOME setting.  I expect to find the OLUI.ps file in the
directory \$OPENWINHOME/etc/NeWS/NDE.
EOF
else
	if \cmp $hnOLUI $owOLUI > /dev/null 2>&1; then
		echo "Your OLUI.ps looks OK."
	else
		cat << EOF

HyperNeWS requires a modified version of the OLUI.ps file under
NeWS.  However, you still have Sun's version.  I will try to move
it to OLUI.ps= and put mine in its place if I can.  If you don't
currently have write permission on \$OPENWINHOME, you'll have to
get someone else to do the copy.

EOF

		msg="Should I try to switch your OLUI.ps file?"
		dflt=y
		check=n
		. .myprompt
		if [ "$ans" != "y" -a "$ans" != "Y" ]; then
			echo ; echo "Skipping OLUI.ps change."
		else
			if \mv -f $owOLUI $owOLUI= > /dev/null 2>&1; then
				echo "$owOLUI moved to $owOLUI="
				\cp $hnOLUI $owOLUI
				echo "$hnOLUI copied to $owOLUI"
				echo "OLUI.ps changed successfully."
			else
				cat << EOF
I can't do it...probably permission problems.  Please get someone
with the proper authority (such as Mr. Root) to copy the file
$hnOLUI to
$owOLUI.
EOF
			fi
		fi
	fi
fi

# Substitute HNHOME & OPENWINHOME into sample script used to
# run HyperNeWS, located in HNHOME/share/hn1.4
cat << EOF > .tmp
# Where is OpenWindows?
setenv OPENWINHOME	$OPENWINHOME
# Where is HyperNeWS?
setenv HNHOME		$HNHOME
EOF
sed "/SEDFLAG/r .tmp" < $HNSRC/share/.hn1.4 > $HNSRC/share/hn1.4
chmod +x $HNSRC/share/hn1.4
/bin/rm .tmp

cat << EOF

The necessary parameters have now been substituted into the
Makefile.config file.  If you wish, you can do the installation now.
EOF

msg="Do you want to do a 'make install' now?"
dflt=y
check=n
. .myprompt
echo
if [ "$ans" != "y" -a "$ans" != "Y" ]; then
	echo "Config script exited without doing 'make install'."
else
	echo "Doing the 'make install'." ; echo
	make install
fi
echo

exit
