#!/bin/sh
#===============================================================================
#
#  Copyright (C) 2022, 2023 by Digi International Inc.
#  All rights reserved.
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License version 2 as published by
#  the Free Software Foundation.
#
#
#  !Description: ConnectCore Cloud Services get started demo start/stop script
#
#===============================================================================

# Source function library: status and killproc
. /etc/init.d/functions

readonly CCCS_DEMO_BINARY="/usr/bin/cccs-gs-demo"
readonly STOP_TIMEOUT="5"

case "$1" in
	start)
		status ${CCCS_DEMO_BINARY} > /dev/null || ${CCCS_DEMO_BINARY}>/dev/null 2>&1 &
		;;
	stop)
		# Try to stop gracefully
		killproc ${CCCS_DEMO_BINARY} >/dev/null 2>&1
		for i in $(seq ${STOP_TIMEOUT}); do
			pid="$(pidof -o $$ "${CCCS_DEMO_BINARY}")" || break
			if [ "${i}" -eq ${STOP_TIMEOUT} ]; then
				kill -KILL "${pid}" >/dev/null 2>&1
			fi
			sleep 1
		done
		;;
	restart)
		$0 stop
		sleep 1
		$0 start
		;;
	*)
		echo "Usage: $0 {start|stop|restart}"
		exit 1
		;;
esac
