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

#
DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/opt/drbl/}"

. $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
. $DRBL_SCRIPT_PATH/conf/drbl-ocs.conf
. $DRBL_SCRIPT_PATH/sbin/ocs-functions

# main
USAGE() {
    echo "Usage:"
    echo "$0 {start|stop|restart|on|off}"
    echo "start   Start all DRBL-related services now"
    echo "stop    Stop all DRBL-related services now"
    echo "restart Restart all DRBL-related services now"
    echo "add     Add all DRBL-related services"
    echo "del     Delete all DRBL-related services"
    echo "Example: To start all DRBL-related services in this DRBL server"
    echo "$0 start"
}

#
drbl_startup_service() {
  local srv="$1"
  local act="$2"
  local ret
  [ -z "$srv" ] && echo "No srv in function drbl_startup_service!" && exit 1
  [ -z "$act" ] && echo "No act in function drbl_startup_service!" && exit 1
  # Traditional service can be started or restart by "/etc/init.d/$srv (start|restart)
  # Upstart service (from 0.6.3 in Ubuntu 9.04), we should use: restart $srv
  if dpkg -L upstart 2>/dev/null | grep -F "/sbin/start" &>/dev/null && \
     [ -e /etc/init/${srv}.conf ] ; then
    if [ "$act" = "restart" ]; then
      # When action is restart, we check the status first, and if it's already stopped, we use "start" instead of "restart", otherwise the service won't be staretd at all. "restart $srv" will just show us: "restart: Unknown instance:"
      [ -n "$(LC_ALL=C status "$srv" | grep -Ei "stop")" ] && act="start"
    fi
    $act $srv
    ret=$?
  else
    /etc/init.d/$srv $act
    ret=$?
  fi
  return $ret
} # end of drbl_startup_service

#
switch=$1

# 
# $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
  USAGE
  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")
      # add them first
      $0 add
      # 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
      ret=""
      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

        if [ "$to_restart" = "yes" ] ; then
          # service is running
	  #/etc/init.d/$serv_st restart
	  drbl_startup_service $serv_st restart
          RETVAL=$?
        else
          # service is stopped
          #/etc/init.d/$serv_st start
	  drbl_startup_service $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
        fi
	ret=$[$ret + $RETVAL]
      done
      turn_on_ipv4_forward
      ;;
   "add")
      [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
      echo "Now add the service: $drbl_service"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      for serv in $drbl_service; do 
        if [ -e /etc/debian_version ]; then
          # Debian
          echo "Force to add $serv service in this Debian DRBL server..."
          case "$serv" in
           portmap)
             update-rc.d $serv start 18 2 3 4 5 . start 32 0 6 . stop 32 1 . &> /dev/null
             ;;
           nis)
             update-rc.d $serv start 19 2 3 4 5 . stop 19 0 1 6 . &> /dev/null
             ;;
           nfs-common)
             update-rc.d $serv start 21 2 3 4 5 . stop 79 0 1 6 . &> /dev/null
             ;;
           nfs-kernel-server)
             update-rc.d $serv start 20 2 3 4 5 . stop 80 0 1 6 . &> /dev/null
             ;;
           unfs3)
             update-rc.d $serv start 25 2 3 4 5 . stop 25 0 1 6 . &> /dev/null
             ;;
           drbl-clients-nat)
             update-rc.d drbl-clients-nat start 40 2 3 4 5 . stop 89 0 6 . &>/dev/null
             ;;
           *)
             # such as dhcp3-server, tftpd-hpa
             update-rc.d $serv defaults &> /dev/null
             ;;
          esac
        elif [ -e /etc/SuSE-release ]; then
           # SuSE
           echo "Force to add $serv service in this SuSE DRBL server..."
           insserv $serv
        else
            # RH-like
           echo "Force to add $serv service in this RH-like DRBL server..."
           chkconfig $serv on
        fi
      done
      ;;
   "del")
      # stop them first
      $0 stop
      [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
      echo "Now delete the service: $drbl_service"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      for serv in $drbl_service; do 
        if [ -e /etc/debian_version ]; then
          # Debian
          echo "Force to delete $serv service in this Debian DRBL server..."
          update-rc.d -f $serv remove &>/dev/null
        elif [ -e /etc/SuSE-release ]; then
          # SuSE
          echo "Force to delete $serv service in this SuSE DRBL server..."
          insserv -f -r $serv
        else
          # RH-like
          echo "Force to delete $serv service in this RH-like DRBL server..."
          chkconfig --del $serv
        fi
      done
      ;;
   "stop")
      ret=""
      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
	  drbl_startup_service $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
          fi
	  ret=$[$ret + $RETVAL]
        fi 
      done
      ;;
    *)
      USAGE
      ret=1
      ;;
esac

#restore the old LC_ALL
export LC_ALL=$LC_ALL_org

exit $ret
