#!/bin/sh

backup_ext=personal

#
# Show arguments and wait for cancellation.
#
#echo "source package path: $1"
#echo "package destination: $2"
#echo "mount point of dest: $3"
#echo "installer framework: $4"

#
# If the ServerSetup directory does not exist then create it.
# If it exists set the ownership and permissions.                       
#
if [ -e "$3/System/Library/ServerSetup" ]; then
    if [ ! -e "$3/System/Library/ServerSetup/Configured" ]; then
        mkdir "$3/System/Library/ServerSetup/Configured"
    fi
    if [ ! -e "$3/System/Library/ServerSetup/UnConfigured" ]; then
        mkdir "$3/System/Library/ServerSetup/UnConfigured"
    fi
    "$3/usr/sbin/chown" 0:0 "$3/System/Library/ServerSetup/Configured"
    "$3/usr/sbin/chown" 0:0 "$3/System/Library/ServerSetup/UnConfigured"
    chmod 755 "$3/System/Library/ServerSetup/Configured"
    chmod 755 "$3/System/Library/ServerSetup/UnConfigured"
fi

# Delete the old version of the Assistant app
# iff we are a Server!
if [ -e "$2/Library/Receipts/ServerEssentials.pkg" ] ; then
    echo -n "Deleting archaic version of Assistant..." 
    rm -rf "$2/System/Library/CoreServices/Setup Assistant.app"
    rm "$2/Applications/Utilities/Setup Assistant.app"
    echo "done."
fi
  
###
#
# Our server apps now live at /Applications/Server
#  used to live at /Applications/Utilities
#  So if we are upgrading then we need to delete the old apps.
#
# *** With the exception of Server Settings.app which in the step above we preserve.
#
###
appsToRemove=('AppleShare IP Migration.app' 'Assistant.app' 'Macintosh Manager.app' 'MySQL Manager.app' 'Network Image Utility.app' 'Open Directory Assistant.app'
'Server Assistant.app' 'Server Monitor.app' 'Server Status.app' 'Workgroup Manager.app')

echo "Removing old Server Applications..."

for((x = 0; x < ${#appsToRemove[*]} ; x += 1))
do
    if [ -e "$2/Applications/Utilities/${appsToRemove[$x]}" ]; then
        rm -rf "$2/Applications/Utilities/${appsToRemove[$x]}"
    fi
done

if [ -e "$2/Applications/QuickTime Streaming Server" ]; then
    rm -rf "$2/Applications/QuickTime Streaming Server"
fi
echo "done."

#
# Add the server dock items for the admin you are logged in as.
#
#echo "Adding server Dock items..."

#ID="$2/usr/bin/id -u"
#ADDDOCKFORUID="$2/System/Library/ServerSetup/AddDockForUID.pl"
#PERL="$2/usr/bin/perl"
#if [ -e "$ADDDOCKFORUID" ] ; then
#   myID=`$ID`
#   result=`$PERL $ADDDOCKFORUID $myID`
#fi
#echo "done."
    
#
# Remove old Network Image Utility, its now called System Image Utility.app
# We can extend this later if there become more Server apps we need to remove.
#
PantherAppsToRemove=('Network Image Utility.app')
echo "Removing old Network Image Utility..."
for((x = 0; x < ${#PantherAppsToRemove[*]} ; x += 1))
do
    if [ -e "$2/Applications/Utilities/${PantherAppsToRemove[$x]}" ]; then
        rm -rf "$2/Applications/Utilities/${PantherAppsToRemove[$x]}" 
    fi
done
echo "done."


# 
# Run the common post- operations.
#  
"$1/Contents/Resources/postcommon" "$1" "$2" "$3" "$4"
