#!/bin/zsh
#####################################################################

use notify

DEPENDENCIES+=(xset)

#####################################################################

MAIN() {
	local SCWRYPTS_NOTIFICATION_ENGINES=(echo notify.desktop)
	case $1 in
		enable | disable | query | toggle ) ACTION="$1" ;;
		* )
			[ ! $1 ] && ACTION=toggle
			echo ${ACTION}
			;;
	esac

	[[ ${ACTION} =~ ^toggle$ ]] && {
		xset -q | grep -qi 'prefer blanking: *no' \
			&& ACTION='enable' || ACTION='disable'
	}

	[ ${ACTION} ] \
		|| notify.error "unknown screen blank action '$1'" \
		|| return 1

	case ${ACTION} in
		enable ) : \
			&& echo.status 'enabling screen blank' \
			&& xset +dpms \
			&& xset s blank \
			&& xset s on \
				&& notify.success 'enabled screen blank' \
				|| notify.error   'error enabling screen blank' \
				|| return 2
			;;

		disable ) : \
			&& echo.status 'disabling screen blank' \
			&& xset dpms 0 0 0 \
			&& xset s noblank \
			&& xset s off \
				&& notify.success 'disabled screen blank' \
				|| notify.error   'error disabling screen blank' \
				|| return 2
			;;

		query )
			xset -q | grep -qi 'prefer blanking: *no' \
				&& CURRENT_STATE='disabled' || CURRENT_STATE='enabled'

			[ ${CURRENT_STATE} ] \
				|| notify.error 'unable to determine current setting' \
				|| return 3

			notify.success "screen blank is currently $(utils.colors.print blue "${CURRENT_STATE}")"
			;;
	esac
}
