#!/bin/bash

PVA_MN_CORE=0

function checkPrerequisites()
{
	rpm -qi virtuozzo-release || is_psbm
	if [ $? -ne 0 ]; then
		abort "Virtuozzo is not installed"
	else
		service vz status
		if [ $? -ne 0 ]; then
			abort "Virtuozzo is not running"
		fi
		rm -rf ${PVA_40_45_UPGRADE_FOLDER}
		mkdir ${PVA_40_45_UPGRADE_FOLDER}
		cp -r /var/vzagent/ ${PVA_40_45_UPGRADE_FOLDER}
		echo "Removing Service Container"
		local d
		for d in `vzlist -o veid -H -h ServiceCT`; do
			echo "Stopping SVE with CTID=$d"
			MSG=`vzctl stop $d`
			[ $? -ne 0 ] && abort "Failed to stop SVE - ${MSG}"
			echo "Removing SVE with CTID=$d"
			MSG=`vzctl delete $d`
			[ $? -ne 0 ] && abort "Failed to remove SVE - ${MSG}"
		done

		echo "Cleaning up old VZagent files"
		rm -rf /var/vzagent
	fi
}

cleanup_agent()
{
	local d private
	for d in `vzlist -o veid -H`; do
		private=`getVEPrivate ${d}`
		rm -rf $private/.vza
	done
	rm -rf ${VZLCORE_ETC_DIR}/configs/*
	rm -rf ${VZLCORE_ETC_DIR}/samples/*
}

function main() 
{
	echo "------------------------------------------------------------------------------------------------"
	date "+%b %e %T PVA Agent for Virtuozzo pre install configuration : start"

	checkPrerequisites

	cleanup_agent

	date "+%b %e %T PVA Agent for Virtuozzo pre install configuration : stop"
	echo "------------------------------------------------------------------------------------------------"
}

###################### Include common files #######################

# VZL defines
scriptDir=`dirname "$0"`
# VZL defines
. ${scriptDir}/pva-agent-helper.sh || abort "Failed to include ${scriptDir}/pva-agent-helper.sh" 

############################### Go  ##############################

main $*

exit 0
