#!/bin/bash
KLNAGENT_SERVER="XXXXXX"
KLNAGENT_PORT="14000"
KLNAGENT_SSLPORT="13000"
KLNAGENT_USESSL="yes"
KLNAGENT_GW_MODE="1"
KLNAGENT_VM_VDI="1"
KESL_EULA_AGREED="yes"
KESL_PRIVACY_POLICY_AGREED="yes"
KESL_USE_KSN="no"
KESL_SERVICE_LOCALE="en_US.utf8"
KESL_UPDATER_SOURCE="SCServer"
KESL_PROXY_SERVER="none"
KESL_UPDATE_EXECUTE="no"
KESL_KERNEL_SRCS_INSTALL="yes"
KESL_USE_GUI="no"

if [[ -f /etc/os-release ]]
then
    osname=`cat /etc/os-release | grep "^ID=" | cut -d= -f2 | tr -d "\"" | tr -d '[:space:]'`
    osver=`cat /etc/os-release | grep "^VERSION_ID=" | cut -d\= -f2 | tr -d "\"" | tr -d "." | tr -d '[:space:]'`
    os=$osname$osver
elif [[ -f /etc/centos-release ]]
then
    osname='centos'
    osver=`cat /etc/system-release | egrep -o "[[:digit:]]{1,}.[[:digit:]]{1,}" | tr -d "." | tr -d '[:space:]'`
    os=$osname$osver
elif [[ -f /etc/redhat-release ]]
then
    osname='rhel'
    osver=`cat /etc/system-release | egrep -o "[[:digit:]]{1,}.[[:digit:]]{1,}" | tr -d "." | tr -d '[:space:]'`
    os=$osname$osver
else
    echo "OS Unsupported" > /tmp/kasperskyinstall.log
    exit
fi
echo $osname > /tmp/kasperskyinstall.log

case $osname in
    "centos"|"rhel"|"ol"|"amzn")
    yum install perl wget -y >> /tmp/kasperskyinstall.log
    wget -q "http://$KLNAGENT_SERVER:8060/Public/klnagent.rpm" -O /tmp/klnagent.rpm >> /tmp/kasperskyinstall.log
    wget -q "http://$KLNAGENT_SERVER:8060/Public/kesl.rpm" -O /tmp/kesl.rpm >> /tmp/kasperskyinstall.log
    yum install /tmp/klnagent.rpm -y >> /tmp/kasperskyinstall.log
    ;;

    "ubuntu"|"debian")
    apt-get update >> /tmp/kasperskyinstall.log
    apt-get install -y perl wget curl >> /tmp/kasperskyinstall.log
    apt-get install -y dpkg-dev >> /tmp/kasperskyinstall.log
    wget -q "http://$KLNAGENT_SERVER:8060/Public/klnagent.deb" -O /tmp/klnagent.deb >> /tmp/kasperskyinstall.log
    wget -q "http://$KLNAGENT_SERVER:8060/Public/kesl.deb" -O /tmp/kesl.deb >> /tmp/kasperskyinstall.log
    dpkg -i /tmp/klnagent.deb >> /tmp/kasperskyinstall.log
    ;; 

    "sles"|"opensuse")
    zypper install -y perl >> /tmp/kasperskyinstall.log
    wget -q "http://$KLNAGENT_SERVER:8060/Public/klnagent.rpm" -O /tmp/klnagent.rpm >> /tmp/kasperskyinstall.log
    wget -q "http://$KLNAGENT_SERVER:8060/Public/kesl.rpm" -O /tmp/kesl.rpm >> /tmp/kasperskyinstall.log
    rpm -i /tmp/klnagent.rpm >> /tmp/kasperskyinstall.log
    ;;
esac

echo "KLNAGENT_SERVER=$KLNAGENT_SERVER" > /tmp/agent-answer.cfg
echo "KLNAGENT_PORT=$KLNAGENT_PORT" >> /tmp/agent-answer.cfg
echo "KLNAGENT_SSLPORT=$KLNAGENT_SSLPORT" >> /tmp/agent-answer.cfg
echo "KLNAGENT_USESSL=$KLNAGENT_USESSL" >> /tmp/agent-answer.cfg
echo "KLNAGENT_GW_MODE=$KLNAGENT_GW_MODE" >> /tmp/agent-answer.cfg
echo "KLNAGENT_VM_VDI=$KLNAGENT_VM_VDI" >> /tmp/agent-answer.cfg
export KLAUTOANSWERS=/tmp/agent-answer.cfg

/opt/kaspersky/klnagent64/lib/bin/setup/postinstall.pl >> /tmp/kasperskyinstall.log

sleep 5

case $osname in
    "centos"|"rhel"|"ol"|"amzn")
    yum install /tmp/kesl.rpm -y >> /tmp/kasperskyinstall.log
    ;;
    
    "ubuntu"|"debian")
    dpkg -i /tmp/kesl.deb >> /tmp/kasperskyinstall.log
    ;; 

    "sles"|"opensuse")
    rpm -i /tmp/kesl.rpm >> /tmp/kasperskyinstall.log
    ;;
esac

echo "EULA_AGREED=$KESL_EULA_AGREED" > /tmp/kesl-answer.cfg
echo "PRIVACY_POLICY_AGREED=$KESL_PRIVACY_POLICY_AGREED" >> /tmp/kesl-answer.cfg
echo "USE_KSN=$KESL_USE_KSN" >> /tmp/kesl-answer.cfg
echo "SERVICE_LOCALE=$KESL_SERVICE_LOCALE" >> /tmp/kesl-answer.cfg
echo "UPDATER_SOURCE=$KESL_UPDATER_SOURCE" >> /tmp/kesl-answer.cfg
echo "PROXY_SERVER=$KESL_PROXY_SERVER" >> /tmp/kesl-answer.cfg
echo "UPDATE_EXECUTE=$KESL_UPDATE_EXECUTE" >> /tmp/kesl-answer.cfg
echo "KERNEL_SRCS_INSTALL=$KESL_KERNEL_SRCS_INSTALL" >> /tmp/kesl-answer.cfg
echo "USE_GUI=$KESL_USE_GUI" >> /tmp/kesl-answer.cfg

/opt/kaspersky/kesl/bin/kesl-setup.pl --autoinstall=/tmp/kesl-answer.cfg >> /tmp/kasperskyinstall.log

/etc/init.d/klnagent64 reload-connectors >> /tmp/kasperskyinstall.log