#!/bin/bash
#
# License: Copyright 2019 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 display manager
# Description: Starting/stopping SpinetiX's device display manager
### END INIT INFO

# Init script information
NAME=spxdispmanager
DESC="SPX display manager"

# Individual Daemon information
DAEMON=/usr/sbin/spxdispmanager
LOGDIR=/var/log
LOGFILE=$LOGDIR/spxdispmanager
RUNDIR=/run/spxdispmanager
IMAGELINK=/run/spxdispmanager/image
ARGS="--image=$IMAGELINK"
BASENAME=spxdispmanager
INACTIVEIMG=/usr/share/resources/default/splash/inactive.png

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

# Load splash configuration
BOOTIMG=
[ -f /etc/default/splash ] && . /etc/default/splash

# 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

# returns zero if in configuration reset mode
in_config_reset() {
    egrep -q '(^| )config=reset($| )' /proc/cmdline && return 0
    [ -s /boot-config ] && grep -q '^reset$' /boot-config && return 0
    return 1
}

# returns zero if running the installer
in_installer() {
    egrep -q '(^| )netinstall=' /proc/cmdline && return 0
    return 1
}

start() {
    local RET ERROR=
    local n

    echo -n "Starting $DESC: "

    if in_installer; then
        KEEP_FB_EMULATION="yes"
        ARGS="$ARGS --card-index=-1"
    fi

    if [ "$KEEP_FB_EMULATION" != "yes" -a -f /sys/module/drm_kms_helper/parameters/fbdev_emulation ]; then
        echo 0 > /sys/module/drm_kms_helper/parameters/fbdev_emulation
    fi

    # the log directory may be a symlink to /var/volatile/log which is created later
    # in the startup sequence so create it now if the log directory is absent
    [ -d $LOGDIR ] || mkdir -m 755 -p /var/volatile/log

    # the daemon will fail to start up if the log file cannot be created, make it conditional
    if [ -d $LOGDIR ]; then
        ARGS="$ARGS --logfile=$LOGFILE"

        # rotate previous log files
        for n in 8 7 6 5 4 3 2 1; do
            [ -f $LOGFILE.$n ] && mv -f $LOGFILE.$n $LOGFILE.$((n+1))
        done
        [ -f $LOGFILE ] && mv -f $LOGFILE $LOGFILE.1
    fi

    # use default factory configuration and reset output list if in config reset mode
    if in_config_reset; then
        ARGS="$ARGS -c /etc/spxdispmanager/config.json.spxsave --outputs="
        [ -f /etc/default/splash.spxsave ] && . /etc/default/splash.spxsave
    fi

    if [ -n "$BOOTIMG" ]; then
        echo -n "splash setup, "
        mkdir -p $RUNDIR && ln -sf "$BOOTIMG" $IMAGELINK
    fi
    if [ -f "$INACTIVEIMG" ]; then
        ARGS="$ARGS --inactive-image=$INACTIVEIMG"
    fi

    echo -n "$NAME "
    $DAEMON $ARGS
    RET=$?
    if [ $RET -eq 0 ]; then
	success; echo
    else
	failure; echo
	return 1
    fi
	
    return 0
}

stop () {
    local RET ERROR=

    echo -n "Stopping $DESC: $NAME "
    $DAEMON -k
    RET=$?
    if [ $RET -eq 0 ]; then
	success; echo
    else
	failure; echo
	return 1
    fi

    return 0
}

restart() {
    local RET

    echo "Restarting $DESC..."
    stop
    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

    # spxenroll has no support for HUP, so just restart
    condrestart
}

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 $?
	    ;;
	cnodrestart|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}" >&2
	    ;;
    esac

    return 1
}

parse $@
