#!/bin/dash

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

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

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
  exec sudo -n -- "$0" "$@" || true

  >&2 printf %s\\n "Error: must run as root"
  exit 1
fi

hub_model=$(head -n1 /usr/local/share/futurehome/smarthub.model)
case "$hub_model" in
cube-2v0-eu)
  exec /usr/share/futurehome/fh-gpio/hubs/cube-2v0-eu "$@"
  ;;
*)
  >&2 printf %s\\n "Error: hub model not supported: $hub_model"
  exit 1
esac