#!/bin/sh

# Set brightness via xbrightness when redshift status changes

# Set brightness values for each status.
# Range from 0 to 100 is valid
brightness_day="100"
brightness_transition="60"
brightness_night="20"
max=$(cat /sys/class/backlight/intel_backlight/max_brightness)
actual=$(cat /sys/class/backlight/intel_backlight/actual_brightness)

case $1 in
	period-changed)
		case $3 in
			night)
				for (( c=$actual; c > $((( $brightness_night * $max / 100))); c-=1 ))
				do
					echo $c > /sys/class/backlight/intel_backlight/brightness
					sleep .2
				done
				;;
			transition)
				old="$2"
				if [[ "$old" == "none" ]]
				then
					if [[ $actual -lt $(( $brightness_transition * $max / 100 )) ]]
					then
						old="night"
					else
						old="daytime"
					fi
				fi
				case $old in
					night)
						for (( c=$actual; c < $((( $brightness_transition * $max / 100))); c+=1 ))
						do
							echo $c > /sys/class/backlight/intel_backlight/brightness
							sleep .2
						done
						;;
					daytime)
						for (( c=$actual; c > $((( $brightness_transition * $max / 100))); c-=1 ))
						do
							echo $c > /sys/class/backlight/intel_backlight/brightness
							sleep .2
						done
						;;
				esac
				;;
			daytime)
				for (( c=$actual; c < $((( $brightness_day * $max / 100))); c+=1 ))
				do
					echo $c > /sys/class/backlight/intel_backlight/brightness
					sleep .2
				done
				echo $max > /sys/class/backlight/intel_backlight/brightness
				;;
		esac
		;;
esac
