#!/bin/bash
# @(#) sqlr40.debian - A.04.00
# SQL/R startup/shutdown script, Debian (and derivates)
#
# (C) Copyright Marxmeier Software AG, 2010-2022
#
# This file is installed as /etc/init.d/sqlr40

### BEGIN INIT INFO
# Provides: sqlr40
# Required-Start: $network $remote_fs $syslog
# Required-Stop: $network $local_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start the SQL/R daemon
# Description: Start/stop the SQL/R daemon
### 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
SYSTEMCTL_SKIP_REDIRECT=true
_SYSTEMCTL_SKIP_REDIRECT=true
if [ ! -s /lib/lsb/init-functions ]; then
   echo "ERROR: /lib/lsb/init-functions file missing."
   exit 1
fi
. /lib/lsb/init-functions

# common SQL/R rc functions
SQLR=${SQLR:=/opt/sqlr/4.0}
if [ ! -s ${SQLR}/etc/rcfunctions ] ; then
   log_failure_msg "ERROR: ${SQLR}/etc/rcfunctions file missing."
   exit 5
fi
. ${SQLR}/etc/rcfunctions

# script exit status
typeset -i rval=0

# source configuration file

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

# check if binaries are installed

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

# check if $pid_dir exists and has proper permissions

if [ $START_SQLR -ne 0 ]; then
   if [ ! -d $pid_dir ]; then
      log_failure_msg "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
      log_failure_msg "ERROR: directory $pid_dir has wrong permissions."
      exit 5
   fi
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
{
   log_warning_msg "$*"
}

sqlr__msg_pfx=""
function sqlr_act_msg1
{
   sqlr__msg_pfx="$*"
}

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) log_success_msg "$sqlr__msg_pfx" ;;
   1) log_failure_msg "$sqlr__msg_pfx" ;;
   7) log_warning_msg "$sqlr__msg_pfx (not running)" ;;
   *) log_failure_msg "$sqlr__msg_pfx (unknown $_rc)" ;;
   esac
}

function sqlr_stat_msg1
{
   sqlr__msg_pfx="$*"
}

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

   case $_rc in
   $sqlr_stat_ok) 
      log_success_msg "$sqlr__msg_pfx (running)" ;;
   $sqlr_stat_deadpid)
      log_warning_msg "$sqlr__msg_pfx (not running)" ;;
   $sqlr_stat_stopped)
       log_success_msg "$sqlr__msg_pfx (unused)" ;;
   *)
       log_failure_msg "$sqlr__msg_pfx (unknown $_rc)" ;;
   esac
}


### 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 && {
      log_failure_msg "ERROR: root privileges required."
      exit 4
   }
   # START_SQLR is only used if no arguments are present
   [ -z "$sqlr_arg_list" ] && [ $START_SQLR = 0 ] && {
      log_warning_msg "Note: SQL/R not configured for automatic startup."
      exit 0
   }
   sqlr_foreach sqlr_odbcd_start
   sqlr_check_args
   ;;

stop)
   [ $START_STOP_AS_ROOT != 0 ] && sqlr_is_not_root && {
      log_failure_msg "ERROR: root privileges required."
      exit 4
   }
   sqlr_foreach sqlr_odbcd_stop
   sqlr_verify_terminated
   sqlr_check_args
   ;;

restart|force-reload)
   [ $START_STOP_AS_ROOT != 0 ] && sqlr_is_not_root && {
      log_failure_msg "ERROR: root privileges required."
      exit 4
   }
   sqlr_foreach sqlr_odbcd_stop
   sqlr_verify_terminated   
   sqlr_foreach sqlr_odbcd_start
   sqlr_check_args
   ;;

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)
   log_failure_msg "WARNING: reload not implemented."
   exit 3
   ;;

*)
   echo "Usage: $script <action> [<instance> ...]"
   echo "action:={start|stop|status|restart|info}"
   echo "instance:={sqlrodbc|instance name ...}"
   exit 2
   ;;
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

exit $rval
