#!/bin/sh

############################

# Invalidate the Pref Panes cache, so the new PrefPane is offered
/usr/bin/touch -c "$3/Library/PreferencePanes"
/usr/bin/touch -c "$3/Library/PreferencePanes/Shake Qmaster.prefPane"
/usr/bin/touch -c "$3/Library/PreferencePanes/Apple Qmaster.prefPane"
/bin/rm -f "$HOME/Library/Caches/com.apple.preferencepanes.cache"

############################

# Create the config file dir
if [ ! -d "$3/Library/Application Support/Apple Qmaster" ] ; then
	/bin/mkdir "$3/Library/Application Support/Apple Qmaster"
fi

############################

# If qmasterservices.plist is a symlink, make a copy of the default
if [ -h "$3/Library/Application Support/Apple Qmaster/qmasterservices.plist" ]; then
        rm "$3/Library/Application Support/Apple Qmaster/qmasterservices.plist"
        cp "$3/Library/Application Support/Apple Qmaster/qmasterservices.plist.default" "$3/Library/Application Support/Apple Qmaster/qmasterservices.plist"
        chmod 666 "$3/Library/Application Support/Apple Qmaster/qmasterservices.plist"
fi

############################

# Merge the keys in the new qmasterservices.plist.default file into the existing qmasterservices.plist
echo Updating qmasterservices.plist
if [ -e "$3/Library/Application Support/Apple Qmaster/qmasterservices.plist.default" ] ; then
        "$1/Contents/Resources/PlistBuddy" "$3/Library/Application Support/Apple Qmaster/qmasterservices.plist" -c "merge -r \"$3/Library/Application Support/Apple Qmaster/qmasterservices.plist.default\"" > /dev/null 2>&1
        /bin/chmod 666 "$3/Library/Application Support/Apple Qmaster/qmasterservices.plist"
fi

############################

# Merge the keys in the transcoderx.plist file into the existing qmasterservices.plist
if [ -e "$3/Library/Application Support/Apple Qmaster/transcoderx.plist.default" ] ; then
        "$1/Contents/Resources/PlistBuddy" "$3/Library/Application Support/Apple Qmaster/qmasterservices.plist" -c "merge \"$3/Library/Application Support/Apple Qmaster/transcoderx.plist.default\"" > /dev/null 2>&1
fi
/bin/chmod 666 "$3/Library/Application Support/Apple Qmaster/qmasterservices.plist"

############################

# Merge the data in the new qmasterconfig.default into the existing qmasterconfig file
if [ -e "$3/Library/Application Support/Apple Qmaster/qmasterconfig" -a -e "$3/Library/Application Support/Apple Qmaster/qmasterconfig.default" ] ; then
        echo Merging keys into qmasterconfig
        "$1/Contents/Resources/mergeQmasterconfig" "$3/Library/Application Support/Apple Qmaster/qmasterconfig" "$3/Library/Application Support/Apple Qmaster/qmasterconfig.default"
elif [ -e "$3/Library/Application Support/Apple Qmaster/qmasterconfig.default" ] ; then
        /bin/cp "$3/Library/Application Support/Apple Qmaster/qmasterconfig.default" "$3/Library/Application Support/Apple Qmaster/qmasterconfig"
fi
/bin/chmod 666 "$3/Library/Application Support/Apple Qmaster/qmasterconfig"

############################

# Add the appropriate keys to the QmasterApplications.plist
echo Configuring QmasterApplications.plist
"$1/Contents/Resources/PlistBuddy" "$3/Library/Application Support/Apple Qmaster/QmasterApplications.plist" -c "add MultiUser string true" > /dev/null 2>&1

############################

# Make sure the log files dir is created with the right permissions
/bin/mkdir -p "$3/Library/Logs/Qmaster"
/bin/chmod 1775 "$3/Library/Logs/Qmaster"

############################

# tickle the qmaster daemon if we're installing onto the boot volume
if [ "$3" = "/" ] ; then 
        /usr/bin/killall -m qmasterd -SIGNAL -9
        # give qmasterd enough time to exit
        # alternately the output of "/usr/bin/killall -s -m qmasterd -SIGNAL -9" could be parsed to see if all qmasterd instances are gone already
        /bin/sleep 5
        /usr/bin/killall -m qmasterqd -SIGNAL -9
        /usr/bin/killall -m qmastertaskd -SIGNAL -9
        /usr/bin/killall -m compressord -SIGNAL -9
        # /var/run/qmasterd.pid may not exist - make sure to ignore the return value of rm!
        /bin/rm /var/run/qmasterd.pid
	/usr/sbin/qmasterprefs -restart
fi

############################

exit 0
