#!/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
#   installlogname - log file name
function logmsg
{
  echo "$( date '+%F %H:%M:%S' ) ${thescriptname} $1" >> ${installlogname}
  echo "$1"
}

################################################################################
# Source the service type setup.
#
if [ $# -ne 2 ]; then
  logmsg "ERROR: invalid number of parameters specified" >&2
  exit 1
fi
export tmppath=$1
export servicename=$2
logmsg "bouncing ${servicename} from ${tmppath}"
if [ ! -d ${tmppath}/ ]; then
  logmsg "ERROR: cannot locate path to tar installer: ${tmppath}" >&2
  exit 1
fi
. ${tmppath}/setservicetype.sh ${servicename}
if [ $? -ne 0 ]; then
  logmsg "ERROR: cannot source ${tmppath}/setservicetype.sh" >&2
  exit 1
fi

################################################################################
# Bounce the service on this machine.
#
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"
      systemctl stop ${servicename}
      sleep 3
    else
      logmsg "${servicename} was not running -- no need to stop it"
    fi
    logmsg "now starting ${servicename}"
    systemctl start ${servicename}
    if [ $? -eq 0 ]; then
      yesnoresult=''
    else
      yesnoresult='not '
    fi
    logmsg "${servicename} was ${yesnoresult}started"
    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"
      service ${servicename} stop
      sleep 3
    else
      logmsg "${servicename} was not running -- no need to stop it"
    fi
    logmsg "now starting ${servicename}"
    service ${servicename} start
    if [ $? -eq 0 ]; then
      yesnoresult=''
    else
      yesnoresult='not '
    fi
    logmsg "${servicename} was ${yesnoresult}started"
    if [ -z "${yesnoresult}" ]; then
      expiration=`date +'%s'`
    fi
    ;;
  "fedora")
    /etc/init.d/${servicename} status > /dev/null 2>&1
    if [ $? -eq 0 ]; then
      logmsg "${servicename} already running so stopping first"
      /etc/init.d/${servicename} stop
      sleep 3
    else
      logmsg "${servicename} was not running -- no need to stop it"
    fi
    logmsg "now starting ${servicename}"
    /etc/init.d/${servicename} start
    expiration=`date +'%s'`
    if [ $? -eq 0 ]; then
      yesnoresult=''
    else
      yesnoresult='not '
    fi
    logmsg "${servicename} was ${yesnoresult}started"
    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"
      /etc/init.d/${servicename} stop
      sleep 3
    else
      logmsg "${servicename} was not running -- no need to stop it"
    fi
    logmsg "now starting ${servicename}"
    /etc/init.d/${servicename} start
    expiration=`date +'%s'`
    if [ $? -eq 0 ]; then
      yesnoresult=''
    else
      yesnoresult='not '
    fi
    logmsg "${servicename} was ${yesnoresult}started"
    if [ -z "${yesnoresult}" ]; then
      expiration=`date +'%s'`
    fi
    ;;
  "debian")
    systemctl daemon-reload
    systemctl status ${servicename} > /dev/null 2>&1
    if [ $? -eq 0 ]; then
      logmsg "${servicename} already running so stopping first"
      systemctl stop ${servicename}
      sleep 3
    else
      logmsg "${servicename} was not running -- no need to stop it"
    fi
    logmsg "now starting ${servicename}"
    systemctl start ${servicename}
    expiration=`date +'%s'`
    if [ $? -eq 0 ]; then
      yesnoresult=''
    else
      yesnoresult='not '
    fi
    logmsg "${servicename} was ${yesnoresult}started"
    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"
      systemctl stop ${servicename}
      sleep 3
    else
      logmsg "${servicename} was not running -- no need to stop it"
    fi
    logmsg "now starting ${servicename}"
    systemctl start ${servicename}
    expiration=`date +'%s'`
    if [ $? -eq 0 ]; then
      yesnoresult=''
    else
      yesnoresult='not '
    fi
    logmsg "${servicename} was ${yesnoresult}started"
    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"
      rc-service ${servicename} stop
      sleep 3
    else
      logmsg "${servicename} was not running -- no need to stop it"
    fi
    logmsg "now starting ${servicename}"
    rc-service ${servicename} start
    expiration=`date +'%s'`
    if [ $? -eq 0 ]; then
      yesnoresult=''
    else
      yesnoresult='not '
    fi
    logmsg "${servicename} was ${yesnoresult}started"
    if [ -z "${yesnoresult}" ]; then
      expiration=`date +'%s'`
    fi
    ;;
  *)
    logmsg "ERROR: unknown linux flavor" >&2
    expiration=`date +'%s'`
    ;;
esac
done

