#!/bin/ksh
# The contents of this file are subject to the Interbase Public
# License Version 1.0 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy
# of the License at http://www.Inprise.com/IPL.html
#
# Software distributed under the License is distributed on an
# "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
# or implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code was created by Inprise Corporation
# and its predecessors. Portions created by Inprise Corporation are
# Copyright (C) Inprise Corporation.
#
# All Rights Reserved.
# Contributor(s): ______________________________________.
#
# /usr/interbase/install	-- Install InterBase
#
# The install script looks for the server
# executable and decides if the install is for superserver or classic.
# 
# Make changes for allowing the install directory to be dependent on 
# $INTERBASE.
#
#

OLD_INTERBASE=$INTERBASE

if [ "$INTERBASE" = "" ]; then
    INTERBASE="/usr/interbase"
fi

PATH=$PATH:/usr/ucb:/usr/bsd
export PATH
HOSTNAME=`hostname | cut -d. -f1`
if [ -f $INTERBASE ]; then
    echo $INTERBASE is not a directory 
    echo InterBase installation failed 
    exit 1
fi
if [ -d $INTERBASE ]; then
    cd $INTERBASE
    TARGET=`pwd`
else
     echo invalid interbase installation directory $INTERBASE
     exit 1
fi
if [ -f $INTERBASE/bin/ibserver ]; then
    SUPERSERVER="SUPERSERVER"
    if [ "$OLD_INTERBASE" = "" ] ; then
        ln -s $INTERBASE /usr/interbase
    fi
else
    SUPERSERVER=""
#   need the link in classic as isc4.gdb is not moved.
	if [ $INTERBASE != "/usr/interbase" ]
	then
		ln -s  $INTERBASE /usr/interbase
	fi
fi
#set -x
#
# Make sure the top level (/usr/interbase) is 777
#
chmod 777 .
#
# Move standard include file to /usr/include
#
rm -f /usr/include/gds.h
rm -f /usr/include/ibase.h
rm -f /usr/include/iberror.h
rm -f /usr/include/ib_util.h
ln -s $INTERBASE/include/gds.h /usr/include/gds.h
ln -s $INTERBASE/include/ibase.h /usr/include/ibase.h
ln -s $INTERBASE/include/iberror.h /usr/include/iberror.h
ln -s $INTERBASE/include/ib_util.h /usr/include/ib_util.h
# 
# Move pipe and full libraries to /usr/lib and re-initialize table of contents
#
rm -f /usr/lib/libgds_b.a
if [ -f lib/gds_b.a ]; then
    ln -s $INTERBASE/lib/gds_b.a /usr/lib/libgds_b.a
fi
if [ -f lib/gds.a ]; then
    rm -f /usr/lib/libgds.a
    ln -s $INTERBASE/lib/gds.a /usr/lib/libgds.a
fi
if [ -f lib/gds.sl ]; then
    rm -f /usr/lib/libgds.sl
        ln -s $INTERBASE/lib/gds.sl /usr/lib/libgds.sl
fi
if [ -f lib/gdsshr.a ]; then
    rm -f /usr/lib/libgdsshr.a
    ln -s $INTERBASE/lib/gdsshr.a /usr/lib/libgdsshr.a
fi
if [ -f lib/gds_s.a ]; then
    rm -f /usr/lib/libgds_s
    rm -f /usr/lib/libgds_s.a
    rm -f /usr/lib/libgdsf_s
    rm -f /usr/lib/libgdsf_s.a
    ln -s $INTERBASE/lib/gds_s /usr/lib/libgds_s
    ln -s $INTERBASE/lib/gds_s.a /usr/lib/libgds_s.a
    ln -s $INTERBASE/lib/gdsf_s /usr/lib/libgdsf_s
    ln -s $INTERBASE/lib/gdsf_s.a /usr/lib/libgdsf_s.a
fi
if [ -f lib/gds.so.0 ]; then
    if [ -d /usr/shlib ]; then
        rm -f /usr/shlib/libgds.so
        rm -f /usr/shlib/libgds.so.0
        ln -s $INTERBASE/lib/gds.so.0 /usr/shlib/libgds.so.0
        ln -s /usr/shlib/libgds.so.0 /usr/shlib/libgds.so
    else
        rm -f /usr/lib/libgds.so
        rm -f /usr/lib/libgds.so.0
        ln -s $INTERBASE/lib/gds.so.0 /usr/lib/libgds.so.0
        ln -s /usr/lib/libgds.so.0 /usr/lib/libgds.so
    fi
fi
# This is only for solaris
if [ -f lib/gdsmt.so.0 ]; then
    rm -f /usr/lib/libgdsmt.so
    rm -f /usr/lib/libgdsmt.so.0
    ln -s $INTERBASE/lib/gdsmt.so.0 /usr/lib/libgdsmt.so.0
    ln -s /usr/lib/libgdsmt.so.0 /usr/lib/libgdsmt.so
fi
if [ -f lib/gdsf.so ]; then
    rm -f /usr/lib/libgdsf.so
    ln -s $INTERBASE/lib/gdsf.so /usr/lib/libgdsf.so
fi
if [ -f lib/gds_pyxis.a ]; then
    rm -f /usr/lib/libgds_pyxis.a
    ln -s $INTERBASE/lib/gds_pyxis.a /usr/lib/libgds_pyxis.a
fi
if [ -f lib/ib_util.sl ]; then
    rm -f /usr/lib/libib_util.sl
        ln -s $INTERBASE/lib/ib_util.sl /usr/lib/libib_util.sl
fi
if [ -f lib/ib_util.so ]; then
    rm -f /usr/lib/libib_util.so
        ln -s $INTERBASE/lib/ib_util.so /usr/lib/libib_util.so
fi
#
# Protect security, examples and QLI help databases
#
chmod uog=rw isc4.gdb
chmod uog=rw help/help.gdb
chmod uog=rw examples/employee.gdb
chmod uog=rw examples/intlemp.gdb
if [ "$SUPERSERVER" = "SUPERSERVER" ]; then
    chmod 4755 $INTERBASE/bin/gstat
fi
#
# If TCP is installed, update both services and servers databases
#
HUPNEEDED='n'	# Relevant to Classic only
if [ -f /etc/services ]; then
    grep -s gds_db /etc/services  > /dev/null 2>&1
    if test $? != 0 ; then
	HUPNEEDED='y'
        cat /etc/services services.isc > services
	mv services /etc/services
    fi
fi
if [ -f /etc/inetd.conf ]; then
    if [ "$SUPERSERVER" = "SUPERSERVER" ]; then
	# Remove gds_db service. We do not want gds_inet_server to be invoked
	cat /etc/inetd.conf | grep -v gds_db > inetd.conf
	mv inetd.conf /etc/inetd.conf
    else
	grep -s gds_db /etc/inetd.conf  > /dev/null 2>&1
	if test $? != 0 ; then
	    HUPNEEDED='y'
	    cat /etc/inetd.conf inetd.conf.isc > inetd.conf
	    mv inetd.conf /etc/inetd.conf
	fi
    fi
fi
if [ -f /usr/etc/inetd.conf ]; then
    if [ "$SUPERSERVER" = "SUPERSERVER" ]; then
	# Remove gds_db service. We do not want gds_inet_server to be invoked
	cat /usr/etc/inetd.conf | grep -v gds_db > inetd.conf
	mv inetd.conf /usr/etc/inetd.conf
    else
	grep -s gds_db /usr/etc/inetd.conf  > /dev/null 2>&1
	if test $? != 0 ; then
	    HUPNEEDED='y'
	    cat /usr/etc/inetd.conf inetd.conf.isc > inetd.conf
	    mv inetd.conf /usr/etc/inetd.conf
	fi
    fi
fi

# Currently SCO is the only classic for 5.5, add other platforms 
# later as required
OSNAME=`uname -s`
if [ "$OSNAME" = "SCO_SV" ] 
then
	if [ "$HUPNEEDED" = 'y' ]
	then
		INETPID=`ps -e | grep inetd | awk '{print $1}'`
		kill -HUP $INETPID
	fi
fi

set +x

if [ "$SUPERSERVER" = "SUPERSERVER" ]; then
#
# create the ibmgr shell script
#
cat > $INTERBASE/bin/ibmgr << EOF
#!/bin/sh
INTERBASE=$INTERBASE
export INTERBASE
exec $INTERBASE/bin/ibmgr.bin \$@
EOF

chmod +x $INTERBASE/bin/ibmgr
fi

#
# Create the license file
#
print -n "Please enter the license certificate id: "
read
case "$REPLY" in
  "eval" | "" )
      print Creating a 90-day evaluation license.
      $INTERBASE/bin/iblicense -eval
      chmod uog=r $INTERBASE/ib_license.dat
      ;;
  * )
      id=$REPLY
      print -n "Please enter the license certificate key: "
      read
      if [ "$REPLY" = "" ] ; then
	print "We need the license key which matches your license id."
	print -n "Please enter the license certificate key: "
	read
      fi
      if [ "$REPLY" != "" ] ; then
	print Creating the license.
	$INTERBASE/bin/iblicense -add -id $id -key $REPLY
        chmod uog=r $INTERBASE/ib_license.dat
      else
	print \
"No license created.  Please see the release notes to create a license file."
      fi
      ;;
esac

#
# Install InterClient if the user chooses to do so.
#

# Skip Interclient install altogether for SCO since Interclient
# is a separate package to be installed through custom on SCO 
if [ "$OSNAME" != "SCO_SV" ]
then

	ic="InterClient*.tar"
	ic_install=NO
	if [ `uname -s` = "HP-UX" ]; then
		ic_path="/SD_CDROM"
	else
		ic_path="/cdrom"
	fi
	ic_done=NO
	while [ $ic_install != YES ] ; do
		print -n "Do you wish to install InterClient[yn]? "
		read
		case $REPLY in
		  y|Y|yes|Yes|YES )
		  while [ $ic_done != YES ] ; do
			  if [ -f $ic_path/$ic ]; then
			  cd /tmp;
			  tar xpf $ic_path/$ic;
			  cd interclient_install_temp_dir;
			  ./install.sh;
			  ic_done=YES;
			  else
			  echo "\nWARNING: $ic_path/$ic not found\n"
			  ic_select=NO
			  while [ $ic_select != YES ] ; do
				  echo "Select \"Q\" to quit, or"
				  echo "       \"S\" to specify the new path."
				  echo "Enter selection : \c"
				  read selection
				  case $selection in
				  Q|q )
					  echo "We did NOT install InterClient."
					  ic_done=YES;
					  ic_select=YES;;
				  S|s )
					  echo "Please specify the path : \c"
					  read ic_path
					  ic_select=YES;;
				  * )
					  echo "\nWARNING: Selection \"$selection\" not understood\n";;
				  esac
			  done
			  fi
		  done
			  ic_install=YES;;
		  n|N|no|No|NO )
		  echo "We did NOT install InterClient."
		  ic_install=YES;;
		  * )
		  echo "Please reply y or n (in upper or lower case)"
		esac
	done
fi
