#!/bin/sh
# Runs when the theme or dark and light mode changes. Starchaser passes the new mode as STARCHASER_THEME_MODE
# (dark or light) and the page's switch as STARCHASER_SETTING_WRITE_MODE (true or false; unset until first changed,
# when the page's default, on, applies).
set -eu
[ "${STARCHASER_SETTING_WRITE_MODE:-true}" = true ] || exit 0
case "${STARCHASER_THEME_MODE:-}" in
    dark|light) ;;
    *) echo "No dark or light mode was given, so the file was left as it was."; exit 0 ;;
esac
# A reader never sees half a file: write beside it, then rename over it.
tmp="$STARCHASER_SATELLITE_DATA/.mode.$$"
printf '%s\n' "$STARCHASER_THEME_MODE" > "$tmp"
mv -f "$tmp" "$STARCHASER_SATELLITE_DATA/mode"
echo "Saved $STARCHASER_THEME_MODE."
