#!/bin/sh

[ -n "${INITDIR:-}" ] || INITDIR=/etc/init.d
# Checks if a service is present in the system
# args: service
service_exists() {
	[ -f "${INITDIR%/}/$1" ]
}

# Checks if a service is in transition.
# args: service
service_test_locked()
{
    service_exists "$1" && \
    /sbin/service "$1" status | grep -q "subsystem is locked$"
}

# Waits for the service to change status 
# args: service
service_wait()
{
    service_exists "$1" || return 0
    x=0;
    while [ $x -lt 5 ] && service_test_locked "$1"; do
        x=$(( x + 1 ))
        sleep 1
    done
}

# Checks if a service is running.
# args: service
service_test_on()
{
    service_exists "$1" || return 1
    service_wait "$1"
    /sbin/service "$1" status | grep -q "is running$"
}

# Checks if a service is stopped.
# args: service
service_test_off()
{
    service_exists "$1" || return 0
    service_wait "$1"
    /sbin/service "$1" status | grep -q "is stopped$"
}

# Checks service running status
# args: service status
service_test()
{
    if [ "$2" = "on" ]; then
      service_test_on "$1"
    else
      service_test_off "$1"
    fi
}

# Checks if a service switched to the specified status for
# the current runlevel?
# args: service status
startup_test()
{
    if ! service_exists "$1"; then
	    [ "$2" = "off" ] && return 0
	    return 1
    fi
    local RUNLEVEL=`/sbin/runlevel | cut -c3`
    local STATUS=`/sbin/chkconfig --list $1 > /dev/null 2>&1`
    if [ $? -ne 0 ]; then
      /sbin/chkconfig --add $1
      STATUS=`/sbin/chkconfig --list $1`
    fi
    echo "$STATUS" | grep -q "${RUNLEVEL}:$2"
}

# Checks if a service is present in the system and prints an error
# message if it is not
# args: service
assert_service() {
	if ! service_exists "$1"; then
		echo "Service is not installed" 1>&2
		return 1
	fi
	return 0
}

# Manages the service status.
# args: service status.
service_control()
{
    local cmd
    if [ "$2" = "on" ]; then
        assert_service "$1" || return 1
        cmd="start"
    else
	service_exists "$1" || return 0
        cmd="stop"
    fi
    service_test "$1" "$2" && return 0

    if [ -n "${ALTERATOR_DEBUG:-}" ]; then
        /sbin/service "$1" $cmd
    else
        /sbin/service "$1" $cmd > /dev/null 2>&1
    fi
    res=$?
    if [ "$res" -eq 0 ]; then
        service_wait "$1"
    else
        if [ -n "${ALTERATOR_DEBUG:-}" ]; then
          echo "Service $2 failed" 1>&2
        fi
    fi

    return $res
}

# Reload the service
# args: service
service_reload()
{
    assert_service "$1" || return 1
    if [ -n "${ALTERATOR_DEBUG:-}" ]; then
        /sbin/service "$1" reload
    else
        /sbin/service "$1" reload > /dev/null 2>&1
    fi
    res=$?
    if [ "$res" -eq 0 ]; then
        service_wait "$1"
    else
        if [ -n "${ALTERATOR_DEBUG:-}" ]; then
          echo "Service reload failed" 1>&2
        fi
    fi

    return $res
}

# Restarts the service
# args: service
service_restart()
{
    assert_service "$1" || return 1
    if [ -n "${ALTERATOR_DEBUG:-}" ]; then
        /sbin/service "$1" restart
    else
        /sbin/service "$1" restart > /dev/null 2>&1
    fi
    res=$?
    if [ "$res" -eq 0 ]; then
        service_wait "$1"
    else
        if [ -n "${ALTERATOR_DEBUG:-}" ]; then
          echo "Service restart failed" 1>&2
        fi
    fi

    return $res
}

# Change service startup status.
# args: service status
startup_control()
{
    startup_test "$1" "$2" && return 0

    assert_service "$1" || return 1
    /sbin/chkconfig "$1" "$2"
    res=$?
    if [ "$res" -ne 0 ] && [ -n "${ALTERATOR_DEBUG:-}" ]; then
      echo "Unable to set $1 startup status" 1>&2
    fi
    return $res
}
