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

# Change History:
# ===============
# 
# 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
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"
                case "${OS_REL}" in
# Solaris 2.5.1 used to install srmsol_s.tar.Z
# The following line was uncommented to re-enable Solaris 2.5.1 support (Bugz 10151).
# JAW, 5/4/01
                  5.5* )     PKGFILE="srmsol_s.tar.Z";;
                  5.[6-9]* ) PKGFILE="srmsol_l.tar.Z";;
                  *)         echo "SunOS version ${OS_REL} not supported for SRM UNIX agent"; exit;;
                esac;;
	"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 [ -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} = 'y' ]; 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
# 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}
sh registersrm -a

echo "End of installsrm"

exit $?
