#!/bin/sh
#
#   sybinstall.sh	Sybase Adaptive Server Enterprise 11.0.3.3
#
#   Script to administrate and configure your Sybase ASE 11.0.3.3
#   installation for the 1st time.  Purpose is to create awareness
#   of the LICENSE agreement, create a system account and have
#   installation permissions set to the base directory.
#

PATH="/etc:/bin:/sbin:/usr/bin:/usr/sbin:$PATH"		; export PATH
PAGER=more						; export PAGER
MATCH=grep						; export MATCH

MsgLicense () {
cat <<EOM

   WARNING:  the LICENSE document was not found on your distribution
   Please report this to your package distributions channel.

EOM
}

MsgNIS () {
cat <<EOM

   Your system is administrated under a NIS setup,  you may like to stop
   this installation at once and contact your network administrator first.
   
   It is recommended to have all files and directories owned by this
   Adaptive Server Enterprise installation under a single group named
   "sybase".  We also recommend you to setup an account for Database
   Administrator (DBA) operations under the owner "sybase".  The
   permissions and ownership of the files should be left as they are.

EOM
}

MsgCompletes () {
cat <<EOM
	
   This completes your Sybase Adaptive Server Enterprise (ASE) package
   installation.  To run Sybase ASE, you may like configuring your master
   and sybsystemprocs databases by continuing the installation procedure
   after logging in as sybase and executing $SYBASE/install/sybinit.

   This action is the default when you login for the first time.
   Re-running this configuration procedure can be initiated by removing
   the .readme and .sybinit files from the 'sybase' installation
   directory.

EOM
}

MsgAgree () {
cat <<EOM

   You did agree with the LICENSE conditions!  Please register your
   installation by visiting:
   
   	<http://linux.sybase.com>.

EOM
}

MsgDeny () {
cat <<EOM

   YOU DID NOT AGREE WITH THE LICENSE CONDITIONS !

EOM
}

MsgInstall () {
cat <<EOM

   You may like to have a group for sybase installed.  Restart the
   installation script under your root account by executing:
   
   $SYBASE/install/sybinstall.sh

EOM
}

if [ "${DISPLAY}" ]; then
    # The user is installing from X11
    PAGER=more
fi

# Does it exist here ???
#
# Below changes to the Sybase ASE rpm package distribution was
# requested by Red Hat just before the final close date.  Problem
# is that the package manager is not work expecting interactive
# package installation.
#
# This way, we decided (last minute) to have the LICENSE and
# configuration script execute from a different installation level.
#
# If your Sybase ASE release area is not /opt/sybase change the
# variable SYBASE below.
# <wtenhave@sybase.com> Tue Oct 13 14:27:17 CEST 1998
#
# export SYBASE=/opt/sybase
export SYBASE=`rpm -q --queryformat \
		'%{installprefix}\n' sybase-ase-11.0.3.3`

if [ -f ${SYBASE}/install/sybinstall.sh ]; then

    # Check if the LICENSE conditions are available
    if [ ! -f ${SYBASE}/LICENSE ]; then
	MsgLicense
	exit 1
    fi

    # Check if the LICENSE conditions are acknowledged
    if [ ! -f ${SYBASE}/.syb_license ]; then
    	while :
	do
	    ${PAGER} ${SYBASE}/LICENSE
	    echo ; echo -n "DO YOU AGREE WITH THE LISTED LICENSE CONDITIONS? [Yes/No/List]: "
	    read answer
	    case $answer in
		[Yy]*) touch ${SYBASE}/.syb_license
			break ;;
		[Nn]*)  rpm -e sybase-doc > /dev/null 2>&1
			rpm -e sybase-ase > /dev/null 2>&1
			exit 1 ;;
		[Ll]*) continue ;;
		*) ;;
    	    esac
	done
    fi

    # Is this a NIS based installation, yes -> use ypmatch over grep
    ypwhich > /dev/null 2>&1
    NIS_INSTALL=$?
    if [ ${NIS_INSTALL} = 0 ]; then

	SYB_GROUP=`ypmatch "sybase" group | grep "^sybase"` > /dev/null
	SYB_USER=`ypmatch "sybase" passwd | grep "^sybase"` > /dev/null
	MsgNIS

    else

	SYB_GROUP=`grep "^sybase" /etc/group` > /dev/null
	SYB_USER=`grep "^sybase" /etc/passwd` > /dev/null

    fi

    while :
    do
	echo ; echo -n "Do you want this script to proceed by installing an account? [y/n]: "
	read answer
	case $answer in
	    [yY]*)
    		rm -f ${SYBASE}/.readme ${SYBASE}/.sybinit
		break ;;

	    [nN]*)
	    	MsgCompletes
		exit 0 ;;

	    *)
	    	continue ;;
	esac
    done


    if [ ! "${SYB_GROUP}" ]; then

	while :
	do
	    echo ; echo -n "Group sybase not found. Create it now? [y/n]: "
	    read answer
	    case $answer in
		[yY]*)
		    groupadd sybase
		    break ;;

		[nN]*)
		    MsgInstall
		    exit 1 ;;

		*)
		    continue ;;
	    esac
	done

    fi

    chgrp sybase ${SYBASE} ${SYBASE}/install ${SYBASE}/init/* \
    			${SYBASE}/sample/*
    chmod g+w ${SYBASE} ${SYBASE}/install ${SYBASE}/init/* ${SYBASE}/sample/*
    echo ; echo "   Group and permissions for ${SYBASE} are set !"

    if [ ! "${SYB_USER}" ]; then

	while :
	do
	    echo ; echo -n "User sybase not found. Create it now? [y/n]: "
	    read answer
	    case $answer in
	    	[yY]*)
		    useradd -g sybase -d ${SYBASE} \
		    	-c "Sybase ASE DBA account" sybase
		    passwd sybase
		    rm -f ${SYBASE}/.bash*
		    break ;;

	    	[nN]*)
		    MsgInstall
		    exit 1 ;;

		*)
		    continue ;;
	    esac
	done
	    
    fi

    chown sybase ${SYBASE} ${SYBASE}/install ${SYBASE}/init/*
    echo ; echo "   User sybase is available, release user id is set !"

    # Update the shared object cache
    if ! grep "^${SYBASE}" /etc/ld.so.conf > /dev/null
    then
	echo "${SYBASE}/lib" >> /etc/ld.so.conf
	ldconfig

	cp ${SYBASE}/install/rc.sybase /etc/rc.d/init.d/sybase
    fi

    MsgCompletes
    exit 0
fi

echo "installation script ${SYBASE}/install/sybinstall.sh not found!"
exit 1
