#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# HP-15C-CE_hid_rule
# Create and activate a udev rule for the HP 15C CE device (VID 03F0, PID 1341)
# Allows users to access this device without root.
# -----------------------------------------------------------------------------

RULE_FILE="/etc/udev/rules.d/71-hid-hp-03f0-1341.rules"
VENDOR_ID="03f0"
PRODUCT_ID="1341"
GROUP_NAME="plugdev"
TAG_NAME="uaccess"
UDEV_TYPE=""

# 1.a Check for arguments
while [[ "$#" -gt 0 ]]; do
  case $1 in
      -group) UDEV_TYPE="group" ;;
      -tag) UDEV_TYPE="tag" ;;
      *) echo "Usage: HP-15C-CE_hid_rule [-group|-tag]"; exit 1 ;;
  esac
  shift
done

# 1.b Check for root privileges
if [ "$EUID" -ne 0 ]; then
  echo "Please run this script as root (use sudo)."
  exit 1
fi

echo "-----------------------------------------------------------"
echo " Installing udev rule for HP 15C CE device ($VENDOR_ID:$PRODUCT_ID)"
echo "-----------------------------------------------------------"
echo ""

# 2. If user doesnt force udev type, determin it
# On systemd systems use TAG=$TAG_NAME and group=$GROUP_NAME elsewhere
if [ -z "$UDEV_TYPE" ]; then
  if [ -d /run/systemd/system ]; then
    UDEV_TYPE="tag"
  else
    UDEV_TYPE="group"
  fi
fi

# 3. Use group plugdev
if [ "$UDEV_TYPE" = "group" ]; then
# 3.a Ensure the plugdev group exists
  if ! getent group "$GROUP_NAME" > /dev/null; then
    echo "Creating group '$GROUP_NAME'"
    groupadd "$GROUP_NAME"
  fi
# 3.b Create the udev rule
  echo "Creating udev rule file at $RULE_FILE"
  cat <<EOF > "$RULE_FILE"
SUBSYSTEM=="usb", ATTR{idVendor}=="$VENDOR_ID", ATTR{idProduct}=="$PRODUCT_ID", MODE="0660", GROUP="$GROUP_NAME"
SUBSYSTEM=="hidraw", KERNEL=="hidraw*", ATTRS{idVendor}=="$VENDOR_ID", ATTRS{idProduct}=="$PRODUCT_ID", MODE="0660", GROUP="$GROUP_NAME"
EOF

# 3.c Add current (non-root) user to the plugdev group
  if [ -n "$SUDO_USER" ]; then
    user="$SUDO_USER"
  else
    user="$USER"
  fi
  echo "Adding user '$user' to group '$GROUP_NAME'"
  usermod -aG "$GROUP_NAME" "$user"

# 4. Use TAG $TAG_NAME
elif [ "$UDEV_TYPE" = "tag" ]; then
  cat <<EOF > "$RULE_FILE"
SUBSYSTEM=="usb", ATTR{idVendor}=="$VENDOR_ID", ATTR{idProduct}=="$PRODUCT_ID", MODE="0660", TAG+="$TAG_NAME"
SUBSYSTEM=="hidraw", KERNEL=="hidraw*", ATTRS{idVendor}=="$VENDOR_ID", ATTRS{idProduct}=="$PRODUCT_ID", MODE="0660", TAG+="$TAG_NAME"
EOF
else
  echo "usage: HP-15C-CE_hid_rule [-group|-tag]"; exit 1 ;
fi

# 5. Reload and apply udev rules
echo "Reloading udev rules"
udevadm control --reload-rules
udevadm trigger

echo ""
echo "-----------------------------------------------------------"
echo "udev rule installed successfully."
echo "  File  : $RULE_FILE"
echo "  Device: $VENDOR_ID:$PRODUCT_ID"
if [ "$UDEV_TYPE" = "group" ]; then
  echo "  Group : $GROUP_NAME"
  echo "-----------------------------------------------------------"
  echo "==> Please log out and back in or run 'newgrp - $GROUP_NAME'"
else
  echo "  Tag   : $TAG_NAME"
fi
echo "-----------------------------------------------------------"
