#!/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 type_var=${hostchan}_type type=${!type_var} if [[ $type == switch ]]; then switch=true fi ;; esac if ! $switch && hidusb-relay-cmd state | grep -F R$CHAN=ON; then cat >&2 <&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 on $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 off $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 on $chan v sleep 4 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 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"