#!/bin/sh 
#
# (c) Copyright Cisco Corporation 2011. All rights reserved.
#
# Cisco 10G Plug-in Install script
#
# Please only modify the User Configuration Section.
#

################################################################

# SRCDIR = the location where the Cisco 10G Plugin RPMs can be
#     found.  If the RPMs are in the same directory as
#     this script, leave SRCDIR as is.
#
SRCDIR=`pwd`
################################################################

SERVER_SW_NAME="cisco_10G_plugin_server-v1.0.13-linux.i386.rpm"
SERVER_PACAGE='cisco_10G_plugin_server-v1.0.13-linux'
TS_SERVER_PACAGE='cisco_10G_plugin_server'

install_server()
{
  returncode="0"
  defaultInstallDir="/opt/IBM/director"
  installDir="$defaultInstallDir"

  if [ -f "/etc/ibm/director/twgserver/twgserver" ]; then
   . /etc/ibm/director/twgserver/twgserver
    relocatePath="$TWG_ROOTDIR"
    installDir="$relocatePath"
  else
    echo -n "IBM Director Server is not installed.Pleae Install IBM Director Server then install the plug-in."	
  	     exit 0
  	   
 fi

  SW_PACKAGE=`rpm -q $SERVER_PACAGE 2>&1`
  TS_SW_PACKAGE=`rpm -q $TS_SERVER_PACAGE 2>&1`

  if [ "$SW_PACKAGE" = "package $SERVER_PACAGE is not installed" ]; then
     rpmOpts="-Uvh"
  else
     rpmOpts="-Uvh --force"
  fi
  
  if [ "$TS_SW_PACKAGE" != "package $TS_SERVER_PACAGE is not installed" ]; then
    rpmOpts="-Uvh --force" 
  fi

  if [ "$relocatePath" ] && \
     [ "$relocatePath" != "$defaultInstallDir" ]; then
     rpmOpts="$rpmOpts --prefix $relocatePath"
  fi

#echo "rpmOpts $rpmOpts"
$installDir/bin/smstatus
           echo ""
           echo "install Cisco 10G  plug-in in $installDir"
           echo "$rpmOpts"
           rpm $rpmOpts $SRCDIR/$SERVER_SW_NAME 
           echo ""
           returncode=$?

  #####################################################
  #            Done - give finish message             #
  #####################################################
  if [ "$returncode" = "0" ]; then
     echo ""
     echo "Installation is successful."
  else
     echo ""
     echo "Installation has failed."
  fi

exit 0

}

erase_server()
{
  returncode="0"
  defaultInstallDir="/opt/IBM/director"
  installDir="$defaultInstallDir"

if [ -f "/etc/ibm/director/twgserver/twgserver" ]; then
   . /etc/ibm/director/twgserver/twgserver
   relocatePath="$TWG_ROOTDIR"
   installDir="$relocatePath"
fi

  SW_PACKAGE=`rpm -q $SERVER_PACAGE 2>&1`
  

  if [ "$SW_PACKAGE" = "package $SERVER_PACAGE is not installed" ]; then
    # package is not pre-installed
    echo "Package $SERVER_PACAGE is not currently installed"
    echo "Skipping erase of $SERVER_PACAGE"
    exit 0

  else
    # package is installed
    $installDir/bin/smstatus
           echo ""
           echo "Uninstall Cisco 10G plug-in in $defaultInstallDir"
           echo "Erasing $SW_PACKAGE"
           echo ""
           rpm -e $SERVER_PACAGE
           returncode=$?
  fi

  if [ "$returncode" = "0" ]; then
     echo ""
     echo "Uninstallation is successful."
  else
     echo ""
     echo "Uninstallation has failed."
  fi

exit 0
}

usage() 
{
  cat << EOF
Usage: $name [--erase-server] [--server] 
  --erase-server Uninstall cisco_10G_plugin_server rpm.
  --server Install cisco_10G_plugin_server rpm.
EOF
exit 0
}

ERASESERVER=0
SERVER=0

  case "$1" in
  --erase-server)
    ERASESERVER=1
    break
    ;;
  --server)
    SERVER=1
    break
    ;;
  esac


if [ "$ERASESERVER" = "1" ]; then
  erase_server
fi

if [ "$SERVER" = "1" ]; then
  install_server
fi

usage
exit 0

