#!/bin/bash

if [[ $PVA_MN_CORE == 1 ]]; then
	VZAGENT=${VZAGENT=pvamnd}
	VZAGENT_CTL=${VZAGENT_CTL=pvamn}
	VZLCORE_INSTALL_DIR=/opt/pva/mn
	VZLCORE_CHANGEABLE_DIR=/var/opt/pva/mn
	VZLCORE_FHS_CHANGEABLE_DIR=$VZLCORE_CHANGEABLE_DIR
	VZLCORE_LOG_DIR=/var/log/pva/mn
	PVA_SOCKET_BASENAME=mn.sock
	PVA_TCP_PORT=4533
	PVA_SSL_PORT=4534
else
	VZAGENT=${VZAGENT=pvaagentd}
	VZAGENT_CTL=${VZAGENT_CTL=pvaagent}
	VZLCORE_INSTALL_DIR=/opt/pva/agent
	VZLCORE_CHANGEABLE_DIR=/vz/pva/agent
	if [ ${__FreeBSD__:-0} -eq 0 ]; then
		VZLCORE_FHS_CHANGEABLE_DIR=/var/opt/pva/agent
	else
		VZLCORE_FHS_CHANGEABLE_DIR=$VZLCORE_CHANGEABLE_DIR
	fi
	VZLCORE_LOG_DIR=/var/log/pva/agent
	PVA_SOCKET_BASENAME=agent.sock
	PVA_TCP_PORT=4433
	PVA_SSL_PORT=4434
fi

PVA_SOCKET_NAME=$VZLCORE_FHS_CHANGEABLE_DIR/$PVA_SOCKET_BASENAME
VZLCORE_LIB_DIR=${VZLCORE_INSTALL_DIR}/lib
VZLCORE_BIN_DIR=${VZLCORE_INSTALL_DIR}/bin
VZLCORE_ETC_DIR=${VZLCORE_FHS_CHANGEABLE_DIR}/etc
if [ $PVA_MN_CORE -eq 0 ] && [ ${__FreeBSD__:-0} -eq 0 ]; then
	x=`$VZLCORE_BIN_DIR/vzlxmlman "argv[name=etc_folder]/value" $VZLCORE_BIN_DIR/pva.conf 2>/dev/null`
	[ $? -eq 0 ] && VZLCORE_ETC_DIR=$x
fi
VZLCORE_PLUGINS_DIR=${VZLCORE_INSTALL_DIR}/plugins
VZLCORE_PSTORAGE_DIR=$VZLCORE_ETC_DIR/../.pstorage
VZLCORE_TMP_DIR=${VZLCORE_FHS_CHANGEABLE_DIR}/tmp
VZLCORE_DUMPS_DIR=${VZLCORE_FHS_CHANGEABLE_DIR}/dumps
VZLCORE_SQL_DIR=${VZLCORE_INSTALL_DIR}/db
VZAGENT_PATH=$VZLCORE_BIN_DIR/$VZAGENT
PVA_TCP_PORT_PLACEHOLDER=PVA_TCP_PORT
PVA_SSL_PORT_PLACEHOLDER=PVA_SSL_PORT
PVA_40_45_UPGRADE_FOLDER=/var/PVA_40_45_UPGRADE_FOLDER
DEFAULT_EID="00000000-0000-0000-0000-000000000002"

LANG=C
RETRY_LIMIT=20

VZAGENT_PATH="${VZA_TMPDIR}/${VZLCORE_BIN_DIR}/${VZAGENT}"
# add modified pathes to environment PATH
PATH="$PATH:${VZA_TMPDIR}/usr/sbin:${VZA_TMPDIR}/${VZLCORE_BIN_DIR}"
LD_LIBRARY_PATH="$LD_LIBRARY_PATH:${VZA_TMPDIR}/${VZLCORE_LIB_DIR}"

# mysql binaries
MYSQL_BIN=${MYSQL_BIN=mysql}
MYSQLADMIN_BIN=${MYSQLADMIN_BIN=mysqladmin}
MYSQL_SERVICE="mysqld"
if [ -e /etc/init.d/mysql ]; then
	MYSQL_SERVICE=mysql
fi

if [ -e /etc/init.d/ ]; then
	SYSTEM_SERVICE_DIR=/etc/init.d/
else
	SYSTEM_SERVICE_DIR=/etc/rc.d/init.d/
fi

PROGNAME=$0

VZASERVICE="${VZLCORE_BIN_DIR}/${VZAGENT_CTL}"

# Echo error
function error()
{
    echo "ERROR:" $* >&2
}

# Abort script execution
function abort()
{
    error "${1:-ABORTED.}"
    exit -1
}

# Check MySQL status
checkMysqlStatus()
{
    # check 'mysql' status. multiple check is needed
    # 'cause during 'vzsveinstall'
    # subsequent call 'mysqld start; srvcontrol start' is failed 
    local a=$RETRY_LIMIT
    while [ $a -ne 0 ]
    do
        if ${MYSQLADMIN_BIN} ping >> /dev/null 2>&1; then
            return 0
        fi
        sleep 1
        a=$(($a-1))
    done

	# mysql is not running
	abort "Mysql is not running"
}
    
# Execute SQL script
function execSql()
{
	echo "# Executing SQL script: $1"
	
	checkMysqlStatus

	MSG=`${MYSQL_BIN} < $1 2>&1`
	[ $? -ne 0 ] && abort "Failed to execute: $1 - ${MSG}"
}

# stop vzagent services
function stop_vzagent_services()
{
	if [ -e "${VZASERVICE}" ]
	then
		echo "# Stopping vzagent service"
		${VZASERVICE} stop
	else
		echo "# vzagent service was not found"
	fi
}

# start vzagent services
function start_vzagent_services()
{
	if [ -e "${VZASERVICE}" ]
	then
		echo "# Starting vzagent services"
		${VZASERVICE} start
	else
		echo "# vzagent service was not found"
	fi
}

function is_psbm()
{
	local msg
	msg=`rpm -q parallels-server-bm-release`
	if [ $? -eq 0 ]; then
		echo "Found '`cat /etc/redhat-release`'"
		return 0
	else
		msg=`rpm -q parallels-server-bm-mac-release`
		if [ $? -eq 0 ]; then
			echo "Found '`cat /etc/redhat-release`'"
			return 0
		fi
		echo "Parallels Server Bare Metal is not installed: '${msg}'"
		return 1
	fi
}

# enable service
function service_enable()
{
	MSG=`whereis -b chkconfig | awk '{print$2}'`
	if test -f "$MSG"; then
		local action=$1
		shift
   
		for i in $@; do
			echo "# Setting service $i $action"
			$MSG $action $i || abort "Can't configure startup of $i"
		done
	else
		abort "# Failed to find chkconfig"
	fi
}
