#!/bin/sh
# $1 has the path to the source package
# $2 has the path to the target folder
# $3 has the path to the target volume
# $USER has the name of the user who launched the installer

# enforcing some critical permissions, just to be sure everything is OK

if [ -e "/System/Library/Extensions/LogitechHIDDevices.kext" ] ; then
	/usr/sbin/chown -R root:wheel "/System/Library/Extensions/LogitechHIDDevices.kext"
	/bin/chmod -R ug+r,go-w "/System/Library/Extensions/LogitechHIDDevices.kext"
elif [ -e "/System/Library/Extensions/LogicoolHIDDevices.kext" ] ; then
	/usr/sbin/chown -R root:wheel "/System/Library/Extensions/LogicoolHIDDevices.kext"
	/bin/chmod -R ug+r,go-w "/System/Library/Extensions/LogicoolHIDDevices.kext"
fi

if [ -e "/System/Library/Extensions/LogitechUnifying.kext" ] ; then
	/usr/sbin/chown -R root:wheel "/System/Library/Extensions/LogitechUnifying.kext"
	/bin/chmod -R ug+r,go-w "/System/Library/Extensions/LogitechUnifying.kext"
elif [ -e "/System/Library/Extensions/LogicoolUnifying.kext" ] ; then
	/usr/sbin/chown -R root:wheel "/System/Library/Extensions/LogicoolUnifying.kext"
	/bin/chmod -R ug+r,go-w "/System/Library/Extensions/LogicoolUnifying.kext"
fi

if [ -e "/Library/LaunchAgents/com.Logitech.Control Center.Daemon.plist" ] ; then
	/usr/sbin/chown -R root:wheel "/Library/LaunchAgents/com.Logitech.Control Center.Daemon.plist"
	/bin/chmod -R 644 "/Library/LaunchAgents/com.Logitech.Control Center.Daemon.plist"
fi

if [ -e "/Applications/Utilities/LCC Connection Utility.app" ]; then
	/usr/sbin/chown -R root:admin "/Applications/Utilities/LCC Connection Utility.app"
	/bin/chmod -R ug+rw,o-w "/Applications/Utilities/LCC Connection Utility.app"
fi

if [ -e "/Applications/Utilities/LCC Uninstaller.app" ]; then
	/usr/sbin/chown -R root:admin "/Applications/Utilities/LCC Uninstaller.app"
	/bin/chmod -R ug+rw,o-w "/Applications/Utilities/LCC Uninstaller.app"
fi

if [ -e "/Library/Application Support/Logitech.localized" ]; then
	/usr/sbin/chown -R root:admin "/Library/Application Support/Logitech.localized"
	/bin/chmod -R ug+rw,o-w "/Library/Application Support/Logitech.localized"
fi

if [ -e "/Library/Application Support/Logitech.localized/Logitech Control Center.localized/LCCDaemon.app" ]; then
	/usr/sbin/chown root:admin "/Library/Application Support/Logitech.localized/Logitech Control Center.localized/LCCDaemon.app/Contents/Resources/SetPriority"
	/bin/chmod u+s "/Library/Application Support/Logitech.localized/Logitech Control Center.localized/LCCDaemon.app/Contents/Resources/SetPriority"
fi

if [ -e "/Library/PreferencePanes/Logitech Control Center.prefPane" ]; then
	/usr/sbin/chown -R root:admin "/Library/PreferencePanes/Logitech Control Center.prefPane"
	/bin/chmod -R ug+rw,o-w "/Library/PreferencePanes/Logitech Control Center.prefPane"
	/bin/chmod ugo+w "/Library/PreferencePanes/Logitech Control Center.prefPane"/Contents/Resources/*.lproj/html/
	/bin/chmod ugo+w "/Library/PreferencePanes/Logitech Control Center.prefPane"/Contents/Resources/*.lproj/html/warranty_keyboards.htm
	/bin/chmod ugo+w "/Library/PreferencePanes/Logitech Control Center.prefPane"/Contents/Resources/*.lproj/html/warranty_mice.htm
elif [ -e "/Library/PreferencePanes/Logicool Control Center.prefPane" ]; then
	/usr/sbin/chown -R root:admin "/Library/PreferencePanes/Logicool Control Center.prefPane"
	/bin/chmod -R ug+rw,o-w "/Library/PreferencePanes/Logicool Control Center.prefPane"
	/bin/chmod ugo+w "/Library/PreferencePanes/Logicool Control Center.prefPane"/Contents/Resources/*.lproj/html/
	/bin/chmod ugo+w "/Library/PreferencePanes/Logicool Control Center.prefPane"/Contents/Resources/*.lproj/html/warranty_keyboards.htm
	/bin/chmod ugo+w "/Library/PreferencePanes/Logicool Control Center.prefPane"/Contents/Resources/*.lproj/html/warranty_mice.htm
fi

# Cleanup

if [ -e "$1/Contents/Resources/LCC Uninstaller Tool" ] ; then
	"$1/Contents/Resources/LCC Uninstaller Tool" --postflight
fi

# Install last bits

"$1/Contents/Resources/RMover" "$1" --postflight

# Be sure that the system realizes that something changed, and rebuild extension cache

if [ -f "/Users/$USER/Library/Caches/.ApplePrefPanesCache" ]; then
	/bin/rm -f "/Users/$USER/Library/Caches/.ApplePrefPanesCache"
fi
if [ -f "/Users/$USER/Library/Caches/com.apple.preferencepanes.cache" ]; then
	/bin/rm -f "/Users/$USER/Library/Caches/com.apple.preferencepanes.cache"
fi
/usr/bin/touch "/Library/PreferencePanes"
/usr/bin/touch "/System/Library/PreferencePanes"
/usr/bin/touch "/Library/Keyboard Layouts"

if [ -f "/System/Library/Extensions.mkext" ]; then
	/bin/rm -f "/System/Library/Extensions.mkext"
fi
/usr/bin/touch "/System/Library/Extensions"

# Daemon, launchd & login items

if [ -e "/Library/Application Support/Logitech.localized/Logitech Control Center.localized/LCCDaemon.app" ] ; then
	/usr/bin/sudo -u $USER /usr/bin/osascript -e "tell application \"System Events\" to delete (every login item whose path ends with \"LCCDaemon.app\")"
	if [ -e "/Library/LaunchAgents/com.Logitech.Control Center.Daemon.plist" ] ; then
		/bin/launchctl load -w "/Library/LaunchAgents/com.Logitech.Control Center.Daemon.plist"
	else
		/usr/bin/sudo -u $USER /usr/bin/osascript -e "tell application \"System Events\" to make new login item with properties { path: \"/Library/Application Support/Logitech.localized/Logitech Control Center.localized/LCCDaemon.app\", hidden: false } at beginning"
	fi
fi

# Add eReg to login items

if [ -e "/Library/Application Support/Logitech.localized/Logitech Control Center.localized/Logitech Registration.app" ] ; then
	/usr/bin/sudo -u $USER /usr/bin/osascript -e "tell application \"System Events\" to make new login item with properties { path: \"/Library/Application Support/Logitech.localized/Logitech Control Center.localized/Logitech Registration.app\", hidden: false } at end"
elif [ -e "/Library/Application Support/Logitech.localized/Logitech Control Center.localized/Logicool Registration.app" ] ; then
	/usr/bin/sudo -u $USER /usr/bin/osascript -e "tell application \"System Events\" to make new login item with properties { path: \"/Library/Application Support/Logitech.localized/Logitech Control Center.localized/Logicool Registration.app\", hidden: false } at end"
fi

# Launch the daemon (no longer needed, handled by launchd)

#if [ -e "/Library/Application Support/Logitech.localized/Logitech Control Center.localized/LCCDaemon.app" ] ; then
#	/usr/bin/sudo -u $USER -b /Library/Application\ Support/Logitech.localized/Logitech\ Control\ Center.localized/LCCDaemon.app/Contents/MacOS/LCCDaemon
#fi

exit 0
