#!/bin/bash
#
# License: Copyright 2015 SpinetiX. This file is licensed
#          under the terms of the GNU General Public License version 2.
#          This program is licensed "as is" without any warranty of any
#          kind, whether express or implied.
#
# Copyright 1999-2003 MontaVista Software, Inc.
# Copyright 2002, 2003, 2004 Sony Corporation
# Copyright 2002, 2003, 2004 Matsushita Electric Industrial Co., Ltd.
#
### BEGIN INIT INFO
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 5
# Default-Stop:
# Short-Description: Starting/stopping SpinetiX's content player
# Description: Starting/stopping SpinetiX's content player
### END INIT INFO

# Init script information
NAME=raperca
DESC="SPX content player"

# Individual Deamon information
DAEMON=/usr/bin/raperca
CONFFILE=/etc/raperca/spxconfig.xml
ALTCONFFILE=/etc/raperca/spxconfig-nonconfigured.xml
ARGS="-daemon"
BASENAME=${DAEMON##*/}
LOADKMODS=/etc/raperca/loadmodules.sh
CLEANCACHE=/var/spool/raperca/clean-cache
CLEANWEBSTORAGE=/var/spool/raperca/clean-webstorage
SAFEINDICATOR=/var/run/raperca/safe-mode
FONTSENTINEL=/var/run/raperca/font-sentinel
APPXDGCONFIGDIR=/usr/share/raperca/xdg
RCRAPERCA=/usr/share/resources/default/init/raperca

# Load identifiers before starting
[ -f /etc/spinetix/identifiers ] && . /etc/spinetix/identifiers

# Load resources
[ -f "$RCRAPERCA" ] && . "$RCRAPERCA"

# Set up the path variables
. /etc/raperca/init-functions
init_dirs "$2"

# We run at nice level -2, kernel threads run at nice -5 or lower
# and essential system daemons such as udev run at -4, so -2
# is a good compromise between boost and not adding system latency
# (which could also adversely affect raperca)
NICELEVEL=-2

# Load init script configuration
[ -f /etc/default/$NAME ] && . /etc/default/$NAME

# Source the init script functions
. /etc/init.d/functions

# Verify daemons are installed
if [ ! -x $DAEMON -a "$1" != "stop" ]; then
    echo -n "Not starting $DESC $NAME, $DAEMON not installed"
    warning
    echo
    exit 0
fi

make_font_sentinel() {
    [ -f "$FONTSENTINEL" ] && return
    # The font sentinel file should be a valid XML fontconfig file
    # to avoid error messages from fontconfig
    cat > "$FONTSENTINEL" <<EOF
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<!-- Dummy sentinel config file to trigger font rescan on mtime change -->
</fontconfig>
EOF
}

start() {
    local RET netiface

    echo -n "Starting $DESC: "

    echo -n "kernel-mods"
    [ ! -e $LOADKMODS ] || $LOADKMODS
    RET=$?
    if [ $RET -eq 0 ]; then
	echo -n ", "
    else
	echo -n " "; failure; echo
	return 1
    fi

    if [ -f $SAFEINDICATOR ]; then
	echo -n "starting in safe mode, "
	ARGS="$ARGS -safemode"
    fi

    if [ -n "$disable_hotplug_hw_type_regex" ] && [[ "$SPX_HW_TYPE" =~ $disable_hotplug_hw_type_regex ]]; then
        echo -n "-no-hotplug option added, "
        ARGS="$ARGS -no-hotplug"
    fi

    # the active (configured) network interface is the first one marked "auto" which is not "lo"
    netiface="$(sed -e '/^\s*auto\s/!d;/^\s*auto\s\+lo\b/d;s/^\s*auto\s*//;q' /etc/network/interfaces)"
    [ -n "$netiface" ] && ARGS="$ARGS -netIface $netiface"

    if type -t splashd > /dev/null; then
	echo -n "splash stop"
	splashd -n -k
	RET=$?
	if [ $RET -eq 0 ]; then
	    echo -n ", "
	else
	    echo -n " "; failure; echo
	    return 1
	fi
    fi

    if [ -x /etc/init.d/vidmode -a ! -e /etc/spxmanage/configured ]; then
	echo -n "setting max resolution (unconfigured system)"
	/etc/init.d/vidmode setmax
	echo -n ", "
    fi

    if [ ! -e /etc/spxmanage/configured ]; then
        echo -n "clearing setup shared vars"
        if [ -f "$WEBSTORAGEDIR"/local.db ] && [ -s "$WEBSTORAGEDIR"/local.db ]; then
            sqlite3 "$WEBSTORAGEDIR"/local.db 'DELETE FROM WEBSTORAGE WHERE KEY GLOB "spx:setup:*"'
        fi
        echo -n ", "
    fi

    echo -n "$BASENAME "
    [ -f /etc/spxmanage/configured ] && conffile=$CONFFILE || conffile=$ALTCONFFILE
    make_font_sentinel && \
	cd "$STARTDIR" && \
	XDG_CONFIG_HOME="$APPXDGCONFIGDIR" \
	GST_REGISTRY_UPDATE=no GST_REGISTRY=/var/cache/gstreamer-1.0/registry.bin \
	nice -n $NICELEVEL $DAEMON $ARGS $conffile
    RET=$?
    if [ $RET -eq 0 ]; then
	success; echo
    else
	failure; echo
	return 1
    fi

    return 0
}

stop () {
    local RET

    echo -n "Stopping $DESC: $NAME "

    killproc $BASENAME
    RET=$?
    if [ $RET -eq 0 ]; then
	success; echo
    else
	failure; echo
	return 1
    fi

    return 0
}

restart() {
    local RET

    echo "Restarting $DESC..."
    stop
    sleep 2
    start
    RET=$?

    return $RET
}

condrestart() {
    local RET

    pidofproc $BASENAME >/dev/null
    RET=$?
    if [ $RET -eq 0 ]; then
	restart
	RET=$?
    else
	RET=1
    fi

    return $RET
}

reload() {
    local RET pid

    echo -n "Reloading $DESC configuration "
    pid=`pidofproc $BASENAME` && kill -HUP $pid && success || failure 1
    RET=$?
    echo

    return $RET
}

forcereload() {
    local RET

    reload
    RET=$?
    if [ $RET -ne 0 ]; then
	restart
	RET=$?
    fi
	
    return $RET
}

parse() {
    case "$1" in
	start)
	    start
	    return $?
	    ;;
	stop)
	    stop
	    return $?
	    ;;
	restart)
	    restart
	    return $?
	    ;;
	condrestart|try-restart)
	    condrestart
	    return $?
	    ;;
	reload)
	    reload
	    return $?
	    ;;
	force-reload)
	    forcereload
	    return $?
	    ;;
	status)
	    status $BASENAME
	    return $?
	    ;;
	*)
	    echo "Usage: $NAME " \
		"{start|stop|restart|condrestart|reload|force-reload|status} [service-dir]" >&2
	    ;;
    esac
	
    return 1
}

parse $@



