#!/bin/bash

# Based on Debian's /etc/init.d/skeleton.

# This file is part of mpd-hits.
# Copyright 1997-2005 Miquel van Smoorenburg <miquels@cistron.nl> and
# the members pkg-sysvinit project.
# Copyright (C) 2010 Dmitry Samoyloff.
#
# mpd-hits is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# mpd-hits is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with mpd-hits. If not, see <http://www.gnu.org/licenses/>.

### BEGIN INIT INFO
# Provides:          mpd-hits
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: mpd-hits daemon
# Description:       Starts mpd-hits daemon to collect playback statistics of
#                    Music Player Daemon (MPD).
### END INIT INFO

get_config_value() {
    eval $1=`grep "^[[:space:]]*$1[[:space:]]\+" $CONFIG | awk '{print $2}'`
    if [ -z ${!1} ]; then
        log_failure_msg "$1 is not specified in $CONFIG."
        return 1
    fi

    return 0
}

read_config() {
    if ! [ -f $CONFIG ]; then
        log_failure_msg "Configuration file $CONFIG does not exist."
        return 1
    fi

    get_config_value LOCK_DIRECTORY || return 1
    PIDFILE="$LOCK_DIRECTORY/mpd-hits.pid"

    get_config_value USER || return 1

    get_config_value GROUP || return 1

    return 0
}

check_lock_dir() {
    # Create lock directory if it not exists
    # (say, /var/run may be mounted as temporary filesystem).
    if ! [ -d "$LOCK_DIRECTORY" ]; then
        mkdir -p "$LOCK_DIRECTORY"
        chown $USER:$GROUP "$LOCK_DIRECTORY"
        chmod 0755 "$LOCK_DIRECTORY"
    fi

    if ! [ -d "$LOCK_DIRECTORY" ]; then
        return 1
    fi

    return 0
}

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/usr/sbin:/usr/bin:/sbin:/bin
DESC="daemon collecting playback statistics of Music Player Daemon (MPD)"
NAME=mpd-hits
DAEMON=/usr/bin/$NAME
DAEMON_ARGS="--daemon"
# PIDFILE will be set in read_config()
SCRIPTNAME=/etc/init.d/$NAME
CONFIG=/etc/mpd-hits.conf

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
[ -f /etc/default/rcS ] && . /etc/default/rcS

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

#
# 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

    read_config || return 2
    check_lock_dir || return 2

    start-stop-daemon --start --quiet --pidfile $PIDFILE \
        --exec $DAEMON --test > /dev/null \
        || return 1
    start-stop-daemon --start --quiet --pidfile $PIDFILE \
        --exec $DAEMON -- $DAEMON_ARGS \
        || return 2
}

#
# 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

    read_config || return 2
    start-stop-daemon --stop --quiet --retry 5 --pidfile $PIDFILE
}

case "$1" in
    start)
        [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
        do_start
        case "$?" in
            0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
            2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
    stop)
        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
        do_stop
        case "$?" in
            0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
            2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
    restart|force-reload)
        log_daemon_msg "Restarting $DESC" "$NAME"
        do_stop
        case "$?" in
            0|1)
                do_start
                case "$?" in
                    0) log_end_msg 0 ;;
                    1) log_end_msg 1 ;; # Old process is still running
                    *) log_end_msg 1 ;; # Failed to start
                esac
                ;;
            *)
                # Failed to stop
                log_end_msg 1
                ;;
        esac
        ;;
    *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
        exit 3
        ;;
esac

:
