#!/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
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 start ${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} start
    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 start ${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} start
    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
    ;;
  debian)
    systemctl daemon-reload ${servicename}
    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 start ${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
    ;;
  ubuntu)
    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 start ${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
    ;;
  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} start
    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

