#!/bin/bash

showHelp=0

source /etc/ioch-channels.conf
valAction=""
valChannel=""
getReturn="int"
setValue=""
pulLen="1"
pulSleep="0"
pulCount="1"
pulInvert="0"

while [ "$1" != "" ]; do
	case "$1" in
	  -h | --help | --usage)
		showHelp="1"
		;;
	  --no-sudo )
		noSudo="1"
		;;
	  set)
		valAction="$1"
		shift
		valChannel="$1"
		shift
		setValue="$1"
		;;
	  get | toggle)
		valAction="$1"
		shift
		valChannel="$1"
		;;
	  pulse)
		valAction="$1"
		shift
		valChannel="$1"
		shift
		if [ "$1" != "" ]; then
			pulLen="$1"
			shift
			if [ "$1" != "" ]; then
				pulSleep="$1"
			fi
		fi
		;;
	  -r | --return)
		shift
		getReturn="$1"
		;;
	  -c | --count)
		shift
		pulCount="$1"
		;;
	  -i | --invert)
		pulInvert="1"
		;;
	esac
	shift
done

if [ "$showHelp" == "1" ]; then
	echo "Syntax: ioch COMMAND [OPTIONS [<values>]] <channel> [<values>]"
	echo " "
	echo "Usage:"
	echo "ioch get [-r|--return int|bool|str] <channel>"
	echo "Reads input channel state."
	echo "  -r <val>, --return <val>  Returned value format - available options: int, "
	echo "                            bool, str. Default: str"
	echo " "
	echo "ioch set <channel> <value>"
	echo "Sets output channel state to <value>. Available values: on, off, 1, 0"
	echo " "
	echo "ioch toggle <channel>"
	echo "Switch output channel on/off state."
	echo " "
	echo "ioch pulse [<length> [<pause>]] [-c|--count <n>] [-i|--invert] <channel> [<length> [<pause>]]"
	echo "Toggles output channel state to on for <length> seconds and then back to off."
	echo "Holds command execution for <pause> seconds to prevent immediately state change."
	echo "Defaults: <length>=1, <pause>=0"
	echo "  -c <n>, --count <n>  Repeats channel pulsing <n> times."
	echo "  -i, --invert         Inverts on/off states while pulsing."
	echo " "
	echo "General options:"
	echo "  -h, --help, --usage  This help."
	echo "  --no-sudo            When root privileges are necessary, application forces"
	echo "                       sudo command. If you want to disable this feature, use"
	echo "                       this prefix."
	echo " "
	echo "(c) 2015 LUnet Studio, www.studio.LUnet.sk"
	exit
fi


sc=""
if [ "$USER" != "root" ] && [ "$noSudo" != "1" ]; then
	sc="sudo "
	sudo echo "" > /dev/null
fi

case "$valAction" in
  get)
	arrReturn=(0 1)
	if [ "$getReturn" == "str" ]; then arrReturn=("off" "on"); fi
	if [ "$getReturn" == "bool" ]; then arrReturn=("false" "true"); fi
	valReturn=`gpio -g read ${gpioPins[$valChannel]}`
	echo "${arrReturn[$valReturn]}"
	;;
  set)
	if [ "$setValue" == "on" ]; then setValue="1"; fi
	if [ "$setValue" == "off" ]; then setValue="0"; fi
	gpio -g write ${gpioPins[$valChannel]} $setValue
	;;
  toggle)
	valReturn=`gpio -g read ${gpioPins[$valChannel]}`
	if [ "$valReturn" == "1" ]; then setValue="0"; fi
	if [ "$valReturn" == "0" ]; then setValue="1"; fi
	gpio -g write ${gpioPins[$valChannel]} $setValue
	;;
  pulse)
	arrPulse=(0 1)
	if [ "$pulInvert" == "1" ]; then arrPulse=(1 0); fi
	pulI=0
	while [ $pulI -lt $pulCount ]; do
		gpio -g write ${gpioPins[$valChannel]} ${arrPulse[1]}
		sleep $pulLen
		gpio -g write ${gpioPins[$valChannel]} ${arrPulse[0]}
		sleep $pulSleep
		let pulI++
	done
	;;
esac
