#! /bin/sh
# Version 0.5 October 26, 2016
### BEGIN INIT INFO
# Provides:          weaved
# Required-Start:    $network $named $portmap $syslog $remote_fs
# Required-Stop:     $remote_fs $syslog 
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Weaved Remot3 it startup
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.
### END INIT INFO

# Author: Mike Johnson <mike@weaved>
#

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Description of the service"
NAME=weavedconnectd
DAEMON=/usr/sbin/$NAME
DAEMON_ARGS="--options args"
START_PIDFILE=/var/run/remot3_start.pid
SCRIPTNAME=/etc/init.d/$NAME

# 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
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
	# startup monitor
	nohup /usr/bin/remot3_start.sh > /tmp/remot3_start.log 2>&1 &
	echo $! > $START_PIDFILE  
	#info=$(remot3_control.sh bprovision all)
	#logger [Weaved] $info
    #sleep 2
    # Update Version Numbers if needed
    # first check package and see if we need to update weaved software version info
    #info=$(remot3_control.sh updatedeb weavedconnectd)
    #logger [Weaved] $info
    # update firmware version info
    #fwver=$(mpd-get-fwversion | awk -F ":" '{ print $2 }')
    #info=$(remot3_control.sh update system_firmware "$fwver")
    #logger [Weaved] $info

	# start things up
	remot3_control.sh start all
	return 0
}

#
# 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
	
	#kill startup monitor
	if [ -e $START_PIDFILE ]; then  
	    pid=`cat $START_PIDFILE`
	    kill $pid
	    rm  $START_PIDFILE
    fi

	remot3_control.sh stop all
	RETVAL="$?"
	
	return "$RETVAL"
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
	#
	# If the daemon can reload its configuration without
	# restarting (for example, when it is sent a SIGHUP),
	# then implement that here.
	#
	#remot3_control.sh restart all
    do_stop
    do_start
	return 0
}

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
	;;
  status)
	status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
	;;
  restart|reload|force-reload)
	#
	# If do_reload() is not implemented then leave this commented out
	# and leave 'force-reload' as an alias for 'restart'.
	#
	log_daemon_msg "Reloading $DESC" "$NAME"
	do_reload
	log_end_msg $?
	;;
  *)
	#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
	exit 3
	;;
esac

:
