#!/bin/sh
###############################################################################
# Product:  OMNIBACK-II
# Fileset:  OMNI-CORE
# Function: Preinstall/Unconfigure
###############################################################################

# RSC $Header: /src/files/customizeRPM/preremove_CORE /main/dp56/blr_dp56/2 2006-01-02 14:48:56 chezhian $


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 '{ if ($4 != "Available") {print $4}}'`
  fi
  if [ $SPACE -gt 300 ]; then
   if [ -f /etc/xinetd.conf ]; then
     XINETDFILE=`cat /etc/xinetd.conf | grep includedir | awk '{ print $2 }'`
     if [ "${XINETDFILE}" != "" ]; then
       XINETDFILE=${XINETDFILE}/omni
     else
       XINETDFILE="/etc/xinetd.conf"
     fi
   fi
   for file in ${SERVICES} ${INETDCONF}; do
    if [ -f ${file} ]; then
     awk '{if(($1 != "omni_" && $1 != SERV) && $2 != "DATA-PROTECTOR") {print}} ' SERV=${SERVICE} ${file} > ${TMP}
     cp ${TMP} ${file}
     rm ${TMP}
    fi
   done
   if [ ${XINETDFILE} ]; then
     if [ "${XINETDFILE}" != "/etc/xinetd.conf" ]; then
       rm -f ${XINETDFILE}
     else
       sed /^"service omni"$/,/^$/d ${XINETDFILE} > /${TOMNI}/new.xinetd.conf
       mv /${TOMNI}/new.xinetd.conf ${XINETDFILE}
     fi
   fi
  fi
}



# ########################################################################
# Removes the cell_server file
# ########################################################################
remove_dir()
{
  rm -f ${CELLSERVER}
}



# #############################################################################
# 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
# ###########

# # export client from the cell
# # ###########################
HOSTNAME=`hostname | tr "[A-Z]" "[a-z]"`
call_omnicc -export_host ${HOSTNAME} -remove_only 1>$DEVNULL 2>$DEVNULL

# # remove log & tmp directories
# # ############################
remove_dir

# # clean OmniBack II entries from inetd configuration files
# # ########################################################
clean

INETDPID=`ps -e | egrep '[/ ]*inetd'| awk '{print $1}'`
if [ "${INETDPID}" ]; then
   kill -HUP ${INETDPID} > /dev/null 2>&1
   kill -HUP ${INETDPID} > /dev/null 2>&1
fi

if [ -f ${OMNICONFIG}/${OMNIINFO} ]
then
  rm -f ${OMNICONFIG}/${OMNIINFO}
fi
if [ -f ${OMNICONFIG}/${OMNIFORMAT} ]
then
  rm -f ${OMNICONFIG}/${OMNIFORMAT}
fi
if [ -f ${OMNICONFIG}/install/omni_info ]
then
  rm -f ${OMNICONFIG}/install/omni_info
fi
if [ -d ${TMP_OMNIHOME} ]
then
  rm -rf ${TMP_OMNIHOME}
fi
# # To remove /opt/omni/lib (untar of site_perl.tar created this directory)
if [ -d ${OMNIHOME}/lib ]
then
  rm -rf ${OMNIHOME}/lib
fi
exit 0
