#!/bin/sh

# This script will be called by different parts of the DOMjudge system
# and allows the administrator to flexibly configure a way to get
# notified of important messages. By default this script calls 'beep'
# if available.

# Calling syntax: $0 <msgtype> [<description>]

MSGTYPE=$1
if [ -z "$MSGTYPE" ]; then
	echo "Error: missing required message type argument."
	exit 1
fi

shift
DESCRIPTION="$*"

# shellcheck disable=SC2230
BEEPCMD=$(which beep 2>/dev/null)

beep()
{
	[ -x "$BEEPCMD" ] && "$BEEPCMD" "$@"
}

case "$MSGTYPE" in
	error) # internal system error
		beep -l 1000 -d 500 -f 800 -n -l 1000 -d 500 -f 1000 -n \
		     -l 1000 -d 500 -f 800 -n -l 1000 -d 500 -f 1000 -n \
		     -l 1000 -d 500 -f 800 -n -l 1000 -d 500 -f 1000
		;;

	warning) # important system warning
		beep -l 1000 -d 500 -f 300 -n -l 1000 -d 500 -f 200 -n \
		     -l 1000 -d 500 -f 300 -n -l 1000 -d 500 -f 200
		;;

	usererr) # user error
		beep -l 1000 -d 500 -f 800 -n -l 1000 -d 500 -f 1000 -n \
		     -l 1000 -d 500 -f 800 -n -l 1000 -d 500 -f 1000 -n \
		     -l 1000 -d 500 -f 800 -n -l 1000 -d 500 -f 1000
		;;

	submit) # team submission received
#		beep -f 400 -l 100 -n -f 400 -l 70
		;;

	accept) # submission judged and accepted
		beep -f 400 -l 100 -n -f 500 -l 70
		;;

	reject) # submission judged and rejected
#		beep -f 400 -l 100 -n -f 350 -l 70
		;;

	*)
		echo "Error: unknown message type '$MSGTYPE' specified."
		exit 1
		;;
esac
