#!/bin/bash

#
# Name:         _svc
# Version:      1.1
# Description:  Manage services
#
# (c) 2016-2020, Rapidseedbox, alexey@rapidseedbox.com
#

# Quick checks before we allow bad things to happen
if [ -z "${BASH_VERSINFO}" ]; then
    echo "ERROR: You must execute this script with BASH"
    exit 1
fi

_uid="$( id -u )"
if [ ${_uid} -ne 0 ] ; then
    echo "Sorry."
    echo "root credentials required to run this script !"
    exit 1
fi

usage() {
    cat <<EOF
Usage: ${0} enable service
       ${0} disable service
       ${0} reload service
       ${0} start service
       ${0} stop service
       ${0} restart service
       ${0} status service
EOF
}

# Main return code for the script
retcode=0

if [ -z "${2}" ] ; then
    usage
    exit 1
fi

_enable() {
    if which systemctl > /dev/null 2>&1 ; then
        systemctl enable "${svc}"
        retcode=$?
    elif which chkconfig > /dev/null 2>&1 ; then
        chkconfig "${svc}" on
        retcode=$?
    else
        echo "Unknown service management system!"
        retcode=1
    fi
    if [ -f "/etc/monit/tmp/${svc}" ] ; then
        mv -fv "/etc/monit/tmp/${svc}" /etc/monit/conf.d/
        monit reload
    fi
}

_disable() {
    if [ -f "/etc/monit/conf.d/${svc}" ] ; then
        [ ! -d "/etc/monit/tmp" ] && mkdir -vp "/etc/monit/tmp"
        mv -fv "/etc/monit/conf.d/${svc}" /etc/monit/tmp/
        monit reload
    fi
    if which systemctl > /dev/null 2>&1 ; then
        systemctl disable "${svc}"
        retcode=$?
    elif which chkconfig > /dev/null 2>&1 ; then
        chkconfig "${svc}" off
        retcode=$?
    else
        echo "Unknown service management system!"
        retcode=1
    fi
}

_start() {
    local m_svc
    m_svc=$( echo "${svc}" | tr '@' '-' )
    if which systemctl > /dev/null 2>&1 ; then
        systemctl start "${svc}"
        retcode=$?
    elif which chkconfig > /dev/null 2>&1 ; then
        service "${svc}" start
        retcode=$?
    else
        echo "Unknown service management system!"
        retcode=1
    fi
    if [ -f "/etc/monit/conf.d/${svc}" ] ; then
        monit monitor "${m_svc}"
    fi
}

_stop() {
    local m_svc
    m_svc=$( echo "${svc}" | tr '@' '-' )
    if [ -f "/etc/monit/conf.d/${svc}" ] ; then
        monit unmonitor "${m_svc}"
    fi
    if which systemctl > /dev/null 2>&1 ; then
        systemctl stop "${svc}"
        retcode=$?
    elif which chkconfig > /dev/null 2>&1 ; then
        service "${svc}" stop
        retcode=$?
    else
        echo "Unknown service management system!"
        retcode=1
    fi
    retcode=$?
}

_status() {
    if which systemctl > /dev/null 2>&1 ; then
        systemctl status "${svc}"
        retcode=$?
    elif which chkconfig > /dev/null 2>&1 ; then
        service "${svc}" status
        retcode=$?
    else
        echo "Unknown service management system!"
        retcode=1
    fi
    retcode=$?
}

_reload() {
    if which systemctl > /dev/null 2>&1 ; then
        systemctl reload "${svc}"
        retcode=$?
    elif which chkconfig > /dev/null 2>&1 ; then
        service "${svc}" reload
        retcode=$?
    else
        echo "Unknown service management system!"
        retcode=1
    fi
    retcode=$?
}

_restart() {
    _stop
    _start
    retcode=$?
}




# -----------------------------
# Main
# -----------------------------

op="${1}"
svc="${2}"
case ${op} in
  enable|disable|start|stop|reload|restart|status)
      "_${op}"
      ;;
  *)
    usage
    exit 1
      ;;
esac

exit ${retcode}
