#!/bin/sh -e

echo "Webroot installation started" >> /opt/ITSPlatform/log/webrootinstall.log 2>&1
date >> /opt/ITSPlatform/log/webrootinstall.log 2>&1

#to check if the number of arguments = 2
if [ $# -ne 1 ]
then
    echo " Installation Failed : Invalid arguments. Webroot Installation key is needed as an additional parameter." >> /opt/ITSPlatform/log/webrootinstall.log 2>&1
    exit 1
fi

BASE_DIR="/opt/ITSPlatform"
webroot_path="$BASE_DIR/plugin"
installer_path="$webroot_path/webroot"
dmg_path="$installer_path/wsamacsme.dmg"
pkg_path="$installer_path/WSAMACSME.pkg"
install_wsa_path="$installer_path/installMacWSA.sh"
web_atch_dtch="/Volumes/Webroot SecureAnywhere/"

pkg_present=false
if [ -f $pkg_path ]
then
    echo "Webroot pkg is present" >> /opt/ITSPlatform/log/webrootinstall.log 2>&1
    pkg_present=true
fi
sleep 2

#Checking webroot if it is already installed
echo "Checking Webroot installation" >> /opt/ITSPlatform/log/webrootinstall.log 2>&1
if [ -f /Library/LaunchDaemons/com.webroot.security.mac.plist ] || [ -f /Library/LaunchAgents/com.webroot.WRMacApp.plist ] || [ -f /Applications/Webroot\ SecureAnywhere.app ]
then
    echo "Installation Failed : webroot already installed" >> /opt/ITSPlatform/log/webrootinstall.log 2>&1
    exit 1
fi
sleep 2

#Checking if webroot is already attached
if [ -d /Volumes/Webroot\ SecureAnywhere/ ]
then
    echo " Installation Failed : Webroot already attach kindly detach it and run the script again" >> /opt/ITSPlatform/log/webrootinstall.log 2>&1
    exit 1
fi
sleep 2

if [ $pkg_present = true ]
then
    echo "Triggering Webroot installation using pkg" >> /opt/ITSPlatform/log/webrootinstall.log 2>&1
    #Trigger the webroot Installation.
    chmod 766 $install_wsa_path >> /opt/ITSPlatform/log/webrootinstall.log 2>&1
    $install_wsa_path --pkg=$pkg_path -silent=true -suppress_auto_launch=true -keycode="$1" >> /opt/ITSPlatform/log/webrootinstall.log 2>&1
    retVal=$?
else
    echo "As Webroot pkg is not present, checking for Webroot dmg" >> /opt/ITSPlatform/log/webrootinstall.log 2>&1
    if [ -f $dmg_path ]
    then
        echo "Webroot dmg is present" >> /opt/ITSPlatform/log/webrootinstall.log 2>&1
    else
        echo "Installation Failed : Webroot dmg is not present" >> /opt/ITSPlatform/log/webrootinstall.log 2>&1
        exit 2
    fi
    echo "Checking hdiutil for Webroot installation" >> /opt/ITSPlatform/log/webrootinstall.log 2>&1
    #mount the dmg
    hdiutil attach $dmg_path >> /opt/ITSPlatform/log/webrootinstall.log 2>&1
    sleep 2

    echo "Checking ditto for Webroot installation" >> /opt/ITSPlatform/log/webrootinstall.log 2>&1
    ditto /Volumes/Webroot\ SecureAnywhere /Applications/ >> /opt/ITSPlatform/log/webrootinstall.log 2>&1
    sleep 2

    echo "Triggering Webroot installation using dmg" >> /opt/ITSPlatform/log/webrootinstall.log 2>&1
    #Trigger the webroot Installation.
    sudo "/Applications/Webroot SecureAnywhere.app/Contents/MacOS/Webroot SecureAnywhere" install -keycode="$1" -silent >> /opt/ITSPlatform/log/webrootinstall.log 2>&1
    retVal=$?
fi

echo $retVal >> /opt/ITSPlatform/log/webrootinstall.log 2>&1

if [ $retVal -ne 0 ]
then
    echo "Installation Failed : webroot installation failed" >> /opt/ITSPlatform/log/webrootinstall.log 2>&1
    exit 5
fi

sleep 5
webrootfile="/Applications/Webroot*.app"

if [ -e $webrootfile ]
then
    echo "Installation Succesfully : Webroot installed successfully" >> /opt/ITSPlatform/log/webrootinstall.log 2>&1
else
    echo "Installation Failed : Unable to install Webroot" >> /opt/ITSPlatform/log/webrootinstall.log 2>&1
    exit 6
fi

if [ $pkg_present = false ]
then
    #Unmount the DMG
    echo "Checking hdiutil for Webroot detach" >> /opt/ITSPlatform/log/webrootinstall.log 2>&1
    hdiutil detach /Volumes/Webroot\ SecureAnywhere/ >> /opt/ITSPlatform/log/webrootinstall.log 2>&1
    sleep 2
fi

echo "Webroot installation completed" >> /opt/ITSPlatform/log/webrootinstall.log 2>&1
exit 0
