#!/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 Crank demo
#
#===============================================================================

readonly DEMO="##CRANK_DEMO_PATH##"
readonly DEMO_DISPLAY="##CRANK_DEMO_DISPLAY##"
readonly DEMO_OPTS="##CRANK_DEMO_OPTIONS##"
readonly DEMO_ENV="##CRANK_DEMO_ENV##"
readonly SB_LAUNCHER="sb-launcher"
readonly SB_LAUNCHER_SCRIPT="/usr/bin/${SB_LAUNCHER}"
readonly CRANK_DEMO="crank-demo"
readonly PID_FILE="/run/${CRANK_DEMO}.pid"

[ -f "/etc/profile.d/tslib.sh" ] && . /etc/profile.d/tslib.sh

log() {
	if type "systemd-cat" >/dev/null 2>/dev/null; then
		systemd-cat -p "${1}" -t "${CRANK_DEMO}" printf "%s" "${2}"
	fi
	logger -p "${1}" -t "${CRANK_DEMO}" "${2}"
}

get_crank_demo_pid() {
	local pid="$(pgrep -f ${DEMO})"

	[ -n "${pid}" ] && { echo "${pid}"; return 0; }

	return 1
}

check_is_running() {
	local pid

	if [ -s "${PID_FILE}" ]; then
		pid="$(cat ${PID_FILE})"
	else
		pid="$(get_crank_demo_pid)"
		echo "${pid}" > ${PID_FILE}
	fi

	if [ "${pid}" ]; then
		kill -0 "${pid}" >/dev/null 2>&1 && return 0
	fi

	rm -f "${PID_FILE}"

	return 1
}

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() {
	check_is_running || return

	local pid="$(cat ${PID_FILE})"
	kill -TERM "${pid}" >/dev/null 2>&1

	local STOP_TIMEOUT="5"
	for i in $(seq ${STOP_TIMEOUT}); do
		check_is_running || { log info "stopped"; break; }
		if [ "${i}" -eq ${STOP_TIMEOUT} ]; then
			log warning "stop: ${CRANK_DEMO} did not stop gracefully"
			kill -KILL "${pid}" >/dev/null 2>&1
		fi
		sleep 1
	done
}

start() {
	check_is_running && { log warning "start: ${CRANK_DEMO} ALREADY running"; exit 0; }

	[ -d "/usr/share/wayland" ] && wait_for_wayland
	env ${DEMO_ENV} ${SB_LAUNCHER_SCRIPT} ${DEMO_OPTS} ${DEMO} >/dev/null 2>&1 &
	if [ $? -eq 0 ]; then
		echo $! > ${PID_FILE}
		log info "$(cat ${PID_FILE})"
		log info "started"
	fi
}

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