#!/bin/sh
#############################################################
path="${0%/*}"
. "$path/inc/gwhw.inc"
. "$path/inc/wd.inc"

needRoot
wdI2COK

usage() {
	die 'gwhw wd status [count/s0/s1/s2/temp]'
}

OPT="${1}"
shift

OPT="$(echo "$OPT" | tr '[:upper:]' '[:lower:]')"
#############################################################

countRemain="$(wdReadWord 0x10)"
countMax="$(   wdReadWord 0x20)"
s0="$(wdReadWord 0x00)"
s1="$(wdReadWord 0x02)"
s2="$(wdReadWord 0x04)"
temp="$(awk "BEGIN {v=$(wdReadWord 0x30); if(and(v,0x8000)){v=v-0x10000}; print v/100}")"

case "${OPT}" in
	count) echo "$((${countRemain:-0}/2))/$((${countMax:-0}/2)) (${countRemain:-?}/${countMax:-?})"
	;;
	s0) echo "${s0:+${s0} $(wdStatusStr "${s0}")}"
	;;
	s1) echo "${s1:+${s1} $(wdStatusStr "${s1}")}"
	;;
	s2) echo "${s2:+${s2} $(wdStatusStr "${s2}")}"
	;;
	temp)  echo "${temp}"
	;;
	*)
		printf "%-6s: %s\n" "Count" "$((${countRemain:-0}/2))/$((${countMax:-0}/2)) (${countRemain:-?}/${countMax:-?})"
		printf "%-6s: %s\n" "S0" "${s0:-?} $(wdStatusStr "${s0}")"
		printf "%-6s: %s\n" "S1" "${s1:-?} $(wdStatusStr "${s1}")"
		printf "%-6s: %s\n" "S2" "${s2:-?} $(wdStatusStr "${s2}")"
		printf "%-6s: %s\n" "temp" "${temp}"
	;;
esac
