#!/bin/bash

# Variables
serviceConfig="/Library/Application Support/JWrapper-Remote Access/JWAppsSharedConfig/serviceconfig.xml"
#---Change these if app is renamed or to rebrand make sure the primary script is updated accordingly
#appDomain - LaunchAgent and app deploy preference
appDomain="com.github.remoteaccess"
#location for Deployment Variables .var.plist, once the script is running as app, "../../../.vars.plist" is in the same directory as the app bundle.
plistDir="/Library/Preferences"
plistPath="$plistDir/$appDomain.plist"
#LaunchDaemon Label
launchdaemonLabel="$appDomain.install"
#Installer LaunchDaemon Location
launchdaemonPath="/Library/LaunchDaemons/$launchdaemonLabel.plist"

#SimpleHelp Deployment variables
#serverURL SimpleHelp base server URL
serverURL="https://YOUR.URL"
#servers SimpleHelp servers listed as such "udp://your.server.tld,udp://1.1.1.1,https://your.server.tld"
servers="udp://YOUR.URL,udp://1.1.1.1,https://YOUR.URL"
#group Remote access deploy group "Deployed/Default"
group="Default/"
#silent Pretty much must be "yes"
silent="yes"
#shortcuts show the app or not. recommend "no"
shortcuts="no"
#confirm does the client need to allow connection from technician. recommend "no"
confirm="no"
#monitor can client be monitored outside of session. recommend "yes"
monitor="yes"
#script can scripts run outside of session. recommend "yes"
script="yes"
#proxy settings "username:password@server:port" or "server:port"
proxy=""
#password does the client need a password to connect 
password=""

#checkExistingPlist
function checkExistingSettings {
existingServerURL=`defaults read "$plistPath" serverURL`
if [ -z "$existingServerURL" ]; then
	existingServerURL="null"
fi
existingGroup=`defaults read "$plistPath" group`
if [ -z "$existingGroup" ]; then
	existingGroup="null"
fi
serviceConfigGroup=`xmllint --xpath "string(//Name)" "$serviceConfig" | awk 'BEGIN{OFS=FS="/"};{$NF=";print $0}'`
if [ -z "$serviceConfigGroup" ]; then
	serviceConfigGroup="null"
fi
}

#writeDeployPlist - write deployment variables to "$plistDir/$appDomain.plist"
function writeDeployPlist {
if [ ! -f "$plistPath" ] || [ "$existingServerURL" != "$serverURL" ] || [ "$existingGroup" != "$group" ] || [ "$serviceConfigGroup" != "null" ];then
	
	defaults write "$plistPath" serverURL "$serverURL"
	defaults write "$plistPath" servers "$servers"
	if [ "$serviceConfigGroup" = "null" ] && [ $existingGroup = "null" ];then
	defaults write "$plistPath" group "$group"
	fi
	if [ "$existingGroup" != "null" ];then
	defaults write "$plistPath" group "$existingGroup"
	fi
	if [ "$serviceConfigGroup" != "null" ];then
	defaults write "$plistPath" group "$serviceConfigGroup"
	fi
	defaults write "$plistPath" silent "$silent"
	defaults write "$plistPath" shortcuts "$shortcuts"
	defaults write "$plistPath" confirm "$confirm"
	defaults write "$plistPath" monitor "$monitor" 
	defaults write "$plistPath" script "$script"
	if [ -n "$proxy" ]; then
		defaults write "$plistPath" proxy "$proxy"
	fi
	if [ -n "$password" ]; then
		defaults write "$plistPath" password "$password"
	fi
	
	chown root:wheel "$plistPath"
	chmod 644 "$plistPath"
fi
if [ `defaults read "$plistPath" serverURL` != $serverURL ];then
	echo "serverURL in $plistDir/$appDomain.plist is not $serverURL. Exit 1"
	exit 1
fi
}

checkExistingSettings
writeDeployPlist

exit 0