#!/bin/dash

# Copyright (C) 2023 Futurehome AS
# All rights reserved

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

ZIGBEE_RST_PIN=32
ZWAVE_RST_PIN=103
ZWAVE_PWR_PIN=79

set -e

if ! [ "$#" -eq 2 ] > /dev/null
then
  >&2 printf %s\\n "Error: usage: $0 gpio_pin_name 0|1"
  exit 1
fi

if [ "$2" != "0" ] && [ "$2" != "1" ]
then
  >&2 printf %s\\n "Error: invalid GPIO value: $2"
  exit 1
fi

# Make sure we're running as root (uid=0).
if [ "$(id -u)" != "0" ]
then
  >&2 printf %s\\n "Error: must run as root"
  exit 1
fi

case "$1" in
zigbee-rst)
  exec /usr/share/futurehome/fh-gpio/set-pin "$ZIGBEE_RST_PIN" "$2"
  ;;
zwave-rst)
  exec /usr/share/futurehome/fh-gpio/set-pin "$ZWAVE_RST_PIN" "$2"
  ;;
zwave-pwr)
  exec /usr/share/futurehome/fh-gpio/set-pin "$ZWAVE_PWR_PIN" "$2"
  ;;
*)
  >&2 printf %s\\n "Error: invalid GPIO pin: $1"
  exit 1
esac
