#!/bin/bash
# Prod variable for install EEA or EES 
# 
# Activation will be attempted if value KEY contains is not empty
# security admin format example: '123-ABC-456:user=security.admin@mail.com:pass=SecurityAdminPass'
# Proxy example: http://Proxy-IP-or-FQDN:3128

Prod_P=$4
Lic_P=$5
Proxy_P=$6

Prod=""
KEY=""
eraa_http_proxy_value=""

if [ -z "$Prod_P" ] && [ -z "$Prod" ] ; then echo 'No product selected please specify EEA or EES' & exit 1; fi

if test -n "$Prod"; then 
  Prod="$(echo $Prod | tr "[:lower:]" "[:upper:]")"
fi

if test -n "$Prod_P"; then 
  Prod="$(echo $Prod_P | tr "[:lower:]" "[:upper:]")"
fi

if test -n "$Lic_P"; then
  KEY=$Lic_P
fi

if test -n "$Proxy_P"; then 
  eraa_http_proxy_value=$Proxy_P
fi

files2del="$(mktemp -q /tmp/XXXXXXXX.files)"
dirs2del="$(mktemp -q /tmp/XXXXXXXX.dirs)"
echo "$dirs2del" >> "$files2del"
dirs2umount="$(mktemp -q /tmp/XXXXXXXX.mounts)"
echo "$dirs2umount" >> "$files2del"

finalize()
{
  set +e

  echo "Cleaning up:"

  if test -f "$dirs2umount"
  then
    while read f
    do
      sudo -S hdiutil detach "$f"
    done < "$dirs2umount"
  fi

  if test -f "$dirs2del"
  then
    while read f
    do
      test -d "$f" && rmdir "$f"
    done < "$dirs2del"
  fi

  if test -f "$files2del"
  then
    while read f
    do
      unlink "$f"
    done < "$files2del"
    unlink "$files2del"
  fi
}

trap 'finalize' HUP INT QUIT TERM EXIT

if [ $Prod == EEA ]; then 
  DMG=eea_osx_en.dmg
  URL=http://download.eset.com/com/eset/apps/business/eea/mac/latest/eea_osx_en.dmg
  APP=Antivirus
fi
if [ $Prod == EES ]; then 
  DMG=ees_osx_en.dmg
  URL=http://download.eset.com/com/eset/apps/business/ees/mac/latest/ees_osx_en.dmg
  APP=Security
fi

if ! [[ $Prod =~ (EEA|EES) ]]; then echo 'No valid product selected please specify EEA or EES' && exit 1; fi 

local_ESET_dmg="$(mktemp -q -u /tmp/ESETInstaller.dmg.XXXXXXXX)"
echo "Downloading ESET Endpoint $APP installer image '$URL':"

if test -n "$eraa_http_proxy_value"
then
  export use_proxy=yes
  export http_proxy="$eraa_http_proxy_value"
  (curl --connect-timeout 300 --insecure -o "$local_ESET_dmg" "$URL" || curl --connect-timeout 300 --noproxy "*" --insecure -o "$local_ESET_dmg" "$URL") && echo "$local_ESET_dmg" >> "$files2del"
else
  curl --connect-timeout 300 --insecure -o "$local_ESET_dmg" "$URL" && echo "$local_ESET_dmg" >> "$files2del"
fi

if [ ! -f $local_ESET_dmg ]; then exit 1 ; fi 
test -d '/Library/Application Support/ESET/esets/cache' || mkdir -p '/Library/Application Support/ESET/esets/cache' 
touch '/Library/Application Support/ESET/esets/cache/do_not_launch_esets_gui_after_installation'

local_ESET_mount="$(mktemp -q -d /tmp/ESETInstaller.mount.XXXXXXXX)" && echo "$local_ESET_mount" | tee "$dirs2del" >> "$dirs2umount"
echo "Mounting image '$local_ESET_dmg':" && sudo -S hdiutil attach "$local_ESET_dmg" -mountpoint "$local_ESET_mount" -nobrowse
local_ESET_pkg="$(ls "$local_ESET_mount"/Resources/ | grep "\.pkg$" | tail -n 1)"

SigCheck="$(pkgutil --check-signature "$local_ESET_mount/Resources/$local_ESET_pkg" | grep -o "ESET, spol. s r.o. (P8DQRXPVLP)")"
if [ "$SigCheck" == "ESET, spol. s r.o. (P8DQRXPVLP)" ]; then
  echo "Signature check Passed"
  echo "$SigCheck"
  echo "Installing package '$local_ESET_mount/Resources/$local_ESET_pkg':" && sudo -S installer -pkg "$local_ESET_mount/Resources/$local_ESET_pkg" -target / 
else
  echo "Signature check failed"
  echo "$SigCheck"
  exit 1 
fi

sleep 10 

if test -n "$KEY"; then
  /Applications/ESET\ Endpoint\ $APP.app/Contents/MacOS/esets_daemon  --wait-respond --activate "key=$KEY"
fi

exit 0