#!/bin/sh
# @(#) eloquence.redhat - B.08.30 ($Revision: 28.2 $)
# Eloquence startup/shutdown script, RedHat Linux
#
# (C) Copyright Marxmeier Software AG, 2003-2018
#
# This file is installed as /etc/rc.d/init.d/eloq83
#
# chkconfig: 2345 80 30
# description: Eloquence is an application runtime environment \
#              This startup script is used to manage the Eloquence \
#              daemons eloqsd and eloqdb

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

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

# Source RedHat function library
SYSTEMCTL_SKIP_REDIRECT=1
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0

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

# script exit status
typeset -i rval=0

# source configuration file

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

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

# check if binaries are installed

if [ $START_ELOQSD -ne 0 -a ! -x $ELOQSD ]; then
   echo -n "ERROR: $ELOQSD is not executable or doesn't exist."
   echo_failure
   echo
   exit 1
fi
if [ $START_ELOQDB -ne 0 -a ! -x $ELOQDB ]; then
   echo -n "ERROR: $ELOQDB is not executable or doesn't exist."
   echo_failure
   echo
   exit 1
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
{
   if [ -t 1 ]; then
      echo -n "$*"
      echo_failure
      echo
   else
      echo "$*"
   fi
}

function eq_act_msg1
{
   if [ -t 1 ]; then
      printf "%-65s" "$*"
   else
      echo -n "$*"
   fi
}

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) echo_success ;;
   1) echo_failure ;;
   7) echo_warning ;;
   *) echo -n " ($_rc)"; echo_failure ;;
   esac
   echo
}

function eq_stat_msg1
{
   if [ -t 1 ]; then
      printf "%-65s" "$*"
   else
      echo -n "$*"
   fi
}

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

   if [ ! -t 1 ]; then
      echo
      return
   fi

   case $_rc in
   $eq_stat_ok) 
      echo_success ;;
   $eq_stat_deadpid)
      echo_failure ;;
   $eq_stat_stopped)
       echo_warning ;;
   *)
       echo -n " ($_rc)"
       echo_failure ;;
   esac
   echo
}


### 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 && {
      echo -n "ERROR: root privileges required." 
      echo_failure
      echo
      exit 1
   }
   # START_ELOQ is only used if no arguments are present
   [ -z "$eq_arg_list" ] && [ $START_ELOQ = 0 -o $START_ELOQ = no ] && {
      echo -n "Note: Eloquence not configured for automatic startup."
      echo_warning
      echo
      exit 0
   }
   eq_eloqsd_start
   eq_foreach eq_eloqdb_start
   eq_foreach_repl eq_repl_start
   eq_check_args
   [ "$(id -u)" -eq 0 ] && touch /var/lock/subsys/eloq83
   ;;

stop)
   # root capabilities are required to stop Eloquence
   [ $START_STOP_AS_ROOT != 0 ] && eq_is_not_root && {
      echo -n "ERROR: root privileges required."
      echo_failure
      echo
      exit 1
   }
   eq_eloqsd_stop
   eq_foreach eq_eloqdb_stop
   eq_foreach_repl eq_repl_stop
   eq_verify_terminated
   eq_check_args
   [ -z "$eq_arg_list" -a "$(id -u)" -eq 0 ] && rm -f /var/lock/subsys/eloq83
   ;;

restart|force-reload)
   # root capabilities are required to restart Eloquence
   [ $START_STOP_AS_ROOT != 0 ] && eq_is_not_root && {
      echo -n "ERROR: root privileges required."
      echo_failure
      echo
      exit 1
   }
   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
   [ "$(id -u)" -eq 0 ] && touch /var/lock/subsys/eloq83
   ;;

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)
   echo -n "WARNING: reload not implemented."
   echo_warning
   echo
   exit 1
   ;;

*)
   echo "Usage: $script <action> [<what> ...]"
   echo "action:={start|stop|status|restart|info}"
   echo "what:={eloqsd|eloqdb instance id|repl instance id}"
   exit 1
   ;;
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

[ $rval -ne 0 ] && rval=1
exit $rval
