#!/bin/sh
# @(#) eloquence.lsb - B.08.30 ($Revision: 28.2 $)
# Eloquence startup/shutdown script, LSB Linux
#
# (C) Copyright Marxmeier Software AG, 2003-2018
#
# This file is installed as /etc/init.d/eloq83

### BEGIN INIT INFO
# Provides: eloquence83
# Required-Start: $network $remote_fs $syslog
# Required-Stop: $network $local_fs $syslog
# Default-Start: 2 3 5
# Default-Stop: 0 1 6
# Short-Description: Start the Eloquence daemons
# Description: Start/stop the Eloquence daemons
### END INIT INFO

# save script name
script=${0##*/}

# define path, in case this is executed from user environment
PATH=/sbin:/bin:/usr/bin

# source LSB functions
if [ ! -s /lib/lsb/init-functions ]; then
   echo "ERROR: /lib/lsb/init-functions file missing."
   exit 1
fi
. /lib/lsb/init-functions

# common eloquence rc functions
ELOQUENCE=${ELOQUENCE:=/opt/eloquence/8.3}
if [ ! -s ${ELOQUENCE}/etc/rcfunctions ] ; then
   log_failure_msg "ERROR: ${ELOQUENCE}/etc/rcfunctions file missing."
   exit 5
fi
. ${ELOQUENCE}/etc/rcfunctions

# script exit status
typeset -i rval=0

# source configuration file

RCFILE=${RCFILE:=/etc/sysconfig/eloquence83}
if [ ! -s $RCFILE ]; then
   log_failure_msg "ERROR: Configuration file $RCFILE does not exist."
   exit 6
fi

. $RCFILE
if [ $? -ne 0 ]; then
   log_failure_msg "Syntax error in $RCFILE configuration file."
   exit 1
fi

# check if binaries are installed

if [ $START_ELOQSD -ne 0 -a ! -x $ELOQSD ]; then
   log_failure_msg "ERROR: $ELOQSD is not executable or doesn't exist."
   exit 5
fi
if [ $START_ELOQDB -ne 0 -a ! -x $ELOQDB ]; then
   log_failure_msg "ERROR: $ELOQDB is not executable or doesn't exist."
   exit 5
fi

# check if $pid_dir exists and has proper permissions

if [ $START_ELOQSD -ne 0 -o $START_ELOQDB -ne 0 ]; then
   if [ ! -d $pid_dir ]; then
      echo "ERROR: $pid_dir is not a directory or does not exist."
      exit 5
   fi
   if [ ! -k $pid_dir -o ! -r $pid_dir -o ! -w $pid_dir -o ! -x $pid_dir ]; then
      echo "ERROR: directory $pid_dir has wrong permissions."
      exit 4
   fi
fi

### functions ###

eq_act_msg1=eq_act_msg1
eq_act_msg2=eq_act_msg2
eq_stat_msg1=eq_stat_msg1
eq_stat_msg2=eq_stat_msg2

function eq_warn_msg
{
   log_warning_msg "$*"
}

eq__msg_pfx=""
function eq_act_msg1
{
   eq__msg_pfx="$*"
}

function eq_act_msg2
{
   typeset -i _rc
   
   _rc=$1

# program is not running which is success if we stop/restart service
   if [ $eq_todo = stop -o "$eq_todo" = restart ]; then
      [ $_rc -eq $eq_rc_notrunning ] && _rc=0
   fi

   case $_rc in
   0) log_success_msg "$eq__msg_pfx" ;;
   1) log_failure_msg "$eq__msg_pfx" ;;
   7) log_warning_msg "$eq__msg_pfx (not running)" ;;
   *) log_failure_msg "$eq__msg_pfx (unknown $_rc)" ;;
   esac
}

function eq_stat_msg1
{
   eq__msg_pfx="$*"
}

function eq_stat_msg2
{
   typeset -i _rc
   
   _rc=$1
   case $_rc in
   $eq_stat_ok)
      ;;
   $eq_stat_stopped) 
      [ $rval -eq $eq_stat_ok ] && rval=$eq_stat_stopped ;;
   $eq_stat_deadpid) 
      [ $rval -eq $eq_stat_ok ] && rval=$eq_stat_deadpid ;;
   *)
      rval=$eq_stat_unknown ;;
   esac

   case $_rc in
   $eq_stat_ok) 
      log_success_msg "$eq__msg_pfx (running)" ;;
   $eq_stat_deadpid)
      log_warning_msg "$eq__msg_pfx (not running)" ;;
   $eq_stat_stopped)
       log_success_msg "$eq__msg_pfx (unused)" ;;
   *)
       log_failure_msg "$eq__msg_pfx (unknown $_rc)" ;;
   esac
}


### main ###

eq_todo="${1:-NOTSET}"
if [ $eq_todo != NOTSET ]; then
   shift
   eq_arg_list="$@"
fi

case "$eq_todo" in
start)
   # root capabilities are required to start Eloquence
   [ $START_STOP_AS_ROOT != 0 ] && eq_is_not_root && {
      log_failure_msg "ERROR: root privileges required."
      exit 4
   }
   # START_ELOQ is only used if no arguments are present
   [ -z "$eq_arg_list" ] && [ $START_ELOQ = 0 -o $START_ELOQ = no ] && {
      log_warning_msg "Note: Eloquence not configured for automatic startup"
      exit 0
   }
   eq_eloqsd_start
   eq_foreach eq_eloqdb_start
   eq_foreach_repl eq_repl_start
   eq_check_args
   ;;

stop)
   # root capabilities are required to stop Eloquence
   [ $START_STOP_AS_ROOT != 0 ] && eq_is_not_root && {
      log_failure_msg "ERROR: root privileges required."
      exit 4
   }
   eq_eloqsd_stop
   eq_foreach eq_eloqdb_stop
   eq_foreach_repl eq_repl_stop
   eq_verify_terminated
   eq_check_args
   ;;

restart|force-reload)
   # root capabilities are required to restart Eloquence
   [ $START_STOP_AS_ROOT != 0 ] && eq_is_not_root && {
      log_failure_msg "ERROR: root privileges required."
      exit 4
   }
   eq_eloqsd_stop
   eq_foreach eq_eloqdb_stop
   eq_foreach_repl eq_repl_stop
   eq_verify_terminated   
   eq_eloqsd_start
   eq_foreach eq_eloqdb_start
   eq_foreach_repl eq_repl_start
   eq_check_args
   ;;

status)
   eq_eloqsd_status
   eq_foreach eq_eloqdb_status
   eq_foreach_repl eq_repl_status
   eq_check_args
   ;;

info) 
   if [ -z "$eq_arg_list" ]; then
      echo "Gobal configuration:"
      echo " START_ELOQ = $START_ELOQ"
      echo " START_ELOQSD = $START_ELOQSD"
      echo " ELOQSD_ARGS = $ELOQSD_ARGS"
      echo " ELOQSD_RUNPFX = $ELOQSD_RUNPFX"
      echo " START_ELOQDB = $START_ELOQDB"
      echo " ELOQDB_DEFAULT_ARGS = $ELOQDB_DEFAULT_ARGS"
      echo " START_STOP_AS_ROOT = $START_STOP_AS_ROOT"
      echo 

      echo "Database server instances:"
      cnt=${#ELOQDB_CFG[*]}
      echo " Number of instances: $cnt"
   fi
   eq_foreach eq_eloqdb_info

   if [ -z "$eq_arg_list" ]; then
      echo "Database replication instances:"
      cnt=${#REPL_CFG[*]}
      echo " Number of instances: $cnt"
   fi
   eq_foreach_repl eq_repl_info
   eq_check_args
   ;;

reload)
   log_failure_msg "WARNING: reload not implemented."
   exit 3
   ;;

*)
   echo "Usage: $script <action> [<what> ...]"
   echo "action:={start|stop|status|restart|info}"
   echo "what:={eloqsd|eloqdb instance id|repl instance id}"
   exit 2
   ;;
esac

# echo debug output

if [ "$eq_todo" = status ]; then
   [ $rval -eq $eq_stat_ok -a $eq_rval != $eq_rc_ok ] && rval=$eq_stat_unknown
else
   [ $rval = 0 ] && rval=$eq_rval;
   if [ "$eq_todo" != info -a $rval = $eq_rc_failed ]; then
      echo -e "\n--- Log messages ---$eq_rmsg\nExit code: $rval"
      [ -d /var${ELOQUENCE} -a "$(id -u)" -eq 0 ] &&
      echo -e "$script: $eq_todo $eq_arg_list\n--- Log messages ---$eq_rmsg\nExit code: $rval" > /var${ELOQUENCE}/rc.log
   fi
fi

exit $rval
