#!/bin/sh

echo "Started the post-install script"

#pbx working dir
PBX_APP_DIR=/usr/Applications
PBX_WD=/usr/Applications/snomone
#pbx install dir
PBXINSTALL=/usr/Applications/snomone

#change dir to install folder
cd $PBXINSTALL

# Sanity checking of the whole setup:
if [ "pbxctrl" == "" -o "$PBXINSTALL" == "/" ]; then
    echo "Installation error"
    exit 1
fi


#assign the executable permission to pbxctrl
chmod a+rx pbxctrl

#copy the audio files
cp -R audio_* $PBX_WD

# Create the lanuch daemon plist filet with the startup items:
cat <<EOF >/Library/LaunchDaemons/com.vodia.snomone.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>com.vodia.snomone</string>
	<key>ProgramArguments</key>
	<array>
		<string>/usr/Applications/snomone/pbxctrl</string>
		<string>--no-daemon</string>
		<string>--dir</string>
		<string>/usr/Applications/snomone</string>
	</array>
	<key>KeepAlive</key>
	<true/>
	<key>RunAtLoad</key>
	<true/>
	<key>StandardErrorPath</key>
	<string>/Library/Logs/snomone-error.log</string>
	<key>StandardOutPath</key>
	<string>/Library/Logs/snomone.log</string>
	<key>UserName</key>
	<string>root</string>
	<key>WorkingDirectory</key>
	<string>/usr/Applications/snomone</string>
</dict>
</plist>
EOF

#start the snomONE
launchctl load -w /Library/LaunchDaemons/com.vodia.snomone.plist

echo "Completed the post-install script"

