#!/bin/sh
LOG="/bin/logger -p user.info -t mdev-usb"
WARN="/bin/logger -p user.warn -t mdev-usb"
SAMSUNG75h=1
flagLCD=/tmp/.lcd-${MDEV}

# supported dpf-devices
DPF="1908:0102"

# supported spf-devices
SPFstorage="04E8:200E 04E8:2012 04E8:2027 04E8:2033 04E8:2025"
SPFmonitor="04E8:200F 04E8:2013 04E8:2028 04E8:2034 04E8:2026"
# SPFmodel: ^SPF-75H  ^SPF-85H  ^SPF-107H ^SPF-87H  ^SPF-75Hold
SPF="${SPFstorage} ${SPFmonitor}"

DEVICES="${DPF} ${SPF}"

for DEVICE in ${DEVICES}; do
	V=$(echo ${DEVICE:0:4} | sed 's/^[0]*//' | tr [:upper:] [:lower:]) # lower case vendor  w/o leading zeros
	P=$(echo ${DEVICE:5:4} | sed 's/^[0]*//' | tr [:upper:] [:lower:]) # lower case product w/o leading zeros

	case "$ACTION" in
		add|"")
			#$LOG "trying to process ${DEVICE} (V:{$V} P:{$P}) on ${MDEV}"

			uevent=/sys/class/usb_device/${MDEV}/device/uevent

			test -e $uevent					|| continue
			grep "^PRODUCT=${V}/${P}/*" $uevent >/dev/null	|| continue

			$LOG "supported device (ID ${DEVICE}) on ${MDEV} found"

			# dpf/spf-support
			if $(echo "${DPF} ${SPF}" | grep -q "${DEVICE}"); then
				$LOG "creating flagfile '$flagLCD'"
				echo "${DEVICE}" > $flagLCD

				if $(echo "${DPF} ${SPFstorage}" | grep -q "${DEVICE}"); then
					if [ $SAMSUNG75h == 1 ]; then
						$LOG "(re)starting lcd4linux for SPF-75h in Storage Modus"
						/var/plugins/spftool/spf1.sh

					else

						$LOG "DPF or SPF in storage mode found"
						$LOG "(re)starting lcd4linux"

						service lcd4linux reload
					fi
				fi

				if $(echo "${SPFmonitor}" | grep -q "${DEVICE}"); then
					$LOG "SPF in monitor mode found"
					if [ $SAMSUNG75h == 1 ]; then
						$LOG "(re)starting lcd4linux for SPF-75h"
						/var/plugins/spftool/spf.sh
						sleep 1
						service lcd4linux reload
					else
						if [ -e /tmp/lcd4linux.pid ]; then
							$LOG "do nothing"
						else
							$LOG "(re)starting lcd4linux"
							service lcd4linux reload
						fi
					fi
				fi
			fi
		;;
		remove)
			#$LOG "trying to process ${DEVICE} (V:{$V} P:{$P}) on ${MDEV}"

			# dpf/spf-support
			if [ -e $flagLCD ]; then
				grep "^${DEVICE}" $flagLCD >/dev/null	|| continue

				$LOG "supported DPF/SPF (ID ${DEVICE}) removed from ${MDEV}"

				if $(echo "${DPF} ${SPFmonitor}" | grep -q "${DEVICE}"); then
					$LOG "DPF or SPF in monitor mode removed"
					$LOG "stopping lcd4linux"

					service lcd4linux stop
				fi

				if $(echo "${SPFstorage}" | grep -q "${DEVICE}"); then
					$LOG "SPF in storage mode removed"
					$LOG "do nothing"
				fi

				$LOG "removing flagfile '$flagLCD'"
				rm -f $flagLCD
			fi
		;;
	esac
done
