#!/bin/sh

# export DBGLVL=10; procd -h /etc/hotplug.json

if [ "${ACTION}" = "add" ]; then
    slot=0
    M2=0
    if [ "${DEVPATH}" = "/devices/platform/101c0000.ehci/usb1/1-1/1-1.1/1-1.1:1.0" -o "${DEVPATH}" = "/devices/platform/1e1c0000.xhci/usb1/1-2/1-2.1/1-2.1:1.0" ]; then
        slot=1
    elif [ "${DEVPATH}" = "/devices/platform/101c0000.ehci/usb1/1-1/1-1.2/1-1.2:1.0" -o "${DEVPATH}" = "/devices/platform/1e1c0000.xhci/usb1/1-2/1-2.2/1-2.2:1.0" ]; then
        slot=2
    elif [ "${DEVPATH}" = "/devices/platform/101c0000.ehci/usb1/1-1/1-1.3/1-1.3:1.0" -o "${DEVPATH}" = "/devices/platform/1e1c0000.xhci/usb1/1-2/1-2.3/1-2.3:1.0" ]; then
        slot=3
    elif [ "${DEVPATH}" = "/devices/platform/101c0000.ehci/usb1/1-1/1-1.4/1-1.4:1.0" ]; then
        slot=4
    elif [ "${DEVPATH}" = "/devices/platform/1e1c0000.xhci/usb2/2-1/2-1:1.0" ]; then
        M2=1
    fi
    if [ "${slot}" != "0" ]; then
        logger -t Hotplug Device insert slot $slot
        cd /sys/devices/platform/leds/leds/led-pcie${slot} && echo 1 >brightness
    fi
    if [ "${M2}" != "0" ]; then
        logger -t Hotplug Device insert M2
        cd /sys/devices/platform/leds/leds/led-M2 && echo 1 >brightness
    fi
fi

if [ "${ACTION}" = "remove" ]; then
    slot=0
    M2=0
    if [ "${DEVPATH}" = "/devices/platform/101c0000.ehci/usb1/1-1/1-1.1/1-1.1:1.0" -o "${DEVPATH}" = "/devices/platform/1e1c0000.xhci/usb1/1-2/1-2.1/1-2.1:1.0" ]; then
        slot=1
    elif [ "${DEVPATH}" = "/devices/platform/101c0000.ehci/usb1/1-1/1-1.2/1-1.2:1.0" -o "${DEVPATH}" = "/devices/platform/1e1c0000.xhci/usb1/1-2/1-2.2/1-2.2:1.0" ]; then
        slot=2
    elif [ "${DEVPATH}" = "/devices/platform/101c0000.ehci/usb1/1-1/1-1.3/1-1.3:1.0" -o "${DEVPATH}" = "/devices/platform/1e1c0000.xhci/usb1/1-2/1-2.3/1-2.3:1.0" ]; then
        slot=3
    elif [ "${DEVPATH}" = "/devices/platform/101c0000.ehci/usb1/1-1/1-1.4/1-1.4:1.0" ]; then
        slot=4
    elif [ "${DEVPATH}" = "/devices/platform/1e1c0000.xhci/usb2/2-1/2-1:1.0" ]; then
        M2=1
    fi
    if [ "${slot}" != "0" ]; then
        logger -t Hotplug Device remove slot $slot
        cd /sys/devices/platform/leds/leds/led-pcie${slot} && echo 0 >brightness
    fi
    if [ "${M2}" != "0" ]; then
        logger -t Hotplug Device remove M2
        cd /sys/devices/platform/leds/leds/led-M2 && echo 0 >brightness
    fi
fi
