#!/bin/sh

export DISPLAY=:0
export PATH=/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin

is_rk3568() {
	local tval="$(grep -i -a -e RK3568 -e ok3568 -e XDDL3568 -e RK3588 /proc/device-tree/model)"
	if [ -z "${tval}" ] ; then
		return 1
	fi

	tval="$(cat /tmp/sysinfo/board_name 2>/dev/null | grep -e RK3588)"
	if [ -n "${tval}" ] ; then
		return 0
	fi

	tval="$(cat /tmp/sysinfo/board_name 2>/dev/null | grep -e WLOSANY_RK3568)"
	if [ -n "${tval}" ] ; then
		return 0
	fi
	return 2
}

check_uptime() {
	local tim0=$1
	while [ ! -f /proc/uptime ] ; do
		sleep 1
	done
	local tim1="$(cat /proc/uptime | awk -F. '{print $1}')"

	if [ ${tim0} -le ${tim1} ] ; then
		return 1
	fi
	while [ ${tim0} -gt ${tim1} ] ; do
		echo "Sleeping: $((tim0 - tim1))"
		sleep $((tim0 - tim1))
		tim1="$(cat /proc/uptime | awk -F. '{print $1}')"
	done
	return 0
}

check_hdmi_display() {
	if [ -z "$(xrandr 2>/dev/null | grep -e HDMI)" ] ; then
		return 1 # no HDMI connected
	fi

	if [ -n "$(xrandr 2>/dev/null | grep -e 'HDMI-1 disconnected')" ] ; then
		return 2 # no HDMI connected
	fi
	return 0
}

TOUCH_ID=0
has_screen_touch_res() {
	local RESOLUTION="$1"
	if [ -z "$(xrandr 2>/dev/null | grep -E -e "HDMI-1 connected.+${RESOLUTION}")" ] ; then
		return 1
	fi
	local TSID=$(xinput | grep -e $2 | head -n 1 | sed -r -n -e 's#^.+id=([0-9]+).+$#\1#p')
	if [ -z "${TSID}" ] ; then
		return 2
	fi
	TOUCH_ID="${TSID}"
	return 0
}

process_rk3568() {
	local hdmi_res=''
	local has_ILITEK=0
	local has_WingCool=0
	local CALIF=/etc/X11/xorg.conf.d/99-calibration.conf
	if has_screen_touch_res '1280x720' ILITEK; then
		has_ILITEK=1
		hdmi_res='1280x720'
	elif has_screen_touch_res '1280x800' ILITEK; then
		has_ILITEK=1
		hdmi_res='1280x800'
	elif has_screen_touch_res '1280x1024' ILITEK; then
		has_ILITEK=1
		hdmi_res='1280x1024'
	elif has_screen_touch_res '1366x768' ILITEK; then
		has_ILITEK=1
		hdmi_res='1366x768'
	elif has_screen_touch_res '1440x900' ILITEK; then
		has_ILITEK=1
		hdmi_res='1440x900'
	elif has_screen_touch_res '1600x900' ILITEK; then
		has_ILITEK=1
		hdmi_res='1600x900'
	elif has_screen_touch_res '1680x1050' ILITEK; then
		has_ILITEK=1
		hdmi_res='1680x1050'
	elif has_screen_touch_res '1920x1080' ILITEK; then
		has_ILITEK=1
		hdmi_res='1920x1080'
	elif has_screen_touch_res '1024x600' WingCool; then
		has_WingCool=1
		hdmi_res='1024x600'
	elif has_screen_touch_res '1920x1080' WingCool; then
		has_WingCool=1
		hdmi_res='1920x1080'
	elif has_screen_touch_res '1280x720' WingCool; then
		has_WingCool=1
		hdmi_res='1280x720'
	elif has_screen_touch_res '1440x576' WingCool; then
		has_WingCool=1
		hdmi_res='1440x576'
	elif has_screen_touch_res '1024x768' WingCool; then
		has_WingCool=1
		hdmi_res='1024x768'
	elif has_screen_touch_res '800x600' WingCool; then
		has_WingCool=1
		hdmi_res='800x600'
	elif has_screen_touch_res '720x576' WingCool; then
		has_WingCool=1
		hdmi_res='720x576'
	elif has_screen_touch_res '720x480' WingCool; then
		has_WingCool=1
		hdmi_res='720x480'
	elif has_screen_touch_res '640x480' WingCool; then
		has_WingCool=1
		hdmi_res='640x480'
	fi

	if [ ${has_ILITEK} -ne 0 ] ; then
		diff -q /app/config/ILITEK-tp-${hdmi_res}.conf ${CALIF} 2>/dev/null
		if [ $? -ne 0 ] ; then
			cp -v -f /app/config/ILITEK-tp-${hdmi_res}.conf ${CALIF}
			# systemctl restart lightdm
		fi
		sleep 1
		xinput map-to-output "${TOUCH_ID}" 'HDMI-1'
	elif [ ${has_WingCool} -ne 0 ] ; then
		diff -q /app/config/WingCool-tp-${hdmi_res}.conf ${CALIF} 2>/dev/null
		if [ $? -ne 0 ] ; then
			cp -v -f /app/config/WingCool-tp-${hdmi_res}.conf ${CALIF}
			# systemctl restart lightdm
		fi
		sleep 1
		xinput map-to-output "${TOUCH_ID}" 'HDMI-1'
	fi
	return 0
}

check_uptime 30
if is_rk3568 ; then
	process_rk3568
fi
