#!/bin/sh

DEBIANCONFIG=/etc/default/util-vserver

if [ -f $DEBIANCONFIG ]
then
	. $DEBIANCONFIG
fi

: ${UTIL_VSERVER_VARS:=/usr/lib/util-vserver/util-vserver-vars}

test -e "$UTIL_VSERVER_VARS" || 
   {
   	echo $"Can not find util-vserver installation (the file '$UTIL_VSERVER_VARS' is expected); aborting..." >&2
   	exit 1
   }

. "$UTIL_VSERVER_VARS"

PIDFILE=/var/run/rebootmgr.pid
LOCKFILE=$MARK
vprocunhide_lockfile=/var/run/vprocunhide
export MARK NUMPARALLEL LOCKFILE

# Test to see if there are legacy configs around
if [ -f /etc/vservers/*.conf ]
then
    legacy=yes
fi

# Print the vserver name in priority/alpha order
# This function is only used for legacy configs
sortserver(){
	(
	cd $__CONFDIR
	for serv in *.conf
	do
	        test -f "$serv" || continue

		PRIORITY=100
		. $serv
		test "$ONBOOT" || continue
		printf "%03d %s\n" $PRIORITY `basename $serv .conf`
	done
	) | sort $* | (while read a b; do echo $b; done)
}

# Start vservers
# This function is only used for legacy configs
startservers(){
	cd $__CONFDIR
	for name in `sortserver`
	do
		ONBOOT=
		. $name.conf
		if [ "$ONBOOT" = "yes" ] ; then
		echo "Starting Vservers..."
			$_VSERVER_LEGACY $name start
		else
			echo "virtual server $name not configured for on boot start"
		fi
	done
}

start()
{

    # First set vserver related attributes for /proc, this
    # makes some necessary /proc entries visible in vservers

    if [ -e /proc/self/vinfo ]
    then

	# Check for vc_set_iattr feature, if its not detected, then we are probably running a 
	# 2.4 kernel where all of /proc is unhidden by default
	$_VSERVER_INFO - FEATURE iattr
	if [ $? -eq 0 ]
	then
		echo -n $"Fixing visibility of /proc entries for Vservers..."
		$_VPROCUNHIDE
		if [ $? -ne 0 ]
		then
			echo "Error in setting Vserver related attributes for /proc"
		else
			touch "$vprocunhide_lockfile"
			echo "done."
		fi
	fi

        # Remove barrier on / if it exists
        if [ -e /proc/self/vinfo ]
        then
        	showattr -d / | awk '{print $1'} | grep -q B
                if [ $? -eq 0 ]
                then
                	setattr --~barrier /
                fi
        fi

        # Then set the chroot barrier
	for vserver in `ls /etc/vservers` 
	do 
		vdiractual=`readlink -f /etc/vservers/$vserver/vdir` 
		if [ -d "$vdiractual" ]
		then
			setattr --barrier $vdiractual/.. 
		fi
	done

	vrootactual=`readlink -f /etc/vservers/.defaults/vdirbase`
	setattr --barrier $vrootactual

	# Then start up vservers with $MARK if $AUTO is set to start things
	if [ "$AUTO" = "true" ]
       	then
		exec /usr/lib/util-vserver/vserver-wrapper start;
	else
       		echo "Auto starting of Vservers is not enabled in $DEBIANCONFIG"
		echo "Please start your vservers manually with 'vserver <vserver-name> start'"
	fi

    else
    	   echo -n $"Vserver capability not detected in kernel."
    fi

    echo
}

stop()
{
    if [ -e /proc/self/vinfo ]
    then
	if [ "$AUTO" = "true" ]
	then
		exec /usr/lib/util-vserver/vserver-wrapper stop;
	else
		echo "Auto stopping of Vservers is not enabled in $DEBIANCONFIG"
		echo "Please stop your vservers manually with 'vserver <vserver-name> stop'"
	fi
    	
	rm -f "$vprocunhide_lockfile"
    else
    	echo -n $"Vserver capability not detected in kernel."
    fi

}

forcestop()
{
	echo "Stopping all running vservers..."
	for vserver in `vserver-stat | grep -v "CTX   PROC    VSZ    RSS  userTIME   sysTIME    UPTIME NAME" |grep -v ^0 | awk '{print $8}'`
	do 
		echo -n "Stopping $vserver: "
		vserver $vserver stop >& /dev/null
		echo "done."
	done
	# This is broken: /usr/lib/util-vserver/start-vservers --stop --running
}

restart()
{
    stop
    start
}

case "$1" in
    start)
          # First run the legacy starts, if there are any
          if [ "$legacy" = "yes" ]; then
	      if [ "$BACKGROUND" = "yes" ] ; then
		  startservers >/dev/tty8 </dev/tty8 2>/dev/tty8 &
	      else
		  startservers
	      fi
	      touch /var/lock/subsys/vservers-legacy
	  fi
	  # Then start the modern vservers, if there are any
	  start
          ;;
    stop)
	 # First stop the legacy vservers
         if [ "$legacy" = "yes" ]; then
             echo "Stopping Vservers"
	     cd $__CONFDIR
	     for name in `sortserver -r`
	     do
	       $_VSERVER_LEGACY $name stop
	     done
	 rm -f /var/lock/subsys/vservers-legacy
	 fi
	 # Then stop the modern vservers
	 stop
	 ;;
    forcestop)
	stop
	forcestop	
	;;
    restart)	
            $1
	    ;;
    reload)		
            ;;
    status)
           if [ "$legacy" = "yes" ]; then
	       	cd $__CONFDIR
		for serv in *.conf
		do
		  ONBOOT=no
		  name=`basename $serv .conf`
		  . $serv
		  echo -n ONBOOT=$ONBOOT " "
		  $_VSERVER_LEGACY $name running
		done
	   fi
	   vserver-info - RUNNING
	   if [ $? -eq 0 ]
	   then
	       vserver-stat
	   else
	       echo "vserver enabled kernel not detected"
	   fi

	   if [ -f $lockfile ]
	   then
	       echo $"/proc entries were fixed for vservers"
	   else
	       echo $"/proc entries are not fixed for vservers"
	   fi
	       ;;
    *)
        echo "Usage: $0 {start|stop|forcestop|reload|restart|status}"
	exit 2
	;;
esac
