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

# This is a script file to be executed during installation

###############################################################################
#       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_INSTALL=$2

BACKUP_DIR=/tmp/obm.pkg-${TIMESTAMP_INSTALL}

###############################################################################
#        B A C K - U P            S E T T I N G             F I L E S         #
###############################################################################

echo "Log Time: ${TIMESTAMP_INSTALL}"
echo "Begin Custom Installation for Tornado Online Backup"

if [ -e "/Applications/Tornado Online Backup" ]; then
	echo "/Applications/Tornado Online Backup/ exists!"
	echo "Now backup directory /Applications/Tornado Online Backup"

	# Backup Required Components

	if [ -e "${BACKUP_DIR}" ]; then
		echo "Remove previous cache"
		rm -rf "${BACKUP_DIR}"
	fi        

	mkdir "${BACKUP_DIR}"

	if [ -d "/Applications/Tornado Online Backup/log" ]; then
		echo "Backup Scheduler log files"
		mkdir "${BACKUP_DIR}/log"
		cd "/Applications/Tornado Online Backup/log"
		mv ./* "${BACKUP_DIR}/log/"
	fi        

	if [ -f "/Applications/Tornado Online Backup/home.txt" ]; then
		echo "Backup user home setting file"
		mv "/Applications/Tornado Online Backup/home.txt" "${BACKUP_DIR}/home.txt"
	fi        

	if [ -f "/Applications/Tornado Online Backup/config.ini" ]; then
		echo "Backup application config file"
		mv "/Applications/Tornado Online Backup/config.ini" "${BACKUP_DIR}/config.ini"
	fi        

	# Application Home
	echo "Remove /Applications/Tornado Online Backup"
	cd "/Applications/Tornado Online Backup"
	rm -rf ./*
else
	echo "/Applications/Tornado Online Backup/ does not exist, create Application Home now"
	mkdir "/Applications/Tornado Online Backup"
	echo "/Applications/Tornado Online Backup/ created"
fi

###############################################################################
#         E X P A N D          A P P L I C A T I O N          D A T A         #
###############################################################################

echo "Extract Application data from $PKG_HOME/Contents/Resources/app-data.tar.gz to /Applications/Tornado Online Backup"
tar -zxvf "$PKG_HOME/Contents/Resources/app-data.tar.gz" -C "/Applications/Tornado Online Backup"
echo "Application data extracted"

if [ -d "${BACKUP_DIR}" ]; then
	echo "Restore Previous Setting backup"

	if [ -d "${BACKUP_DIR}/log" ]; then
		echo "Restore Scheduler log files"
		mkdir "/Applications/Tornado Online Backup/log/"
		cd "/Applications/Tornado Online Backup/log/"
		mv -f ${BACKUP_DIR}/log/* .
	fi      

	if [ -f "${BACKUP_DIR}/home.txt" ]; then
		echo "Restore user home setting file"
		mv -f "${BACKUP_DIR}/home.txt" "/Applications/Tornado Online Backup/home.txt" 
	fi        

	if [ -f "${BACKUP_DIR}/config.ini" ]; then
		echo "Restore application config file"
		mv -f "${BACKUP_DIR}/config.ini" "/Applications/Tornado Online Backup/config.ini" 
	fi        

	rm -rf "${BACKUP_DIR}"
	echo "Previous Setting backup restored"
fi

echo "Copy Taskkill utility to Application Home"
cp "$PKG_HOME/Contents/Resources/taskkill" "/Applications/Tornado Online Backup/bin/taskkill"
echo "Taskkill utility is available at: /Applications/Tornado Online Backup/bin/taskkill"

echo "Restore /Applications/Tornado Online Backup folder permissions"
chmod -R 775 "/Applications/Tornado Online Backup"
echo "Restored default 775 permission at /Applications/Tornado Online Backup"

if [ ! -d "/Library/StartupItems" ]; then
	mkdir "/Library/StartupItems"
fi        

###############################################################################
#         I N S T A L L         S C H E D U L E R          S E R V I C E      #
###############################################################################

echo "Create StartupItem : Scheduler for Tornado Online Backup"
mkdir "/Library/StartupItems/TornadoOnlineBackup"
cd "/Applications/Tornado Online Backup/bin/TornadoOnlineBackup"
cp -R * "/Library/StartupItems/TornadoOnlineBackup/"
chown -R root:wheel "/Library/StartupItems/TornadoOnlineBackup"
echo "StartupItem Created at /Library/StartupItems/TornadoOnlineBackup"

###############################################################################
#       I N S T A L L       A U T O - U P D A T E         S E R V I C E       #
###############################################################################

echo "Create StartupItem : AutoUpdateAgent for Tornado Online Backup"
mkdir "/Library/StartupItems/TornadoOnlineBackup-AutoUpdate"
cd "/Applications/Tornado Online Backup/aua/bin/TornadoOnlineBackup-AutoUpdate"
cp -R * "/Library/StartupItems/TornadoOnlineBackup-AutoUpdate/"
chown -R root:wheel "/Library/StartupItems/TornadoOnlineBackup-AutoUpdate"
echo "StartupItem Created at /Library/StartupItems/TornadoOnlineBackup-AutoUpdate"

###############################################################################
#             E N D            O F            I N S T A L L A T I O N         #
###############################################################################

echo "Finished Custom Installation for Tornado Online Backup"

exit 0
