#!/bin/bash -l

exec 2>&1

EX_OK=0
EX_USAGE=64

function noperm() {
    echo "Permission denied"
    exit $EX_USAGE
}

[ ${UID} -ne 0 ] && noperm

function start_stop_status() {
    case "${2}" in
        start|stop|status|validate-config)
            exec ${1} ${2}
        ;;
        *)
            noperm
        ;;
    esac
    return $?
}

case "${1}" in
    /usr/sbin/kdiagnostic_run.sh|/etc/init.d/kibs|/etc/init.d/klogserver|/etc/init.d/kmpserver|/etc/init.d/k3lserver|/etc/init.d/kqueryserver|/etc/init.d/kgateway)
        [ "${3}" != "" ] && {
            noperm
        } || {
            start_stop_status "${1}" "${2}"
        }
    ;;
    /usr/sbin/system_report.sh)
        exec ${1}
    ;;
    ## gateway only executables
    /sbin/shutdown|/usr/sbin/kgateway-upgrade)
        [ -x /usr/sbin/kgateway ] && {
            CMD=${1}
            shift
            exec $CMD "${@}"
        } || {
            noperm
        }
    ;;
    *)
        noperm
    ;;
esac

exit $?
