#!/bin/bash
################################################################################
# installstartup.sh - install-anywhere script for CBU client run-on-boot setup
#
# installstartup.sh <tar-files-folder> <service-name>
# - tar-files-folder: after extracting the tar, the files are in this folder
# - service-name: name of the service that is being started
#
# 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" ;;
  alma) templatefile="${tmppath}/${servicename}.service.d.script" ;;
  rocky) templatefile="${tmppath}/${servicename}.service.d.script" ;;
  redhat) templatefile="${tmppath}/${servicename}.init.redhat" ;;
  debian) templatefile="${tmppath}/${servicename}.service.d.script" ;;
  ubuntu) templatefile="${tmppath}/${servicename}.init.debian"
    which systemctl > /dev/null 2>&1 && templatefile="${tmppath}/${servicename}.service.d.script"
    ;;
  gentoo) templatefile="${tmppath}/${servicename}.init.gentoo" ;;
  *) logmsg "ERROR: unknown linux flavor: ${myplatform}" ${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 ;;
  alma) destination=/etc/systemd/system/${servicename}.service ;;
  rocky) destination=/etc/systemd/system/${servicename}.service ;;
  redhat) destination=/etc/init.d/${servicename} ;;
  debian) destination=/etc/systemd/system/${servicename}.service ;;
  ubuntu) destination=/etc/init.d/${servicename};
    which systemctl > /dev/null 2>&1 &&  destination=/etc/systemd/system/${servicename}.service
    ;;
  gentoo) destination=/etc/init.d/${servicename} ;;
  *) logmsg "ERROR: unknown linux flavor: ${myplatform}" ${LINENO} ;;
esac
if [ "${destination}" == "unknown" ]; then
  logmsg "ERROR: unrecognized service script configuration" ${LINENO}
  exit 1
fi
# Copy service script template file to destination.
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' ;;
  alma) systemctl is-enabled ${servicename} && installstatus='installed' ;;
  rocky) systemctl is-enabled ${servicename} && installstatus='installed' ;;
  redhat) chkconfig --list 2>/dev/null | grep ${servicename} && installstatus='installed' ;;
  debian) systemctl is-enabled ${servicename} && installstatus='installed' ;;
  ubuntu) which systemctl > /dev/null 2>&1 && systemctl is-enabled ${servicename} && installstatus='installed'
    which systemctl > /dev/null 2>&1 || 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
#  NOTE: Ubuntu special case must remove old init.d script because conflicts.
  logmsg "bootstrapping ${servicename} on this ${myplatform} server" ${LINENO}
  case ${myplatform} in
    arch) systemctl enable ${servicename} ;;
    centos) chkconfig --add ${servicename} ;;
    fedora) systemctl enable ${servicename} ;;
    alma) systemctl enable ${servicename} ;;
    rocky) systemctl enable ${servicename} ;;
    redhat) chkconfig --add ${servicename} ;;
    debian) systemctl enable ${servicename} ;;
    ubuntu) which systemctl > /dev/null 2>&1 && systemctl enable ${servicename} && rm -f /etc/init.d/${servicename}
      which systemctl > /dev/null 2>&1 || logmsg "not using systemctl" ${LINENO}
      ;;
    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

