#!/bin/bash

function usage()
{
    echo -e "Usage: `basename $1` [-i input streamer dmg file] [-d deploy code] [-a account based setting] [-w show deploy warning] ..."
    echo -e "\t-i : input streamer dmg file."
    echo -e "\t-k : input streamer pkg file."
    echo -e "\t-d : deploy code."
    echo -e "\t-w : show deploy warning (0/1). (default 1)"
    echo -e "\t-n : computer name."
    echo -e "\t-s : show Streamer UI after installation (0/1). (default 1)"
    echo -e "\t-c : init security code."
    echo -e "\t-e : Require additional password to connect (0:off, 1:Require security code, 2:Require Mac login). (default 0)"
    echo -e "\t-r : Request permission to connect (0:off, 1:reject after request expires, 2:connect after request expires)."
    echo -e "\t-l : loopback connection only (0/1). (default 0)"
    echo -e "\t-v : install/update driver (0/1). (default 1)"
    echo -e "\t-h : hide tray icon (0/1). (default 0)"
    echo -e "\t-b : default client name on connection bubble."
}

CHECK_NEED_DMG_IN="0"
CHECK_NEED_DEPLOY_CODE="0"
CHECK_NEED_PKG_IN="0"
DEPLOY_CODE=""
COMPUTER_NAME=""
REQUEST_PERMISSION=""
INIT_SECURITY_CODE=""
SECURITY_OPTION=""
LOOPBACK_ONLY="0"
INSTALL_DRIVER="1"
HIDE_TRAY_ICON="0"
DEFAULT_CLIENT_NAME=""

#check options
OPTIND=1 # Reset is necessary if getopts was used previously in the script.  It is a good idea to make this local in a function.
while getopts i:d:w:n:p:s:c:e:r:l:v:h:b:k: o
do	case "$o" in
	i)
        DMG_IN="$OPTARG"
        CHECK_NEED_DMG_IN="1"
		;;
    k)
        PKG_IN="$OPTARG"
        CHECK_NEED_PKG_IN="1"
        ;;
    d)
        DEPLOY_CODE="$OPTARG"
        CHECK_NEED_DEPLOY_CODE="1"
        ;;
    w)
        SHOW_DEPLOY_WARNING="$OPTARG"
        ;;
    n)
        COMPUTER_NAME="$OPTARG"
        ;;
    s)
        SHOW_STREAMER_UI="$OPTARG"
        ;;
    c)
        INIT_SECURITY_CODE="$OPTARG"
        ;;
    e)
        SECURITY_OPTION="$OPTARG"
        ;;
    r)
        REQUEST_PERMISSION="$OPTARG"
        ;;
    l)
        LOOPBACK_ONLY="$OPTARG"
        ;;
    v)
        INSTALL_DRIVER="$OPTARG"
        ;;
    h)
        HIDE_TRAY_ICON="$OPTARG"
        ;;
    b)
        DEFAULT_CLIENT_NAME="$OPTARG"
        ;;
	[?])
		echo "invalid option: $1" 1>&2;
		usage "$0"
		exit 1;;
	esac
done
shift $((OPTIND-1)) # Shift off the options and optional --.

if [ "$CHECK_NEED_DMG_IN" == "0" ] && [ "$CHECK_NEED_PKG_IN" == "0" ]; then
echo "Please input streamer dmg or pkg file!"
usage "$0"
exit 1
fi

#if [ "$CHECK_NEED_DEPLOY_CODE" == "0" ]; then
#echo "No deploy code!"
#usage "$0"
#exit 1
#fi

#write .PreInstall
echo "Inject settings"
mkdir /Users/Shared/SplashtopStreamer

if [ "$INSTALL_DRIVER" == "0" ]; then
NO_DRIVER="/Users/Shared/SplashtopStreamer/.NoDriver"
touch "$NO_DRIVER"
fi

PRE_INSTALL="/Users/Shared/SplashtopStreamer/.PreInstall"
rm -rf $PRE_INSTALL
touch "$PRE_INSTALL"
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"              > "$PRE_INSTALL"
echo "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">" >> "$PRE_INSTALL"
echo "<plist version=\"1.0\">"                                 >> "$PRE_INSTALL"
echo "<dict>"                                                  >> "$PRE_INSTALL"

echo "    <key>Common</key>"                                   >> "$PRE_INSTALL"
echo "        <dict>"                                          >> "$PRE_INSTALL"
echo "             <key>HidePreferenceDomainSelection</key>"   >> "$PRE_INSTALL"
echo "                 <true/>"                                >> "$PRE_INSTALL"
echo "             <key>EulaAccepted</key>"                    >> "$PRE_INSTALL"
echo "                 <true/>"                                >> "$PRE_INSTALL"
echo "        </dict>"                                         >> "$PRE_INSTALL"


echo "    <key>STP</key>"                                      >> "$PRE_INSTALL"
echo "        <dict>"                                          >> "$PRE_INSTALL"
echo "             <key>ShowDeployMode</key>"                  >> "$PRE_INSTALL"
echo "                 <true/>"                                >> "$PRE_INSTALL"
echo "             <key>SplashtopAccount</key>"                >> "$PRE_INSTALL"
echo "                 <string>""</string>"                    >> "$PRE_INSTALL"

if [ ! "$DEPLOY_CODE" == "" ]; then
echo "             <key>DeployCode</key>"                      >> "$PRE_INSTALL"
echo "                 <string>""$DEPLOY_CODE""</string>"      >> "$PRE_INSTALL"
echo "             <key>DeployTeamNameCache</key>"             >> "$PRE_INSTALL"
echo "                 <string>""</string>"                    >> "$PRE_INSTALL"
echo "             <key>DeployTeamOwnerCache</key>"            >> "$PRE_INSTALL"
echo "                 <string>""</string>"                    >> "$PRE_INSTALL"
echo "             <key>LastDeployCode</key>"                  >> "$PRE_INSTALL"
echo "                 <string>""</string>"                    >> "$PRE_INSTALL"
echo "             <key>TeamCode</key>"                        >> "$PRE_INSTALL"
echo "                 <string>""</string>"                    >> "$PRE_INSTALL"
echo "             <key>TeamCodeInUse</key>"                   >> "$PRE_INSTALL"
echo "                 <string>""</string>"                    >> "$PRE_INSTALL"

#if [ "$COMPUTER_NAME" == "" ]; then
#echo "             <key>HostName</key>"                        >> "$PRE_INSTALL"
#echo "                 <string>""</string>"                    >> "$PRE_INSTALL"
#fi

fi

echo "             <key>ShowDeployLoginWarning</key>"          >> "$PRE_INSTALL"
if [ "$SHOW_DEPLOY_WARNING" == "0" ]; then
echo "                 <false/>"                               >> "$PRE_INSTALL"
else
echo "                 <true/>"                                >> "$PRE_INSTALL"
fi

if [ ! "$COMPUTER_NAME" == "" ]; then
echo "             <key>HostName</key>"                        >> "$PRE_INSTALL"
echo "                 <string>""$COMPUTER_NAME""</string>"    >> "$PRE_INSTALL"
fi

if [ "$REQUEST_PERMISSION" == "0" ]; then
echo "             <key>EnablePermissionProtection</key>"      >> "$PRE_INSTALL"
echo "                 <integer>0</integer>"                   >> "$PRE_INSTALL"
fi

if [ "$REQUEST_PERMISSION" == "1" ]; then
echo "             <key>EnablePermissionProtection</key>"      >> "$PRE_INSTALL"
echo "                 <integer>1</integer>"                   >> "$PRE_INSTALL"
fi

if [ "$REQUEST_PERMISSION" == "2" ]; then
echo "             <key>EnablePermissionProtection</key>"      >> "$PRE_INSTALL"
echo "                 <integer>2</integer>"                   >> "$PRE_INSTALL"
fi

if [ "$SECURITY_OPTION" == "0" ]; then
echo "             <key>EnableSecurityCodeProtection</key>"    >> "$PRE_INSTALL"
echo "                 <false/>"                               >> "$PRE_INSTALL"
echo "             <key>EnableOSCredential</key>"              >> "$PRE_INSTALL"
echo "                 <false/>"                               >> "$PRE_INSTALL"
fi

if [ "$SECURITY_OPTION" == "1" ]; then
echo "             <key>EnableSecurityCodeProtection</key>"    >> "$PRE_INSTALL"
echo "                 <true/>"                                >> "$PRE_INSTALL"
echo "             <key>EnableOSCredential</key>"              >> "$PRE_INSTALL"
echo "                 <false/>"                               >> "$PRE_INSTALL"
fi

if [ "$SECURITY_OPTION" == "2" ]; then
echo "             <key>EnableSecurityCodeProtection</key>"    >> "$PRE_INSTALL"
echo "                 <false/>"                               >> "$PRE_INSTALL"
echo "             <key>EnableOSCredential</key>"              >> "$PRE_INSTALL"
echo "                 <true/>"                                >> "$PRE_INSTALL"
fi

if [ ! "$INIT_SECURITY_CODE" == "" ]; then
echo "             <key>init_security_code</key>"              >> "$PRE_INSTALL"
echo "                 <string>""$INIT_SECURITY_CODE""</string>">> "$PRE_INSTALL"
fi

if [ "$LOOPBACK_ONLY" == "1" ]; then
echo "             <key>LegacyConnectionLoopbackOnly</key>"    >> "$PRE_INSTALL"
echo "                 <true/>"                                >> "$PRE_INSTALL"
fi

if [ "$HIDE_TRAY_ICON" == "1" ]; then
echo "             <key>HideTrayIcon</key>"                    >> "$PRE_INSTALL"
echo "                 <true/>"                                >> "$PRE_INSTALL"
fi

if [ ! "$DEFAULT_CLIENT_NAME" == "" ]; then
echo "             <key>DefaultClientDeviceName</key>"         >> "$PRE_INSTALL"
echo "                 <string>""$DEFAULT_CLIENT_NAME""</string>">> "$PRE_INSTALL"
fi

if [ "$SHOW_STREAMER_UI" == "0" ]; then
echo "             <key>FirstTimeClose</key>"                  >> "$PRE_INSTALL"
echo "                 <false/>"                               >> "$PRE_INSTALL"
echo "             <key>FirstTimeLogin</key>"                  >> "$PRE_INSTALL"
echo "                 <false/>"                               >> "$PRE_INSTALL"
fi

echo "        </dict>"                                         >> "$PRE_INSTALL"
echo "</dict>"                                                 >> "$PRE_INSTALL"
echo "</plist>"                                                >> "$PRE_INSTALL"


if [ "$CHECK_NEED_DMG_IN" == "1" ]; then
#mount dmg
echo "Mounting dmg file"
hdiutil attach $DMG_IN

echo "Install silently"
NORMAL_INSTALLER="/Volumes/SplashtopStreamer/Splashtop Streamer.pkg"
HIDDEN_INSTALLER="/Volumes/SplashtopStreamer/.Splashtop Streamer.pkg"
if [ -f "$NORMAL_INSTALLER" ]; then
sudo installer -pkg "$NORMAL_INSTALLER" -target /
else
sudo installer -pkg "$HIDDEN_INSTALLER" -target /
fi

echo "Unmount dmg"
hdiutil detach /Volumes/SplashtopStreamer

else

sudo installer -pkg "$PKG_IN" -target /

fi

#echo "Launch Streamer"
#open "/Applications/Splashtop Streamer.app"

echo "Done!"

exit 0

