#!/bin/sh
#
# vservers:	starts and stops vservers
#
# chkconfig:	345 98 02
# description:	Wrapper to start and stop vservers
#		This script does not care for vservers not started by it
#
# Copyright 1999-2004 Gentoo Foundation
# Modified for PLD by Jan Rkorajski <baggins@pld-linux.org>
# Distributed under the terms of the GNU General Public License v2
#

# Source function library
. /etc/rc.d/init.d/functions

[ -n "$UTIL_VSERVER_VARS" ] || UTIL_VSERVER_VARS=/usr/lib/util-vserver/util-vserver-vars
if [ ! -e "$UTIL_VSERVER_VARS" ] ; then
	echo "Can not find util-vserver installation (the file '$UTIL_VSERVER_VARS' would be expected); aborting..." >&2
	exit 1
fi
. "$UTIL_VSERVER_VARS"

[ -f /etc/sysconfig/vservers ] && . /etc/sysconfig/vservers

[ -n "$NUMPARALLEL" ] || NUMPARALLEL=1
[ -n "$LOCKDIR" ] || LOCKDIR="/var/lock/vservers"

_tellResult()
{
	local rc=$1
	deltext
	case "$rc" in
	  0) ok;;
	  2) ok; rc=0;;
	  *) fail;;
	esac
	return $rc
}

case "$1" in
start)
	if [ ! -f /var/lock/subsys/vprocunhide ]; then
		echo "Run \"/sbin/service vprocunhide start\" first"
		exit 1
	fi

	if is_yes "$STARTALL"; then
		if [ ! -f /var/lock/subsys/vservers-all ]; then
			show "Starting all types of vservers"
			busy
			$_START_VSERVERS -j $NUMPARALLEL --all --start
			_tellResult $?
			rc=$?
			[ $rc -eq 0 ] && touch /var/lock/subsys/vservers-all
		else
			echo "All types of vservers are already startred"
		fi
	else
		for MARK in $MARKS; do
			if [ -f /var/lock/subsys/vservers-$MARK ]; then
				echo "Vservers of type '$MARK' are already startred"
				continue
			fi
			show "Starting vservers of type '$MARK'"
			busy
			$_START_VSERVERS -m $MARK -j $NUMPARALLEL --all --start
			_tellResult $?
			rc=$?
			[ $rc -eq 0 ] && touch /var/lock/subsys/vservers-$MARK
		done
	fi

	for VSERVER in $START_VSERVERS; do
		if [ -f /var/lock/subsys/vserver-$VSERVER ]; then
			echo "Vserver '$VSERVER' is already startred"
			continue
		fi
		show "Starting single vserver '$VSERVER'"
		busy
		$_VSERVER $VSERVER start
		_tellResult $?
		rc=$?
		[ $rc -eq 0 ] && touch /var/lock/subsys/vserver-$VSERVER
	done
	touch /var/lock/subsys/vservers
	;;
stop)
	__STOP_VSERVERS=
	for __V in $START_VSERVERS; do
		__STOP_VSERVERS="$__V $__STOP_VSERVERS"
	done
	for VSERVER in $__STOP_VSERVERS; do
		if [ ! -f /var/lock/subsys/vserver-$VSERVER ]; then
			echo "Vserver '$VSERVER' is not running"
			continue
		fi
		show "Stopping single vserver '$VSERVER'"
		busy
		$_VSERVER $VSERVER stop
		_tellResult $?
		rc=$?
		rm -f /var/lock/subsys/vserver-$VSERVER
	done

	if is_yes "$STARTALL"; then
		if [ -f /var/lock/subsys/vservers-all ]; then
			show "Stopping all types of vservers"
			busy
			$_START_VSERVERS -j $NUMPARALLEL --all --stop
			_tellResult $?
			rc=$?
			rm -f /var/lock/subsys/vservers-all
		else
			echo "All types of vservers are not running"
		fi
	else
		__STOP_MARKS=
		for __V in $MARKS; do
			__STOP_MARKS="$__V $__STOP_MARKS"
		done
		for MARK in $__STOP_MARKS; do
			if [ ! -f /var/lock/subsys/vservers-$MARK ]; then
				echo "Vservers of type '$MARK' are not running"
				continue
			fi
			show "Stopping vservers of type '$MARK'"
			busy
			$_START_VSERVERS -m $MARK -j $NUMPARALLEL --all --stop
			_tellResult $?
			rc=$?
			rm -f /var/lock/subsys/vservers-$MARK
		done
	fi
	rm -f /var/lock/subsys/vservers
	;;
status)
	echo "The following types of vservers are running:"

	for i in /var/lock/subsys/vservers-*; do
		[ -f "$i" ] || continue
		echo $i | awk '{gsub("/var/lock/subsys/vservers-",""); printf("	%s\n",$0); }'
		running="true"
	done

	if [[ "${running}" != "true" ]]; then
		echo "  none"
	fi

	echo
	echo "/proc/virtual/ says these are running:"

	for i in /proc/virtual/*; do
		[ -d $i ] || continue
		NAME=$( basename $( vuname -g --xid $( basename ${i} ) CONTEX ) )
		echo "  ${NAME}"
	done

	echo
	echo "vserver-stat says these are running:"
	/usr/sbin/vserver-stat
	;;
*)
	msg_usage "$0 {start|stop|status}"
	exit 3
	;;
esac

exit $RETVAL

# This must be last line !
# vi:syntax=sh
