#!/bin/sh
echo ""
echo "*******************************************************"
echo "*    Cyclades NNM Integration Manager Installation    *"
echo "*******************************************************"
echo ""
PATH=$PATH:/opt/OV/bin:/usr/bin
export PATH

. /opt/OV/bin/ov.envvars.sh
crle -u -l /usr/lib -l /usr/local/lib -l /opt/OV/lib

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
export LD_LIBRARY_PATH

unset check_root
check_root()
{
    IM=`id | sed -e 's,uid=\([0-9][0-9]*\).*,\1,'`

    if [ $IM -ne 0 ]; 
    then
        echo "You must be root to run the installation"
        exit 1
    fi
    return 0
}

unset check_NNM
check_NNM()
{
 nnmpath=/opt/OV/bin/ovstart
 ls $nnmpath
 if [ $? -eq 1 ]
 then
  echo "Installation Aborted"
  echo "******NNM not found*******"
  exit 1 
fi 
}

unset check_Browser
check_Browser()
{
fVal="/etc/opt/OV/share/conf/ovweb.conf"
fileval=`wc -c /etc/opt/OV/share/conf/ovweb.conf | tr -s ' ' | cut -f2 -d' '`
if [ -f $fVal ]
then
if [ $fileval -lt 9 ] 
then
echo ""
echo ""
echo "************************************************************"
echo "*                     Warning!                             *" 
echo "*    OV Browser configuration was not found ......         *"
echo "* Please update ovweb.conf in /etc/opt/OV/share/conf       *" 
echo "*                                                          *"
echo "************************************************************"
fi
else
echo "************************************************************"
echo "*                    Warning!                              *"
echo "* OV Browser configuration file ovweb.conf was not found   *"
echo "*                                                          *"
echo "************************************************************"
fi
}

unset check_Lib
check_Lib()
{
LibTest=`pkginfo | grep SMCgcc`
LibTest1=`pkginfo | grep SMClibgcc`
if [ "$LibTest" = "" ]
then
 if [ "$LibTest1" = "" ]
 then
echo "***********************************************************"
echo "*                        Warning!                         *"
echo "*              SMClibgcc library is not found             *"
echo "*                                                         *"
echo "***********************************************************"
echo ""
echo "Do you want to install the library file{y/n): "
read choice
if [ "$choice" = y ] || [ "$choice" = Y ]; then
 echo ""
 echo "************************************************************************************"
 echo "*                                                                                  *"
 echo "*      libgcc3.3-sol9-sparc-local.gz package comes from sunfreeware.com            *"  
 echo "*             Source file for libgcc3.3 is on sunfreeware.com                      *"
 echo "* license file for libgcc3.3-sol9-sparc-local.gz package is in installation folder *" 
 echo "*                                                                                  *"
 echo "************************************************************************************"
 echo ""
 echo "Extracting the file ........"
 gunzip libgcc-3.4.1-sol9-sparc-local.gz
 echo "Adding the package........."
 pkgadd -d libgcc-3.4.1-sol9-sparc-local
 echo "..........................."
else
 echo "Installation aborted by user..."
 exit 1
fi 
fi
fi
}

check_root
check_NNM
check_Browser
check_Lib

MIBS=$OV_SNMP_MIBS
REG=$OV_REGISTRATION
LRF=$OV_LRF
FIELDS=$OV_FIELDS
BIN=$OV_BIN
TEMP=temp
TEMP_BINDIR=$TEMP/bin
TEMP_LRFDIR=$TEMP/lrf
#T_SRC_DIR=$0
SRC_DIR=`dirname $0`
#T="/install"
#SRC_DIR=${T_SRC_DIR/%$T/''} 

#echo $SRC_DIR

 
echo ""
echo "************************************************************"
echo "* Warning!                                                 *"
echo "* The Netmon Service Will Be Stopped!                      *"
echo "* Please Exit All OVw Sessions Before Install/Un-Install   *"
echo "************************************************************"
echo ""
echo "Do You Want To Continue (y/n):"
read choice

if [ "$choice" = y ] || [ "$choice" = Y ]; then
    echo ""
    echo "*********************************************************"
    echo "*     Default Installation path is /opt/Cyclades/IM     *"
    echo "*********************************************************"
    echo ""
    echo "Do you want change the installation path (y/n):"
    read choice1
    echo ""
     if [ "$choice1" = y ] || [ "$choice1" = Y ]; then
       echo "" 
       echo "Please enter the path to install Cyclades APM Integration Manager"
       echo "{Example:/export/home/jack}:"
         read APMPATH
           if [ "$APMPATH" = "" ] || [ "$APMPATH" = " " ]; then
             echo ""
             echo "***** Invalid path Aborting the Installation *****"
             exit 1
           else 
             mkdir -p $APMPATH/Cyclades/IM > /dev/null 2>/dev/null   
             APMPATH=$APMPATH/Cyclades/IM 
           fi
      else
         mkdir -p /opt/Cyclades/IM > /dev/null 2>/dev/null
         APMPATH=/opt/Cyclades/IM
      fi
   
    echo ""
    echo "Base path: $APMPATH" 
    
    APM_TEMP=`echo "$APMPATH" | sed s/'\/'/'\\\\\\/'/g` 
 
    sed s/__APM_IM_INSTALL__/$APM_TEMP/ $SRC_DIR/bin/PxIMEvents > $SRC_DIR/$TEMP_BINDIR/PxIMEvents
    sed s/__APM_IM_INSTALL__/$APM_TEMP/ $SRC_DIR/lrf/ovspmd_px.lrf > $SRC_DIR/$TEMP_LRFDIR/ovspmd_px.lrf
    
    cp $SRC_DIR/bin/* $BIN	
    cp $SRC_DIR/license/* $APMPATH     
    cp $SRC_DIR/arf/* $REG/C
    cp $SRC_DIR/$TEMP_LRFDIR/* $LRF
    cp $SRC_DIR/fields/* $FIELDS/C
    echo $APMPATH > /var/opt/PxIMinstallPath
    cp $SRC_DIR/cyclades/* $APMPATH

echo "Checking OV Services......."

ovstop  netmon
sleep 5
ovstart ovwdb
sleep 10
ovw -fields >ovdbfields.load
ovstart ovuispmd
sleep 10

echo "Registering Files.......... "
ovstart  netmon
sleep 10
ovaddobj $LRF/ovspmd_px.lrf
sleep 5

echo "Starting Services......."
ovstart ovspmd_px
sleep 5
echo "Installation Complete!"

else

echo "Installation Aborted by user!"
fi
echo ""

