#! /bin/ksh
# installsrm - SRM Unix Agent installation script
# Copyright (c) 2000, HighGround Systems Inc.
# All rights reserved

# Change History:
# ===============
# 
# 01/10/02 CLM - New pkgadd stuff for solaris.
# 05/04/01 JAW - Re-enabled Solaris 2.5.1 support (Bugz 10151).
# 06/15/00 GKK - removed support for AIX 4.1 and Solaris 2.5.1
#                (commented out)
# 04/14/00 EWP - Removed "^M" from every line.
#
#
rollback() {
  echo "Installation terminated by user"
  if [ ${AGENT_PATH_EXISTED} -eq 0 ]; then
      echo ${AGENT_PATH} "is deleted"
      cd /; rm -Rf ${AGENT_PATH}
  fi
  exit 255
}
#--------------------------------------------------------------------
AGENT_PATH=/usr/srmagent
AGENT_NAME=${AGENT_PATH}/srmagent
TMP_PATH=/var/tmp
FILE_LIST="srmagent registersrm removesrm revertsrm"
SYSTEM_TYPE=`uname -s`
OS_REL=`uname -r`
CURRENT_DIR=`pwd`
typeset -lL1 CHOICE


if [ -d ${AGENT_PATH} ]; then
    AGENT_PATH_EXISTED=1
else
    AGENT_PATH_EXISTED=0
fi

case ${SYSTEM_TYPE} in
	"AIX" )
		AWK="awk"
                ECHO="echo"
                OS_VER=`uname -v`
                WHOAMI="whoami"
                if [ ${OS_VER} -lt 4 ]; then
                    echo "AIX version ${OS_VER} not supported for SRM UNIX agent"
                    exit
                fi
                if [ ${OS_REL} -eq 1 ]; then
                    echo "AIX version 4.${OS_REL} not supported for SRM UNIX agent"
                    exit
# for AIX4.1 - used to install ...
#                   PKGFILE="srmaix_s.tar.Z"
                else
                    PKGFILE="srmaix_l.tar.Z"
                fi;;
	"SunOS")
		AWK="/usr/bin/nawk"
                ECHO="echo"
                WHOAMI="/usr/ucb/whoami"
		PKGFILE="solarisagent.tar.Z";;		
	"HP-UX")
		AWK="/usr/bin/awk"
                ECHO="echo"
                WHOAMI="whoami"
                PKGFILE="srmhpux_l.tar.Z";;
        "Linux")
                AWK="/bin/awk"
                ECHO="echo -e"
                WHOAMI="whoami"
                MACHINE=`uname -m`
                case "${MACHINE}" in
                  i[3-9]86 ) PKGFILE="srmlinux.tar.Z";;
                  *)         echo "Unsupported hardware platform ${MACHINE}"; exit;;
                esac;;
	"OSF1")
		SYSTEM_TYPE="Compaq Tru64 UNIX"
		AWK="/usr/bin/awk"
                ECHO="echo"
                WHOAMI="whoami"
                PKGFILE="srmtru64_l.tar.Z";;
	*)
		echo "Unknown system type: ${SYSTEM_TYPE}"
                exit 255;;
esac

# Check if the user is root
if [ `${WHOAMI}` != "root" ]; then
   echo "You must be logged in as 'root' to run this script"
   exit 255
fi

DESCRIPTION="
##********************************************************************************
##  ***********
##  installsrm
##  ***********
##  
##  Installs files for Storage Resource Manager UNIX agent for ${SYSTEM_TYPE}
##  and updates necessary system and product configuration files
##  
##********************************************************************************
"

echo "${DESCRIPTION}" | grep "^##" | sed -e s/^##//

if [ ! -f ${PKGFILE} ]; then
    echo "File ${PKGFILE} not found in the current directory."
    echo "installsrm must be run from the directory which includes"
    echo "agent package file ${PKGFILE}"
    echo "Terminating installation"
    exit 255
fi

trap "rollback" 2

if [ "${SYSTEM_TYPE}" != "SunOS" ]; then
	if [ -f "${AGENT_NAME}" ]; then
  		VER=`${AGENT_NAME} -ver 2>/dev/null`
  		if [ $? -eq 0 ]; then
    			echo "Version ${VER} of the UNIX SRM Agent is currently installed"
    			echo "Do you want to retain this version in a local compressed TAR file"
    			${ECHO} "in case you need to return to it (y/n)?\c"
    			read CHOICE
    			if [ "${CHOICE}x" = "yx" ]; then
        			mkdir "${AGENT_PATH}"/backup 2>/dev/null
        			AR_NAME="${AGENT_PATH}/backup/srmver${VER}.tar.Z"
        			cd "${AGENT_PATH}"
        			tar cf - "${FILE_LIST}" | compress >"${AR_NAME}"
        			if [ $? -eq 0 ]; then
            				echo "Backup archive stored at ${AR_NAME}"
        			else
            				echo "Backup archive creation at ${AR_NAME} failed"
            				${ECHO} "Do you want to continue with the installation (y/n)?\c"
            				read CHOICE
            				if [ ${CHOICE} != "y" ]; then
               					echo "Terminating installation"
               					exit 255
            				fi
        			fi
    			fi
  		fi
	fi
else
  # SOLARIS INSTALL    
  # cleanup after any old installs.
  rm -rf "${TMP_PATH}"/tmp_srm_unpackdir_0000 2>/dev/null

  echo "Unpacking installation files"
  cd "${TMP_PATH}" 2>/dev/null
  if [ $? -ne 0 ]; then
	echo "Error accessing the default temporary directory: ${TMP_PATH} " 
	exit 255
  fi

  mkdir "${TMP_PATH}"/tmp_srm_unpackdir_0000
  if [ $? -ne 0 ]; then
	echo "Error creating a temporary directory in ${TMP_PATH}" 
	exit 255
  fi

  cd "${TMP_PATH}"/tmp_srm_unpackdir_0000
  uncompress -c "${CURRENT_DIR}"/"${PKGFILE}" | tar xef - 

  echo "Starting pkgadd"
  pkgadd -d "${TMP_PATH}"/tmp_srm_unpackdir_0000/pkg_out SUNWsrmag
  cd ..
  echo "Removing temporary installation files"
  rm -rf "${TMP_PATH}"/tmp_srm_unpackdir_0000 2>/dev/null
  if [ $? -ne 0 ]; then
	echo "Error while deleting temporary files located in ${TMP_PATH}/tmp_srm_unpackdir_0000" 
  fi

  echo "End of installsrm for Solaris"
  exit 0
fi

# Beyond here for the non-Solaris UNIX installations
# OK, extract the stuff
cd /
zcat "${CURRENT_DIR}"/"${PKGFILE}" | tar xfo -
if [ $? -ne 0 ]; then
   echo "Installation failed"
   exit 255
fi

# Force userid to root on files
# especially good since Linux and GNU "tar" implementations don't support traditional tar "o" option
chown -f root:root "${AGENT_PATH}" 2>/dev/null
chown -f root:root "${AGENT_PATH}/*" 2>/dev/null

mkdir "${AGENT_PATH}"/trace 2>/dev/null
mkdir "${AGENT_PATH}"/dump 2>/dev/null
VER=`${AGENT_NAME} -ver`
if [ $? -ne 0 ]; then
   echo "${AGENT_NAME} executable inoperable"
   exit $?
fi
echo "Files for version ${VER} installed"
cd ${AGENT_PATH}
ksh registersrm -a

echo "End of installsrm"

exit $?
