#! /bin/sh
#
# License: Copyright 2016 SpinetiX S.A. 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 4 5
# Default-Stop: 
# Short-Description: starts/stops the spxupnpd daemon
# Description: starts/stops the spxupnpd daemon
### END INIT INFO 

# Init script information
NAME=spxmanage-spxupnpd
DESC="SPX SSDP/UPnP daemon"

# config
RCMAIN=/usr/share/resources/default/init/main
RCRAPERCA=/usr/share/resources/default/init/raperca

# Individual Daemon information
DAEMON=/usr/sbin/spxupnpd
ARGS="-I lo"
BASENAME=spxupnpd

# Config elements
SAFEINDICATOR=/var/run/raperca/safe-mode
SPXHTDEFS=/etc/apache2/conf.d/01-spxmanage-defs.conf

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

# Load apache2 configuration to detect SSL
[ -f /etc/default/apache2 ] && . /etc/default/apache2

# Load llmnrd configuration to detect if it is enabled or not
DISABLE_LLMNRD=
[ -f /etc/default/llmnrd ] && . /etc/default/llmnrd

# Load output from licensecheck
[ -f /run/licensecheck/discovery.conf ] && . /run/licensecheck/discovery.conf

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

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

# Source the init script functions
. /etc/init.d/functions
. /etc/spxmanage/init-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

start() {
    local RET ERROR=
    local sn="${SPX_SN:-unknown}"
    local no_cport

    load_web_server_opts "$ENABLESSL" "$rc_host_prefix" "$sn"
    
    if [ -n "$DISCOVERY_NO_PUBLISH" -o "$NOHTTPPUBLISH" = "yes" ]; then
        no_cport=true
    else
        no_cport="$rc_dns_sd_player_no_cport"
    fi

    if [ -f $SAFEINDICATOR ]; then
        ARGS="$ARGS --mode safe"
    else
        ARGS="$ARGS --mode normal"
    fi

    if [ ! -e /etc/spxmanage/configured ]; then
        ARGS="$ARGS --not-configured"
    fi

    if [ "$ENABLEUPNP" != "yes" ] || [ "$NOHTTP" = "yes" -a "$NOHTTPS" = "yes" ]; then
        ARGS="$ARGS --disabled"
    fi

    if [ -n "$no_cport" ]; then
        ARGS="$ARGS --cport -1 --scport -1"
    else
        [ "$NODAVHTTP" = "yes" -o "$HTTPREDIRECT" = "yes" ] && ARGS="$ARGS --cport -1"
        [ "$NOHTTPS" = "yes" ] && ARGS="$ARGS --scport -1"
    fi

    if [ -n "$HTTPSHOST" ]; then
        ARGS="$ARGS --fqdn $HTTPSHOST"
    fi
    
    if [ "$NOHTTP" = "yes" ]; then
        ARGS="$ARGS --url-scheme https"
    fi

    if [ -z "$DISABLE_LLMNRD" ]; then
        ARGS="$ARGS --use-hostname"
    fi

    if grep -q '^[[:space:]]*Define[[:space:]]\+SSL_DISABLE_SRP\($\|[[:space:]]\)' "$SPXHTDEFS"; then
        ARGS="$ARGS --no-srpv"
    fi 

    echo -n "Starting $DESC: $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..."
    # indicate to the watchdog daemon that restart was on purpose
    touch /var/run/spxmanage/spxupnpd-restart
    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

    # spxupnp 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 $@

