#!/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.
#
# TODO: MAKE SURE REQUIRED PACKAGES ARE INSTALLED (AND PACKAGE MANAGERS, IF NECESSARY) -- e.g., python, and apt-get
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 [ $# -lt 2 ]; then
  logmsg "ERROR: syntax: installstartup.sh <tar-installer-path> <service-name>" >&2
  logmsg "Number of parameters: $# Parameters: $*" >&2
  exit 1
fi
export tmppath=$1
export servicename=$2
export bounceservice=no
if [ $# -gt 2 ]; then
  bounceservice=yes
fi
logmsg "IP address: `hostname -I`"
logmsg "TMP PATH: ${tmppath}"
logmsg "SERVICE NAME: ${servicename}"
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

################################################################################
# Create service script.
#
export templatefile=unknown
case ${myplatform} in
  arch) templatefile="${tmppath}/${servicename}.service.d.script" ;;
  centos) templatefile="${tmppath}/${servicename}.init.redhat" ;;
  fedora) templatefile="${tmppath}/${servicename}.init.redhat" ;;
  redhat) templatefile="${tmppath}/${servicename}.init.redhat" ;;
  debian) templatefile="${tmppath}/${servicename}.service.d.script" ;;
  ubuntu) templatefile="${tmppath}/${servicename}.init.debian" ;;
  gentoo) templatefile="${tmppath}/${servicename}.init.gentoo" ;;
  *) logmsg "ERROR: unknown linux flavor" >&2 ;;
esac
if [ "${templatefile}" == "unknown" ]; then
  logmsg "ERROR: unrecognized template name" >&2
  exit 1
fi
if [ ! -f ${templatefile} ]; then
  logmsg "ERROR: cannot locate template file: ${templatefile}" >&2
  exit 1
fi
logmsg "TEMPLATE NAME: ${templatefile}"

################################################################################
# Create service script.
#
export destination=unknown
case ${myplatform} in
  arch) destination=/etc/systemd/system/${servicename}.service ;;
  centos) destination=/etc/init.d/${servicename} ;;
  fedora) destination=/etc/init.d/${servicename} ;;
  redhat) destination=/etc/init.d/${servicename} ;;
  debian) destination=/etc/systemd/system/${servicename}.service ;;
  ubuntu) destination=/etc/init.d/${servicename} ;;
  gentoo) destination=/etc/init.d/${servicename} ;;
esac
if [ "${destination}" == "unknown" ]; then
  logmsg "ERROR: unrecognized service script configuration" >&2
  exit 1
fi
logmsg "COPYING ${templatefile} to ${destination}"
cp -f ${templatefile} ${destination}
if [ $? -ne 0 ]; then
  logmsg "ERROR: while copying ${templatefile} to ${destination}"
  exit 1
fi

################################################################################
# Make sure service is not already 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' ;;
  # fedora) chkconfig --list 2>/dev/null | grep ${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}"
if [ ${installstatus} == "installed" ]; then
  logmsg "NOTE: ${servicename} is already installed" >&2
  exit 0
fi
logmsg "bootstrapping ${servicename} on this ${myplatform} server"

################################################################################
# Bootstrap service for this machine.
#
case ${myplatform} in
  arch) systemctl enable ${servicename} ;;
  centos) chkconfig --add ${servicename} ;;
  fedora) systemctl enable ${servicename} ;;
  # fedora) chkconfig --add ${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}"
  exit 1
fi
logmsg "bootstrap successful for ${myplatform}"

################################################################################
# Bounce service for this machine.
#
if [ "${bounceservice}" == "yes" ]; then
  logmsg "bouncing service ${servicename}"
  ${tmppath}/bounceservice.sh "${tmppath}" "${servicename}"
  if [ $? -ne 0 ]; then
    logmsg "ERROR: Failure bouncing service ${servicename}"
    exit 1
  fi
  logmsg "Service ${servicename} bounced"
fi

