#!/bin/sh
# Rikkiti night light — wlsunset driven by ~/.config/rikkiti/power.conf. Re-run to
# apply changes (replaces the previous instance). Modes: off | on | auto.
#   off  — no colour shift
#   on   — constant warm temperature (nightlight_temp)
#   auto — warm at night, neutral by day (by lat/long if set, else a fixed
#          07:00/19:00 schedule)
conf="$HOME/.config/rikkiti/power.conf"
val() { grep -E "^[[:space:]]*$1[[:space:]]*=" "$conf" 2>/dev/null | tail -1 | sed 's/^[^=]*=//; s/#.*//; s/[[:space:]]//g'; }

mode=$(val nightlight);       [ -z "$mode" ] && mode=off
temp=$(val nightlight_temp);  [ -z "$temp" ] && temp=4000
lat=$(val nightlight_lat)
long=$(val nightlight_long)
day=6500

pkill -x wlsunset 2>/dev/null
[ "$mode" = off ] && exit 0

if [ "$mode" = on ]; then
	# Constant warmth: clamp the "day" temperature down to the night one.
	exec wlsunset -T "$((temp + 100))" -t "$temp" -S 00:00 -s 23:59
fi

# auto
if [ -n "$lat" ] && [ -n "$long" ]; then
	exec wlsunset -T "$day" -t "$temp" -l "$lat" -L "$long"
fi
exec wlsunset -T "$day" -t "$temp" -S 07:00 -s 19:00
