#!/bin/bash
################################################################################
# bounceservice.sh - bounce-anywhere script for service run-on-boot setup
#  This script assumes that the service script is already installed in it's proper
#   location.

export thescriptname=`basename $0`
export installlogname=/var/log/cbu-installer.log

################################################################################
#  Requires environment variables:
#   $1 - log message
#   $2 - line number
#   installlogname - log file name
function logmsg
{
  export linenumber=''
  if [ ! -z "$2" ]; then
    linenumber=":$2"
  fi
  echo "$( date '+%F %H:%M:%S' ) ${thescriptname}${linenumber} $1" >> ${installlogname}
}

################################################################################
# Source the service type setup.
#  Requires environment variables:
#   $1 - Path to /tmp folder where we extracted the installer files
#   $2 - Name of the service we are bouncing
if [ $# -ne 2 ]; then
  logmsg "ERROR: invalid number of parameters specified" ${LINENO}
  exit 1
fi
export tmppath=$1
export servicename=$2
logmsg "bouncing ${servicename} from ${tmppath}" ${LINENO}
if [ ! -d ${tmppath}/ ]; then
  logmsg "ERROR: cannot locate path to tar installer: ${tmppath}" ${LINENO}
  exit 1
fi
. ${tmppath}/setservicetype.sh ${servicename}
if [ $? -ne 0 ]; then
  logmsg "ERROR: cannot source ${tmppath}/setservicetype.sh" ${LINENO}
  exit 1
fi

################################################################################
# Bounce the service on this machine.
#  Requires environment variables:
#   myplatform - Code for this platform
#   servicename - Name of service we are bouncing
expiration=$(( `date +'%s'` + 30 ))
while [ `date +'%s'` -lt ${expiration} ]; do
logmsg "Processing platform ${myplatform}" ${LINENO}
case ${myplatform} in
  arch)
    systemctl status ${servicename} > /dev/null 2>&1
    if [ $? -eq 0 ]; then
      logmsg "${servicename} already running so stopping first" ${LINENO}
      systemctl stop ${servicename}
      sleep 3
    else
      logmsg "${servicename} was not running -- no need to stop it" ${LINENO}
    fi
    logmsg "now starting ${servicename}" ${LINENO}
    systemctl restart ${servicename}
    if [ $? -eq 0 ]; then
      yesnoresult=''
    else
      yesnoresult='not '
    fi
    logmsg "${servicename} was ${yesnoresult}started" ${LINENO}
    if [ -z "${yesnoresult}" ]; then
      expiration=`date +'%s'`
    fi
    ;;
  centos)
    service ${servicename} status > /dev/null 2>&1
    if [ $? -eq 0 ]; then
      logmsg "${servicename} already running so stopping first" ${LINENO}
      service ${servicename} stop
      sleep 3
    else
      logmsg "${servicename} was not running -- no need to stop it" ${LINENO}
    fi
    logmsg "now starting ${servicename}" ${LINENO}
    service ${servicename} restart
    if [ $? -eq 0 ]; then
      yesnoresult=''
    else
      yesnoresult='not '
    fi
    logmsg "${servicename} was ${yesnoresult}started" ${LINENO}
    if [ -z "${yesnoresult}" ]; then
      expiration=`date +'%s'`
    fi
    ;;
  fedora)
    systemctl status ${servicename} > /dev/null 2>&1
    if [ $? -eq 0 ]; then
      logmsg "${servicename} already running so stopping first" ${LINENO}
      systemctl stop ${servicename}
      sleep 3
    else
      logmsg "${servicename} was not running -- no need to stop it" ${LINENO}
    fi
    logmsg "now starting ${servicename}" ${LINENO}
    systemctl restart ${servicename}
    expiration=`date +'%s'`
    if [ $? -eq 0 ]; then
      yesnoresult=''
    else
      yesnoresult='not '
    fi
    logmsg "${servicename} was ${yesnoresult}started" ${LINENO}
    if [ -z "${yesnoresult}" ]; then
      expiration=`date +'%s'`
    fi
    ;;
  redhat)
    /etc/init.d/${servicename} status > /dev/null 2>&1
    if [ $? -eq 0 ]; then
      logmsg "${servicename} already running so stopping first" ${LINENO}
      /etc/init.d/${servicename} stop
      sleep 3
    else
      logmsg "${servicename} was not running -- no need to stop it" ${LINENO}
    fi
    logmsg "now starting ${servicename}" ${LINENO}
    /etc/init.d/${servicename} restart
    expiration=`date +'%s'`
    if [ $? -eq 0 ]; then
      yesnoresult=''
    else
      yesnoresult='not '
    fi
    logmsg "${servicename} was ${yesnoresult}started" ${LINENO}
    if [ -z "${yesnoresult}" ]; then
      expiration=`date +'%s'`
    fi
    ;;
  alma)
    logmsg "now starting ${servicename}" ${LINENO}
    ps -ef | grep "/${servicename} " | grep daemon | grep -v grep >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      which systemctl > /dev/null 2>&1
      if [ $? -eq 0 ]; then
        logmsg "systemctl starting ${servicename}" ${LINENO}
        rm -f /etc/init.d/${servicename}
        systemctl daemon-reload
        systemctl restart ${servicename}
      fi
    fi
    ps -ef | grep "/${servicename} " | grep daemon | grep -v grep >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      which service > /dev/null 2>&1
      if [ $? -eq 0 ]; then
        logmsg "service starting ${servicename}" ${LINENO}
        service ${servicename} restart
      fi
    fi
    ps -ef | grep "/${servicename} " | grep daemon | grep -v grep >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      if [ -f /etc/init.d/${servicename} ]; then
        logmsg "init.d starting ${servicename}" ${LINENO}
        /etc/init.d/${servicename} restart
      fi
    fi
    ps -ef | grep "/${servicename} " | grep daemon | grep -v grep >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      logmsg "manually starting ${servicename}" ${LINENO}
      nohup ${servicename} --daemon 2>&1
    fi
    ps -ef | grep "/${servicename} " | grep daemon | grep -v grep >/dev/null 2>&1
    if [ $? -eq 0 ]; then
      yesnoresult=''
    else
      yesnoresult='not '
    fi
    logmsg "${servicename} was ${yesnoresult}started" ${LINENO}
    if [ -z "${yesnoresult}" ]; then
      expiration=`date +'%s'`
    fi
    ;;
  rocky)
    logmsg "now starting ${servicename}" ${LINENO}
    ps -ef | grep "/${servicename} " | grep daemon | grep -v grep >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      which systemctl > /dev/null 2>&1
      if [ $? -eq 0 ]; then
        logmsg "systemctl starting ${servicename}" ${LINENO}
        rm -f /etc/init.d/${servicename}
        systemctl daemon-reload
        systemctl restart ${servicename}
      fi
    fi
    ps -ef | grep "/${servicename} " | grep daemon | grep -v grep >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      which service > /dev/null 2>&1
      if [ $? -eq 0 ]; then
        logmsg "service starting ${servicename}" ${LINENO}
        service ${servicename} restart
      fi
    fi
    ps -ef | grep "/${servicename} " | grep daemon | grep -v grep >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      if [ -f /etc/init.d/${servicename} ]; then
        logmsg "init.d starting ${servicename}" ${LINENO}
        /etc/init.d/${servicename} restart
      fi
    fi
    ps -ef | grep "/${servicename} " | grep daemon | grep -v grep >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      logmsg "manually starting ${servicename}" ${LINENO}
      nohup ${servicename} --daemon 2>&1
    fi
    ps -ef | grep "/${servicename} " | grep daemon | grep -v grep >/dev/null 2>&1
    if [ $? -eq 0 ]; then
      yesnoresult=''
    else
      yesnoresult='not '
    fi
    logmsg "${servicename} was ${yesnoresult}started" ${LINENO}
    if [ -z "${yesnoresult}" ]; then
      expiration=`date +'%s'`
    fi
    ;;
  debian)
    logmsg "now starting ${servicename}" ${LINENO}
    ps -ef | grep "/${servicename} " | grep daemon | grep -v grep >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      which systemctl > /dev/null 2>&1
      if [ $? -eq 0 ]; then
        logmsg "systemctl starting ${servicename}" ${LINENO}
        rm -f /etc/init.d/${servicename}
        systemctl daemon-reload
        systemctl restart ${servicename}
      fi
    fi
    ps -ef | grep "/${servicename} " | grep daemon | grep -v grep >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      which service > /dev/null 2>&1
      if [ $? -eq 0 ]; then
        logmsg "service starting ${servicename}" ${LINENO}
        service ${servicename} restart
      fi
    fi
    ps -ef | grep "/${servicename} " | grep daemon | grep -v grep >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      if [ -f /etc/init.d/${servicename} ]; then
        logmsg "init.d starting ${servicename}" ${LINENO}
        /etc/init.d/${servicename} restart
      fi
    fi
    ps -ef | grep "/${servicename} " | grep daemon | grep -v grep >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      logmsg "manually starting ${servicename}" ${LINENO}
      nohup ${servicename} --daemon 2>&1
    fi
    ps -ef | grep "/${servicename} " | grep daemon | grep -v grep >/dev/null 2>&1
    if [ $? -eq 0 ]; then
      yesnoresult=''
    else
      yesnoresult='not '
    fi
    logmsg "${servicename} was ${yesnoresult}started" ${LINENO}
    if [ -z "${yesnoresult}" ]; then
      expiration=`date +'%s'`
    fi
    ;;
  ubuntu)
    logmsg "now starting ${servicename}" ${LINENO}
    ps -ef | grep "/${servicename} " | grep daemon | grep -v grep >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      which systemctl > /dev/null 2>&1
      if [ $? -eq 0 ]; then
        logmsg "systemctl starting ${servicename}" ${LINENO}
        rm -f /etc/init.d/${servicename}
        systemctl daemon-reload
        systemctl restart ${servicename}
      fi
    fi
    ps -ef | grep "/${servicename} " | grep daemon | grep -v grep >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      which service > /dev/null 2>&1
      if [ $? -eq 0 ]; then
        logmsg "service starting ${servicename}" ${LINENO}
        service ${servicename} restart
      fi
    fi
    ps -ef | grep "/${servicename} " | grep daemon | grep -v grep >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      if [ -f /etc/init.d/${servicename} ]; then
        logmsg "init.d starting ${servicename}" ${LINENO}
        /etc/init.d/${servicename} restart
      fi
    fi
    ps -ef | grep "/${servicename} " | grep daemon | grep -v grep >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      logmsg "manually starting ${servicename}" ${LINENO}
      nohup ${servicename} --daemon 2>&1
    fi
    ps -ef | grep "/${servicename} " | grep daemon | grep -v grep >/dev/null 2>&1
    if [ $? -eq 0 ]; then
      yesnoresult=''
    else
      yesnoresult='not '
    fi
    logmsg "${servicename} was ${yesnoresult}started" ${LINENO}
    if [ -z "${yesnoresult}" ]; then
      expiration=`date +'%s'`
    fi
    ;;
  gentoo)
    /etc/init.d/${servicename} status > /dev/null 2>&1
    if [ $? -eq 0 ]; then
      logmsg "${servicename} already running so stopping first" ${LINENO}
      rc-service ${servicename} stop
      sleep 3
    else
      logmsg "${servicename} was not running -- no need to stop it" ${LINENO}
    fi
    logmsg "now starting ${servicename}" ${LINENO}
    rc-service ${servicename} restart
    expiration=`date +'%s'`
    if [ $? -eq 0 ]; then
      yesnoresult=''
    else
      yesnoresult='not '
    fi
    logmsg "${servicename} was ${yesnoresult}started" ${LINENO}
    if [ -z "${yesnoresult}" ]; then
      expiration=`date +'%s'`
    fi
    ;;
  *)
    logmsg "ERROR: unknown linux flavor" ${LINENO}
    expiration=`date +'%s'`
    ;;
esac
done

