#!/bin/sh
###############################################################################
# Product:  OMNIBACK-II
# Package:  OB2-CORE
# Function: Preinstall
###############################################################################


OS=`uname -s`

# Exit code
# #########
if [ "${OS}" = "HP-UX" ]; then
   # Failed:1 Partialy Failed:2
   SUCCESS=0
   EXIT_FAILED=1
   EXIT_WARNING=2
else
   # Fatal:1 Warning:2 Halt:3
   SUCCESS=0
   EXIT_FAILED=3
   EXIT_WARNING=2
fi

DEVNULL=/dev/null

# OmniBack II files & directories
# ###############################
OMNIHOME=/opt/omni/
OMNICONFIGBASE=/etc${OMNIHOME}
OMNICONFIG=${OMNICONFIGBASE}server/
OMNICONFIGCLIENT=${OMNICONFIGBASE}client/
OMNIVAR=/var${OMNIHOME}server/
OMNIVARCLIENT=/var${OMNIHOME}
OMNILOG=${OMNIVAR}/log
OMNILOGCLIENT=${OMNIVARCLIENT}/log
OMNITMP=${OMNIVARCLIENT}/tmp
NEWCONFIG=${OMNIHOME}/newconfig/
OMNILBIN=${OMNIHOME}lbin
OMNIINET=${OMNILBIN}/inet
OMNIWINDUREG=${OMNIVARCLIENT}/windu
OMNIINFO=client/omni_info
OMNIFORMAT=client/omni_format
OMNI_IS=${OMNICONFIG}cell/installation_servers
CELLINFO=${OMNICONFIG}/cell/cell_info
CELLSERVER=${OMNICONFIGCLIENT}/cell_server
USERCONF=${OMNICONFIG}/users
DATABASES=${OMNIHOME}databases/vendor/
OMNISITEPERL=${OMNIHOME}/lib/perl

OLD_OMNIHOME=/usr/omni/
OLD_OMNICONFIG=${OLD_OMNIHOME}config/
TMP_OMNIHOME=/var/tmp/omni/
TMP_OMNICONFIG=${TMP_OMNIHOME}config/

UPGRADE_SCRIPT=${OMNIHOME}sbin/DBupgrade.sh

# Temporary OmniBack II directory
# ###############################
TOMNI=/tmp/omni_tmp
SOCKET_DAT=${TOMNI}/socket.dat
MGMT_SV=${TOMNI}/mgmt_sv.dat

# The service file, inet config directory
# #######################################
SERVICES=/etc/services
INETDCONF=/etc/inetd.conf
INETDSEC=/var/adm/inetd.sec

SERVICE=omni

TMP=/tmp/inst.tmp


# #############################################################################
# Checks for a MC/SG environment
# #############################################################################

OMNICC_ARG=""
UPGCFG_ARG=""
CHECK_MCSG_ENV=""

check_mcsg()
{
    if [ ! -z "$CHECK_MCSG_ENV" ]; then return; fi
    CHECK_MCSG_ENV=0
    if [ "${OS}" != "HP-UX" ]; then return; fi
    if [ ! -x /usr/sbin/cmviewcl ]; then return; fi
    if [ ! -f $OMNICONFIG/sg/sg.conf -o \
         ! -f $OMNICONFIG/cell/cell_info ]; then return; fi
    if [ \( -h /etc/$OMNIHOME/server -a -h /var/$OMNIHOME/server \) -o \
         \( -h /etc/opt/omni -a -h /var/opt/omni \) ]
    then
        CSHOST=`cat $OMNICONFIGCLIENT/cell_server 2>/dev/null`
        if [ -z "$CSHOST" ]; then return; fi
        grep "[-]host [ \"]*$CSHOST\"* .*-virtual" $OMNICONFIG/cell/cell_info >/dev/null 2>/dev/null
        if [ $? != 0 ]; then return; fi
        grep "^[^#]*CS_SERVICE_HOSTNAME=\"*$CSHOST\"*" $OMNICONFIG/sg/sg.conf >/dev/null 2>/dev/null
        if [ $? != 0 ]; then return; fi
        SHARED_DISK_ROOT=`grep "^[^#]*SHARED_DISK_ROOT=" $OMNICONFIG/sg/sg.conf 2>/dev/null | cut -d= -f2 | tr -d "\"'"`
        if [ -z "$SHARED_DISK_ROOT" ]; then return; fi
        if [ ! -d "$SHARED_DISK_ROOT" ]; then return; fi
        OMNICC_ARG="-server "`hostname | tr "[A-Z]" "[a-z]"`
        UPGCFG_ARG="$OMNICC_ARG"
        CHECK_MCSG_ENV=1
    fi
}

call_omnicc()
{
    check_mcsg
    ${OMNIHOME}/bin/omnicc $OMNICC_ARG "$@"
}




# #############################################################################
# Removes OmniBack II entries from inetd configuration files
# #############################################################################
clean()
{
  if [ "${OS}" = "HP-UX" ]; then
    SPACE=`bdf /tmp | sed 1d | awk '{print $4}'`
  else
    SPACE=`df /tmp | awk '{print $4}'`
  fi
  if [ $SPACE -gt 300 ]; then
    for FILE in ${SERVICES} ${INETDCONF} ${INETDSEC}
    do
      if [ -r $FILE ]
      then
        awk '{if($1 != SERV && $2 != "DATA-PROTECTOR") {print}}' SERV=${SERVICE} ${FILE} > ${TMP}
        cp ${TMP} ${FILE}
        rm ${TMP}
      fi
    done
  fi
}



# #############################################################################
# Updates the information about the installed OmniBack II agents
# in the omni_info file
# #############################################################################
clean_omni_info()
{
  PAC=$1

  if [ ! -f "${OMNIHOME}/bin/omnicc" ]; then
    echo "ERROR:   Cannot find ${OMNIHOME}/bin/omnicc" >&2
    exit ${EXIT_FAILED}
  else
    if [ -f "${OMNICONFIGBASE}/$OMNIINFO" ]; then
      OUTPUT=`call_omnicc -remove_component_str $PAC $OMNIFORMAT $OMNIINFO 2>&1`
      if [ $? -ne 0 -a $? -ne 3 ]
      then
        echo "WARNING: $OUTPUT"
        echo "WARNING: Update of the omni_info file failed for $PAC!"
      fi
    fi
  fi
}


# Script body
# ###########

# # remove sybolic link
#######################
if [ -h ${OLD_OMNIHOME} -a -x ${OLD_OMNIHOME}/bin/omnicc ]
then
	rm ${OLD_OMNIHOME} > /dev/null 2>&1
fi

# # Copy old config to the tmp directory
# ######################################
if [ -d ${OLD_OMNIHOME} -a -x ${OLD_OMNIHOME}/bin/omnicc ]
then
	if [ -d ${OLD_OMNICONFIG} ]
	then
		rm -rf ${TMP_OMNIHOME} > /dev/null 2>&1
		mkdir -p ${TMP_OMNICONFIG} > /dev/null 2>&1
		if [ $? -ne 0 ]
		then
			echo "Directory creation error, cannot create ${TMP_OMNICONFIG}" 
			exit ${EXIT_FAILED}
		fi

		cp -r ${OLD_OMNICONFIG}/* ${TMP_OMNICONFIG} 
		if [ $? -ne 0 ]
		then
			echo "Warning: ${OLD_OMNICONFIG} could not be copied to ${TMP_OMNICONFIG}"
		fi
	fi
        if [ -f ${OLD_OMNIHOME}/.omnirc ]
        then
	        if [ ! -d ${TMP_OMNICONFIG} ]
	        then
		     mkdir -p ${TMP_OMNICONFIG} > /dev/null 2>&1
                fi
                cp ${OLD_OMNIHOME}/.omnirc ${TMP_OMNICONFIG}
                if [ $? -ne 0 ]
                then
                        echo "Warning: ${OLD_OMNIHOME}/.omnirc could not be copied to ${TMP_OMNICONFIG}"
                fi
        fi
        #The below section finds the binaries from the /usr/omni/.CII* files
        #and removes the same before moving the contents to /var/opt/omni/tmp/usr_omni directory.
        for i in `ls ${OLD_OMNIHOME}/.CII*`
        do
            cat $i >> /tmp/old_omni_bin
        done
        if [ -f /tmp/old_omni_bin ]
        then
           cat /tmp/old_omni_bin | cut -d ' ' -f 1 > /tmp/old_omni_bin_tmp
           mv /tmp/old_omni_bin_tmp /tmp/old_omni_bin
           for i in `cat /tmp/old_omni_bin`
           do
               rm -f $i
           done
           rm -f /tmp/old_omni_bin
        fi

fi

if [ -f ${OMNICONFIG}/${OMNIFORMAT} ]
then
  rm -f ${OMNICONFIG}/${OMNIFORMAT}
fi

# remove softlink file from DP 5.00
if [ -h ${OMNIHOME}/databases/vendor/ma/sun/sparc/solaris-7 ]; then
  rm ${OMNIHOME}/databases/vendor/ma/sun/sparc/solaris-7
fi

exit 0
#postinstall scriptlet (through /bin/sh):
