#!/bin/bash

################################################################################
#
# gnome-toggle-night-light
# Toggles GNOME's Night Light feature
#
# Requires notify-send
#
################################################################################

################################################################################
# User configuration
################################################################################

title="Night Light"
body_enabled="Night light has been enabled."
body_disabled="Night light has been disabled."
icon_enabled="night-light"
icon_disabled="night-light-disabled"
key="org.gnome.settings-daemon.plugins.color night-light-enabled"

################################################################################
# GSettings toggle code
################################################################################

# Get original value of key
value=$(gsettings get $key)

# Determine a temp file to store the notification ID in
notif_id_file_name=/tmp/tmp.$(echo $key | tr -d ' ').nid
{ prev_notif_id=$(<$notif_id_file_name); } 2> /dev/null

# If we have an ID from a previous notification,
# set the replace flag so that we override it
replace_flag=""
case $prev_notif_id in
    ''|*[!0-9]*) ;;
    *) replace_flag="--replace-id=$prev_notif_id" ;;
esac

# Toggle the key and send the notification
if [[ $value == "true" ]]; then
    gsettings set $key false
    notify-send "$title Disabled" "$body_disabled" --app-name "$title" --icon "$icon_disabled" $replace_flag --transient --expire-time=0 --print-id > "$notif_id_file_name"
else
    gsettings set $key true
    notify-send "$title Enabled" "$body_enabled" --app-name "$title" --icon "$icon_enabled" $replace_flag --transient --expire-time=0 --print-id > "$notif_id_file_name"
fi