#!/bin/sh

##############################################################################
#
# GPIO teardown script for SvxLink server. Run this script to undo the GPIO
# configuration done by the svxlink_gpio_up script.
#
##############################################################################

GPIO_CONF="@SVX_SYSCONF_INSTALL_DIR@/gpio.conf"
[ -r "$GPIO_CONF" ] && . "$GPIO_CONF"

# Unset GPIO IN/OUT pins, if used
for pin in $GPIO_IN_HIGH $GPIO_IN_LOW $GPIO_OUT_HIGH $GPIO_OUT_LOW; do
  if [ -e $GPIO_PATH/$pin ]; then
    # Extract pinnumber from the full pin name (e.g. gpio4_pd0 -> 4)
    pinnum=$(echo "$pin" | sed 's/^[^0-9]*\([0-9][0-9]*\).*$/\1/')
    echo $pinnum > $GPIO_PATH/unexport
  fi
done
