#!/bin/sh
#
# $Id: //depot/prod/smis/Rsmis5.2.4x/build/install_smisprovider#2 $
#
# Copyright (c) 2008 NetApp, Inc.
# All rights reserved.
#

# NOTE: This script must be kept as 100% generic and portable as
# possible across *all* Unixes. Since we can't test on all Unixes,
# please be very careful. Try to assume *only* a very vanilla bourne
# shell and vanilla /bin utilities.
#

# Die on any failure.
set -e

MYNAME=install_smisprovider
EULA=NetAppSMISProviderEULA
VERSION=5.2.4
TEMPDIR=smisprovider*$VERSION;
BASEDIR=/usr;

USAGE="\
usage: $MYNAME [-f] [-s] [-k]
  -f  Force reinstallation 
  -s  Keep SLP config files if doing a reinstallation. 
  -k  (Keep) Do not remove tar_image, $MYNAME, and $EULA."

KEEP="${KEEP:=0}"
KSLP="${KSLP:=0}"
FORCE="${FORCE:=0}"

for ARG in "$@"
 do
    case "$ARG" in
      -k)
          KEEP=1;
          ;;
      -s)
          KSLP=1;
          ;;
      -f)
          FORCE=1;
          ;;
      *)
          echo "$USAGE"; exit 1;
          ;;
    esac
  done

USER=`whoami`
if [ "$USER" != "root" ]; then        
	echo "Please run this program as 'root' user."
	echo "Exiting ..."
	exit
fi

# Convert destination to full path
DEST="`cd \`dirname $BASEDIR\`; pwd`/`basename $BASEDIR`";


if [ ! -d "$DEST" ]
  then echo "$MYNAME: \"$DEST\" is not a directory."; exit 1; fi

if [ ! -w "$DEST" ]
  then echo "$MYNAME: \"$DEST\" is not writable."; exit 1; fi

INSTALLDIR="$DEST/netapp/smis"
PEGASUS_INSTALLDIR="$INSTALLDIR/pegasus"

if [ -d "$PEGASUS_INSTALLDIR" ] && [ $FORCE -eq 0 ]
  then
	echo "$MYNAME: A previous installation of the NetApp SMI-S Provider is detected.";
 	echo "Please use the command '$MYNAME -f' to overwrite the existing installation.";
    exit 1
  fi

function show_eula {
  # EULA information
  response=
  until [ "$response" = "y" ]
  do
    echo
    echo -n "Please read the NetApp SMI-S Provider EULA carefully."
    echo
    echo -n " Do you accept this License Agreement?"
    echo
    echo -n " Press 'y' for yes, 'n' for no, or 'r' to read this agreement <Enter>:"
    echo
    read response
    if [ "$response" = "r" ]; then
      cat $EULA | more
      elif [ "$response" != "y" ]; then
      echo "Exiting installation."
      exit 1
    fi
  done
}

#show_eula;

UNCOMPRESS="${UNCOMPRESS:=uncompress}"
UNCOMPRESSOPTS="${UNCOMPRESSOPTS:=}"

PATH="$PATH:/bin:/usr/bin:/usr/ucb"

if [ -f tar_image.Z ]
  then
    echo "$MYNAME: $UNCOMPRESS $UNCOMPRESSOPTS tar_image.Z"
    $UNCOMPRESS $UNCOMPRESSOPTS tar_image.Z
  fi

if [ ! -f tar_image ]
  then echo "$MYNAME: no file \"tar_image\"."; exit 1; fi

here=`/bin/pwd`

TAR="${TAR:=tar}"

echo "$MYNAME: cd \"$DEST\""
cd "$DEST"

echo "$MYNAME: $TAR xvf \"$here/tar_image\""
$TAR xvf "$here/tar_image"

cd $TEMPDIR;

# Copy all libraries to the $PEGASUS_INSTALLDIR/lib directory.
mkdir -p $PEGASUS_INSTALLDIR
cp -r -f ./lib  $PEGASUS_INSTALLDIR
ln -s -f $PEGASUS_INSTALLDIR/lib/libcrypto.so.0.9.7a $PEGASUS_INSTALLDIR/lib/libcrypto.so.4

# Create a directory for trace files
mkdir -p $PEGASUS_INSTALLDIR/traces

# Copy the binaries to the $PEGASUS_INSTALLDIR/bin directory.
cp -r -f ./bin $PEGASUS_INSTALLDIR

# Copy the repository to the $PEGASUS_INSTALLDIR directory.
cp -r -f ./repository $PEGASUS_INSTALLDIR

# Change the file permissions on smis and slpdservice scripts in
# $PEGASUS_INSTALLDIR/bin directory.
chmod 744 $PEGASUS_INSTALLDIR/bin/smis
chmod 744 $PEGASUS_INSTALLDIR/bin/slpdservice

# Copy license file to $PEGASUS_INSTALLDIR directory.
cp -f ./license.txt  $PEGASUS_INSTALLDIR

# Copy net-snmp-license file to $PEGASUS_INSTALLDIR directory.
cp -f ./NET-SNMP-LICENSE  $PEGASUS_INSTALLDIR

# Copy the cimom key and certificate files.
cp -f ./cimom.key $PEGASUS_INSTALLDIR
cp -f ./cimom.cert $PEGASUS_INSTALLDIR
chmod 700 $PEGASUS_INSTALLDIR/cimom.key
chmod 755 $PEGASUS_INSTALLDIR/cimom.cert

# Move the PAM configuration file to /etc/pam.d directory.
mv -f ./wbem /etc/pam.d
chmod 0644 /etc/pam.d/wbem

# Create cimserver.passwd file if none exists
if [ ! -f $PEGASUS_INSTALLDIR/cimserver.passwd ]
  then
    touch $PEGASUS_INSTALLDIR/cimserver.passwd
  fi

# Copy agent_mof.zip file to $PEGASUS_INSTALLDIR/mof directory
mkdir -p $PEGASUS_INSTALLDIR/mof
cp ./agent_mof.zip $PEGASUS_INSTALLDIR/mof

# Copy slp config files to $PEGASUS_INSTALLDIR/cfg directory
if [ ! -d $PEGASUS_INSTALLDIR/cfg ] || [ $KSLP -eq 0 ]
  then
    mkdir -p $PEGASUS_INSTALLDIR/cfg
    cp ./cfg/slp.conf $PEGASUS_INSTALLDIR/cfg
    cp ./cfg/slp.reg $PEGASUS_INSTALLDIR/cfg
  fi

# Need to copy any custom files here

# Copy version file to $INSTALLDIR directory.
cp ./version $PEGASUS_INSTALLDIR/version.txt

#remove temporary directory
cd ..
rm -r -f $TEMPDIR

if [ $KEEP -eq 0 ]
  then
    echo "$MYNAME: cd \"$here\"; rm -f tar_image $MYNAME $EULA"
    cd $here; rm -f tar_image $MYNAME $EULA
  fi

# Start the cimserver automatically
echo "Starting NetApp SMI-S Provider..."
$PEGASUS_INSTALLDIR/bin/smis cimserver start

exit 0
