#!/bin/bash trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR set -eE -o pipefail # verbose command v() { printf "$0 running: %s\n" "$*" "$@" } usage() { cat <&2 exit 1 fi fi if hidusb-relay-cmd state | grep ON; then printf "%s\n" "WARNING: output from hidusb-relay-cmd state shows an ON relay. this could mean another command instance is running, or it got stuck on due to an uncompleted command." >&2 # TODO: exit in this case and print out the command to turn it off. fi case $(hidusb-relay-cmd state|wc -l) in 0) echo "error: got 0 lines from running hidusb-relay-cmd state" >&2 exit 1 ;; 1) : ;; *) if [[ ! $board_id ]]; then echo "error: more than 1 relay device, so passing its id is required" >&2 exit 1 fi ;; esac if [[ $board_id ]]; then # leave this as an empty var if its not passed board_id_arg="id=$board_id" fi # ignore errors and continue on if a command fails from here set +eE +o pipefail # ignore hup so we complete even if there is a connection problem, and # force anyone to kill -9 so we might complete in more cases, for # example if a reboot is happening trap '' HUP INT QUIT TERM echo "$0: doing $action. shell commands will be printed to the terminal." case $action in poweroff) if $switch; then v hidusb-relay-cmd $board_id_arg off $chan else v hidusb-relay-cmd $board_id_arg on $chan v sleep 6 v hidusb-relay-cmd $board_id_arg off $chan fi ;; poweron) if $switch; then v hidusb-relay-cmd $board_id_arg on $chan else v hidusb-relay-cmd $board_id_arg on $chan v sleep 1 v hidusb-relay-cmd $board_id_arg off $chan fi ;; reboot) if $switch; then v hidusb-relay-cmd $board_id_arg off $chan v sleep 4 v hidusb-relay-cmd $board_id_arg on $chan else v hidusb-relay-cmd $board_id_arg on $chan v sleep 6 v hidusb-relay-cmd $board_id_arg off $chan v sleep 1 v hidusb-relay-cmd $board_id_arg on $chan v sleep 1 v hidusb-relay-cmd $board_id_arg off $chan fi ;; *) echo "error: action arg not supported" >&2 exit 1 esac echo "$0: script ended. exiting"