#!/bin/bash

trap "" 15

ARCH=x64
AGENT_LIB_LINK=/usr/local/lib/jlib64
CONFIRM_ENFORCE=yes

AGENT_PATH=`cat /var/log/version_control`

cd $AGENT_PATH

$PROG_TYPE
name=`cat command_control | grep script_server: | awk '{print $2}'`
if [ "$name" = "yunsuo" ];then
	PROG_TYPE="normal"
elif [ "$name" = "jhse" ];then
	PROG_TYPE="jhse"
elif [ "$name" = "anyu" ];then
	PROG_TYPE="anyu"
elif [ "$name" = "wsssr" ];then
	PROG_TYPE="wsssr"
elif [ "$name" = "hps" ];then
	PROG_TYPE="hps"
elif [ "$name" = "wappsec" ];then
	PROG_TYPE="wappsec"
else
	PROG_TYPE="gov"
fi

# get password
if [ $# -gt 0 ];then
	while getopts "p:" opt
	do
		case $opt in
			p)	uninstall_passwd=$OPTARG ;;
		esac
	done
fi


function confirm_uninstall()
{
	while true
	do
		echo -n "Are you sure to uninstall?(y/n)"
		read confirm

		if [ ! -z "$confirm" ];then
			break
		fi
	done

	if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ];then
		echo "Exit Uninstall program!"
		exit
	fi
}

export LC_ALL="C"
export LANG="C"
export JHSE_SAFE_PATH=$AGENT_PATH

if [ "$CONFIRM_ENFORCE" = "yes" ];then
	confirm_uninstall
fi

if [ -f $AGENT_PATH/ubuntu ];then
	OS_TYPE=ubuntu
elif [ -f $AGENT_PATH/suse ];then
	OS_TYPE=suse
else
	OS_TYPE=centos
fi

# confirm password
./upsd $uninstall_passwd
if [ $? != 0 ];then
	echo "Uninstall password is incorrect!"
	exit
fi

# 解除绑定
$AGENT_PATH/register_cloud_center unbind

# 卸载云锁
# 生成configure_file文件, 该文件内容为具体的配置信息
echo "2:$PROG_TYPE:$ARCH:$AGENT_PATH:$OS_TYPE" > configure_file
./setup_configure -c setup.xml -l runlog/setup.log -e "ALL" -t "ALL" -s safe -m cmdline 2>/dev/null

rm -rf $AGENT_LIB_LINK
rm -rf $AGENT_PATH

echo ""
echo "Uninstall Success."
