#!/bin/sh
# Implicit arguments
#  PREFIX - prefix of Likewise installation
#  SERVICE_NAME - name of the service

LWSM="${PREFIX}/bin/lwsm"

PLATFORM=""
if [ -f /etc/init.d/functions ]; then
    . /etc/init.d/functions
    PLATFORM="REDHAT"
elif [ -f /etc/rc.status ]; then
    . /etc/rc.status
    PLATFORM="SUSE"
elif [ -f /etc/debian_version ]; then
    . /lib/lsb/init-functions
    PLATFORM="DEBIAN"
elif [ "`uname -s`" = 'AIX' ]; then
    PLATFORM="AIX"
elif [ "`uname -s`" = 'HP-UX' ]; then
    PLATFORM="HP-UX"
elif [ "`uname -s`" = 'SunOS' ]; then
    PLATFORM="SOLARIS"
elif [ "`uname -s`" = 'VMkernel' ]; then
    PLATFORM="ESXI"
elif [ "`uname`" = "FreeBSD" ]; then
    PLATFORM="FREEBSD"
else
    PLATFORM="UNKNOWN"
fi

service_description()
{
    echo "Likewise Storage Services"
}

service_start()
{
    status=""

    case "${PLATFORM}" in
        REDHAT)
            printf "%s" "Starting `service_description`: " 
            ${LWSM} -q autostart
            status=$?
            if [ $status -eq 0 ]
            then
                echo_success
                echo
            else
                echo_failure
                echo
            fi
            ;;
        SUSE)
            printf "%s" "Starting `service_description`"
            for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
                if [ -e "/var/lib/likewise/.lwsm" ]; then
                    break;
                fi
                sleep 1
            done
            ${LWSM} -q autostart
            status=$?
            if [ $status -eq 0 ]
            then
                rc_reset
                rc_status -v
            else
                rc_failed $status
                rc_status -v
            fi
            ;;
        DEBIAN)
            log_daemon_msg "Starting `service_description`"
            ${LWSM} -q autostart
            status=$?
            log_end_msg $status
            ;;
         HP-UX | SOLARIS | FREEBSD | ESXI | AIX)
            printf "%s" "Starting `service_description`"
            if [ -x /usr/sbin/svcadm ]; then
                # Don't do anything!
                status=0
            else
                ${LWSM} -q autostart
                status=$?
            fi

            if [ $status -eq 0 ]
            then
                echo "...ok"
            else
                echo "...failed"
            fi
            ;;
        UNKNOWN)
            ${LWSM} -q autostart
            status=$?
            ;;
    esac

    return $status
}

service_restart()
{
    status=""

    case "${PLATFORM}" in
        REDHAT)
            printf "%s" "Restarting `service_description` (not supported)"
            status=0
            echo_success
            ;;
        SUSE)
            printf "%s" "Restarting `service_description` (not supported)"
            status=0
            rc_reset
            rc_status -v
            ;;
        DEBIAN)
            log_daemon_msg "Restarting `service_description` (not supported)"
            status=0
            log_end_msg $status
            ;;
         HP-UX | SOLARIS | FREEBSD | ESXI | AIX)
            printf "%s" "Restarting `service_description` (not supported)"
            status=0
            echo "...ok"
            ;;
        UNKNOWN)
            status=0
            ;;
    esac

    return $status
}

service_stop()
{
    status=""

    case "${PLATFORM}" in 
        REDHAT)
            printf "%s" "Stopping `service_description` (not supported) "
            status=0
            echo_success
            ;;
        SUSE)
            printf "%s" "Stopping `service_description` (not supported)"
            status=0
            rc_reset
            rc_status -v
            ;;
        DEBIAN)
            log_daemon_msg "Stopping `service_description` (not supported)"
            status=0
            log_end_msg $status
            ;;
        AIX | HP-UX | SOLARIS | FREEBSD | ESXI)
            printf "%s" "Stopping `service_description` (not supported)"
            status=0
            echo "...ok"
            ;;
        UNKNOWN)
            status=0
            ;;
    esac

    return $status
}

service_refresh()
{
    case "${PLATFORM}" in
        REDHAT)
            printf "%s" "Refreshing `service_description $1` configuration"
            status=0
            echo_success
            ;;
        SUSE)
            printf "%s" "Refreshing `service_description $1` configuration"
            status=0
            rc_reset
            rc_status -v
            ;;
        DEBIAN)
            log_daemon_msg "Refreshing `service_description $1` configuration"
            status=0
            log_end_msg $status
            ;;
        AIX | HP-UX | SOLARIS | FREEBSD | ESXI)
            printf "%s" "Refreshing `service_description $1` configuration"
            status=0
            echo "...ok"
            ;;
        UNKNOWN)
            status=0
            ;;
    esac

    return $status
}

service_dump()
{
    status=""
    case "${PLATFORM}" in
        FREEBSD)
            printf "%s" "Dumping `service_description`: " 
            DUMP_PROGRAM="/usr/local/libexec/likewise-platform/likewise_diagnostics.sh"
            if [ -f ${DUMP_PROGRAM} ]; then
              ${DUMP_PROGRAM}
              status=$?
            else
              printf "%s" "Unable to dump ${DUMP_PROGRAM} does not exist."
              status=1
            fi

            if [ $status -eq 0 ]
            then
                echo "...ok"
            else
                echo "...failed"
            fi
	    ;;
        UNKNOWN)
            echo "Dumping `service_description` on ${PLATFORM} (not supported)"
            status=0
            ;;
        *)
            echo "Dumping `service_description` on ${PLATFORM} (not supported)"
            status=0
            ;;
    esac
}

service_health()
{
    status=0

    # lwsmd's pid/lock file and its name as it appears via ps 
    #
    args="-p /var/db/likewise/.lwsmd-lock"
    args="$args -P /usr/local/sbin/lwsmd"

    case "${PLATFORM}" in
        FREEBSD)
	    if [ -n "$HEALTH_CHECK" ]
	    then
	    	if [ -x $HEALTH_CHECK ]
		then
            	    PREFIX=$PREFIX $HEALTH_CHECK $args
		    status=$?
		fi
	    fi
	    ;;

        *)
            echo \
	    "health check `service_description` on ${PLATFORM} (not supported)"
            status=0
            ;;
    esac

    return $status
}

service_stopall()
{
    status=0

    case "${PLATFORM}" in
        FREEBSD)
	    lwsm shutdown 
	    status=$?
	    ;;

        *)
            echo \
	    "stopall `service_description` on ${PLATFORM} (not supported)"
            status=0
            ;;
    esac

    return $status
}

service_startall()
{
    status=0

    case "${PLATFORM}" in
        FREEBSD)
	    service lwsmd start
	    status=$?
	    if [ "$status" -eq 0 ]
	    then
		service_start
	    	status=$?
	    fi
	    ;;

        *)
            echo \
	    "startall `service_description` on ${PLATFORM} (not supported)"
            status=0
            ;;
    esac

    return $status
}

if [ -f /etc/rc.subr ]; then
    . /etc/rc.subr
fi

case "$1" in
    start|faststart|forcestart|onestart)
        service_start "$SERVICE_NAME"
        exit $?
        ;;
    stop|faststop|forcestop|onestop)
        service_stop "$SERVICE_NAME"
        exit $?
        ;;
    status|faststatus|forcestatus|onestop)
        echo "not supported"
        exit 0
        ;;
    restart|fastrestart|forcerestart|onerestart)
        service_restart "$SERVICE_NAME"
        exit $?
        ;;
    refresh|reload)
        service_refresh "$SERVICE_NAME"
        exit $?
        ;;
    start_msg)
        echo "Starting `service_description`"
        exit 0
        ;;
    stop_msg)
        echo "Stopping `service_description` (not supported)"
        exit 0
        ;;
    rcvar|fastrcvar|forcercvar|onercvar)
        if type run_rc_command >/dev/null 2>&1; then
            # Looks like this is a FreeBSD based system.
            name=likewise
            rcvar="likewise_enable"
            start_cmd="service_start"
            stop_cmd="service_stop"
            eval "${name}_enable=YES"

            load_rc_config "$name"
            run_rc_command "$1" || exit $?
            exit 0
        fi
        ;;
    dump)
        service_dump "$SERVICE_NAME"
        exit 0
        ;;

    health)
	service_health "$SERVICE_NAME"
	exit $?
	;;
	
    stopall)
	service_stopall "$SERVICE_NAME"
	exit $?
	;;

    startall)
	service_startall "$SERVICE_NAME"
	exit $?
	;;

    *)
        echo "Unrecognized command: $1"
        exit 1
        ;;
esac
