#!/bin/sh

# curl -s https://www.homedepot.com/p/HDX-Black-5-Tier-Plastic-Garage-Storage-Shelving-Unit-36-in-W-x-74-in-H-x-18-in-D-241592/306331734 | grep -v href | md5sum | awk '{print $1}'

verbose=""
size=0

while [ -n "$1" ]; do
  case "$1" in
    -v) verbose="true"; shift 1;;
    -u) url="$2"; shift 2;;
    -s) size="$2"; shift 2;;
    *) shift 1;;
  esac
done

# curl -skL "https://www.wyze.com/products/wyze-cam-v3" -w '%{size_download}' -o /dev/null
agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0"
[ -n "$verbose" ] && echo "DEBUG: curl --connect-timeout 10 -A \"$agent\" -skL \"$url\" -w \"%{size_download}\" -o /dev/null"
checkSize=$(curl --connect-timeout 10 -A "$agent" -skL "$url" -w "%{size_download}" -o /dev/null)
status=$?
if [ "$status" != "0" ]; then
  sleep 2
  checkSize=$(curl --connect-timeout 10 -A "$agent" -skL "$url" -w "%{size_download}" -o /dev/null)
  status=$?
fi

case $status in
  0) ;;
  3) echo "UNKNOWN: malformed URL $url" && exit 3;;
  6) echo "WARNING: could not resolve host for $url" && exit 3;;
  7) echo "WARNING: could not connect to host $url" && exit 1;;
  28) echo "WARNING: curl timeout" && exit 1;;
  35) echo "WARNING: TLS/SSL handshake failure - $output" && exit 1;;
  47) echo "WARNING: too many redirects" && exit 1;;
  52) echo "WARNING: $url returned no data" && exit 1;;
esac

if [ "$size" = "$checkSize" ]; then
  echo "OK; received $size bytes|bytes=${checkSize}B"
  exit 0
else
  echo "CRITICAL; $checkSize is not expected value of $size bytes|bytes=${checkSize}B"
  exit 2
fi
