#!/bin/bash
################################################################################
# installstartup.sh - install-anywhere script for CBU client run-on-boot setup
#
# installstartup.sh <tar-files-folder>
# - tar-files-folder: after extracting the tar, the files are in this folder
#
# This script will overwrite the current service script with the new one
# regardless of whether a script already exists. It will then check to see if
# the service is alredy bootstrapped and bootstrap it if it is not.
#
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}
  # echo "$1"
}

################################################################################
# Source the service type setup.
#  Requires environment variables:
#   $1 - Path to /tmp where the installer files are extracted
#   $2 - Name of the service to be installed
#   $3 - Optional: if this contains any value at all, we bounce the service; otherwise, not
if [ $# -lt 2 ]; then
  logmsg "ERROR: syntax: installstartup.sh <tar-installer-path> <service-name>" ${LINENO}
  logmsg "Number of parameters: $# Parameters: $*" ${LINENO}
  exit 1
fi
export tmppath=$1
export servicename=$2
export bounceservice=no
if [ $# -gt 2 ]; then
  bounceservice=yes
fi
logmsg "IP address: `hostname -I`" ${LINENO}
logmsg "TMP PATH: ${tmppath}" ${LINENO}
logmsg "SERVICE NAME: ${servicename}" ${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

################################################################################
# Get template file name.
#  Requires environment variables:
#   myplatform - Code for this platform
#   servicename - Name of service to be installed
export templatefile=unknown
case ${myplatform} in
  arch) templatefile="${tmppath}/${servicename}.service.d.script" ;;
  centos) templatefile="${tmppath}/${servicename}.init.redhat" ;;
  fedora) templatefile="${tmppath}/${servicename}.service.d.script" ;;
  redhat) templatefile="${tmppath}/${servicename}.init.redhat" ;;
  debian) templatefile="${tmppath}/${servicename}.service.d.script" ;;
  disableubuntu) templatefile="${tmppath}/${servicename}.init.debian" ;;
  ubuntu) templatefile="${tmppath}/${servicename}.service.d.script" ;;
  gentoo) templatefile="${tmppath}/${servicename}.init.gentoo" ;;
  *) logmsg "ERROR: unknown linux flavor" ${LINENO} ;;
esac
if [ "${templatefile}" == "unknown" ]; then
  logmsg "ERROR: unrecognized template name" ${LINENO}
  exit 1
fi
if [ ! -f ${templatefile} ]; then
  logmsg "ERROR: cannot locate template file: ${templatefile}" ${LINENO}
  exit 1
fi
logmsg "TEMPLATE NAME: ${templatefile}" ${LINENO}

################################################################################
# Create service script.
#  Requires environment variables:
#   myplatform - Code for this platform
#   servicename - Name of service to be installed
#   templatefile - Name of the template file to use for service setup
export destination=unknown
case ${myplatform} in
  arch) destination=/etc/systemd/system/${servicename}.service ;;
  centos) destination=/etc/init.d/${servicename} ;;
  fedora) destination=/etc/systemd/system/${servicename}.service ;;
  redhat) destination=/etc/init.d/${servicename} ;;
  debian) destination=/etc/systemd/system/${servicename}.service ;;
  disableubuntu) destination=/etc/init.d/${servicename} ;;
  ubuntu) destination=/etc/systemd/system/${servicename}.service ;;
  gentoo) destination=/etc/init.d/${servicename} ;;
esac
if [ "${destination}" == "unknown" ]; then
  logmsg "ERROR: unrecognized service script configuration" ${LINENO}
  exit 1
fi
logmsg "COPYING ${templatefile} to ${destination}" ${LINENO}
cp -f ${templatefile} ${destination}
if [ $? -ne 0 ]; then
  logmsg "ERROR: while copying ${templatefile} to ${destination}" ${LINENO}
  exit 1
fi

################################################################################
# Make sure service is not already installed.
#  Requires environment variables:
#   myplatform - Code for this platform
#   servicename - Name of service to be installed
installstatus='uninstalled'
case ${myplatform} in
  arch) systemctl is-enabled ${servicename} && installstatus='installed' ;;
  centos) chkconfig --list 2>/dev/null | grep ${servicename} && installstatus='installed' ;;
  fedora) systemctl is-enabled ${servicename} && installstatus='installed' ;;
  redhat) chkconfig --list 2>/dev/null | grep ${servicename} && installstatus='installed' ;;
  debian) systemctl is-enabled ${servicename} && installstatus='installed' ;;
  ubuntu) systemctl is-enabled ${servicename} && installstatus='installed' ;;
  gentoo) rc-update show | grep ${servicename} && installstatus='installed' ;;
esac
logmsg "INSTALL STATUS: ${installstatus}" ${LINENO}
if [ ${installstatus} == "installed" ]; then
  logmsg "NOTE: ${servicename} is already installed" ${LINENO}
else

################################################################################
# Bootstrap service for this machine.
#  Requires environment variables:
#   myplatform - Code for this platform
#   servicename - Name of service to be installed
  logmsg "bootstrapping ${servicename} on this ${myplatform} server" ${LINENO}
  case ${myplatform} in
    arch) systemctl enable ${servicename} ;;
    centos) chkconfig --add ${servicename} ;;
    fedora) systemctl enable ${servicename} ;;
    redhat) chkconfig --add ${servicename} ;;
    debian) systemctl enable ${servicename} ;;
    ubuntu) systemctl enable ${servicename} ;;
    gentoo) rc-update add ${servicename} default ;;
  esac
  if [ $? -ne 0 ]; then
    logmsg "ERROR: bootstrapping ${servicename} on ${myplatform}" ${LINENO}
    exit 1
  fi
  logmsg "bootstrap successful for ${myplatform}" ${LINENO}
fi

################################################################################
# Bounce service for this machine.
#  Requires environment variables:
#   bounceservice - Boolean flag for bouncing the service
#   servicename - Name of service to be installed
#   tmppath - Path to installer files
if [ "${bounceservice}" == "yes" ]; then
  logmsg "bouncing service ${servicename}" ${LINENO}
  ${tmppath}/bounceservice.sh "${tmppath}" "${servicename}"
  if [ $? -ne 0 ]; then
    logmsg "ERROR: Failure bouncing service ${servicename}" ${LINENO}
    exit 1
  fi
  logmsg "Service ${servicename} bounced" ${LINENO}
fi

