}
-if (( $# < 2 )); then
- usage
-fi
switch=false
if [[ $1 == --switch ]]; then
shift
fi
-read action chan board_id <<<"$@"
+if (( $# < 2 )); then
+ usage
+fi
+
+read action hostchan board_id <<<"$@"
if [[ -e /etc/libremanage.conf ]]; then
source /etc/libremanage.conf
# Use config vars when appropriate. We know the arg is a HOSTNAME if it
# doesn't start with a number.
-if [[ $chan != [0-9]* ]]; then
- if [[ ! $board_id ]]; then
- board_id_var=${chan}_board_id
- board_id=${!board_id_var}
- fi
- chan_var=${chan}_channel
- chan=${!chan_var}
+case $hostchan in
+ [0-9]*)
+ chan=$hostchan
+ ;;
+ *)
- if [[ ! $chan ]]; then
- echo "error: channel not found in /etc/libremanage.conf" >&2
- exit 1
- fi
-fi
+ # allow board_id to be empty if its unset
+ if [[ ! $board_id ]]; then
+ board_id_var=${hostchan}_board_id
+ board_id=${!board_id_var}
+ fi
+
+ chan_var=${hostchan}_channel
+ chan=${!chan_var}
+ if [[ ! $chan ]]; then
+ echo "error: channel not set on cli and not found in /etc/libremanage.conf" >&2
+ exit 1
+ fi
+
+ type_var=${hostchan}_type
+ type=${!type_var}
+ if [[ $type == switch ]]; then
+ switch=true
+ fi
+ ;;
+esac
-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.
+if ! $switch && hidusb-relay-cmd state | grep -F R$CHAN=ON; then
+ cat >&2 <<EOF
+$0: ERROR: Output from hidusb-relay-cmd state shows an ON relay, as if
+the power button is stuck in the pressed state. This could mean another
+command instance is running, or it got stuck on due to an uncompleted
+run. Exiting now so you can investigate. To manually switch it back to
+OFF, run the following command, or unplug the relay from usb power.
+
+hidusb-relay-cmd $board_id_arg off $chan
+
+EOF
+ exit 1
fi