#!/bin/sh
#
# uninstall-darwin.sh used to remove SSWF from your
# hard disk
# Written by Alexis Wilke
#
# Copyright (c) 2004-2009 Made to Order Software Corp.
#
# Permission is hereby granted, free of charge, to any
# person obtaining a copy of this software and
# associated documentation files (the "Software"), to
# deal in the Software without restriction, including
# without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom
# the Software is furnished to do so, subject to the
# following conditions:
#
# The above copyright notice and this permission notice
# shall be included in all copies or substantial
# portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
# ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
# LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
# EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#


set -e

echo "This script will remove SSWF from your Macintosh"
echo "(This script MUST be run from the mounted .dmg file or CD if you burned it)"
echo
echo "INTERESTING IN RUNNING A DRY RUN? Use -n on the command line"
echo
echo "WARNING: files will be DELETED even if you modified them;"
echo "         in other words, you will lose any of your changes."
echo
echo "Note: for the removal to succeed you need to be root"
echo "      thus you need to run this script as root or use 'sudo';"
echo "      for instance:"
echo "          su"
echo "          install.sh"
echo "      or:"
echo "          sudo install.sh"
echo "      To use 'su' you need to know the root password. To use"
echo "      sudo, you need to enter your own password and have"
echo "      administrative rights."
echo
echo "If you want to proceed, type 'yes' at the prompt and then enter."
echo "For files which changed, you will be prompted for confirmation."


dryrun=false
if test ! -z "$1"
then
	if test "$1" = "-n"
	then
		dryrun=true
	else
		echo "ERROR: unknown option $1"
		exit 1;
	fi
fi


# You have 10 minutes to answer or it cancels
answer="no"
read -p "So (yes/[no])? " -t 600 answer

if test "$answer" != "yes"
then
	echo "WARNING: Removal aborted"
	exit 1;
fi

p=`dirname "$0"`
cd "$p/sswf-"*

# Files which have been changed/disappeared?
# (note that softlinks aren't tested nor directories)
echo "Testing files for modifications or previous removal..."
found=false
for f in `find usr -type f`;
do
	if ! test -f /$f
	then
		echo "/$f is missing"
		found=true
	elif ! cmp -s /$f $f
	then
		echo "/$f was modified"
		found=true
	fi
done
if $found
then
	echo "WARNING: some of the files to be removed are missing or have been modified!"
	read -p "Do you want to continue (yes/[no])? " -t 600 answer

	if test "$answer" != "yes"
	then
		echo "WARNING: Removal aborted"
		exit 1;
	fi
fi

# For now, the following doesn't delete the sub-directories
if $dryrun
then
	echo "Listing the files to be deleted from your hard disk..."
else
	echo "Removing the files from your hard disk..."
fi
for f in `find usr -type f` `find usr -type l`
do
	if $dryrun
	then
		echo rm -rf /$f
	else
		if ! rm -rf /$f
		then
			# We want to continue on errors
			# but warn the user
			echo "/$f could not be removed"
		fi
	fi
done
