#!/bin/bash
# RG DS: switch 5V VBUS on the OTG-C port. VBUS is off by default because while
# it is on the rk817 ignores a charger on the DC port and the battery drains.
#   rgds-otg            show the current state
#   rgds-otg on|off     power bus-powered devices on the OTG port / charge again
#   rgds-otg toggle
state=$(ls /sys/bus/platform/drivers/reg-userspace-consumer/*/state 2>/dev/null | head -1)
[[ -n "$state" ]] || { echo "rgds-otg: OTG VBUS switch not found" >&2; exit 1; }
cur=$(cat "$state")
case "$1" in
	"")     echo "OTG VBUS: $cur"; exit 0 ;;
	on)     new=enabled ;;
	off)    new=disabled ;;
	toggle) [[ "$cur" == enabled ]] && new=disabled || new=enabled ;;
	*)      echo "usage: rgds-otg [on|off|toggle]" >&2; exit 2 ;;
esac
echo "$new" > "$state" || exit 1
echo "OTG VBUS: $(cat "$state")"
