#!/bin/bash
#
#    /etc/rc.d/init.d/oned
#
# Starts the OpenNebula Accounting daemon
#
# chkconfig: 345 26 74
# description: Starts the OpenNebula Accounting daemon
# processname: sunstone-server

### BEGIN INIT INFO
# Provides: oneacctd
# Required-Start: $local_fs $remote_fs oned
# Required-Stop: $local_fs $remote_fs oned
# Default-Start:  2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop oneacctd
# Description: start and stop oneacctd
### END INIT INFO

ONE_BIN=/usr/bin/oneacctd

# Source function library.
. /etc/rc.d/init.d/functions

RETVAL=0

check() {
    # Check that we're a privileged user
    [ `id -u` = 0 ] || exit 4

    # Check if oned is executable
    test -x $ONE_BIN || exit 5
}

start() {

    check

    ONE_AUTH_FILE=/var/lib/one/auth
    if [ ! -f $ONE_AUTH_FILE ]; then
	exit 6
    fi

    echo -n $"Starting OpenNebula Accounting daemon: "


    daemon --user oneadmin ONE_AUTH=$ONE_AUTH_FILE  $ONE_BIN start
    RETVAL=$?
    echo
    return $RETVAL
}

stop() {

    check

    echo -n $"Stopping OpenNebula Accounting daemon: "
    su oneadmin -s /bin/sh -c "$ONE_BIN stop"
    RETVAL=$?
    echo
    return $RETVAL
}

restart() {
    stop
    start
}


case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
restart)
    restart
    ;;
status)
    status oned
    RETVAL=$?
    ;;
*)
    echo $"Usage: $0 {start|stop|status|restart}"
    RETVAL=2
esac

exit $RETVAL
