#!/usr/bin/env dash WORK_TIME=1200 PAUSE_TIME=300 timer_state_pid=0 timer_state_state="inactive" timer_state_time="00:00" timer_state_progress=100000 sub_timer_pid=0 start_timer() { timer_type=$1 duration=0 timer_state_state="$timer_type" # Update done later if [ "$timer_type" = "work" ]; then duration=$WORK_TIME timer_type="pause" elif [ "$timer_type" = "pause" ]; then duration=$PAUSE_TIME timer_type="work" else echo "Invalid argument" exit 1 fi i=$duration while [ "$i" -ge 0 ] ; do timer_state_progress=$((i * 100000 / duration)) timer_state_time="$(date -u -d @$i +'%M:%S')" update_timer_state sleep 1 i=$(( i - 1 )) done # Notify user if [ "$timer_type" = "pause" ]; then notify-send -c grind-timer -i ~/Pictures/Icons/moai.jpg "Work period over" "You can take a little nap now." elif [ "$timer_type" = "work" ]; then notify-send -c grind-timer -i ~/Pictures/Icons/moai.jpg "Pause period over" "Back to grinding we go." fi start_timer "$timer_type" } update_timer_state() { echo "{\"pid\": $timer_state_pid, \"state\": \"$timer_state_state\", \"time\": \"$timer_state_time\", \"progress\": $timer_state_progress}" } handle_signal() { # If a timer is running, kill it if [ $sub_timer_pid -ne 0 ]; then kill -9 $sub_timer_pid sub_timer_pid=0 timer_state_state="inactive" timer_state_progress=100000 update_timer_state else start_timer "work" & sub_timer_pid=$! # Will wait forever wait $sub_timer_pid fi } # Set up the SIGUSR1 signal handler trap handle_signal 10 timer_state_pid=$$; update_timer_state while true; do sleep infinity & wait done