#!/bin/sh


brand=lanschool
folder=LanSchool

sysVersion=$(uname -r)
sysMajorVersion=${sysVersion%%.*}

function StartStudent()
{
	if [ $sysMajorVersion -ge 9 ]; then
		echo "starting student on Leopard or greater..."
		username=`/usr/bin/who | /usr/bin/grep console | /usr/bin/cut -f 1 -d ' '`
		echo "logged-on user is $username"
		/usr/bin/sudo -u ${username} /usr/bin/open -g -a Finder "/Library/Application Support/${folder}/lslaunch.app"
	else
		echo "lsdaemon will start student on Tiger..."
	fi
}


function StartDaemon()
{
	echo "Starting lsdaemon..."
	/usr/bin/sudo /bin/launchctl load /Library/LaunchDaemons/com.lanschool.lsdaemon.plist
}


echo "pkg postflight"


/usr/bin/sudo /bin/cat <<EOT >/Library/LaunchDaemons/com.lanschool.lsdaemon.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>com.lanschool.lsdaemon.plist</string>
	<key>ProgramArguments</key>
	<array>
		<string>/Library/Application Support/${folder}/lsdaemon</string>
		<string>/daemon</string>
	</array>
	<key>RunAtLoad</key>
	<true/>
	<key>OnDemand</key>
	<false/>
</dict>
</plist>
EOT


if [ $sysMajorVersion -ge 9 ]; then
	echo "Writing user agent plist..."
/usr/bin/sudo /bin/cat <<EOT >/Library/LaunchAgents/com.lanschool.student.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>com.lanschool.student.plist</string>
	<key>LimitLoadToSessionType</key>
	<array>
		<string>Aqua</string>
		<string>LoginWindow</string>
	</array>
	<key>ProgramArguments</key>
	<array>
		<string>/Library/Application Support/${folder}/student.app/Contents/MacOS/student</string>
		<string>/agent</string>
	</array>
	<key>RunAtLoad</key>
	<true/>
	<key>OnDemand</key>
	<false/>
	<key>KeepAlive</key>
        <true/>
</dict>
</plist>
EOT

else
	echo "Tiger - NOT writing user agent plist"
fi

# install and lock down the settings file if this isn't an update
# this is an update if the Plugins folder has been deleted
if [ -d "$1/Contents/Plugins/" ] || [ -f "$1/updatesettings" ]; then
	echo "Not an update - installing settings file"
	/bin/cp "$1/Contents/Resources/com.${brand}.student.settings.plist" /Library/Preferences/
else
	echo "This is an update - not installing settings file."
fi
/usr/sbin/chown root:wheel /Library/Preferences/com.${brand}.student.settings.plist
/bin/chmod 644 /Library/Preferences/com.${brand}.student.settings.plist

plist="/Library/Preferences/com.${brand}.student.config.plist"
if [ ! -f ${plist} ]; then
	echo "Copying config file..."
	/bin/cp "$1/Contents/Resources/com.${brand}.student.config.plist" /Library/Preferences/
	/usr/sbin/chown root:wheel ${plist}
	/bin/chmod 644 ${plist}
else
	echo "Config file already exists"
fi

# make sure ownership and permissions on all files are correct
/usr/sbin/chown root:wheel "/Library/LaunchDaemons/com.lanschool.lsdaemon.plist"
/bin/chmod 644        "/Library/LaunchDaemons/com.lanschool.lsdaemon.plist"


if [ $sysMajorVersion -ge 9 ]; then
	echo "setting rights on user agent plist"
	/usr/sbin/chown root:wheel "/Library/LaunchAgents/com.lanschool.student.plist"
	/bin/chmod 644        "/Library/LaunchAgents/com.lanschool.student.plist"
else
	echo "not setting rights on user agent plist"
fi

/bin/chmod 740        "/Library/Application Support/${folder}/lsdaemon"
/bin/chmod 740	      "/Library/Application Support/${folder}/lsconfig"

# apply the settings created by the install

if [ -f "/var/tmp/lanschool.install.plist" ]; then
	echo "Applying installation settings..."
	unused=`/Library/Application\ Support/${folder}/lsconfig /file /var/tmp/lanschool.install.plist`
	exitCode=$?
	if [ $((exitCode)) -eq 0 ]; then
		echo "Successfully applied settings."
	else
		echo "Failed to apply settings - ${exitCode}"
	fi 
fi

# bless the student
notused=`/Library/Application\ Support/${folder}/student.app/Contents/MacOS/student /bless`
exitCode=$?
if [ $((exitCode)) -eq 0 ]; then
	echo "Successfully blessed the student."
else
        echo "Failed to bless the student - ${exitCode}"
fi



StartDaemon

sleep 2

StartStudent

exit 0

