#!/bin/bash

PORT_NUM=$1

# export gpio if needed
if [ ! -e /sys/class/gpio/gpio${PORT_NUM} ]; then
    echo ${PORT_NUM} > /sys/class/gpio/export
fi

# set gpio as in if needed
direction=$(cat /sys/class/gpio/gpio${PORT_NUM}/direction)
if [ "$direction" != "in" ]; then
    echo "GPIO ${PORT_NUM} is not in mode, but $direction mode"
    echo "Set GPIO ${PORT_NUM} as in mode..."
    echo in > /sys/class/gpio/gpio${PORT_NUM}/direction
    echo "GPIO ${PORT_NUM} has been set as in mode."
else
    echo "GPIO ${PORT_NUM} is in mode now."
fi

cat /sys/class/gpio/gpio${PORT_NUM}/value