#!/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: Initialize the ConnectCore demo example
#
#===============================================================================

# Source function library
. /etc/init.d/functions

readonly DEMO_DISPLAY="##CC_DEMO_DISPLAY##"
readonly DEMO_ENV="##CC_DEMO_ENV##"
readonly COG_BINARY="/usr/bin/cog"
readonly STOP_TIMEOUT="5"

wait_for_wayland() {
	local count=20
	local wayland_socket="/run/user/0/${DEMO_DISPLAY}"

	while [ ! -S "${wayland_socket}" ]; do
		sleep 1
		count=$((count-1))
		if [ "${count}" = 0 ]; then
			return 1
		fi
	done
	return 0
}

stop_process() {
	# try to stop gracefully
	killproc "${1}" >/dev/null 2>&1
	for i in $(seq ${STOP_TIMEOUT}); do
		pid=$(pidofproc "${1}") || break
		if [ "${i}" -eq ${STOP_TIMEOUT} ]; then
			kill -KILL "${pid}" >/dev/null 2>&1
		fi
		sleep 1
	done
}

start() {
	local COG_ENV="${DEMO_ENV}"

	if [ -d "/usr/share/wayland" ]; then
		wait_for_wayland
		COG_ENV="${COG_ENV} COG_PLATFORM_WL_VIEW_FULLSCREEN=1"
		# FULLSCREEN variable will have preference over other geometry variables.
		if [ -f "/sys/class/graphics/fb0/virtual_size" ]; then
			width="$(cut -d',' -f1 /sys/class/graphics/fb0/virtual_size)"
			heigth="$(cut -d',' -f2 /sys/class/graphics/fb0/virtual_size)"
			[ -n "${width}" ] && COG_ENV="${COG_ENV} COG_PLATFORM_WL_VIEW_WIDTH=${width}"
			[ -n "${heigth}" ] && COG_ENV="${COG_ENV} COG_PLATFORM_WL_VIEW_HEIGHT=${heigth}"
		fi
	fi

	env ${COG_ENV} ${COG_BINARY} \
			--allow-file-access-from-file-urls=true \
			--allow-universal-access-from-file-urls=false \
			--enable-offline-web-application-cache=false \
			--enable-page-cache=false \
			--allow-modal-dialogs=true \
			/srv/www/index.html > /dev/null 2>&1 &
}

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