#!/bin/bash
# Last Modified: 2008-26-06 02:56:33
# Tornado Online Backup Version: 5.2.6.0 (Build:8)

# This is a script file to be executed before installation (if installation path exist)
# This will clean up the previous version's garbage

###############################################################################
#       D E F I N E           P A T H       A N D        N A M E S            #
###############################################################################

# $1 is the PKG path
PKG_HOME=$1

# $2 is the timestamp
TIMESTAMP_CLEANUP=$2

JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home
BACKUP_DIR=/tmp/obm.pkg-${TIMESTAMP_CLEANUP}

###############################################################################
#       S H U T D O W N           R E L A T E D          P R O C E S S        #
###############################################################################

echo "Log Time: ${TIMESTAMP_CLEANUP}"
echo "Begin Custom Cleanup for Tornado Online Backup"

if [ -e "/Applications/Tornado Online Backup/ipc/Scheduler/running" ]; then
	echo "Shutdown Scheduler"
	touch "/Applications/Tornado Online Backup/ipc/Scheduler/stop"
	sleep 2
fi        

sh "$PKG_HOME/Contents/Resources/taskkill" "/Applications/Tornado Online Backup/jvm/bin/java"
sh "$PKG_HOME/Contents/Resources/taskkill" "/Applications/Tornado Online Backup/aua/jvm/bin/java"
sh "$PKG_HOME/Contents/Resources/taskkill" "/Applications/Tornado Online Backup/BackupManager.app"
sh "$PKG_HOME/Contents/Resources/taskkill" "/Applications/Tornado Online Backup/bin/BackupManager.app"

###############################################################################
#     S H U T D O W N      O L D     V E R S I O N        P R O C E S S       #
###############################################################################

sh "$PKG_HOME/Contents/Resources/taskkill" "obm.jar"
sh "$PKG_HOME/Contents/Resources/taskkill" "obcs.jar"
sh "$PKG_HOME/Contents/Resources/taskkill" "AuaObmJW"

###############################################################################
#       R E M O V E        A P P L I C A T I O N         C O M P O N E N T    #
###############################################################################

# Remove Components before version 5.2.2.5
if [ -e "${JAVA_HOME}/bin/AuaJW" ]; then
	echo "Remove ${JAVA_HOME}/bin/AuaJW"
	rm -f "${JAVA_HOME}/bin/AuaJW"
fi   

if [ -e "${JAVA_HOME}/bin/AuaObmJW" ]; then
	echo "Remove ${JAVA_HOME}/bin/AuaObmJW"
	rm -f "${JAVA_HOME}/bin/AuaObmJW"
fi   

if [ -e "/Library/StartupItems/AuaObm" ]; then
	echo "Remove /Library/StartupItems/AuaObm"
	rm -rf "/Library/StartupItems/AuaObm"
fi   

# Remove Components since version 5.2.2.5
if [ -e "/Library/StartupItems/TornadoOnlineBackup" ]; then
	echo "Remove /Library/StartupItems/TornadoOnlineBackup"
	rm -rf "/Library/StartupItems/TornadoOnlineBackup"
fi   

if [ -e "/Library/StartupItems/TornadoOnlineBackup-AutoUpdate" ]; then
	echo "Remove /Library/StartupItems/TornadoOnlineBackup-AutoUpdate"
	rm -rf "/Library/StartupItems/TornadoOnlineBackup-AutoUpdate"
fi   

###############################################################################
#      R E M O V E       D E S K T O P       S Y M B O L I C      L I N K     #
###############################################################################

# Remove Desktop Symbolic Link
find /Users -name Desktop -type d -maxdepth 3 -mindepth 2 | while read DESKTOP_PATH;
do
    if [ -e "${DESKTOP_PATH}/Tornado Online Backup.app" ]; then
        echo "Remove old Desktop Link Tornado Online Backup from path ${DESKTOP_PATH}"
        rm -f "${DESKTOP_PATH}/Tornado Online Backup.app"
        echo "Removed old Tornado Online Backup Desktop Link"
    fi

    if [ -e "${DESKTOP_PATH}/Tornado Online Backup" ]; then
        echo "Remove Desktop Link Tornado Online Backup from path ${DESKTOP_PATH}"
        rm -f "${DESKTOP_PATH}/Tornado Online Backup"
        echo "Removed Tornado Online Backup Desktop Link"
    fi
done                                         

###############################################################################
#           E N D                   O F                C L E A N - U P        #
###############################################################################

echo "Finished Custom Cleanup for Tornado Online Backup"

exit 0
