#!/bin/sh
#
# Install script for Linux InterBase software
#
# Copyright (c)1998,1999, Inprise Corporation
# All Rights Reserved
#
############################################################################

PATH=$PATH:/usr/ucb:/usr/bsd:/bin:/usr/bin
export PATH

LogFile="ibinstall.log"
TmpLogFile="/tmp/$LogFile"

# NOTE: On Linux, the install directory is /opt
DefaultInstDir="/opt"
IBINSTALL="$DefaultInstDir"
InetSrvInstDir="/usr/local/sbin"
SUPERSERVER=""

# NOTE: TarFile must contain the absolute path name
TarFile=""

ExitError()
{
echo -e "\n*** Install completed with errors ***" | tee -a $TmpLogFile
echo "Install log can be found at $TmpLogFile"
exit 1
}

ExitSuccess()
{
echo -e "\nInstall completed successfully" | tee -a $INTERBASE/$LogFile
echo "Install log can be found at $INTERBASE/$LogFile"
exit 0
}

CheckInstallDir()
{
if [ ! -z "$1" ]; then
    if [ -d "$1/interbase" ]; then
	echo "Found a previous installation of InterBase." | tee -a $TmpLogFile
        echo "Renaming existing $IBINSTALL/interbase to $IBINSTALL/interbase.$$" | tee -a $TmpLogFile
        echo "mv $IBINSTALL/interbase $IBINSTALL/interbase.$$" >> $TmpLogFile
	mv $IBINSTALL/interbase $IBINSTALL/interbase.$$
    fi
fi
oldib="/usr/interbase"
if [ -L $oldib -o -f $oldib -o -d $oldib ]; then
    echo "Found a previous installation of InterBase." | tee -a $TmpLogFile
    echo "Renaming existing $oldib to $oldib.$$" | tee -a $TmpLogFile
    echo "mv $oldib $oldib.$$" >> $TmpLogFile
    mv $oldib $oldib.$$
fi
}

#
# Main entry
#

if [ -f $TmpLogFile ]; then
    mv $TmpLogFile $TmpLogFile.old
    echo -e "Old $TmpLogFile moved to $TmpLogFile.old\n" >> $TmpLogFile
fi
echo "***********************************************" >> $TmpLogFile
echo "InterBase Install in progress" >> $TmpLogFile
echo "`date`" >> $TmpLogFile
echo "***********************************************" >> $TmpLogFile

if [ "`whoami`" != "root" ]; then
    echo "ERROR: No permissions to perform operation." | tee -a $TmpLogFile
    echo "       Log in as root and try again." | tee -a $TmpLogFile
    ExitError
fi

ibserver=`ps -ef|grep ibserver|grep -c -v grep`
gds_inet_server=`ps -ef|grep gds_inet_server|grep -c -v grep`

if [ $ibserver -ne 0 -o $gds_inet_server -ne 0 ]; then
    echo "ERROR: Detected another Interbase server running on the system." | tee -a $TmpLogFile
    echo "       Shutdown the InterBase server and try again." | tee -a $TmpLogFile
    ExitError
fi

if [ $# -gt 2 ]; then
    echo "ERROR: too many parameters." | tee -a $TmpLogFile
    echo "Usage: $0 [-c | -s] AbsolutePathOfTheTarFile" | tee -a $TmpLogFile
    ExitError
fi

for i in $1 $2 ; do
    case $i in
	-c | -C )
	    SUPERSERVER="";;
	-s | -S )
	    SUPERSERVER="SUPERSERVER";;
	* )
	    TarFile="$i";;
    esac
done

if [ -z "$TarFile" ]; then
    echo "ERROR: Syntax error, expected archive file." | tee -a $TmpLogFile
    echo "Usage: $0 [-c | -s] AbsolutePathOfTheTarFile" | tee -a $TmpLogFile
    ExitError
fi

if [ ! -d "$IBINSTALL" ]; then
    mkdir -p $IBINSTALL
    echo -e "\nmkdir -p $IBINSTALL" >> $TmpLogFile
fi

CheckInstallDir $IBINSTALL

cd $IBINSTALL
if [ ! -f $TarFile ]; then
    echo "ERROR: File \"$TarFile\" not found." | tee -a $TmpLogFile
    ExitError
fi

INTERBASE="$IBINSTALL/interbase"

#
# Extract files from a tar archive
#
echo -e "----------------------------\n" >> $TmpLogFile
echo -e "\nExtracting files" >> $TmpLogFile
echo -e "\nExtracting files... \c"
tar vxfz $TarFile >> $TmpLogFile 2>&1
if [ $? -ne 0 ]; then
    echo "ERROR: Can't extract the files from the archive." | tee -a $TmpLogFile
    ExitError
fi
echo "Done"
echo -e "----------------------------\n" >> $TmpLogFile

#
# If no switches were specified then set SUPERSERVER depending on the
# content of the tar file
#
if [ $# -ne 2 ]; then
    if [ -f $INTERBASE/bin/ibserver ]; then
	SUPERSERVER="SUPERSERVER"
    else
	SUPERSERVER=""
    fi
fi

#
# Access permissions
#
echo -e "Updating access permissions... \c"
echo "Updating access permissions" >> $TmpLogFile
chmod 666 $INTERBASE/isc4.gdb
echo "chmod 666 $INTERBASE/isc4.gdb" >> $TmpLogFile
chmod 666 $INTERBASE/examples/*.gdb
echo "chmod 666 $INTERBASE/examples/*.gdb" >> $TmpLogFile
chmod 666 $INTERBASE/help/help.gdb
echo "chmod 666 $INTERBASE/help/help.gdb" >> $TmpLogFile
echo "Done"
echo -e "----------------------------\n" >> $TmpLogFile

#
# Header files
#
for file in /usr/include/gds.h /usr/include/ibase.h /usr/include/iberror.h /usr/include/ib_util.h
do
    if [ -L $file -o -f $file ]
    then
        echo "Found a previous version of $file" | tee -a $TmpLogFile
        echo "Renaming it to $file.$$, and moving it into /tmp" | tee -a $TmpLogFile
        mv $file $file.$$
	mv $file.$$ /tmp
	echo "mv $file $file.$$" >> $TmpLogFile
	echo "mv $file.$$ /tmp" >> $TmpLogFile
    fi
done

echo -e "Installing the header files... \c"
echo "Installing the header files" >> $TmpLogFile
    
cp -f interbase/include/gds.h /usr/include/gds.h
cp -f interbase/include/iberror.h /usr/include/iberror.h
cp -f interbase/include/ibase.h /usr/include/ibase.h
cp -f interbase/include/ib_util.h /usr/include/ib_util.h
chmod 744 /usr/include/ib_util.h /usr/include/ibase.h /usr/include/iberror.h /usr/include/gds.h
echo "cp -f interbase/include/gds.h /usr/include/gds.h" >> $TmpLogFile
echo "cp -f interbase/include/iberror.h /usr/include/iberror.h" >> $TmpLogFile
echo "cp -f interbase/include/ibase.h /usr/include/ibase.h" >> $TmpLogFile
echo "cp -f interbase/include/ib_util.h /usr/include/ib_util.h" >> $TmpLogFile
echo "Done"
echo -e "----------------------------\n" >> $TmpLogFile

#
# Libraries
#
for file in /usr/lib/libgds.so /usr/lib/libgds.so.0 /usr/lib/libgds.a /usr/lib/libib_util* /usr/lib/libgds_pyxis.a
do
    if [ -L $file -o -f $file ]
    then
        echo "Found a previous version of $file" | tee -a $TmpLogFile
        echo "Renaming it to $file.$$, and moving it into /tmp" | tee -a $TmpLogFile
        mv $file $file.$$
        mv $file.$$ /tmp
	echo "mv $file $file.$$" >> $TmpLogFile
	echo "mv $file.$$ /tmp" >> $TmpLogFile
    fi
done

echo -e "Installing the libraries... \c"
echo "Installing the libraries" >> $TmpLogFile

mv -f interbase/lib/gds_pyxis.a /usr/lib/libgds_pyxis.a
mv -f interbase/lib/ib_util /usr/lib/libib_util.so
mv -f interbase/lib/gds.so /usr/lib/libgds.so.0
ln -s libgds.so.0 /usr/lib/libgds.so
echo "mv -f interbase/gds_pyxis.a /usr/lib/libgds_pyxis.a" >> $TmpLogFile
echo "mv -f interbase/ib_util /usr/lib/libib_util.so" >> $TmpLogFile
echo "mv -f interbase/gds.so /usr/lib/libgds.so.0" >> $TmpLogFile
echo "ln -s libgds.so.0 /usr/lib/libgds.so" >> $TmpLogFile
if [ -z "$SUPERSERVER" ]; then
    mv -f interbase/lib/gds.a /usr/lib/libgds.a
    echo "mv -f interbase/lib/gds.a /usr/lib/libgds.a" >> $TmpLogFile
fi

echo "Done"
echo -e "----------------------------\n" >> $TmpLogFile

#
# Inet server
#
if [ -z "$SUPERSERVER" ]; then
    echo -e "Installing inet server... \c"
    echo "Installing inet server" >> $TmpLogFile
    if [ ! -d $InetSrvInstDir ]; then
        mkdir -p $InetSrvInstDir
        echo "mkdir -p $InetSrvInstDir" >> $TmpLogFile
        chmod 755 $InetSrvInstDir
        echo "chmod 755 $InetSrvInstDir" >> $TmpLogFile
    fi
    mv -f interbase/bin/gds_inet_server $InetSrvInstDir
    echo "mv -f interbase/bin/gds_inet_server $InetSrvInstDir" >> $TmpLogFile
    echo "Done"
    echo -e "----------------------------\n" >> $TmpLogFile
fi
    
#
# Update both services and servers databases
#
echo -e "Updating service name database... \c"
echo "Updating service name database" >> $TmpLogFile
if grep "^gds_db" /etc/services > /dev/null
then
    grep -v "^gds_db" /etc/services > /etc/services.tmp
    echo "gds_db        3050/tcp  # InterBase Database Remote Protocol" >> /etc/services.tmp
    mv /etc/services.tmp /etc/services
else
    echo "gds_db        3050/tcp  # InterBase Database Remote Protocol" >> /etc/services
fi
echo "Entry in /etc/services: gds_db        3050/tcp  # InterBase Database Remote Protocol" >> $TmpLogFile

echo "Done"
echo -e "----------------------------\n" >> $TmpLogFile

echo -e "Updating configuration file for inetd... \c"
echo "Updating configuration file for inetd" >> $TmpLogFile

if [ -f /etc/inetd.conf ]; then
if grep "^gds_db" /etc/inetd.conf > /dev/null
then
    grep -v "^gds_db"  /etc/inetd.conf > /etc/inetd.conf.tmp
    mv -f /etc/inetd.conf.tmp /etc/inetd.conf
fi
if [ -z "$SUPERSERVER" ]; then
    echo "gds_db  stream  tcp     nowait.30000      root $InetSrvInstDir/gds_inet_server gds_inet_server # InterBase Database Remote Server" >> /etc/inetd.conf
    
    echo "Entry in /etc/inetd.conf: gds_db  stream  tcp     nowait.30000      root $InetSrvInstDir/gds_inet_server gds_inet_server # InterBase Database Remote Server" >> $TmpLogFile
fi
fi

# Create /etc/hosts.equiv
if [ ! -f /etc/hosts.equiv ]; then
    echo -e "Creating /etc/hosts.equiv ... \c"
    echo "Creating /etc/hosts.equiv" >> $TmpLogFile
    echo "localhost" >> /etc/hosts.equiv
fi


# Poke inetd
if [ -f /var/run/inetd.pid ]
then
    kill -HUP `cat /var/run/inetd.pid`
fi

echo "Done"
echo -e "----------------------------\n" >> $TmpLogFile

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

    chmod +x $INTERBASE/bin/ibmgr
    echo "Done"
    echo -e "----------------------------\n" >> $TmpLogFile
fi

#
# Lock files
#
touch $INTERBASE/isc_init1.`hostname`
chmod 666 $INTERBASE/isc_init1.`hostname`
touch $INTERBASE/isc_lock1.`hostname`
chmod 666 $INTERBASE/isc_lock1.`hostname`
touch $INTERBASE/isc_event1.`hostname`
chmod 666 $INTERBASE/isc_event1.`hostname`
if [ -n "$SUPERSERVER" ]; then
    touch $INTERBASE/isc_guard1.`hostname`
    chmod 666 $INTERBASE/isc_guard1.`hostname`
fi
touch $INTERBASE/interbase.log
chmod 666 $INTERBASE/interbase.log

#
# Move the log file to the $INTERBASE
#
if [ -f $INTERBASE/$LogFile ]; then
    cp $INTERBASE/$LogFile $INTERBASE/$LogFile.old
    echo "Old $INTERBASE/$LogFile moved to $INTERBASE/$LogFile.old" >> $TmpLogFile
fi
mv $TmpLogFile $INTERBASE/$LogFile
ExitSuccess
