#!/bin/bash

# This constant is replaced by the build system. In the result script this bool value will reflect 
# the configfuration of the PVA core. If PVA_MN_CORE is 1, then we work for PVA management node core,
# otherwise the core we deal with is for slave agent.    
PVA_MN_CORE=0 #0

function checkPrerequisites()
{
	echo "Checking prerequisites"
}

function checkSeEnforce()
{
	echo "# Checking if SELinux is enabled"

	if [ -e "/usr/sbin/setenforce" ]
	then
		echo "# Setting Permissive mode for SELinux"
		`/usr/sbin/setenforce Permissive`
		echo "# Changing config for SELinux"
		perl -pi -e 's/SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/config
	fi
}

function checkMySQLVersion()
{	
	echo "# Checking if older version of MySQL is installed"

	rpm -q --whatprovides mysql | grep mysql-4
	if [ $? -eq 0 ]; then
		abort "You can not setup PVA while MySQL 4 is installed. Please upgrade MySQL to the 5th version or uninstall it"
	fi

	rpm -q --whatprovides mysql | grep mysql-3
	if [ $? -eq 0 ]; then
		abort "You can not setup PVA while MySQL 3 is installed. Please upgrade MySQL to the 5th version or uninstall it"
	fi
}

# main
function main() 
{
	echo "------------------------------------------------------------------------------------------------"
	date "+%b %e %T PVA Agent preinstall configuration : start"

	if [[ $PVA_MN_CORE == 1 ]]; then
		checkMySQLVersion
	fi
	
	stop_vzagent_services

	checkPrerequisites

	checkSeEnforce

	date "+%b %e %T PVA Agent preinstall 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
