#!/bin/bash

vendor() {
	if lsusb | grep -E "0404:" > /dev/null; then
		echo "NCR"
		return 0
	fi

	if lsusb | grep -E "1a75:" > /dev/null; then
		if lsusb | grep -E "154f:1300|0d9f:0004|154f:154f" > /dev/null; then
			echo "SAGA"
			return 0
		else
			echo "Hyosung"
			return 0
		fi
	fi

	if lsusb | grep -E "0471:" > /dev/null; then
		echo "GRG"
		return 0
	fi

	echo "Unknown"
	return 0
}

case $1 in

list)
	systemctl list-units --type=service | grep -Po '(?<=xfs4-).*'
;;

state)
	systemctl list-unit-files | grep -Po '(?<=xfs4-).*'
;;

start)
	if [ $# -lt 2 ]; then
		echo "usage: x4 start <service name>"
		exit 1
	fi
	systemctl start xfs4-$2
;;

restart)
	if [ $# -lt 2 ]; then
		echo "usage: x4 restart <service name>"
		exit 1
	fi
	systemctl restart xfs4-$2
;;

stop)
	if [ $# -lt 2 ]; then
		echo "usage: x4 stop <service name>"
		exit 1
	fi
	systemctl stop xfs4-$2
;;

shutdown)
	systemctl stop $(systemctl list-units --type=service | grep -E -o xfs4-[0-9A-Za-z.\-]+)
;;

trigger)
    systemctl start xfs4-discovery
    systemctl start xfs4-pakard-application

	udevadm control --reload-rules
	udevadm trigger --subsystem-match=usb --action=add
	udevadm trigger --subsystem-match=hidraw --action=add

	VENDOR=$(vendor)
    if [ "$VENDOR" = "SAGA" ]; then
		if [ -f /lib/systemd/system/xfs4-oem-cardreader.service ]; then
			systemctl start xfs4-oem-cardreader
		fi
    fi

    if [ "$VENDOR" = "GRG" ]; then
		if [ -f /lib/systemd/system/xfs4-oem-cardreader.service ]; then
			systemctl start xfs4-oem-cardreader
		fi
		if [ -f /lib/systemd/system/xfs4-grg-keyboard-epp.service ]; then
			systemctl start xfs4-grg-keyboard-epp
		fi
    fi
;;

vendor)
	echo "$(vendor)"
;;

adjust-touchscreen)
    for (( ;; ))
    do
        monitor=$(xrandr --current | grep " connected primary " | awk '{ print$1 }')

        if [ -z "$monitor" ]; then
            monitor=$(xrandr --current | grep " connected " | awk '{ print$1 }')
            orientation=$(xrandr --query --verbose | grep "$monitor" | cut -d ' ' -f 5)
        else
            orientation=$(xrandr --query --verbose | grep "$monitor" | cut -d ' ' -f 6)
        fi

        if [ -z "$monitor" ]; then
            sleep 1
            continue
        fi

        if [ -n "$orientation" ]; then
            if [ "$orientation" == "$prevOrientation" ]; then
                sleep 1
                continue
            else
                prevOrientation="$orientation"
            fi

            echo "Monitor found  at '$monitor' output, orientation: $orientation, prevOrientation: $prevOrientation"
        else
            echo "Unable to determine '$monitor' output orientation";
            sleep 1
            continue
        fi


        IFS=$'\n'
        inputs=($(xinput --list --name-only))
        touchscreens=($(cat /etc/pakard/known_touchscreens.list))

        itouchscreens="${touchscreens[*]}"

        for item in ${inputs[@]}; do
            if [[ $itouchscreens =~ "$item" ]] ; then
                touchscreen="$item"
            fi
        done

        if [ -n "$touchscreen" ]; then
            echo "Bind '$touchscreen' touchscreen to '$monitor' output"
            xinput map-to-output "$touchscreen" "$monitor"
        fi

        sleep 1
    done
;;

logs)
	if [ $# -lt 2 ]; then
		journalctl -o cat -f $(systemctl list-units --type=service | grep -E -o 'xfs4-[0-9A-Za-z.\-]+' |  sed 's/.*/-u &/')
		exit 0
	fi

	journalctl -o cat -f -u xfs4-$2
;;

*)
	echo ""
	echo "Usage: x4 [cmd]"
	echo ""
	echo "Commands"
	echo ""
	echo "    list                   list all XFS4 services"
	echo "    start [name]           start service"
	echo "    stop [name]            stop service"
	echo "    restart [name]         restart service"
	echo "    shutdown               stop all processes"
	echo "    trigger                restart services if not running"
    echo "    vendor                 guess ATM vendor"
    echo "    adjust-touchscreen     try to bind known touchscreens to primary monitor"
	echo "    logs                   show all service logs"
	echo "    logs [name]            show log of specific service"
	exit 1
;;
esac