#!/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.
#

################################################################################
# Source the service type setup.
#
if [ $# -ne 2 ]; then
  echo "ERROR: syntax: installstartup.sh <tar-installer-path> <service-name>" >&2
  exit 1
fi
export tmppath=$1
export servicename=$2
echo "TMP PATH: ${tmppath}"
echo "SERVICE NAME: ${servicename}"
if [ ! -d ${tmppath}/ ]; then
  echo "ERROR: cannot locate path to tar installer: ${tmppath}" >&2
  exit 1
fi
. ${tmppath}/setservicetype.sh ${servicename}
if [ $? -ne 0 ]; then
  echo "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" ;;
  *) echo "ERROR: unknown linux flavor" >&2 ;;
esac
if [ "${templatefile}" == "unknown" ]; then
  echo "ERROR: unrecognized template name" >&2
  exit 1
fi
if [ ! -f ${templatefile} ]; then
  echo "ERROR: cannot locate template file: ${templatefile}" >&2
  exit 1
fi
echo "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
  echo "ERROR: unrecognized service script configuration" >&2
  exit 1
fi
echo "COPYING ${templatefile} to ${destination}"
cp -f ${templatefile} ${destination}

################################################################################
# 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") 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
echo "INSTALL STATUS: ${installstatus}"
if [ ${installstatus} == "installed" ]; then
  echo "NOTE: ${servicename} is already installed" >&2
  exit 0
fi
echo "bootstrapping ${servicename} on this server"

################################################################################
# Bootstrap service for this machine.
#
case ${myplatform} in
  "arch") systemctl enable ${servicename} ;;
  "centos") chkconfig --add ${servicename} ;;
  "fedora") chkconfig --add ${servicename} ;;
  "redhat") chkconfig --add ${servicename} ;;
  "debian") systemctl enable ${servicename} ;;
  "ubuntu") systemctl enable ${servicename} ;;
  "gentoo") rc-update add ${servicename} default ;;
esac

