# A few compatibility functions, to make it possible having the same
# OpenVZ init scripts for Red Hat/Fedora/Debian/SUSE.

VZCONF=/etc/vz/vz.conf
[ -f ${VZCONF} ] || exit 0
. ${VZCONF}
[ "${VIRTUOZZO}" = "no" ] && exit 0

VZCTL=/usr/sbin/vzctl
[ -x ${VZCTL} ] || exit 0

rc_done='..done'
rc_failed='..failed'

VARLOCK=/var/lock/subsys

# Source function library.
if [ -r /etc/init.d/functions ]; then
	source /etc/init.d/functions
	if [ -r /etc/redhat-release ] || [ -r /etc/centos-release ]; then
		DISTR=redhat
	fi
elif [ -r /etc/rc.status ]; then
	source /etc/rc.status
	if [ -r /etc/SuSE-release ]; then
		DISTR=suse
	fi
elif [ -r /etc/debian_version ]; then
	DISTR=debian
	VARLOCK=/var/lock
fi

print_success()
{
	if [ "$DISTR" = "redhat" ]; then
		echo_success
	else
		echo -n "$rc_done"
	fi
	echo
}

print_failure()
{
	echo -n "$1"
	if [ "$DISTR" = "redhat" ]; then
		failure $"$1"
	else
		echo -n "$rc_failed"
	fi
	echo
}

print_warning()
{
	if [ "$DISTR" = "redhat" ]; then
		echo -n "$1"
		warning $"$1"
	else
		echo -n "- Warning: $1 "
	fi
	echo
}

# Calls either print_success or print_failure, depending on $?
# Optional argument $1 -- an error string passed to print_failure.
print_result()
{
	if [ $? -eq 0 ] ; then
		print_success
	else
		print_failure "$1"
	fi
}

__echo()
{
	if [ "$DISTR" = "redhat" ]; then
		echo -n $"$1"
	else
		echo -n "$1"
	fi
}

vzdaemon_start()
{
	case $DISTR in
	   redhat)
		daemon $*
		;;
	   suse)
		startproc $*
		;;
	   debian)
		P=$1
		shift 1
		start-stop-daemon --start --exec /usr/sbin/$P -- $*
		;;
	esac
}

vzdaemon_stop()
{
	case $DISTR in
	   redhat|suse)
		killproc $*
		;;
	   debian)
		P=$1
		shift 1
		start-stop-daemon --stop --exec /usr/sbin/$P -- $*
		[ $? -eq 0 ] && echo "Stopped"
		;;
	esac
}

vzdaemon_status()
{
	case $DISTR in
	   redhat|suse)
		echo "$P: Status check not supported on redhat and suse."
		;;
	   debian)
		P=$1
		shift 1
		start-stop-daemon --start -t --quiet --exec /usr/sbin/$P -- $*
		RETVAL=$?
		if [ $RETVAL -eq 0 ]; then
			echo "Checking status of $P... (not running)"
	        else
			echo "Checking status of $P... (running)"
		fi
		return $RETVAL
		;;
	esac
}
