#!/bin/sh

#  connectd_schannel.sh
#  startup script for the server channel listener
#  one instance of this can serve all installed Connectd daemons
#  Copyright (C) 2019 remot3.it, Inc. All rights reserved.
#

VERSION="v1.8"
AUTHOR="Gary Worsham"
MODIFIED="February 26, 2019"
BASEDIR=$CONNECTD_BASEDIR
. "$BASEDIR"/usr/bin/connectd_options

DAEMON=connectd_schannel."$PLATFORM"
CONF_FILE="$BASEDIR"/etc/connectd/schannel.conf

BIN_PATH=$BIN_DIR/$DAEMON
PIDPATH=$PID_DIR/$DAEMON.pid

##### Version #####
displayVersion()
{
    printf "remote.it server channel daemon start/stop script Version: %s \n" "$VERSION"
    # check for sudo user at this point
    if ! [ "$(id -u)" = 0 ]; then
        echo "Running $0 requires root access." 1>&2
        echo "Please run sudo $0" 1>&2
	exit 1
    fi
}
##### End Version #####


# using same isRunningCmd that are in the connectd startup routines
isRunningCmd()
{
    isRunning=0
    # get the PID from the file that is created by this script
    if [ -f "$PIDPATH" ]; then
        runningPID="$(cat $PIDPATH)"
        # see if there is a corresponding entry in /proc
        if [ -d /proc/$runningPID ]; then
            isRunning=1
        fi
    fi
}

stopConnectd()
{
	isRunningCmd
	if [ $isRunning != 0 ]; then
		if [ "$2" != "-q" ]; then
			echo "Stopping $DAEMON..."
		fi
		kill $runningPID 2> /dev/null
		rm $PIDPATH 2> /dev/null
	else
		if [ "$2" != "-q" ]; then
			echo "$DAEMON not currently active. Nothing to stop."
		fi
	fi
}

startConnectd()
{
	isRunningCmd
	if [ $isRunning = 0 ]; then
		if [ "$2" != "-q" ]; then
			echo "Starting $DAEMON..."
		fi
		 $BIN_DIR/$DAEMON -f $CONF_FILE -d $PID_DIR/$DAEMON.pid
	else
		if [ "$2" != "-q" ]; then
			echo "$DAEMON is already started"
		fi
	fi
}

restartConnectd()
{
	stopConnectd
	sleep 2
	startConnectd
}

displayVersion

if [ -z $1 ]; then
	echo "You need one of the following arguments: start|stop|restart"
	exit
elif [ "$(echo "$1" | tr '[A-Z]' '[a-z]' | tr -d ' ')" = "stop" ]; then 
	stopConnectd
elif [ "$(echo "$1" | tr '[A-Z]' '[a-z]' | tr -d ' ')" = "start" ]; then
	startConnectd
elif [ "$(echo "$1" | tr '[A-Z]' '[a-z]' | tr -d ' ')" = "restart" ]; then
	restartConnectd
else
	echo "This option is not supported"
fi

