#!/bin/bash

# Copyright (C) 2012 NTT DATA Corporation
# 
# This program is free software; you can redistribute it and/or
# Modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, version 2.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details

# bash configuration
SCRIPT_DIR=$(cd $(dirname $0);pwd)
. ${SCRIPT_DIR}/install.cfg || exit 255
. /opt/hinemos/hinemos.cfg

export PROG=`basename $0`
DIR=`dirname $0`
USER=`/usr/bin/whoami`
HOST=`hostname`

########################################
#  Local Variable
########################################

LANGUAGE=$1

########################################
#  Local Message
########################################

# load language file
if [ ! -e ${SCRIPT_DIR}/lib/${LANGUAGE}_uninstall.lng ]
then
	echo "not supported language : ${LANGUAGE}"
	exit 255
fi
. ${SCRIPT_DIR}/lib/${LANGUAGE}_uninstall.lng

########################################
# Function
########################################

function EchoOK()
{
	echo " [OK]"
}

function EchoNG()
{
	echo " [NG]"
}

function Welcome()
{
	if [ -e ${SCRIPT_DIR}/hinemos/_version ]
	then
		VERSION=`cat ${SCRIPT_DIR}/hinemos/_version`
	elif [ -e ${SCRIPT_DIR}/../_version ]
	then
		VERSION=`cat ${SCRIPT_DIR}/../_version`
	else
		VERSION=`UNKNOWN`
	fi
	echo
	echo "-------------------------------------------------------"
	echo "     Uninstaller for Hinemos Manager"
	echo "                             Version ${VERSION}"
	echo
	echo "       Copyright (C) 2013 NTT DATA Corporation"
	echo "-------------------------------------------------------"
}

function ConfirmInstallation()
{
	echo
	while [ 1 ]
	do
		read -p "`echo_msg MSG_I001` : " INPUT
		INPUT=${INPUT:-N}
		case ${INPUT} in
			y|Y)
				break
				;;
			n|N)
				echo_msg MSG_I003
				return 255
				;;
			*)
				echo_msg MSG_W001
				;;
		esac
	done
	return 0
}

function CheckEnvironment()
{
	# check user executed
	echo
	echo_msg_n MSG_I002
	if [ "x"${USER} != "xroot" ]
	then
		EchoNG
		echo_msg MSG_E002 && echo_msg MSG_E003
		return 255
	fi
	EchoOK
	
	# check 
	echo
	echo_msg_n MSG_I024
	if ! [ -d /opt/hinemos ]
	then
		EchoNG
		echo_msg MSG_E012 && echo_msg MSG_E003
		return 255
	fi
	EchoOK
	
	# check process
	echo
	echo_msg MSG_I010
	if [ -f ${JVM_PID_FILE} ]
	then
		read JVM_PID < ${JVM_PID_FILE}
		if [ "x"${JVM_PID} != "x" ] && [ `ps --no-headers --pid ${JVM_PID} e | grep "${JAVA_HOME}/bin/java.*com.clustercontrol.HinemosManagerMain" | wc -l` -eq 0 ]
		then
			JVM_PID=""
		fi
	fi
	if [ -f ${PG_PID_FILE} ]
	then
		PG_PID=`head -n 1 ${PG_PID_FILE}`
		if [ "x"${PG_PID} != "x" ] && [ `ps --no-headers --pid ${PG_PID} e | grep "${PG}/bin/postgres -D ${PGDATA}" | wc -l` -eq 0 ]
		then
			PG_PID=""
		fi
	fi
	if [ "x${JVM_PID}" != "x" ] || [ "x${PG_PID}" != "x" ]
	then
		echo_msg MSG_E005
		return 255
	fi
	
	# check agent existence
	echo
	echo_msg_n MSG_I022
	if [ `find /opt -mindepth 1 -maxdepth 1 -type d -path "/opt/hinemos_agent*" | wc -l` -ne 0 ]
	then
		EchoNG
		echo_msg MSG_E008 && echo_msg MSG_E009
		return 255
	fi
	EchoOK
	
	return 0
}

function RemoveUser()
{
	echo
	echo_msg MSG_I012
	id ${HINEMOS_OS_USER} > /dev/null 2>&1
	if [ $? -ne 0 ]
	then
		echo_msg MSG_I013
		return 0
	fi
	
	while [ 1 ]
	do
		read -p "`echo_msg MSG_I009` : " INPUT
		INPUT=${INPUT:-Y}
		case ${INPUT} in
			y|Y)
				break
				;;
			n|N)
				echo_msg MSG_W004
				return 0
				;;
			*)
				echo_msg MSG_W001
				;;
		esac
	done
	
	userdel -f -r ${HINEMOS_OS_USER}
	if [ $? -ne 0 ]
	then
		echo_msg MSG_E006
		return 255
	fi
	echo_msg MSG_I011
	
	return 0
}

function RemoveFiles()
{
	echo
	echo_msg MSG_I014
	if [ ! -d ${HINEMOS_HOME} ]
	then
		echo_msg MSG_I015
		return 0
	fi
	
	while [ 1 ]
	do
		read -p "`echo_msg MSG_I016` : " INPUT
		INPUT=${INPUT:-Y}
		case ${INPUT} in
			y|Y)
				break
				;;
			n|N)
				echo_msg MSG_W005
				return 0
				;;
			*)
				echo_msg MSG_W001
				;;
		esac
	done
	
	rm -rf ${HINEMOS_HOME}
	if [ $? -ne 0 ]
	then
		echo_msg MSG_E007
		return 255
	fi
	echo_msg MSG_I017
	
	return 0
}

function DisplayNotice()
{
	echo
	echo_msg MSG_I018
	echo_msg MSG_I019
	echo_msg MSG_I020
}

function SIGINTExit()
{
	echo
	echo_msg MSG_E011
	exit 255
}

function echo_msg()
{
	local PARAM=$1
	
	if [ -e ${SCRIPT_DIR}/lib/${LANGUAGE}_uninstall.lng ]
	then
		. ${SCRIPT_DIR}/lib/${LANGUAGE}_uninstall.lng
	fi
	
	eval 'echo "${'${PARAM}'}"'
}

function echo_msg_n()
{
	local PARAM=$1
	echo -n "`echo_msg ${PARAM}`"
}

########################################
# SHELL
########################################

#
# check parameters
#
if [ $# -ne 1 ]; then
	echo_msg MSG_E001
	exit 255
fi

# define trap
trap SIGINTExit 2

# print welcome
Welcome

# confirm installation
ConfirmInstallation || exit 254

# check environment
CheckEnvironment|| exit 253

# remove user
RemoveUser

# remove files
RemoveFiles

# display notice
DisplayNotice

echo && echo_msg MSG_I021 && echo

exit 0