#!/bin/sh

SYSTEMCTL=$(which systemctl 2> /dev/null)
SERVICE=$(which service 2> /dev/null)
CHKCONFIG=$(which chkconfig 2> /dev/null)


######################################################
# (SYSTEMCTL)
######################################################
if [ -n "${SYSTEMCTL}" ]; then
  if [ -e "/etc/selinux/config" ]; then
    # SELinux setting
    SELINUX_OPTION=$(awk -F= '/^SELINUX=/{print $2}' /etc/selinux/config)
    if [ ${SELINUX_OPTION} != "disabled" ]; then
      chcon -t bin_t {{ EXEM_HOME }}/manager/script/binary_start.sh
      chcon -R -t var_run_t {{ EXEM_HOME }}/manager/data
      chcon -R -t var_run_t {{ EXEM_HOME }}/manager/service
    fi
  fi

  if [ ! -e "/etc/systemd/system/exem-manager-agent.service" ]; then
    systemctl enable {{ EXEM_HOME }}/manager/service/exem-manager-agent.service
  fi

  systemctl daemon-reload
  echo "exem-manager-agent is enabled [service_start.sh]"

  systemctl start exem-manager-agent
  echo "exem-manager-agent is started [service_start.sh]"

  exit 0

######################################################
# (others)
######################################################
else
  echo "this OS version that does not support service registration"
  exit 1
fi
