#!/bin/bash
#
# chkconfig: 2345 93 07
# description: Mount the Vesta repository
# config: /etc/vesta/initconfig
# config: /etc/vesta.cfg
#
### BEGIN INIT INFO
# Provides: vmount
# Required-Start: $network
# Should-Start: vrepository autofs
# X-UnitedLinux-Should-Start: vrepository autofs
# Required-Stop: $network
# Should-Stop: vrepository autofs
# X-UnitedLinux-Should-Stop: vrepository autofs
# Default-Start:2 3 4 5
# Default-Stop: 0 1 6
# Description: Mount the Vesta repository
### END INIT INFO

# Notes on ordering:
#
# - Start the Vesta repository first if it's installed on the same
# host
#
# - Start autofs first in case the configuration files are in an
# auto-mounted alias for another local filesystem
#
# Note also that we use both "Should-Start" / "Should-Stop" (which LSB
# defines) and "X-UnitedLinux-Should-Start" /
# "X-UnitedLinux-Should-Stop" (which some vendors defined before LSB
# added "Should-Start"/"Should-Stop") to be compatible with more
# systems

if [ -f /etc/vesta/initconfig ]; then
    . /etc/vesta/initconfig
else
    echo $"Vesta services are not configured"
    exit 0;
fi

# Exit if we don't have networking.
if [ -f /etc/sysconfig/network ]; then
  # RedHat style
  . /etc/sysconfig/network
  # Exit if networking isn't up.
  [ ${NETWORKING} = "no" ] && exit 0
elif [ -f /etc/network/ifstate ]; then
  # Debian style.  Assume that if any network interfaces are active
  # that we should proceed.
  [ -s /etc/network/ifstate ] || exit 0
fi

if [ -f /etc/init.d/functions ]; then
   # Source RedHat function library.
   . /etc/init.d/functions
else
   # Define an "action" function that works like the RedHat one but
   # acts like the Debian policy on init script output.
   action ()
   {
     STRING=$1
     shift
     echo -n "$STRING" 
     eval "$*"
     echo "."
   }
fi

# On RedHat we put a file in /var/lock/subsys to indicate that the
# rpeository has been mounted.
if [ -d /var/lock/subsys ]; then
  lockfile=/var/lock/subsys/vmount
fi

RETVAL=0

start() {
	action $"Mounting the Vesta repository: " mountrepos

	RETVAL=$?
	[ $RETVAL -eq 0 -a -n "$lockfile" ] && touch $lockfile
}

stop() {
	action $"Unmounting the Vesta repository: " umountrepos

	RETVAL=$?
	[ $RETVAL -eq 0 -a -n "$lockfile" ] && rm -f $lockfile
}

require_root()
{
    # Make sure we're running as root 
    if [ `id -u` -ne 0 ]; then 
	echo "$0 $1: Must be run as root"
        exit 1
    fi
}

case "$1" in
  start)
	require_root $1
	start
	;;
  stop)
	require_root $1
	stop
	;;
  restart|reload|force-reload)
	require_root $1
	stop
	start
	RETVAL=$?
	;;
  try-restart|condrestart)
	require_root $1
	# Note: this does nothing on Debian
	if [ -n "$lockfile" -a -f "$lockfile" ]; then
	    stop
	    start
	    RETVAL=$?
	fi
	;;
  status)
	repos_root=`vgetconfig UserInterface AppendableRootName`
	mutable_root=`vgetconfig UserInterface MutableRootName`
	volatile_root=`vgetconfig Run_Tool VolatileRootName`

	df $repos_root 2> /dev/null | grep -q $repos_root
	if [ $? -ne 0 ]; then
	    echo $"Vesta repository ($repos_root) is not mounted"
	    RETVAL=1
	fi
	df $mutable_root 2> /dev/null | grep -q $mutable_root
	if [ $? -ne 0 ]; then
	    echo $"Vesta repository ($mutable_root) is not mounted"
	    RETVAL=1
	fi
	df $volatile_root 2> /dev/null | grep -q $volatile_root
	if [ $? -ne 0 ]; then
	    echo $"Vesta repository ($volatile_root) is not mounted"
	    RETVAL=1
	fi
	if [ $RETVAL -eq 0 ]; then
	    echo $"Vesta repository is mounted"
	fi
	;;
  *)
	echo $"Usage: $0 {start|stop|restart|reload|force-reload|try-restart|condrestart|status}"
	exit 1
esac

exit $RETVAL

