diff --git a/sunset-alarm b/sunset-alarm index 45a1c03..617cc31 100755 --- a/sunset-alarm +++ b/sunset-alarm @@ -1,21 +1,20 @@ #!/bin/sh # sunset-alarm -- Alert before or after sunset. -# Displays a persistent alert when current time is within -# (sunset minus specified minutes) of midnight. +# Displays an alert when current time is within (sunset - specified minutes). ############################################################################### # Defaults ############################################################################### -declare LATLONG="" SUNSET_TIME="" MINUTES="" LAST_DATE="" -typeset -i SUNSET_EPOCH=0 +declare LATLONG="" SUNSET_TIME="" MINUTES="" LAST_DATE="" PERSIST="" NOTIFIED="" +typeset -i SUNSET_EPOCH=0 CURRENT_TIME=0 ############################################################################### # Usage ############################################################################### usage() { cat <&2; usage ;; @@ -141,13 +143,13 @@ show_alert() { TITLE="Sunset Alarm ($ALERT)" MESSAGE="It's ${MINUTES} minute${PLURAL} to $SUNSET." - persistent="-t 0 --wait" + keep_alive="-t 0 --wait" icon="-i weather-clear -u critical" if command -v fyi >/dev/null 2>&1; then - fyi $icon $persistent "$TITLE" "$MESSAGE" + fyi $icon $keep_alive "$TITLE" "$MESSAGE" elif command -v notify-send >/dev/null 2>&1; then - notify-send $icon $persistent "$TITLE" "$MESSAGE" + notify-send $icon $keep_alive "$TITLE" "$MESSAGE" elif command -v wall >/dev/null 2>&1; then echo "${TITLE}: ${MESSAGE}" | wall else @@ -157,6 +159,18 @@ show_alert() { fi } +############################################################################### +# Sleep until a certain amount of time +############################################################################### +rest() { + if [[ "$CURRENT_TIME" -ge 0 ]]; then + TARGET="$1" + sleep_seconds=$(( TARGET - CURRENT_TIME )) + echo "Sleeping ${sleep_seconds}s until $(date -d @$TARGET +%H:%M:%S)." + sleep "$sleep_seconds" + fi +} + ############################################################################### # Main loop ############################################################################### @@ -208,19 +222,31 @@ while true; do # Alert while current time is within the window (from window start until midnight). if [[ "$SUNSET_EPOCH" -ge 0 \ + && ( -z "$NOTIFIED" || -n "$PERSIST" ) \ && "$CURRENT_TIME" -ge "$ALERT_WINDOW_START" \ && "$CURRENT_TIME" -lt "$MIDNIGHT" ]] then echo "Alert time has been reached, show message." show_alert + NOTIFIED="true" + echo "Alert has been dismissed, sleeping for 5 minutes." sleep 290 echo "Waking back up from notification dismissal." + elif [[ "$SUNSET_EPOCH" == 0 ]]; then + echo "ERROR: Sunset time has not been calculated yet, cannot alert." >&2 + elif [[ -n "$NOTIFIED" && -z "$PERSIST" ]]; then + echo "Already notified for today ($TODAY)." + rest $MIDNIGHT + else + echo "Not time to notify yet." + rest $ALERT_WINDOW_START fi - # Reset when we've passed midnight so we fetch new time tomorrow. + # Reset when we've passed midnight so we fetch the new time for the day. if [[ "$CURRENT_TIME" -ge "$MIDNIGHT" ]]; then LAST_DATE="" + NOTIFIED="" fi sleep 10