#!/bin/sh

echo "Welcome to the Linux BFRIS demo installation program."
echo "No files will be installed until after a final warning."
echo
echo "Please read the following license agreement.  If you do"
echo "agree to this license, you may press Control-C at any time"
echo "to abort this installation."
echo
echo -n "Press enter to see the license agreement:"

read ans

more license.txt
echo
ans=x

while [ $ans != y -a $ans != n ]; do
 echo -n "Do you agree to the terms of this license agreement? [n] "
 read ans
 ans=`echo ${ans}n | sed -e 'y/YN/yn/' -e 's/^\(.\).*/\1/'`
done

if [ $ans != y ]; then
  echo "Installation aborted"
  exit 1
fi


if [ $EUID != 0 ]; then
	echo "You must run the BFRIS install program as root."
	exit 1
fi

dest=/usr/local/games/bfrisdemo
echo -n "Where would you like the BFRIS demo installed? [$dest] "
read ans
if [ x$ans != "x" ]; then
	dest=$ans
fi

instvar=y
echo
echo "The BFRIS launcher can be installed in /usr/local/bin.  To use it,"
echo "you must set the BFRISDIR environment variable to point to "
echo -n $dest
echo ", where you will install BFRIS.  We can set this for you"
echo "in your .cshrc and .profile files; would you like to install the BFRIS"
echo -n "launcher and set this variable automatically upon login? [y] "
read ans
ans=`echo ${ans}y | sed -e 'y/YN/yn/' -e 's/^\(.\).*/\1/'`
if [ $ans = n ]; then
	installvar=n
fi
echo

intro=n

echo
echo "BFRIS requires hardware-accelerated OpenGL.  BFRIS ships with Mesa 3.0"
echo "compiled with support for 3Dfx Voodoo Graphics, Voodoo Rush, and Voodoo2"
echo "chipsets."
echo
installMesa=y
ans=x
while [ $ans != y -a $ans != n ]; do
	echo -n "Do you want to install Mesa 3.0 libraries packaged with BFRIS? [y] "
	read ans
	ans=`echo ${ans}y | sed -e 'y/YN/yn/' -e 's/^\(.\).*/\1/'`
done
if [ $ans = n ]; then
	installMesa=n
fi

mesaDest=$dest/lib

echo
echo -n "About to begin installation.  Press enter to proceed, or control-C to abort."
read ans

echo "(You may see warnings about three directories that are not copied, and if you"
echo "elected to install the Mesa libraries, you may see a warning about ldconfig;"
echo "these messages can usually be safely ignored.)"
echo

if [ ! -d $dest ]; then
	mkdir -p $dest
	if [ ! -d $dest ]; then
		echo "Failed to make bfris directory $dest"
		exit 1
	fi
fi
if [ $installMesa = y -a ! -d $mesaDest ]; then
	mkdir -p $mesaDest
	if [ ! -d $mesaDest ]; then
		echo "Failed to make Mesa directory $mesaDest"
		exit 1
	fi
fi


cp -f disk1/* $dest

if [ $intro = y ]; then
  cp disk1/intro/intro.mvy $dest
fi

if [ $installMesa = y ]; then
  cp disk1/mesa3dfx/libMesaGL.so.3.0 $mesaDest
  cp disk1/mesa3dfx/libMesaGLU.so.3.0 $mesaDest
  chmod a+rx $mesaDest/*
fi

if [ $instvar = y ]; then
  cp disk1/bfris /usr/local/bin
  chmod a+rx /usr/local/bin/bfris
  chmod a+rx $dest/bfrisc
  chmod a+rx $dest/lnx_sdrv
  chmod a+rx $dest/bfrisd
  egrep "^$dest/lib$" $HOME/.profile >/dev/null 2>&1
  if [ $? -ne 0 ]; then
    echo "export BFRISDIR="$dest >> $HOME/.profile
  fi
  egrep "^$dest/lib$" $HOME/.cshrc >/dev/null 2>&1
  if [ $? -ne 0 ]; then
    echo "setenv BFRISDIR "$dest >> $HOME/.cshrc
  fi
fi

ldConfig=n
if [ $installMesa = y ]; then
	egrep "^$dest/lib$" /etc/ld.so.conf >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo $dest/lib >> /etc/ld.so.conf
		ldConfig=y
	fi
fi
if [ $ldConfig = y ]; then
	/sbin/ldconfig
fi

echo
echo
echo "Installation complete.  Make sure you are logged in as root,"
echo "then change to $dest and type ./bfris to play." 
echo

