#!/bin/sh
echo "WSA Mac install script"
# All Valid Parameters are listed below.
# Required Command: pkg. Location of the WSA Mac package. Format: --pkg=<pkg/location>
# Command: keycode. Installation will not prompt for keycode. Format: -keycode=<keycode>
# Command: silent. This will make the installation silent. Format: -silent=true
# Command: suppress_auto_launch. When set to true, suppresses both the auto-launch of the client after an install and the FDA setting reminder dialog. This setting is ignored if the keycode parameter is not passed. Format: -suppress_auto_launch=true
# Command: proxy_auth, proxy_host, proxy_port, proxy_user, proxy_pass. This designates proxy install arguments include authorization method, host, port, user, and password. Format: -proxy_auth=auth_any_0 -proxy_host=<host> -proxy_port=<port> -proxy_user=<user> -proxy_pass=<password>

# Full example string
# sudo ./installMacWSA.sh --pkg=WSAMAC.pkg -silent=true -suppress_auto_launch=true -keycode=XXXX-XXXX-XXXX-XXXX -proxy_auth=auth_basic_1 -proxy_host=proxy.proxy.com -proxy_port=8080 -proxy_user=proxyuser.com -proxy_pass=proxypass

# Establish the user that WSA is to be installed on.
set -x
installerUser=$(stat -f '%Su' $HOME)
echo "installerUser is $installerUser"

# Create output log for install logging
scriptname=$(basename $0)
tempfilenosuffix=$(mktemp /tmp/${scriptname}.XXXXXX)
tempfile="$tempfilenosuffix.txt"
touch $tempfile
chmod 666 $tempfile
logfile="${tempfile}"

# This loop will parse through the passed parameters and write
# them to the install Users defaults as well as the root users.
# Information on the install will also be passed to the log file.
for i in "$@"
do
case $i in 
    --pkg=*)
    package="${i#*=}"
    echo "package is \"$package\""
    shift
    ;;
    -p=*|--pwd=*)
    password="${i#*=}"
    shift
    ;;
    --log=*)
    logfile="${i#*=}"
    shift
    ;;
    -*=*)
    parameter=${i#-}
    parameter=${parameter%=*}
    value=${i#*=}
    # Write to user defaults
    echo "defaults write group.com.webroot.wsa ${parameter} -string \"${value}\""
    echo "writing to user defaults"
    sudo -u ${installerUser} defaults write group.com.webroot.wsa ${parameter} -string "${value}"
    # Write to root user defaults
    echo "writing to root user defaults"
    defaults write group.com.webroot.wsa ${parameter} -string "${value}"
    shift
    ;;
esac
done

# Dump install information into the logfile.
sudo -u $installerUser defaults write group.com.webroot.wsa installerTempFile -string "${logfile}"
echo "dumping log to ${logfile}"

#Kick off install
installer -dumplog -verboseR -pkg "${package}" -target /  >> ${logfile} 2>&1
echo "install finished, printing postinstall output"
cat ${logfile}
exit 0