#!/bin/sh
# @(#) sqlr40.redhat - A.04.00
# SQL/R startup/shutdown script, RedHat Linux
#
# (C) Copyright Marxmeier Software AG, 2010-2022
#
# This file is installed as /etc/init.d/sqlr40
#
# chkconfig: 2345 80 30
# description: SQL/R is an extension to Eloquence that provides \
#              SQL access to the database \
#              This startup script is used to manage the SQL/R \
#              sqlrodbcd daemon

# 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 SQL/R rc functions
SQLR=${SQLR:=/opt/sqlr/4.0}
if [ ! -s ${SQLR}/etc/rcfunctions ] ; then
   echo -n "ERROR: ${SQLR}/etc/rcfunctions file missing."
   echo_warning
   echo
   exit 1
fi
. ${SQLR}/etc/rcfunctions

# script exit status
typeset -i rval=0

# source configuration file

RCFILE=/etc/sysconfig/sqlr40
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 "Sytax error in $RCFILE configuration file."
   echo_failure
   echo
   exit 1
fi

# check if binaries are installed

if [ $START_SQLR -ne 0 -a ! -x $ODBCD ]; then
   echo -n "ERROR: $ODBCD is not executable or doesn't exist."
   echo_failure
   echo
   exit 1
fi

### functions ###

sqlr_act_msg1=sqlr_act_msg1
sqlr_act_msg2=sqlr_act_msg2
sqlr_stat_msg1=sqlr_stat_msg1
sqlr_stat_msg2=sqlr_stat_msg2

function sqlr_warn_msg
{
   if [ -t 1 ]; then
      echo -n "$*"
      echo_failure
      echo
   else
      echo "$*"
   fi
}

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

function sqlr_act_msg2
{
   typeset -i _rc
   
   _rc=$1

# program is not running which is success if we stop/restart service
   if [ $sqlr_todo = stop -o "$sqlr_todo" = restart ]; then
      [ $_rc -eq $sqlr_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 sqlr_stat_msg1
{
   if [ -t 1 ]; then
      printf "%-65s" "$*"
   else
      echo -n "$*"
   fi
}

function sqlr_stat_msg2
{
   typeset -i _rc
   
   _rc=$1
   case $_rc in
   $sqlr_stat_ok)
      ;;
   $sqlr_stat_stopped) 
      [ $rval -eq $sqlr_stat_ok ] && rval=$sqlr_stat_stopped ;;
   $sqlr_stat_deadpid) 
      [ $rval -eq $sqlr_stat_ok ] && rval=$sqlr_stat_deadpid ;;
   *)
      rval=$sqlr_stat_unknown ;;
   esac

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

   case $_rc in
   $sqlr_stat_ok) 
      echo_success ;;
   $sqlr_stat_deadpid)
      echo_failure ;;
   $sqlr_stat_stopped)
       echo_warning ;;
   *)
       echo -n " ($_rc)"
       echo_failure ;;
   esac
   echo
}


### main ###

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

case "$sqlr_todo" in
start)
   [ $START_STOP_AS_ROOT != 0 ] && sqlr_is_not_root && {
      echo -n "ERROR: root privileges required." 
      echo_failure
      echo
      exit 1
   }
   # START_SQLR is only used if no arguments are present
   [ -z "$sqlr_arg_list" ] && [ $START_SQLR = 0 ] && {
      echo -n "Note: SQL/R not configured for automatic startup."
      echo_warning
      echo
      exit 0
   }
   sqlr_foreach sqlr_odbcd_start
   sqlr_check_args
   [ $(id -u) = 0 ] && touch /var/lock/subsys/sqlr40
   ;;

stop)
   [ $START_STOP_AS_ROOT != 0 ] && sqlr_is_not_root && {
      echo -n "ERROR: root privileges required."
      echo_failure
      echo
      exit 1
   }
   sqlr_foreach sqlr_odbcd_stop
   sqlr_verify_terminated
   sqlr_check_args
   [ $(id -u) = 0 ] && rm -f /var/lock/subsys/sqlr40
   ;;

restart|force-reload)
   [ $START_STOP_AS_ROOT != 0 ] && sqlr_is_not_root && {
      echo -n "ERROR: root privileges required."
      echo_failure
      echo
      exit 1
   }
   sqlr_foreach sqlr_odbcd_stop
   sqlr_verify_terminated   
   sqlr_foreach sqlr_odbcd_start
   sqlr_check_args
   [ $(id -u) = 0 ] && touch /var/lock/subsys/sqlr40
   ;;

status)
   sqlr_foreach sqlr_odbcd_status
   sqlr_check_args
   ;;

info) 
   if [ -z "$sqlr_arg_list" ]; then
      echo "Gobal configuration:"
      echo " START_SQLR = $START_SQLR"
      echo " START_STOP_AS_ROOT = $START_STOP_AS_ROOT"
      echo 

      echo "sqlrodbcd instances:"
      cnt=${#ODBCD_CFG[*]}
      echo " Number of instances: $cnt"
   fi
   sqlr_foreach sqlr_odbcd_info
   sqlr_check_args
   ;;

reload)
   echo -n "WARNING: reload not implemented."
   echo_warning
   echo
   exit 1
   ;;

*)
   echo "Usage: $script <action> [<instance> ...]"
   echo "action:={start|stop|status|restart|info}"
   echo "instance:={sqlrodbc|instance name ...}"
   exit 1
   ;;
esac

# echo debug output

if [ "$sqlr_todo" = status ]; then
   [ $rval -eq $sqlr_stat_ok -a $sqlr_rval != $sqlr_rc_ok ] && rval=$sqlr_stat_unknown
else
   [ $rval = 0 ] && rval=$sqlr_rval;
   if [ "$sqlr_todo" != info -a $rval = $sqlr_rc_failed ]; then
      echo -e "\n--- Log messages ---$sqlr_rmsg\nExit code: $rval"
      [ -d /var/${SQLR} -a $(id -u) = 0 ] &&
      echo -e "$script: $sqlr_todo\n--- Log messages ---$sqlr_rmsg\nExit code: $rval" > /var/${SQLR}/rc.log
   fi
fi

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