################################################################################
# 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    Alma      Rocky
#   systemctl app      C       .        C        Y        .        Y        Y       C         Y         Y
# lib systemd dir      Y       .        .        .        Y        Y        Y       .         Y         Y
# etc systemd dir      Y       Y        Y        Y        .        Y        Y       Y         Y         Y
#      init.d dir      .       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       .         .         .
#     service app      .       Y        Y        .        Y        Y        Y       Y         .         .
#
# A == chkconfig --add
# B == rc-update add servicename default
# C == systemctl enable servicename
#

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"
}

if [ $# -eq 0 ]; then
  logmsg "ERROR: missing service name parameter" ${LINENO}
  exit 1
fi

################################################################################
# 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;;
    "AlmaLinux") myplatform=alma;;
    "Rocky") myplatform=rocky;;
    "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" ${LINENO}
  exit 1
fi
logmsg "PLATFORM=${myplatform}" ${LINENO}

################################################################################
# Log availability of service config tools.
which chkconfig > /dev/null 2>&1
if [ $? -eq 0 ]; then
  logmsg "Has chkconfig" ${LINENO}
fi
which update-rc.d > /dev/null 2>&1
if [ $? -eq 0 ]; then
  logmsg "Has update-rc.d" ${LINENO}
fi
which rc-update > /dev/null 2>&1
if [ $? -eq 0 ]; then
  logmsg "Has rc-update" ${LINENO}
fi
which systemctl > /dev/null 2>&1
if [ $? -eq 0 ]; then
  logmsg "Has systemctl" ${LINENO}
fi

