#!/bin/bash
################################################################################
# Filename: ./src/base/qrm-uninstall
# 
# The contents of this file are subject to the Qlusters Public License
# Version 1.1 (the License); you may not use this file except in compliance
# with the License.
# You may obtain a copy of the License at
# http://www.openqrm.org/content/view/33/69/.
# 
# Software distributed under the License is distributed on an AS IS basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License.
# 
# The Original Code is openQRM.
# 
# The Initial Developer of the Original Code is Qlusters Corporation. Portions
# created by Qlusters Corporation are Copyright (C) 2003-2005. All Rights
# Reserved.
# 
# Contributor(s):
# 
# Description:
# 
# Comments:
################################################################################
/usr/bin/clear

# Qrm uninstall procedure

export QRM_SERVER_BASE_DIR=`pushd \`dirname $0\`/../ 1>/dev/null && pwd && popd 1>/dev/null`
CONFIG_FILE=$QRM_SERVER_BASE_DIR/qrm/etc/qrm.ini

if [ ! -f "$CONFIG_FILE" ]; then
	echo "ERROR: Could not find the main configuration file $CONFIG_FILE"
	exit 1
fi

# check if there is something to uninstall
if [ ! -h /etc/init.d/qrm-server ]; then
	echo "ERROR: Qrm-server is not installed on this system"
	exit 1
fi

# include functions
. $QRM_SERVER_BASE_DIR/qrm/include/qrm-functions
. $QRM_SERVER_BASE_DIR/qrm/include/qrm-setup-functions
. $QRM_SERVER_BASE_DIR/qrm/include/qrm-server-functions
qrm_get_server_config 1>/dev/null

# log
QRM_UNINSTALLATION_LOGFILE=$QRM_SERVER_LOG_DIR/installation.log
mkdir -p $QRM_SERVER_LOG_DIR
exec 1> >(tee -a $QRM_UNINSTALLATION_LOGFILE) 2> >(tee -a $QRM_UNINSTALLATION_LOGFILE)
START_TIME=`date +"%m.%d.%Y_%H.%M.%S"`
echo "$0 started at $START_TIME" >> $QRM_UNINSTALLATION_LOGFILE
trap "set_install_log_end_time Uninstallation $QRM_UNINSTALLATION_LOGFILE" SIGKILL
trap "set_install_log_end_time Uninstallation $QRM_UNINSTALLATION_LOGFILE" EXIT


echo "#####################################################"
echo "#                                                   #"
echo "#                                                   #"
echo "#               Welcome to QRM 2.1                  #"
echo "#               Uninstall procedure                 #"
echo "#                                                   #"
echo "#                                  by Qlusters Inc  #"
echo "#                                                   #"
echo "#####################################################"
echo "(logfile for the uninstallation at $QRM_UNINSTALLATION_LOGFILE)"

# check for user root
WHOAMI=`whoami`
if [ "$WHOAMI" != "root" ]; then
	echo
	echo "ERROR: You have to be root to uninstall QRM"
	echo
	exit 1
fi

# confirm
echo
echo
echo
echo "     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "	WARNING !"
echo "  The next step will remove/uninstall Qrm from this system!"
echo
echo "     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo
echo -en "Do you want to continue ? (y/n)"
read CONFIRM
while [ "$CONFIRM" != "y" ] && [ "$CONFIRM" != "Y" ]; do
	if  [ "$CONFIRM" == "n" ] || [ "$CONFIRM" == "N" ]; then
		echo
		echo "NOTICE: QRM Uninstallation procedure canceled"
		echo
		exit 1
	fi
	echo -en "Do you want to continue ? (y/n)"
	read CONFIRM
done

# stop service
echo
echo "Stopping QRM-server"
QRM_INIT_SCRIPT=$QRM_SERVER_BASE_DIR/qrm/etc/init.d/qrm-server 
$QRM_INIT_SCRIPT stop 1>/dev/null 2>&1

echo "Uninstalling plugins"
for PLUGIN in $QRM_SERVER_BASE_DIR/qrm/plugins/*; do
	if [ -d $PLUGIN ]; then
		PLUGIN=$(basename $PLUGIN)
		echo "Uninstalling $PLUGIN"
		qrm_plugin $PLUGIN uninstall || echo "Failed to uninstall $PLUGIN" >&2
	fi
done

if [ "$(readlink /etc/init.d/qrm-server)" == $QRM_INIT_SCRIPT ]; then
	# remove from runlevels
	qrm_chkconfig del qrm-server

	# restore backups

	/bin/rm -f /etc/init.d/qrm-server 1>/dev/null 2>&1
fi

echo
echo "############################################"
echo "QRM uninstallation finished successfully"
echo "############################################"
echo



