#!/bin/sh
CURR_SSD_VERSION=0750
CURR_SKPI_VERSION=0131
CURR_SDK_VERSION=0131


SUPRO_PCKG_PATH=/Library/Receipts/SentinelSuperPro*
SUPRO_DRIVER_PATH=/Library/Receipts/SentinelDriver1.0.0*

FRMWRK1_INST_PATH="/Library/Frameworks"
FRMWRK2_INST_PATH="/System/Library/Frameworks"

SSP1_KEXT_PATH="/System/Library/Extensions/Sentinel.kext"
SSP2_KEXT_PATH="/Library/Extensions/Sentinel.kext"
SHK_KEXT_PATH="/System/Library/Extensions/Sentinel.kext"

SKPI_PATH=/Applications/Safenet\ Sentinel/Common\ Files/Sentinel\ Keys\ Server
SPI_PATH=/Applications/Sentinel/Sentinel\ Protection\ Server

### Check whether Sentinel Keys Protection Server is installed
    [ -d "$SKPI_PATH" ]
    if [ $? -eq 0 ]
    then
      ### Stop the Sentinel Keys Server
      sudo sh /Applications/Safenet\ Sentinel/Common\ Files/Sentinel\ Keys\ Server/loadserv.sh stop
    fi

### Check whether Ultrapro SPI is installed
    [ -d "$SPI_PATH" ]
    if [ $? -eq 0 ]
    then
      ## Stop the Sentinel Protection Server
      sudo sh /Applications/Sentinel/Sentinel\ Protection\ Server/loadserv.sh stop
    fi

### Check whether Superpro package is installed or not
    [ -d $SUPRO_PCKG_PATH ]
    if [ $? -eq 0 ]
    then      
       sudo kextstat | grep com.rainbow.iokit.Sentinel      
       if [ $? -eq 0 ]
       then
           if [ -d $SSP1_KEXT_PATH ]
           then 
               sudo kextunload /System/Library/Extensions/Sentinel.kext
               if [ $? -ne 0 ]
               then
                    return 1
                    exit 
               fi
           fi     
       
           if [ -d $SSP2_KEXT_PATH ]
           then 
               sudo kextunload /Library/Extensions/Sentinel.kext
               if [ $? -ne 0 ]
               then
                    return 1
                    exit 
               fi
           fi
        fi

        if [ -d $SSP1_KEXT_PATH ]
        then 
               sudo rm -rf $SSP1_KEXT_PATH
        fi
               
        if [ -d $SSP2_KEXT_PATH ]
        then 
              sudo rm -rf $SSP2_KEXT_PATH
        fi
    fi
       

### Check whether Superpro Driver package is installed or not
    [ -d $SUPRO_DRIVER_PATH ]
    if [ $? -eq 0 ]
    then
       sudo kextstat | grep com.rainbow.iokit.Sentinel      
       if [ $? -eq 0 ]
       then
           if [ -d $SSP1_KEXT_PATH ]
           then 
               sudo kextunload /System/Library/Extensions/Sentinel.kext
               if [ $? -ne 0 ]
               then
                    return 1
                    exit 
               fi
           fi     
       
           if [ -d $SSP2_KEXT_PATH ]
           then 
               sudo kextunload /Library/Extensions/Sentinel.kext
               if [ $? -ne 0 ]
               then
                    return 1
                    exit 
               fi
           fi
        fi

        if [ -d $SSP1_KEXT_PATH ]
        then 
               sudo rm -rf $SSP1_KEXT_PATH
        fi
               
        if [ -d $SSP2_KEXT_PATH ]
        then 
              sudo rm -rf $SSP2_KEXT_PATH
        fi
    fi

### Removing the Sentinel.Framework
    [ -d "$FRMWRK1_INST_PATH"/Sentinel.framework ]
    if [ $? = 0 ]
    then 
        sudo rm -rf "$FRMWRK1_INST_PATH"/Sentinel.framework
    fi

    [ -d "$FRMWRK2_INST_PATH"/Sentinel.framework ]
    if [ $? = 0 ]
    then 
        sudo rm -rf "$FRMWRK2_INST_PATH"/Sentinel.framework
    fi 

### Check whether old version of Kext is installed or not
       sudo kextstat | grep com.SafeNet.driver.Sentinel | egrep "(7.3.0)|(7.4.0)|(7.4.1)|(7.5.0)"    
       if [ $? -eq 0 ]
       then
           if [ -d $SHK_KEXT_PATH ]
           then 
               sudo kextunload /System/Library/Extensions/Sentinel.kext
               if [ $? -ne 0 ]
               then
                    return 1
                    exit 
               fi
           fi        
       
        if [ -d $SHK_KEXT_PATH ]
        then 
               sudo rm -rf $SHK_KEXT_PATH
        fi              
     fi



### Check for higher version of SHK
    [ -f "/Library/Receipts/SafenetSentinelProduct.Info" ]
    if [ $? -eq 0 ]
    then
       ssd_version=`grep SentinelSystemDriver /Library/Receipts/SafenetSentinelProduct.Info | cut -d = -f 2 | cut -d : -f 1 | cut -d . -f 1`
       skpi_version=`grep SentinelKeysProtection /Library/Receipts/SafenetSentinelProduct.Info | cut -d = -f 2 | cut -d : -f 1 | cut -d . -f 1`
       sdk_version=`grep SentinelKeysSDK /Library/Receipts/SafenetSentinelProduct.Info | cut -d = -f 2 | cut -d : -f 1 | cut -d . -f 1`
       lm_version=`grep SentinelKeysLicenseManager /Library/Receipts/SafenetSentinelProduct.Info | cut -d = -f 2 | cut -d : -f 1 | cut -d . -f 1`
 
      if [ $ssd_version ]
      then
        if [ $ssd_version -gt $CURR_SSD_VERSION ] 
        then  
           return 1
           exit
        fi
     fi 

     if [ $skpi_version ]
      then
        if [ $skpi_version -gt $CURR_SKPI_VERSION ] 
        then  
           return 1
           exit
        fi
     fi 
    
     if [ $sdk_version ]
      then
        if [ $sdk_version -gt $CURR_SDK_VERSION ] 
        then  
           return 1
           exit
        fi
     fi 

     if [ $lm_version ]
      then
        if [ $lm_version -gt $CURR_SDK_VERSION ] 
        then  
           return 1
           exit
        fi
     fi 
   fi
