#!/bin/bash
# Optional parameters
# Activation:
# Security admin format example: 123-ABC-456:user=security.admin@mail.com:pass=SecurityAdminPass
# Proxy for file download: 
# Example: http://Proxy-IP-or-FQDN:3128

SA_P=$4
Proxy_P=$5

Prod="EEA"
eraa_http_proxy_value=""

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

if test -n "$SA_P"; then
	lpid="$(sed -r 's/^(.*):[U|u][S|s][E|e][R|r]=.*:[P|p][A|a][S|s][S|s]=.*$/\1/' <<<$SA_P)"
	saU="$(sed -r 's/^.*:[U|u][S|s][E|e][R|r]=(.*):[P|p][A|a][S|s][S|s]=.*$/\1/' <<<$SA_P)"
	saP="$(sed -r 's/^.*:[U|u][S|s][E|e][R|r]=.*:[P|p][A|a][S|s][S|s]=(.*)$/\1/' <<<$SA_P)"
	if [ -z "$lpid" ] || [ -z "$saU" ]  || [ -z "$saP" ] ; then
		echo 'Unable to parse activation string'
	else
		ACT="-u $saU -p $lpid --stdin-pass"
	fi
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 
  URL=http://download.eset.com/com/eset/apps/business/eea/mac/g2/latest/eea_osx.dmg
  APP=Antivirus
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 

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

if test -n "$ACT"; then
  echo $saP | /Applications/ESET\ Endpoint\ $APP.app/Contents/MacOS/lic $ACT
fi
