#!/bin/bash
#
# This starts and stops squeezelite
#
### BEGIN INIT INFO
# Provides:          squeezelite
# Required-Start:    $all networking
# Required-Stop:     $all networking
# Should-Start:      autofs $network $named alsa-utils
# Should-Stop:       autofs $network $named alsa-utils
# Short-Description: squeezelite
# Description:       squeezelite
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
### END INIT INFO


# Source function library.
. /lib/lsb/init-functions

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

NAME=squeezelite
ZONE_NAME=squeezelite-name
ALSA_DEVICE=hw:0
SUPPORTED_SAMPLE_RATES=32000-192000 # setting this allows Squeezelite to start without the dac present
SERVER=mysqueezebox.com
PORT=3483
MAC=`ifconfig -a |grep eth0 | awk '{print $5}'`
USER=squeezelite

DAEMON=/usr/local/bin/$NAME
PIDLOCATION=/var/run/$NAME
PIDFILE=$PIDLOCATION/$NAME.pid
LOGFILE=/var/log/$NAME.log
DAEMON_ARGS="-f "$LOGFILE" -z -n "$ZONE_NAME" -m "$MAC" -o "$ALSA_DEVICE" -r "$SUPPORTED_SAMPLE_RATES" -C 1 -s "$SERVER":"$PORT" -P "$PIDFILE

[ -x $binary ] || exit 0

[ -d $PIDLOCATION ] || mkdir $PIDLOCATION

chown $USER:$USER $PIDLOCATION

RETVAL=0

start() {
    echo -n "Starting squeezelite: "
    start-stop-daemon --start --chuid $USER --quiet --pidfile "$PIDFILE" \
                      --exec "$DAEMON" -b --oknodo -- $DAEMON_ARGS
    log_end_msg $?
}

stop() {
    echo -n "Shutting down squeezelite: "
    start-stop-daemon --stop --quiet --pidfile "$PIDFILE" \
                --retry 1 --oknodo
    log_end_msg $?
}

restart() {
    stop
    sleep 1
    start
}

case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    status)
        status_of_proc -p $PIDFILE $DAEMON $NAME
    ;;
    restart)
        restart
    ;;
    *)
        echo "Usage: $0 {start|stop|status|restart}"
    ;;
esac

exit 0



