#!/bin/sh /etc/rc.common
# 

# version 2019-09-05

START=99

USE_PROCD=1
QUIET=""
PROCD_DEBUG=1

restart() {
	echo "$0 restart_service (ati_server) called....."
	reload
}


start_service() {
	logger "$0 start_service (ati_server) called....."

	/www/ati/scripts/led.sh --network;

	procd_open_instance

	procd_set_param env NODE_PATH=/usr/lib/node_modules
	tz=`uci get system.@system[0].timezone`
	procd_set_param env TZ=$tz
	procd_set_param command /bin/sh -c 'cd  /www/ati; node --expose_gc server.js ; echo exited with status $? '

  	procd_set_param respawn ${respawn_threshold:-15} ${respawn_timeout:-1} ${respawn_retry:-1}

  	procd_set_param stdout 1 # forward stdout of the command to logd
  	procd_set_param stderr 1 # same for stderr

  	procd_close_instance

	logger "$0 start_service (reset_service) called....."
	ps | grep 'sh reset-settings.sh' | grep -v grep | awk '{print $1}' | xargs -r kill -9

	procd_open_instance
	procd_set_param command /bin/sh -c 'cd  /www/ati/scripts; sh reset-settings.sh ; echo exited with status $? '
  	procd_set_param respawn ${respawn_threshold:-15} ${respawn_timeout:-1} ${respawn_retry:-1}
  	procd_set_param stdout 1 # forward stdout of the command to logd
  	procd_set_param stderr 1 # same for stderr
  	procd_close_instance

}


stop_service() {
	logger "$0 stop_service (ati_server) called....."
	nodepids=`pidof nodejs node` && kill $nodepids
	echo "done kill pidof nodejs node --> $nodepids"
}

reload_service() {
	logger "$0 reload_service (ati_server) called....."
	restart
	logger "done reload_service (ati_server)"
}

boot() {
	# check initialized...
	sh /www/ati/scripts/initialize.sh

	start
}