#!/bin/sh
SNTLSHK_TIMESTAMP=200912070131.00
SHK_VERSION=0131.00
CURR_SKPI_VERSION=0131


CURR_USER=`logname`
CURR_DIR=`pwd`

### Framework Variables
FRMWRK_INST_PATH=/Library/Frameworks
SNTL_COMM_PATH=/Applications/Safenet\ Sentinel
SNTL_COMM_INST_PATH="/Applications/Safenet Sentinel/Common Files"

DRIVER_INST_PATH=/System/Library/Extensions
UPRO_PCKG_PATH="/Library/Receipts/SentinelUltraPro(English).pkg"
UPRO_SRVR_PATH=/Applications/Sentinel

### Protection Variables
SKPI_INST_PATH="$SNTL_COMM_PATH/Sentinel Keys/1.3/Sentinel Keys Protection Installer"
SRVR_INST_PATH="$SNTL_COMM_PATH/Common Files/Sentinel Keys Server"

UPRO_PCKG_PATH="/Library/Receipts/SentinelUltraPro(English).pkg"
UPRO_SPI_PATH="/Library/Receipts/SentinelProtectionInstaller.pkg"
UPRO_DRIVER_PATH="/Library/Receipts/SentinelDriver1.0.0.2.pkg"
UPRO_FRMWRK_PATH="/Library/Receipts/SentinelFramework.pkg"
UPRO_KM_PATH="/Library/Receipts/SentinelKeyManager(English).pkg"

cd $FRMWRK_INST_PATH
#tar -xvf Sentinel.tar
#rm -rf Sentinel.tar
cd $CURR_DIR


# Remove PPC libs if it is an Intel machine
(sudo arch | grep "i386") 
if [ $? = 0 ] 
then
  sudo rm -rf "$SNTL_COMM_INST_PATH"/Sentinel\ Keys\ Server/PPC\ libs
  cp -R "$SNTL_COMM_INST_PATH"/Sentinel\ Keys\ Server/Intel\ libs/ "$SNTL_COMM_INST_PATH"/Sentinel\ Keys\ Server 
  sudo rm -rf "$SNTL_COMM_INST_PATH"/Sentinel\ Keys\ Server/Intel\ libs 
else
  sudo rm -rf "$SNTL_COMM_INST_PATH"/Sentinel\ Keys\ Server/Intel\ libs
  cp -R "$SNTL_COMM_INST_PATH"/Sentinel\ Keys\ Server/PPC\ libs/ "$SNTL_COMM_INST_PATH"/Sentinel\ Keys\ Server 
  sudo rm -rf "$SNTL_COMM_INST_PATH"/Sentinel\ Keys\ Server/PPC\ libs 
fi

touch -t $SNTLSHK_TIMESTAMP "$SNTL_COMM_INST_PATH"/Sentinel\ Keys\ Server/libcrypto.dylib
touch -t $SNTLSHK_TIMESTAMP "$SNTL_COMM_INST_PATH"/Sentinel\ Keys\ Server/libssl.dylib 

#if the machine is ppc, replace the Info.plist in Sentinel.kext
machine |grep ppc
if [ $? -eq 0 ]
then
  sudo rm -rf "$DRIVER_INST_PATH"/Sentinel.kext/Contents/Info.plist
  sudo cp "$SKPI_INST_PATH"/English/Info.plist "$DRIVER_INST_PATH"/Sentinel.kext/Contents/Info.plist
fi
sudo rm -rf "$SKPI_INST_PATH"/English/Info.plist

##
# Add commond in rc script to start the Keys Server 
# on System restart 
##
sudo chmod 744 /etc/rc

## Stop the Sentinel Keys Server if it is already running
sudo sh /Applications/Safenet\ Sentinel/Common\ Files/Sentinel\ Keys\ Server/loadserv.sh stop

##
# Remove the entry from rc script to start Server
##

sudo echo "/SentinelKeysServer/" > /tmp/SNTLCMD
sudo echo "d" >> /tmp/SNTLCMD; echo "w" >> /tmp/SNTLCMD; echo "q" >> /tmp/SNTLCMD

(sudo grep "SentinelKeysServer" /etc/rc && sudo ed /etc/rc < /tmp/SNTLCMD) > /dev/null 2>&1

sudo echo "/Keys/" > /tmp/SNTLCMD
sudo echo "d" >> /tmp/SNTLCMD; echo "w" >> /tmp/SNTLCMD; echo "q" >> /tmp/SNTLCMD
(sudo grep "Keys" /etc/rc && sudo ed /etc/rc < /tmp/SNTLCMD) > /dev/null 2>&1

sudo echo "/loadserv/" > /tmp/SNTLCMD
sudo echo "d" >> /tmp/SNTLCMD; echo "w" >> /tmp/SNTLCMD; echo "q" >> /tmp/SNTLCMD
(sudo grep "loadserv" /etc/rc && sudo ed /etc/rc < /tmp/SNTLCMD) > /dev/null 2>&1

sudo echo "exit 0" > /tmp/SNTLCMD
sudo echo "d" >> /tmp/SNTLCMD; echo "w" >> /tmp/SNTLCMD; echo "q" >> /tmp/SNTLCMD
(sudo grep "exit 0" /etc/rc && sudo ed /etc/rc < /tmp/SNTLCMD) > /dev/null 2>&1

# Add Command to start the Sentinel Keys Server   
echo "$" > /tmp/SNTLCMD
(echo "d" >> /tmp/SNTLCMD; echo "w" >> /tmp/SNTLCMD; echo "q" >> /tmp/SNTLCMD)
(ed /etc/rc < /tmp/SNTLCMD)

(echo "##SAFENET: Start the SentinelKeysServer") >> /etc/rc
echo "cd /Applications/Safenet\ Sentinel/Common\ Files/Sentinel\ Keys\ Server" >> /etc/rc
echo "sh loadserv.sh start" >> /etc/rc

(echo "exit 0" >> /etc/rc)
rm -rf /tmp/SNTLCMD


####Add Command to start the Sentinel Keys Server on 10.5.x and 10.6 ####
(sw_vers | egrep "10.5|10.6") >/dev/null 2>&1
 if [ $? -eq 0 ]
  then
     cd /etc
     sudo touch /etc/rc.local
     sudo grep "sh /etc/rc" /etc/rc.local
     if [ $? -ne 0 ]
     then
          sudo chmod 777 rc.local
          echo "sh /etc/rc" >> /etc/rc.local
          sudo chmod 644 rc.local
     fi
fi 

  
sudo chmod 444 /etc/rc


##
# Set file permissions for installed product
##
sudo chmod -R 775 "$SNTL_COMM_PATH"
sudo chmod 755 "$SNTL_COMM_INST_PATH"

sudo chmod -R 775 "$SRVR_INST_PATH"
sudo chgrp daemon "$SRVR_INST_PATH"
sudo chmod -R 777 "$SRVR_INST_PATH"/root
#sudo chmod 775 "$SRVR_INST_PATH"/root
#sudo chmod 775 "$SRVR_INST_PATH"/root/resources
sudo chmod -R 755 "$SKPI_INST_PATH"
sudo chmod 755 "$SKPI_INST_PATH"/English/UninstallSentinelKeysProtection.sh

#sudo chmod 754 "$SRVR_INST_PATH"/SntlKeysSrvrmac
#sudo chmod 754 "$SRVR_INST_PATH"/loadserv.sh

sudo chown -R $CURR_USER /Library/Frameworks/Sentinel.framework
sudo chown -R $CURR_USER /Library/Frameworks/SentinelKeys.framework
sudo chown -R $CURR_USER /Applications/Safenet\ Sentinel
sudo chown -R root:wheel "$DRIVER_INST_PATH"/Sentinel.kext
sudo chown -R root:wheel "$DRIVER_INST_PATH"/SNTLClassicNotSeizeDriver.kext

sudo chmod -R 755 "$DRIVER_INST_PATH"/Sentinel.kext
sudo chmod -R 755 "$DRIVER_INST_PATH"/SNTLClassicNotSeizeDriver.kext

sudo kextload /System/Library/Extensions/Sentinel.kext
sudo kextload /System/Library/Extensions/SNTLClassicNotSeizeDriver.kext

# Add Command to load Sentinel.kext in rc script 
#sudo chmod 777 /etc/rc
#(echo "##SAFENET: Load the Sentinel.kext") >> /etc/rc
#echo "sudo kextload /System/Library/Extensions/Sentinel.kext" >> /etc/rc
#(echo "exit 0" >> /etc/rc)
#sudo chmod 444 /etc/rc


### Check whether Ultrapro Framework package is installed or not
    [ -d "$UPRO_FRMWRK_PATH" ]
    if [ $? -eq 0 ]
    then
        rm -rf "/Applications/Sentinel/Sentinel Framework"
    fi

### Instance count

[ -f "/Library/Receipts/SafenetSentinelProduct.Info" ]
if [ $? -ne 0 ]
then
    echo "SentinelKeysProtection="$SHK_VERSION":Count=0"  >> /Library/Receipts/SafenetSentinelProduct.Info
else
   skpi=`grep -c SentinelKeysProtection /Library/Receipts/SafenetSentinelProduct.Info | cut -d = -f 1`
   if [ $skpi = 0 ]
   then 
     echo "SentinelKeysProtection="$SHK_VERSION":Count=0"  >> /Library/Receipts/SafenetSentinelProduct.Info
   fi   
fi

skpi_version=`grep SentinelKeysProtection /Library/Receipts/SafenetSentinelProduct.Info | cut -d = -f 2 | cut -d : -f 1`
if [ $skpi_version = $SHK_VERSION ]
then
    current_inst=`grep SentinelKeysProtection /Library/Receipts/SafenetSentinelProduct.Info | cut -d = -f 3`
    increment_inst=1
    count_skpi=`expr $current_inst + $increment_inst`

   sudo echo "/SentinelKeysProtection/" > /tmp/SNTLCMD
   sudo echo "d" >> /tmp/SNTLCMD; echo "w" >> /tmp/SNTLCMD; echo "q" >> /tmp/SNTLCMD
   (sudo grep "SentinelKeysProtection" /Library/Receipts/SafenetSentinelProduct.Info && sudo ed /Library/Receipts/SafenetSentinelProduct.Info < /tmp/SNTLCMD) > /dev/null 2>&1

   echo "SentinelKeysProtection="$SHK_VERSION":Count="$count_skpi  >>  /Library/Receipts/SafenetSentinelProduct.Info
   rm -rf /tmp/SNTLCMD
fi

###If previous version of SKPI is installed
skpi_version=`grep SentinelKeysProtection /Library/Receipts/SafenetSentinelProduct.Info | cut -d = -f 2 | cut -d : -f 1 | cut -d . -f 1`
if [ $skpi_version -lt $CURR_SKPI_VERSION ] 
then
    current_inst=`grep SentinelKeysProtection /Library/Receipts/SafenetSentinelProduct.Info | cut -d = -f 3`
    increment_inst=1
    count_skpi=`expr $current_inst + $increment_inst`

   sudo echo "/SentinelKeysProtection/" > /tmp/SNTLCMD
   sudo echo "d" >> /tmp/SNTLCMD; echo "w" >> /tmp/SNTLCMD; echo "q" >> /tmp/SNTLCMD
   (sudo grep "SentinelKeysProtection" /Library/Receipts/SafenetSentinelProduct.Info && sudo ed /Library/Receipts/SafenetSentinelProduct.Info < /tmp/SNTLCMD) > /dev/null 2>&1

   echo "SentinelKeysProtection="$SHK_VERSION":Count="$count_skpi  >>  /Library/Receipts/SafenetSentinelProduct.Info
   rm -rf /tmp/SNTLCMD
fi


##
# Start the SentinelKeysServer
##
sh /Applications/Safenet\ Sentinel/Common\ Files/Sentinel\ Keys\ Server/loadserv.sh start


### Check whether Ultrapro SDK package is installed or not
    [ -d "$UPRO_PCKG_PATH" ]
    if [ $? -eq 0 ]
    then
        rm -rf "/Applications/Sentinel/Sentinel Framework"  
        ### Start the SentinelProtectionServer
        sh "$UPRO_SRVR_PATH"/Sentinel\ Protection\ Server/loadserv.sh stop
        sh "$UPRO_SRVR_PATH"/Sentinel\ Protection\ Server/loadserv.sh start
    fi

### Check whether Ultrapro SPI package is installed or not
    [ -d "$UPRO_SPI_PATH" ]
    if [ $? -eq 0 ]
    then
        ### Start the SentinelProtectionServer
        sh "$UPRO_SRVR_PATH"/Sentinel\ Protection\ Server/loadserv.sh stop
        sh "$UPRO_SRVR_PATH"/Sentinel\ Protection\ Server/loadserv.sh start
    fi

sudo chmod 775 /Library/Receipts/SafenetSentinelProduct.Info

