#!/bin/sh

DESC="Internet Radio Server for Linux"
USER=http
GROUP=http
#USER=root
#GROUP=root
PREFIX=/usr/local/icecast
PATH=$PREFIX/bin:$PREFIX/sbin:$PATH
LOGFILE=$PREFIX/log/access.log
CHDIR=$PREFIX
SCRIPTNAME=/var/packages/icecast/scripts/start-stop-status

LANG=ru_RU.utf8
export LANG
LC_ALL=$LANG
export LC_ALL

do_select()
{
if  [ "$1" == "1" ] ; then
    NAME=icecast
    DAEMON_ARGS="-c $PREFIX/conf/icecast.xml"
else
    NAME=ices
    DAEMON_ARGS="-c $PREFIX/conf/ices.conf"
fi
    PIDFILE=$PREFIX/pid/${NAME}.pid
    DAEMON=$PREFIX/bin/${NAME}
}

#
# Function that starts the daemon/service
#
do_start()
{
        # Return
        #   0 if daemon has been started
        #   1 if daemon was already running
        #   2 if daemon could not be started
        start-stop-daemon --start --quiet --make-pidfile --pidfile ${PIDFILE} --chuid ${USER}:${GROUP} --chdir ${CHDIR} --background \
            --exec ${DAEMON} --test > /dev/null || return 1
        start-stop-daemon --start --quiet --make-pidfile --pidfile ${PIDFILE} --chuid ${USER}:${GROUP} --chdir ${CHDIR} --background \
            --exec ${DAEMON} -- ${DAEMON_ARGS} || return 2
        # Add code here, if necessary, that waits for the process to be ready
        # to handle requests from services started subsequently which depend
        # on this one.  As a last resort, sleep for some time.
}

#
# Function that stops the daemon/service
#
do_stop()
{
        # Return
        #   0 if daemon has been stopped
        #   1 if daemon was already stopped
        #   2 if daemon could not be stopped
        #   other if a failure occurred
        start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile ${PIDFILE} --name ${NAME}
        RETVAL="$?"
        [ "${RETVAL}" = 2 ] && return 2
        # Wait for children to finish too if this is a daemon that forks
        # and if the daemon is only ever run from this initscript.
        # If the above conditions are not satisfied then add some other code
        # that waits for the process to drop all resources that could be
        # needed by services started subsequently.  A last resort is to
        # sleep for some time.
        start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec ${DAEMON}
        [ "$?" = 2 ] && return 2
        # Many daemons don't delete their pidfiles when they exit.
        rm -f ${PIDFILE}
        return "${RETVAL}"
}

case "$1" in
  start)
        chown -R ${USER}:${GROUP} $PREFIX/log
        for i in 1 2 ; do do_select $i; echo "Starting $DESC: ${NAME}"; do_start; if  [ "$?" == "2" ]; then break; fi; done
        ;;
  stop)
        for i in 2 1 ; do do_select $i; echo "Stopping $DESC: ${NAME}"; do_stop; done
        ;;
  status)
        for i in 1 2 ; do do_select $i
        if [ -s "${PIDFILE}" ]
        then
            kill -s 0 `cat ${PIDFILE}` > /dev/null 2>&1
            if  [ "$?" == "0" ]
            then
                echo "Status $DESC: ${NAME} running, PID `cat ${PIDFILE}`"
            else
                echo "Status $DESC: ${NAME} not running, PID file present, but no process with PID `cat ${PIDFILE}` running"
                rm -f ${PIDFILE}
                break
            fi
        else
            echo "Status $DESC: ${NAME} not running, PID file ${PIDFILE} not found or empty"
            break
        fi
        done
        ;;
  log)
        echo $LOGFILE
#        exit 0
        ;;
  *)
        echo "Usage: $SCRIPTNAME {start|stop|status|log}"
        exit 1
        ;;
esac

exit 0
