#!/usr/bin/env bash

# Path to your Razer HID battery info
#BASE="/sys/bus/hid/drivers/razermouse/0003:1532:00AB.000A"
#BASE_MOUSE="/sys/devices/pci0000:00/0000:00:01.2/0000:02:00.0/usb1/1-8/1-8.1/1-8.1.2/1-8.1.2:1.0/0003:1532:00AB.0009"
BASE_MOUSE=''

# autodetect the razermouse sysfs folder that contains charge_level
for d in /sys/bus/hid/drivers/razermouse/*; do
  [[ -f $d/charge_level ]] && { BASE_MOUSE=$d; break; }
done

# Read raw level and charging status
raw=$(cat "$BASE_MOUSE/charge_level")
status=$(cat "$BASE_MOUSE/charge_status")  # 1=charging, 0=not charging
type=$(cat "$BASE_MOUSE/device_type")

# Compute %
percent=$(( raw * 100 / 255 ))

tooltip="$type"
icon="󰍽"

# Choose icon + class
if [[ $status -eq 1 ]]; then
    icon="󰍽⚡"
    class="charging"
    text="$percent%"
else
    class="not_charging"
    text="$percent%"
fi

jq -nc \
    --arg text    "<span color=\"#4F84CC\">$icon</span>  <span color=\"#CAD3E8\">$text</span>" \
    --arg class   "$class" \
    --arg tooltip "$tooltip" \
    '{text: $text, class: $class, tooltip: $tooltip}'

