#!/bin/sh -e

croak() {
	logger -s -t${0##*/} -p err -- $*
	exit 1
}

usage() {
	logger -s -t${0##*/} -p err -- "Usage: $0 ir_filter_off [channel]"
	logger -s -t${0##*/} -p err -- "Usage: $0 ir_filter_on [channel]"
	exit 1
}

nbr_of_leds=$(gdbus call --system --dest=com.axis.LightController \
		--object-path=/com/axis/LightController \
		--method=com.axis.LightController.Common.GetNbrOfLEDs) ||
			croak "Failed to get GetNbrOfLEDs"
nbr_of_leds=${nbr_of_leds#*\(}
nbr_of_leds=${nbr_of_leds%\,*}

if [ $# -gt 1 ]; then
	CHANNEL=$(($2 - 1))
	#If there is only one led object but multiple sources this check is needed
	[ $nbr_of_leds -ne 1 ] || CHANNEL=0
else
	CHANNEL=0
fi

STREAM="0"

GDBUS_CALL="gdbus call --system"
GDBUS_IMG="$GDBUS_CALL --dest=com.axis.ImageControl \
		--object-path=/com/axis/ImageControl/$CHANNEL/$STREAM"
GDBUS_IMG_GET="$GDBUS_IMG \
		--method=org.freedesktop.DBus.Properties.Get \
		com.axis.ImageControl"
GDBUS_LIGHT="$GDBUS_CALL --dest=com.axis.LightController \
		--object-path=/com/axis/LightController/led/$CHANNEL"
GDBUS_LIGHT_GET="$GDBUS_LIGHT \
		--method=org.freedesktop.DBus.Properties.Get \
		com.axis.LightController.Common"
GDBUS_LIGHT_SETLIGHTSTATUS="$GDBUS_LIGHT \
		--method=com.axis.LightController.Common.ActivateLight"
GDBUS_LIGHT_GETLIGHTSTATUS="$GDBUS_LIGHT \
		--method=com.axis.LightController.Common.GetLightStatus"
GDBUS_IMG_GET_BY_CHANNEL="$GDBUS_CALL \
		--dest=com.axis.ImageControl \
		--method=org.freedesktop.DBus.Properties.Get \
		com.axis.ImageControl \
		--object-path=/com/axis/ImageControl"
GDBUS_GET_NBR_OF_SOURCES="$GDBUS_CALL --dest=com.axis.CaptureMode1 \
		--object-path=/com/axis/CaptureMode1 \
		--method=com.axis.CaptureMode1.GetChannelObjects"

turn_on_irlight() {
	local day use_day_night sync

	# When night and sync is active, set irlight on
	use_day_night=$($GDBUS_LIGHT_GET UseDayNight) ||
		croak "Failed to get use day night"
	sync=$($GDBUS_LIGHT_GET SyncDayNight) ||
		croak "Failed to get SyncDayNight"

	use_day_night=${use_day_night#*\<}
	use_day_night=${use_day_night%\>*}
	sync=${sync#*\<}
	sync=${sync%\>*}

	case $use_day_night in
		true)
			day=$($GDBUS_IMG_GET IsAboveIlluminanceLevel) ||
				croak "Failed to get IsAboveIlluminanceLevel"
			day=${day#*\<}
			day=${day%\>*}
			case $day in
				0)
					case $sync in
						true)
							$GDBUS_LIGHT_SETLIGHTSTATUS true >/dev/null ||
							croak "Failed to turn on light"
							;
					esac
					;
			esac
			;
		false)
			case $sync in
				true)
					$GDBUS_LIGHT_SETLIGHTSTATUS true >/dev/null ||
					croak "Failed to turn on light"
					;
			esac
			;
	esac
}

turn_off_irlight() {
	local s m i=0 IFS isabove isabovecounter nbrofsources=-1 ircut ircutcounter=-1

	if eval $GDBUS_IMG_GET IrCutFilter >/dev/null 2>&; then
		IMAGE_CONTROL_SUPPORT=true
	else
		IMAGE_CONTROL_SUPPORT=false
	fi
	if [ $IMAGE_CONTROL_SUPPORT = true ]; then
		isabovecounter=0
		s=$(eval $GDBUS_GET_NBR_OF_SOURCES 2>&1) ||
			croak "Failed to get number of sources '$s'"

		IFS="'"
		for m in $s; do
			case $m in
				/com/axis/CaptureMode1/*)
					nbrofsources=$(($nbrofsources + 1))
					;
			esac
		done
		if [ $nbrofsources -le 1 ]; then
			eval $GDBUS_LIGHT_SETLIGHTSTATUS false >/dev/null ||
				croak "Failed to turn off light"
			return 0
		fi

		while [ $i -le $nbrofsources ]; do
			isabove=$(eval $GDBUS_IMG_GET_BY_CHANNEL/$i/$STREAM IsAboveIlluminanceLevel 2>&1) ||
				croak "Failed to get IsAboveIlluminanceLevel"
			isabove=${isabove#*\<}
			isabove=${isabove%\>*}
			[ $isabove -eq 1 ] || isabovecounter=$(($isabovecounter + 1))
			ircut=$(eval $GDBUS_IMG_GET_BY_CHANNEL/$i/$STREAM IrCutFilter 2>&1) ||
				croak "Failed to get IrCutFilter"
			ircut=${ircut#*\'}
			ircut=${ircut%\'*}
			case $ircut in
				yes)
					ircutcounter=$(($ircutcounter + 1))
					;
			esac
			i=$(($i + 1))
		done
		if [ $isabovecounter -eq 0 ] || [ $ircutcounter -eq $nbrofsources ]; then
			eval $GDBUS_LIGHT_SETLIGHTSTATUS false >/dev/null ||
				croak "Failed to turn off light"
		else
			echo "A channel is above illuminance level"
		fi
	else
		eval $GDBUS_LIGHT_SETLIGHTSTATUS false >/dev/null ||
			croak "Failed to turn off light"
	fi
}

card_detected=$($GDBUS_LIGHT_GET CardDetected) ||
	croak "Failed to get CardDetected"
card_detected=${card_detected#*\<}
card_detected=${card_detected%\>*}

case $card_detected in
	false)
		#If no card detected do nothing
		exit 0
		;
esac

correct_poe=$($GDBUS_LIGHT_GET CorrectPoe) ||
	croak "Failed to get CorrectPoe"
correct_poe=${correct_poe#*\<}
correct_poe=${correct_poe%\>*}

case $correct_poe in
	false)
		#If wrong poe class do nothing
		exit 0
		;
esac

case $1 in
	ir_filter_off)
		turn_on_irlight
		;
	ir_filter_on)
		turn_off_irlight
		;
	*)
		usage
		;
esac