#!/bin/sh
#
# chkconfig: - 80 20
# description: starts and stops headless firefox instance
#
HOME=/root
screen=:1
logroot=/var/log/royaltyshare

prog=firefox;
name='Headless Firefox'
params=--display=$screen
log=$logroot/firefox.log

xprog=Xvfb;
xname='Virtual X Window Frame Buffer';
xparams=$screen
xlog=$logroot/xvfb.log


# Source function library.
if [ -f /etc/init.d/functions ] ; then
  . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
  . /etc/rc.d/init.d/functions
else
  exit 1
fi

RETVAL=0

start() {
    echo -n $"Starting $xname services: "
    $xprog $xparams > $xlog &
    RETVAL=$?
    if [ "$RETVAL" = 0 ]; then
        success
    else
        failure
    fi
    echo

    echo -n $"Starting $name services: "
    if [ "$RETVAL" <> 0 ]; then
        $prog $params > $log &
        RETVAL=$?
    fi
    if [ "$RETVAL" = 0 ]; then
        success
    else 
        failure
    fi
    echo

    return $RETVAL
}

stop() {
    echo -n $"Shutting down $name services: "
    killproc $prog
    RETVAL=$?
    if [ "$RETVAL" = 0 ]; then
        success
    else 
        failure
    fi
    echo

    echo -n $"Shutting down $xname services: "
    killproc $xprog
    RETVAL=$?
    if [ "$RETVAL" = 0 ]; then
        success
    else 
        failure
    fi
    echo
    return $RETVAL
}

restart() {
    stop
    start
}


case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        restart
    ;;
    *)
    	echo $"Usage: `basename $0` {start|stop|restart}"
    	exit 2
esac

exit $?
