#!/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



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()
{
FileVal=`wc -c /etc/opt/OV/share/conf/ovweb.conf | tr -s ' ' | cut -f2 -d' '`
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   
}

check_root
check_NNM
check_Browser


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 -n "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 -n "Do you want to change the installation path (y/n):"
    read choice1
    echo ""
     if [ "$choice1" = y ] || [ "$choice1" = Y ]; then
       echo "" 
       echo "Please enter the path to install Cyclades NNM Integration Manager"
       echo -n "{Example:/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
   
 
    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 10
ovstart ovwdb
sleep 20
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 ""

