#!/bin/bash
#===============================================================================
# Copyright (c) 2014-2015 Wind River Systems, Inc.
# The right to copy, distribute, modify, or otherwise
# make use of this software may be licensed only pursuant
# to the terms of an applicable Wind River license agreement.
#
#===============================================================================

. /lib/wra/systemd.sh
. /lib/wra/config.sh

WRA_PROC="/usr/bin/wra"
WRA_PROC_BASENAME="wra"
WRA_OTA_PROC="/usr/bin/wra-ota"

# Differences in small rootfs:
# ps from busybox doesn't support -ef option
ps --help 2>&1 | grep -q "BusyBox" && PS="ps" || PS="ps -ef"

#===============================================================================
# WR agent functions
#===============================================================================
wra_syslog_daemon_check()
{
	# wr-iot-agent service depend on syslog and crond. But
	# maybe they are stopped in some cases, check again.
	systemd_service_check syslog.service
	systemd_service_check crond.service

	return 0
}

wra_remote_session_check()
{
	wra_jconf_load_rs

	if systemd_sshd_installed ; then
		wra_jconf_rs_sshd_enabled && systemd_start_sshd
	else
		echo "WARNNING: sshd server is not installed!"
		echo "WARNNING: SSH remote session will not work right!"
	fi

	# Only try to start related service, no matter failed or succussfully
	return 0
}

wra_server_ready_check()
{
	local cnt="$1"
	local saddr="$(wra_jconf_server_addr)"

	[ -z "$saddr" ] && return 1

	while [ $cnt -gt 0 ] ; do
		ping -w 1 -c 1 $saddr >/dev/null 2>&1 && return 0
		# sleep 10 = 9 + 1 (in -w option)
		sleep 9
		let cnt=cnt-1
	done

	return 1
}

wra_setup()
{

	[ -e "$WRA_CONF" ] || {
		echo "No configuration file \"$WRA_CONF\""
		return 1
	}

	wra_syslog_daemon_check || return 1
	wra_remote_session_check || return 1

	# wait max 60s for upstream interface ready.
	# in IDP platform,  netifd service is working in asynchronous mode,
	# so upstream interface may be not set up when wr agent is started.
	wra_server_ready_check 6

	return 0
}

wra_clear()
{
	# Workaroud for runing wra second time failed issue.
	[ -e /dev/mqueue ] || mkdir -p /dev/mqueue
	mount | grep -q "/dev/mqueue" || mount -t mqueue none /dev/mqueue
	rm -rf /dev/mqueue/wra_rtp_q
	rm -rf /dev/mqueue/wra_internal_exec_q
	rm -rf /dev/mqueue/wrac_*
}

wra_status()
{
    ps -A | grep -v "grep" | grep -q "${WRA_PROC_BASENAME}$" && return 0 || return 1
}

wra_start()
{
	local log_opt=""
	local log_level=$(wra_jconf_log_level)

	wra_clear
	wra_setup || return 1

	[ -n "$log_level" ] && log_opt="-l $log_level"
	${WRA_PROC} -f ${WRA_CONF} ${log_opt} >/dev/null 2>&1 &
}

wra_stop_wait()
{
	local cnt=$1
	local i=0

	while wra_status && [ $i -lt "$cnt" ] ; do
		#echo "Waiting for wr-iot-agent stop: $i"
		sleep 1
		let i++
	done
}

wra_stop()
{
	killall -s USR1 $(basename $WRA_OTA_PROC)
	killall $(basename $WRA_PROC)
	wra_stop_wait 30
	wra_clear

}

#===============================================================================
# Service functions
#===============================================================================
start()
{
	wra_status && {
		echo "WR-Agent has already been started."
		return 0
	}

	echo "Starting WR-Agent ... "
	wra_start
}

stop()
{
	wra_status || {
		echo "WR-Agent has already been stopped."
		wra_clear
		return 0
	}

	echo "Stopping WR-Agent ... "
	wra_stop
}

restart()
{
	wra_status && {
		echo "Stopping WR-Agent ... "
		wra_stop
	}

	echo "Starting WR-Agent ... "
	wra_start
}

status()
{
	wra_status && {
		echo "WR-Agent is running ... "
		return 0
	} || {
		echo "WR-Agent is stopped."
	}

	return 1
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload|restart)
		restart
		;;
	status)
		status
		;;
	*)
		echo $"Usage: $0 {start|stop|restart|reload|status}"
		exit 1
esac

