#!/bin/sh
#  Copyright (c) 2018, The OpenThread Authors.
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#  3. Neither the name of the copyright holder nor the
#     names of its contributors may be used to endorse or promote products
#     derived from this software without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGE.
#
### BEGIN INIT INFO
# Provides:          otbr-web
# Required-Start:    otbr-agent
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: web server for boreder router
# Description: otbr-web provides a web gui for boder router
### END INIT INFO

set -e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="thread web interface"
NAME=otbr-web
DAEMON=@CMAKE_INSTALL_FULL_SBINDIR@/otbr-web
PIDFILE=/var/run/otbr-web.pid

OTBR_WEB_CONF=@CMAKE_INSTALL_FULL_SYSCONFDIR@/default/otbr-web

. /lib/lsb/init-functions
. /lib/init/vars.sh

if [ -f $OTBR_WEB_CONF ]; then
    . $OTBR_WEB_CONF
fi

start_web()
{
    if [ -e $PIDFILE ]; then
        if $0 status > /dev/null ; then
            log_success_msg "$DESC already started; not starting."
            return
        else
            log_success_msg "Removing stale PID file $PIDFILE."
            rm -f $PIDFILE
        fi
    fi

    log_daemon_msg "Starting $DESC" "$NAME"
    start-stop-daemon --start --quiet \
        --pidfile $PIDFILE --make-pidfile \
        -b --exec $DAEMON -- $OTBR_WEB_OPTS
    log_end_msg $?
}

stop_web()
{
    log_daemon_msg "Stopping $DESC" "$NAME"
    start-stop-daemon --stop --retry 5 --quiet --oknodo \
        --pidfile $PIDFILE --remove-pidfile
    log_end_msg $?
}

case "$1" in
    start)
        start_web
        ;;
    restart|reload|force-reload)
        stop_web
        start_web
        ;;
    stop|force-stop)
        stop_web
        ;;
    status)
        status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $?
        ;;
    *)
        log_action_msg "Usage: /etc/init.d/$NAME {start|stop|status|restart|reload|force-reload}"
        exit 2
        ;;
esac
