#!/usr/bin/env bash
# Drive Pi HS OUTs idle low: BCM10 OIS_OUT, BCM11 M-S_OUT.
# Never high-Z — TXS A-side pull-up would hold DE on (Rev E+).
# Older POG-COM-PI-HAT PCBs have no connection to these pins (safe no-op).
#
#   bash ~/EtherGold/sync/hs_out_idle_low.sh
#   sudo bash ~/EtherGold/sync/hs_out_idle_low.sh
#
# Does not reboot. Does not inject. Does not change UART mux jumpers.

if grep -q $'\r' "${BASH_SOURCE[0]}" 2>/dev/null; then
  sed -i 's/\r$//' "${BASH_SOURCE[0]}"
  exec bash "${BASH_SOURCE[0]}" "$@"
fi

set -euo pipefail

PINCTRL="$(command -v pinctrl || true)"
if [[ -z "${PINCTRL}" ]]; then
  echo "hs_out_idle_low: pinctrl not found — skip (not a Pi?)"
  exit 0
fi

_run() {
  if [[ "$(id -u)" -eq 0 ]]; then
    "${PINCTRL}" "$@"
  else
    sudo -n "${PINCTRL}" "$@"
  fi
}

_run set 10 op dl
_run set 11 op dl
echo "hs_out_idle_low: BCM10 OIS_OUT + BCM11 M-S_OUT op dl"
