#!/bin/sh
# /***********************************************/
# /* Copyright (C) 2007, 2008, Hitachi,Ltd.      */
# /***********************************************/
#   Hitachi Storage Navigator Modular 2
#   Version 4.30
#         (2008.06.10)

PACKAGE_NAME='HSNM2-0520-L-GUI-P01.rpm'
PLATFORM='linux.x86'

LANG=C; export LANG
unset DISPLAY
umask 022
set -e

cd "`dirname "$0"`"

MSG_DMEI000012='The processor type of this computer is not supported.'
MSG_DMEI000016='Unauthorized user. The root user is allowed to execute the operation.'
MSG_DMEI000019='Installer runs doubly or Installer had halted abnormally and temporary files remained.'
MSG_DMEI000021='Unable to detect the directory where HiCommand Suite Common Component is installed. '
MSG_DMEI000022='Unable to detect the "hcmdssrv" command.'
MSG_DMEI000031='Disk Free Space is not enough.'
MSG_DMEI000032='IP address or host name has already been set.'
MSG_DMEI000033='Port number has already been set.'
MSG_DMEI000034='IP address or host name is not specified.'
MSG_DMEI000035='Specified port number is not decimal in the range from 0 to 65535.'
MSG_DMEI000101='Unable to continue installation, as some of HiCommand Suite Common Components daemons are not stopped. Please refer to manuals of applications using HiCommand Suite Common Components to stop all of the daemons of common components.'
MSG_DMEI000102='Unable to get the status of HiCommand Suite Common Component daemons. Some applications using HiCommand Suite Common Component is using those daemons. Please stop those daemons according to the manual of those applications.'
MSG_DMEI000199='An undefined error occurred.'
MSG_DMEI000202='Unable to continue installation since there are HiCommand Suite Common Components configured as active clustering. Please refer to manuals of applications using HiCommand Suite Common Components to configure all of common components as non clustering. If no other applications are used, please release clustering configuration by "hcmdsdbclustersetup -removecluster" command.'
MSG_DMEI000203='Unable to continue installation since there are HiCommand Suite Common Components configured as passive clustering. Please refer to manuals of applications using HiCommand Suite Common Components to configure all of common components as non clustering. If no other applications are used, please release clustering configuration by "hcmdsdbclustersetup -removecluster" command.'
MSG_DMEI000298='Failed to get the status of the Database. Stop all applications and services (such as security, process monitoring) and retry to install.'
MSG_DMEI000299='An undefined error occurred.'
MSG_DMEI000398='An attempt to stop the HiCommand Suite Common Component daemons has failed. Please wait a few minutes, and then try again.'
MSG_DMEI000399='An undefined error occurred.'
MSG_DMEI000401='An attempt to stop the HiCommand Suite Common Component daemons has been time out. Please wait a few minutes, and then try again.'
MSG_DMEI000498='Unable to get the status of HiCommand Suite Common Component daemons.'
MSG_DMEI000499='An undefined error occurred.'
ASK_STOP_MSG1='The daemons of HiCommand Suite Common Component are running. These daemons will be stopped during installation if you choose to continue.'
ASK_STOP_MSG2='Are you sure to stop the daemons? [Y/N]'
WAIT_STOP_MSG='Now stopping the HiCommand Suite Common Component daemons. Please wait.'

error_occured(){
  dmei="${1}"
  eval "echo \"ERROR[${dmei}]: \${MSG_${dmei}}\"" 1>&2
  exit 1
}

check_platform(){
  case "`uname -sm`" in
    Linux\ i?86)  current_platform='linux.x86';;
    SunOS\ sun4*) current_platform='solaris.sparc';;
    *) error_occured 'DMEI000012';;
  esac
  if [ "x${current_platform}" != "x${PLATFORM}" ]; then
    error_occured 'DMEI000012'
  fi
}

get_hbase_installed_path(){
  case "${PLATFORM}" in
    linux.x86)
      if [ -f '/etc/.hitachi/HBase/hcmdsPath' ]; then
        hicpath="`sed -ne 's/^install\.path=//p' '/etc/.hitachi/HBase/hcmdsPath' 2> /dev/null`" \
            || error_occured 'DMEI000021'
        [ -x "${hicpath}/Base/bin/hcmdssrv" ] || error_occured 'DMEI000022'
        echo "${hicpath}"
      fi
      ;;
    solaris.sparc)
      if [ -f '/opt/HiCommand/Base/bin/hcmdssrv' ]; then
        hicpath='/opt/HiCommand'
        [ -x "${hicpath}/Base/bin/hcmdssrv" ] || error_occured 'DMEI000022'
        echo "${hicpath}"
      fi
      ;;
  esac
}

check_database_mode(){
  ret=0
  "${HIC_PATH}/Base/bin/hcmdsdbmode" -nolog || ret=$?
  case "${ret}" in
    0)   echo "HiRDB is not installed.";;
    1)   echo "HiRDB runs stand-alone mode.";;
    2)   error_occured 'DMEI000202';;
    3)   error_occured 'DMEI000203';;
    255) error_occured 'DMEI000298';;
    *)   error_occured 'DMEI000299';;
  esac
}

check_stop_daemon1(){
  ret=0
  "${HIC_PATH}/Base/bin/hcmdssrv" -status -nolog || ret=$?
  case $ret in
    0) return 0;;
    1) return 1;;
    255) error_occured 'DMEI000102';;
    *) error_occured 'DMEI000199';;
  esac
}

stop_daemon(){
  ret=0
  "${HIC_PATH}/Base/bin/hcmdssrv" -stop -nolog || ret=$?
  case $ret in
    0) return 0;;
    1) return 0;;
    255) error_occured 'DMEI000398';;
    *) error_occured 'DMEI000399';;
  esac
}

check_stop_daemon2(){
  ret=0
  "${HIC_PATH}/Base/bin/hcmdssrv" -status -nolog || ret=$?
  case $ret in
    0) return 0;;
    1) return 1;;
    255) error_occured 'DMEI000498';;
    *) error_occured 'DMEI000499';;
  esac
}

ask_stop_daemon(){
  if tty > /dev/null; then
    echo "${ASK_STOP_MSG1}"
    echo "${ASK_STOP_MSG2}"
    read yorn
    case "${yorn}" in
      [yY]|[yY][eE][sS]) return 0;;
      *) error_occured 'DMEI000101';;
    esac
  else
    error_occured 'DMEI000101'
  fi
}

wait_stop_daemon(){
  echo "${WAIT_STOP_MSG}"
  n=120
  while [ $n -gt 0 ]; do
    if check_stop_daemon2; then
      return 0
    fi
    wait 5
    n=`expr $n - 1`
  done
  error_occured 'DMEI000401'
}

check_daemons_status(){
  if [ "x${HIC_PATH}" != 'x' ]; then
    echo 'HBase is installed.'
    check_database_mode
    if check_stop_daemon1; then
      echo 'HBase is stopped'
    else
      echo 'HBase is running.'
      if ask_stop_daemon; then
        echo 'Stop HBase.'
        stop_daemon
        wait_stop_daemon
      else
        exit 1
      fi
    fi
    SNM2SRV_INIT_SCRIPT="${SNM_PATH}/StorageNavigatorModular/server/snm2srv"
    if [ -x "${SNM2SRV_INIT_SCRIPT}" ]; then
      echo "Check SNM2Server..."
      "${SNM2SRV_INIT_SCRIPT}" stop ||:
    fi
  else
    echo 'HBase is not installed.'
  fi
}

print_help(){
  installer_name="$1"
  echo "Hitachi Storage Navigator Modular 2 Installer"
  echo "Usage:"
  echo "  `basename "${installer_name}"` [-h] -a addr -p port"
  echo "     -h      : Print help."
  echo "     -a addr : Specify IP address or host name. (Required when initial install)"
  echo "     -p port : Specify port number for access SNM2 Server. (Defalut: 1099)"
  exit 0
}

check_diskfreespace(){
  directory_path="$1"
  required_space_kb="$2"
  while [ ! -d "${directory_path}" ]; do
    directory_path="`dirname "${directory_path}"`"
  done 

  case "${PLATFORM}" in
    linux.x86)     df_option='-Pk';;
    solaris.sparc) df_option='-k';;
  esac
  diskfree="`df "${df_option}" "${directory_path}" | awk 'NR==2{print $4}'`"
  if [ ! "${diskfree}" -gt "${required_space_kb}" ]; then
    echo "${diskfree} kByte avaiable in directory '${directory_path}'. HSNM2 requires ${required_space_kb} kByte." 1>&2
    error_occured 'DMEI000031'
  fi 
}

check_parameter(){
  SNM2SRV_PROPERTIES="${SNM_PATH}/StorageNavigatorModular/server/snmserver.properties"
  if [ -r "$SNM2SRV_PROPERTIES" ]; then
    ADDR="`sed -ne 's/^jp\\.co\\.hitachi\\.str\\.diskarray\\.rmi\\.hostname=//p' "${SNM2SRV_PROPERTIES}"`"
    PORT="`sed -ne 's/^jp\\.co\\.hitachi\\.str\\.diskarray\\.rmi\\.port=//p'     "${SNM2SRV_PROPERTIES}"`"
    if [ -n "$HSNM2_INST_RMI_ADDR" ] && [ "x${HSNM2_INST_RMI_ADDR}" != "x${ADDR}" ]; then
      error_occured 'DMEI000032'
    fi
    if [ -n "$HSNM2_INST_RMI_PORT" ] && [ "x${HSNM2_INST_RMI_PORT}" != "x${PORT}" ]; then
      error_occured 'DMEI000033'
    fi
  else
    if [ ! -n "$HSNM2_INST_RMI_ADDR" ]; then
      error_occured 'DMEI000034'
    fi
    if [ -n "$HSNM2_INST_RMI_PORT" ]; then
      if [ ! \( 0 -le "$HSNM2_INST_RMI_PORT" -a "$HSNM2_INST_RMI_PORT" -lt 65536 \) ]; then
        error_occured 'DMEI000035'
      fi
    fi
  fi
}

check_root_user(){
  uid="`expr "\`id\`" : '\\(uid=[0-9]*\\)[^0-9]'`"
  if [ "x${uid}" != 'xuid=0' ]; then
    error_occured 'DMEI000016'
  fi
}

check_preinstalled_package_solaris(){
  if pkginfo 'HSNM2' > /dev/null 2> /dev/null; then
    OLD_VERSION=`pkginfo -l 'HSNM2' | sed -ne 's/^ *VERSION: *//p'`
    NEW_VERSION=`pkginfo -l -d "${PACKAGE_NAME}" | sed -ne 's/^ *VERSION: *//p'`
    if awk "BEGIN{if(\"${NEW_VERSION}\">\"${OLD_VERSION}\") exit 0; else exit 1;}"; then
      echo "Update HSNM2 [${OLD_VERSION}]->[${NEW_VERSION}] ..."
      pkgrm -n -a "${TMP_PATH}/pkg_admin" 'HSNM2'
    else
      echo "The newer or same version of HSNM2 is already installed." 1>&2
      echo "Now installing: $NEW_VERSION" 1>&2
      echo "Already installed: $OLD_VERSION" 1>&2
      exit 1
    fi
  fi
}

keep_update_compatibility(){
  SNM2SEV_JRE_MAN_JP_DIR="${SNM_PATH}/StorageNavigatorModular/server/jre1.4.2/man/ja"
  if [ -d "${SNM2SEV_JRE_MAN_JP_DIR}" ]; then
    rm -rf "${SNM2SEV_JRE_MAN_JP_DIR}"
  fi
  HIC_BASE_TMP_DIR="${HIC_PATH}/Base/tmp"
  V_HIC_BASE_TMP_DIR="/var${HIC_BASE_TMP_DIR}"
  if [ -d "${HIC_BASE_TMP_DIR}/SNM2" ]; then
    mkdir -p "${V_HIC_BASE_TMP_DIR}"
    mv -f "${HIC_BASE_TMP_DIR}/SNM2" "${V_HIC_BASE_TMP_DIR}"
    rmdir "${HIC_BASE_TMP_DIR}" ||:
  fi
  DVM_TREEIF_XML="${V_HIC_BASE_TMP_DIR}/SNM2/DvM_treeIF.xml"
  if [ -f "${DVM_TREEIF_XML}" ]; then
    rm -f "${DVM_TREEIF_XML}"
  fi
}

# =====================================================================

HSNM2_INST_RMI_ADDR=''
HSNM2_INST_RMI_PORT=''
while getopts 'a:p:h' optflag; do
  case "${optflag}" in
    a) HSNM2_INST_RMI_ADDR="${OPTARG}";;
    p) HSNM2_INST_RMI_PORT="${OPTARG}";;
    h) print_help "$0";;
    *) error_occured 'DMEI000012';;
  esac
done

if [ ! -f "${PACKAGE_NAME}" ]; then
  echo "Can not find the package file '${PACKAGE_NAME}'" 1>&2; exit 1
fi

# - - - - - - Create Temporary Directory

TMP_PATH="/tmp/.hsnm2_install_`date '+%Y%m%d%H%M%S'`_$$"
TMP_PATH_LINK="/tmp/.hsnm2_install"
INST_SCRIPT_PID_FNAME='/tmp/hsnm2_installer.pid'

test ! -d "${TMP_PATH}"
if [ -h "${TMP_PATH_LINK}" ]; then
  echo "Symbolic link '${TMP_PATH_LINK}' already exists." 1>&2
  error_occured 'DMEI000019'
fi
if [ -f "${INST_SCRIPT_PID_FNAME}" ]; then
  echo "File '${INST_SCRIPT_PID_FNAME}' already exists." 1>&2
  error_occured 'DMEI000019'
fi

delete_tmpdir(){
  set +e
  cd /
  rm -fr "${TMP_PATH}"
  rm -f "${TMP_PATH_LINK}"
  rm -f "${INST_SCRIPT_PID_FNAME}"
}
trap 'delete_tmpdir' 0 
trap 'delete_tmpdir; exit 1' 1 2 3 15 

mkdir "${TMP_PATH}"
ln -s "${TMP_PATH}" "${TMP_PATH_LINK}"
echo "$$" > "${INST_SCRIPT_PID_FNAME}"

# - - - - - - Check Enviroment

check_platform
HIC_PATH="`get_hbase_installed_path`"
SNM_PATH='/opt/HiCommand'
check_root_user
check_parameter

# - - - - - - Install

case "${PLATFORM}" in
  linux.x86)
    check_diskfreespace '/opt/HiCommand' 1500000
    check_daemons_status
    keep_update_compatibility
    export HSNM2_INST_RMI_ADDR
    export HSNM2_INST_RMI_PORT
    __EXECUTE_BY_INSTALL_HSNM2_SH__='__EXECUTE_BY_INSTALL_HSNM2_SH__'
    export __EXECUTE_BY_INSTALL_HSNM2_SH__
    rpm --nodeps -Uhv "${PACKAGE_NAME}"
    ;;
  solaris.sparc)
    check_diskfreespace '/opt/HiCommand' 1500000
    check_diskfreespace '/var/opt/HiCommand' 1000000 
    check_diskfreespace '/var/tmp' 1000000
    check_daemons_status
    { echo 'action=nocheck'; echo 'mail='; } > "${TMP_PATH}/pkg_admin"
    check_preinstalled_package_solaris
    keep_update_compatibility
    pkgadd -n -a "${TMP_PATH}/pkg_admin" -d "${PACKAGE_NAME}" 'HSNM2'
    __EXECUTE_BY_PACKAGE_MANAGER__='__EXECUTE_BY_PACKAGE_MANAGER__'
    export __EXECUTE_BY_PACKAGE_MANAGER__
    "${SNM_PATH}/StorageNavigatorModular/install/postinstall.sh" -a "${HSNM2_INST_RMI_ADDR}" -p "${HSNM2_INST_RMI_PORT}"
    ;;
esac

exit 0
