#!/bin/bash
# Steven Shiau <steven@nchc.org.tw>
# License: GPL

# Load DRBL setting and functions
if [ ! -f "/opt/drbl/sbin/drbl-conf-functions" ]; then
  echo "Unable to find /opt/drbl/sbin/drbl-conf-functions! Program terminated!" 
  exit 1
fi
. /opt/drbl/sbin/drbl-conf-functions

# main
USAGE="Usage: $0 {start|stop|restart}"
switch=$1
# 
unalias ls 2>/dev/null

# $drbl_server_service_chklist is loaded from conf/drbl.conf
drbl_service=""
# check if the service listed exists
for iser in $drbl_server_service_chklist; do
   # For SuSE, the NFS server name is nfsserver, not nfs (which is nfs client).
   # Exclude nfs.
   [ -e /etc/SuSE-release -a "$iser" = "nfs" ] && continue
   [ -e "/etc/init.d/$iser" ] && drbl_service="$drbl_service $iser"
done
#
check_if_root

#
if [ $# -ne 1 ]; then
  echo "$USAGE"
  echo "Example: use the following to start all the DRBL services in server"
  echo "$0 start"
  exit 1
fi

 
# store the orig LC_ALL, then set it as C
LC_ALL_org=\$LC_ALL
export LC_ALL=C

case "$switch" in
   "start"|"restart")
      # rm /var/lib/nfs/rmtab to avoid a long time try when restart NFS
      [ -f /var/lib/nfs/rmtab ] && rm -f /var/lib/nfs/rmtab

      [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
      echo "Now start the service: $drbl_service"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      for serv_st in $drbl_service; do 
        if [ -e /etc/debian_version -o -e /etc/SuSE-release ] ; then
          # It's Debian/SuSE... no /var/lock/subsys/$serv_st tag, so we just restart it always.
          to_restart=yes
        else
	  # NOT debian, it's RH-like, we can use the /var/lock/subsys/$serv_st
	  if [ -f /var/lock/subsys/$serv_st ]; then
	     to_restart=yes
          else
	     to_restart=no
          fi
        fi

        RETVAL=""
        if [ "$to_restart" = "yes" ] ; then
          # service is running
	  /etc/init.d/$serv_st restart
          RETVAL=$?
        else
          # service is stopped
          /etc/init.d/$serv_st start
          RETVAL=$?
        fi 
        if [ "$RETVAL" -gt 0 ]; then
             [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
             echo "XXXXXXX        XXXXXXX       XXXXXXX"
             echo "Failed to start service $serv_st !!!"
             [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
             exit $RETVAL
        fi
      done
      
      # Turn on ip_forward now.
      if [ "`cat /proc/sys/net/ipv4/ip_forward `" != "1" ]; then
        echo "Turn on ip_forward now."
        echo 1 > /proc/sys/net/ipv4/ip_forward
      else 
        echo "ip_forward is already on."
      fi

      ;;
   "stop")
      for serv_st in $drbl_service; do 
        if [ -e /etc/debian_version -o -e /etc/SuSE-release ] ; then
          # It's Debian... no /var/lock/subsys/$serv_st tag, so we just stop it always.
	  # (or SuSE ? not sure... since SuSE uses lock file differs from that service name).
          to_stop=yes
        else
	  # NOT debian, it's RH-like
	  # We can use the /var/lock/subsys/$serv_st
	  if [ -f /var/lock/subsys/$serv_st ]; then
	     to_stop=yes
          else
	     to_stop=no
          fi
        fi
        if [ "$to_stop" = "yes" ] ; then
          # service is running
          RETVAL=0
          /etc/init.d/$serv_st stop
          RETVAL=$?
          if [ "$RETVAL" -gt 0 ]; then
               [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
               echo "XXXXXXX        XXXXXXX       XXXXXXX"
               echo "Failed to stop service $serv_st !!!"
               [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
               exit $RETVAL
          fi
        fi 
      done
      ;;
    *)
      echo "$USAGE"
esac

#restore the old LC_ALL
export LC_ALL=$LC_ALL_org
