#!/bin/bash

# Checks if the monitor is on.

CODE=0

if [[ "$#" -gt "0" ]]; then
	cat <<EOF
MOnitor ON-test
Checks if the monitor is on.
Usage: $0
It takes the liberty of assuming that as long as you can read it,
 the monitor is on. However, the exit code should be accurate.
EOF
	CODE=0
else
	XRANDR="`which xrandr`"
	if [[ ! -x "$XRANDR" ]]; then
		echo "XRandr not found, but your monitor is assumed on anyway."
		CODE=127
	fi

	$XRANDR 2>&1 | grep \ connected 2>&1 >/dev/null

	CONNECTED=$?

	if [[ "$CONNECTED" -eq "0" ]]; then
		echo "Your monitor is on."
		CODE=0
	else
		echo "Your monitor is assumed on."
		CODE=1
	fi
fi

(( MOON = RANDOM % 10 ))
if [[ "$MOON" -eq "0" ]]; then
	echo "(_|_)"
fi

exit $CODE
