#!/bin/bash
###############################################################################
# Product:  Data Protector
# Version:  A.09.00
# 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

SUBPRODUCT=""

# OmniBack II files & directories
# ###############################
OMNIHOME=/opt/omni/
OMNIDBSERVER=`hostname`
OMNIDBINSTALLLOG=/tmp/DPDBInstall.log
OMNIDBUNINSTALLLOG=/tmp/DPDBUnInstall.log
OMNIDBUSERSHELL=`echo $SHELL`
OMNICONFIGBASE=/etc${OMNIHOME}
OMNICONFIG=${OMNICONFIGBASE}server/
OMNICONFIGCLIENT=${OMNICONFIGBASE}client/
OMNICONFIGIS=${OMNICONFIGBASE}IS/
OMNIVAR=/var${OMNIHOME}server/
OMNIVARCLIENT=/var${OMNIHOME}
OMNILOG=${OMNIVAR}/log
OMNILOGCLIENT=${OMNIVARCLIENT}/log
OMNITMP=${OMNIVARCLIENT}/tmp
OMNIENHINCRDB=${OMNIVARCLIENT}/enhincrdb
NEWCONFIG=${OMNIHOME}/newconfig/
OMNIIDBCOMPOSITE=${NEWCONFIG}${OMNICONFIG}idb/
OMNIBIN=${OMNIHOME}bin
OMNILBIN=${OMNIHOME}lbin
OMNILIB=${OMNIHOME}/lib
OMNIINET=${OMNILBIN}/inet
OMNIINFO=client/omni_info
OMNIINFO_IS=IS/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
COMPONENTS=/etc/opt/omni/client/components/

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

UPGRADE_SCRIPT=${OMNIHOME}sbin/IDBsetup.sh

# Temporary OmniBack II directory
# ###############################
TOMNI=/tmp/omni_tmp
OMNIDBPARAMFILE=${TOMNI}/omnidbparam
SOCKET_DAT=${TOMNI}/socket.dat
DPDAT=${TOMNI}/dpdat
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 "$@"
}

remove_duplicated_push_packets()
{
    # remove old push packets
    DIR_LST=`find ${DATABASES}/ -name '*.*.*' 2>$DEVNULL`
    if [ "${DIR_LST}" ]; then
     for DIR in ${DIR_LST}; do
       if [ `basename "${DIR}"` != "A.09.00" -a -d `dirname "${DIR}"`/A.09.00 ]; then
         rm -rf ${DIR}
       fi
     done
    fi
}




# #############################################################################
# Updates the information about the installed OmniBack II agents
# in the omni_info file
# #############################################################################
clean_omni_info()
{
  PAC=$1
  if [ "$2" ]
  then
    OMNIINFO=$2
  fi
  
  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
}



# ############################################################################
# Updates the cell_info file
# ############################################################################
update_cell_info ()
{
  HOSTNAME=`hostname | tr "[A-Z]" "[a-z]"`

  if [ -f ${DPDAT} ]; then
    NOENCRYPTION=`cat ${DPDAT} |grep NOENCRYPTION | awk -F"=" '{ print $2 }'`
  fi

  if [ "${NOENCRYPTION}" = "1" ]; then
    call_omnicc -update_host ${HOSTNAME} -encr_disable 1>$DEVNULL 2>$DEVNULL
  else
    call_omnicc -update_host ${HOSTNAME} 1>$DEVNULL 2>$DEVNULL
  fi
}


OMNI_DB=/var${OMNIHOME}/db
OMNI_DB40=/var${OMNIHOME}/db40
OMNI_DB55=/var${OMNIHOME}/server/db40
OMNI_DB80=/var${OMNIHOME}/server/db80

OMNI_CDB=${OMNI_DB}/cdb
OMNI_CATALOG=${OMNI_DB}/catalog
OMNI_CATALOG40=${OMNI_DB40}/datafiles/catalog
OMNI_CATALOG55=${OMNI_DB55}/datafiles/catalog
OMNI_CATALOG80=${OMNI_DB80}/datafiles/catalog

OMNIIDBCONFIG=${OMNICONFIG}idb/

DB_VERS=${OMNI_CATALOG}/version.txt
DB_VERS40=${OMNI_CATALOG40}/version.txt
DB_VERS55=${OMNI_CATALOG55}/version.txt
DB_VERS80=${OMNIIDBCONFIG}/version.txt

# Script body
# ###########
if [ -f ${OMNIHOME}/bin/omnicc ]
then
   clean_omni_info core
   update_cell_info
fi

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


# version.txt has hardcoded (not branded!) product strings (see CC:/ob/src/lib/cmn/defines.h)
# For DP:    "HP OpenView OmniBack II"
# For APPRM: "HP Application Recovery Manager"
# after this the version string follows: "A.06.10"

# check for current database version
if [ -f "$DB_VERS80" ]; then
  DB_PRODUCT=`awk '{sub(" +A.[0-9].*",""); sub(" +X.[0-9].*",""); sub("HP +",""); sub("(OpenView|StorageWorks) +",""); sub(" +software",""); sub("OmniBack II", "Data Protector"); print}' $DB_VERS80`
  DB_VERSION_STR=`awk '{sub("HP OpenView OmniBack II +",""); sub("HP( StorageWorks | )Application Recovery Manager +",""); print}' $DB_VERS80`
  DB_VERSION=${DB_VERSION_STR#A.}; DB_VERSION=${DB_VERSION#X.}; DB_VERSION=${DB_VERSION#0}; DB_VERSION=${DB_VERSION%.\%*}
  echo "NOTE:    $DB_PRODUCT Internal Database version $DB_VERSION_STR found."

else
  # detect older database version 5.50 6.00 6.10 6.11 6.20 6.21 7.00
  if [ -f "$DB_VERS55" ]; then
    DB_PRODUCT=`awk '{sub(" +A.[0-9].*",""); sub(" +X.[0-9].*",""); sub("HP +",""); sub("(OpenView|StorageWorks) +",""); sub(" +software",""); sub("OmniBack II", "Data Protector"); print}' $DB_VERS55`
    DB_VERSION_STR=`awk '{sub("HP OpenView OmniBack II +",""); sub("HP( StorageWorks | )Application Recovery Manager +",""); print}' $DB_VERS55`
    DB_VERSION=${DB_VERSION_STR#A.}; DB_VERSION=${DB_VERSION#X.}; DB_VERSION=${DB_VERSION#0}; DB_VERSION=${DB_VERSION%.\%*}
    echo "NOTE:    $DB_PRODUCT Internal Database version $DB_VERSION_STR found."
  
  else
    # detect older database version 4.00 4.10 5.00 5.10
    if [ -f "$DB_VERS40" ]; then
      DB_PRODUCT=`awk '{sub(" +A.[0-9].*",""); sub("HP +",""); sub("(OpenView|StorageWorks) +",""); sub(" +software",""); sub("OmniBack II", "Data Protector"); print}' $DB_VERS40`
      DB_VERSION_STR=`awk '{sub("HP OpenView OmniBack II +",""); sub("HP( StorageWorks | )Application Recovery Manager +",""); print}' $DB_VERS40`
      DB_VERSION=${DB_VERSION_STR#A.}; DB_VERSION=${DB_VERSION#0}; DB_VERSION=${DB_VERSION%.\%*}
      echo "NOTE:    $DB_PRODUCT Internal Database version $DB_VERSION_STR found."
  
    else
      # detect very old database version 3.51 3.50 3.10 3.00 2.55
      if [ -d "$OMNI_CDB" ]; then
        if [ -f "$DB_VERS" ]; then
          DB_PRODUCT=`awk '{sub(" +A.[0-9].*",""); sub("HP +",""); sub("(OpenView|StorageWorks) +",""); sub(" +software",""); sub("OmniBack II", "Data Protector"); print}' $DB_VERS`
          DB_VERSION_STR=`awk '{sub("HP OpenView OmniBack II +",""); sub("HP( StorageWorks | )Application Recovery Manager +",""); print}' $DB_VERS`
          DB_VERSION=${DB_VERSION_STR#A.}; DB_VERSION=${DB_VERSION#0}; DB_VERSION=${DB_VERSION%.\%*}
          echo "NOTE:    $DB_PRODUCT Internal Database version $DB_VERSION_STR found."
  
        else
          DB_PRODUCT=OmniBack
          DB_VERSION=OLD
        fi

      else
        DB_PRODUCT=""
        DB_VERSION=0
      fi
    fi
  fi
fi

case $DB_VERSION in
  0)
    # OK (fresh install)
    ;;

  6.11 | 6.20 | 6.21 | 7.00)
    # OK (Velocis upgrade is needed for 6.11, 6.20 and 7.00)
    #Check that Velocis database is exported
    export_ok=`grep "STATUS=OK" /var/opt/omni/server/exported/omnimigrate_chk 2>/dev/null`
    if [  x"$export_ok" = x"" ]; then
      echo "  Setup cannot continue, old database not exported."
      echo "  Refer to documentation for upgrade procedure."
      exitval=$EXIT_FAILED
      exit $exitval
    fi
  
    ;;

  8.00 | 8.10 | 9.00 | 34.01 | 35.01 | 99.01 | 99.02)
    #Nothing to be done
    ;;

  *)
    # Unsupported version
    echo "  Unknown/unsupported $DB_PRODUCT Internal Database version $DB_VERSION_STR detected."
    echo "  Data Protector software can not function properly with this"
    echo "  database. Refer to documentation for manual upgrade procedure."

    exitval=$EXIT_FAILED
    exit $exitval
    ;;
esac

exit 0