#!/bin/bash
# @(#) eloquence - B.08.40 ($Revision: 29.6 $)
# Eloquence startup/shutdown script
#
# (C) Copyright Marxmeier Software AG, 2003-2024
#

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

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

# terminal messages
if [ -t 1 -a "$TERM" != "raw" -a "$TERM" != "dumb" ]; then
   stat=$(echo -en "\033[71G")
   norm=$(echo -en "\033[m")
   extd=$(echo -en "\033[1m")
   warn=$(echo -en "\033[1;31m")
   done=$(echo -en "\033[1;32m")
   attn=$(echo -en "\033[1;33m")
   rc_done="${stat}${done}done${norm}"
   rc_running="${stat}${done}running${norm}"
   rc_failed="${stat}${warn}failed${norm}"
   rc_skipped="${stat}${attn}skipped${norm}"
   rc_dead="${stat}${warn}dead${norm}"
   rc_unused="${stat}${extd}unused${norm}"
   rc_unknown="${stat}${attn}unknown${norm}"
else
   rc_done="[done]"
   rc_running="[running]"
   rc_failed="[failed]"
   rc_skipped="[skipped]"
   rc_dead="[dead]"
   rc_unused="[unused]"
   rc_unknown="[unknown]"
fi

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

# script exit status
typeset -i rval=0

# source configuration file

RCFILE=${RCFILE:=/etc/opt${ELOQUENCE:=/opt/eloquence/8.4}/eloquence.rc}

if [ -s $RCFILE ]; then
   . $RCFILE
   if [ $? -ne 0 ]; then
      echo "Syntax error in configuration file. $rc_failed"
      echo "       $RCFILE"
      exit 1
   fi
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 "$* $rc_failed"
   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 "$rc_done" ;;
   1) echo "$rc_failed" ;;
   7) echo "$rc_dead" ;;
   *) echo "$rc_failed ($_rc)" ;;
   esac
}

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 "$rc_running" ;;
   $eq_stat_deadpid)
      echo "$rc_dead" ;;
   $eq_stat_stopped)
       echo "$rc_unused" ;;
   *)
       echo "$rc_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 && {
      echo "ERROR: root privileges required. $rc_failed" 
      exit 4
   }
   # START_ELOQ is only used if no arguments are present
   [ -z "$eq_arg_list" ] && [ $START_ELOQ = 0 -o $START_ELOQ = no ] && {
      echo "Note: Eloquence not configured for automatic startup. $rc_skipped"
      exit 0
   }
   eq_eloqld_start
   eq_eloqsd_start
   eq_foreach eq_eloqdb_start
   eq_foreach_repl eq_repl_start
   eq_eloqwebd2_start
   eq_check_args
   ;;

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

restart)
   # root capabilities are required to restart Eloquence
   [ $START_STOP_AS_ROOT != 0 ] && eq_is_not_root && {
      echo "ERROR: root privileges required. $rc_failed"
      exit 4
   }
   eq_eloqsd_stop
   eq_foreach eq_eloqdb_stop
   eq_foreach_repl eq_repl_stop
   eq_eloqwebd2_stop
   eq_eloqld_stop
   eq_verify_terminated
   eq_eloqld_start
   eq_eloqsd_start
   eq_foreach eq_eloqdb_start
   eq_foreach_repl eq_repl_start
   eq_eloqwebd2_start
   eq_check_args
   ;;

status)
   eq_eloqsd_status
   eq_foreach eq_eloqdb_status
   eq_foreach_repl eq_repl_status
   eq_eloqwebd2_status
   eq_eloqld_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_ELOQWEBD2 = $START_ELOQWEBD2"
      echo " ELOQWEBD2_ARGS = $ELOQWEBD2_ARGS"
      echo " START_ELOQLD = $START_ELOQLD"
      echo " ELOQLD_ARGS = $ELOQLD_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
   ;;

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