################################################################################
# setservicetype.sh - detect service type and setup environment to use it
#
# WARNING! This script must be sourced by the calling script!
#
# This script checks for available service config folders and apps to identify
# which kind of service is used on the current server. Depending on what it
# finds, it sets up environment variables to be used by other scripts that
# perform tasks that use services on the server.
#
#                   Arch    Centos   Debian   Fedora   Gentoo   RedHat   Suse    Ubuntu
#   systemctl app      C       .        C        Y        .        Y        Y       C
# lib systemd dir      Y       .        .        .        Y        Y        Y       .
# etc systemd dir      Y       Y        Y        Y        .        Y        Y       Y
#      init.d dir      .       Y        Y        Y        Y        Y        Y       Y
# update-rc.d app      .       .        Y        .        .        .        .       Y
#   rc-update app      .       .        .        .        B        .        .       .
#  rc-service app      .       .        .        .        Y        .        .       .
#   chkconfig app      .       A        .        A        .        A        A       .
#     service app      .       Y        Y        Y        Y        Y        Y       Y
#
# A == chkconfig --add
# B == rc-update add servicename default
# C == systemctl enable servicename
#

export installlogname=/var/log/cbu-installer.log

################################################################################
# TODO: MAKE A LIST OF ENVIRONMENT VARIABLES AND/OR PARAMETERS REQURIED BY THIS FUNCTION
#  Requires environment variables:
#   $1 - log message
#   installlogname - log file name
function logmsg
{
  echo "$( date '+%F %H:%M:%S' ) $1" >> ${installlogname}
  echo "$1"
}

if [ $# -eq 0 ]; then
  logmsg "ERROR: missing service name parameter" >&2
  exit 1
fi
# TODO: WE THINK THIS VARIABLE IS UNUSED -- CONSIDER DELETING THIS LINE OF CODE
export servicename=$1

################################################################################
# Identify platform.
#
export myplatform=unknown
if [ -f /etc/redhat-release ]; then
  export myrelease=`cut -d' ' -f1 /etc/redhat-release`
  case ${myrelease} in
    "CentOS") myplatform=centos;;
    "Fedora") myplatform=fedora;;
    "Red") myplatform=redhat;;
  esac
elif [ -f /etc/debian_version ]; then
  export myrelease=`head -1 /etc/issue | cut -d" " -f1`
  case ${myrelease} in
    "Debian") myplatform=debian;;
    "Ubuntu") myplatform=ubuntu;;
  esac
elif [ -f /etc/arch-release ]; then
  myplatform=arch
elif [ -f /etc/gentoo-release ]; then
  myplatform=gentoo
else
  logmsg "ERROR: unrecognized platform" >&2
  exit 1
fi
logmsg "PLATFORM=${myplatform}"

################################################################################
# Identify configuration app.
#
# Doublecheck availability of service config tools
which chkconfig > /dev/null 2>&1
if [ $? -eq 0 ]; then
  logmsg "Has chkconfig"
fi
which update-rc.d > /dev/null 2>&1
if [ $? -eq 0 ]; then
  logmsg "Has update-rc.d"
fi
which rc-update > /dev/null 2>&1
if [ $? -eq 0 ]; then
  logmsg "Has rc-update"
fi
which systemctl > /dev/null 2>&1
if [ $? -eq 0 ]; then
  logmsg "Has systemctl"
fi
logmsg "BOOTSTRAP CONFIG APP: ${myconfigurator}"

