#!/bin/bash

# Check if count.txt exists
if [ ! -f ~/Ressources/Divscripts/Timers/count.txt ]; then
    echo "Error: count.txt does not exist." >&2
    exit 1
fi

# Ask the user for a duration to increase the timer by
times=$(zenity --entry --title="Timer" --text="Enter a duration for the increase in hours.")
if [ $? -ne 0 ]; then
    echo "Error: User cancelled input dialog." >&2
    exit 1
fi

# Add the specified number of hours to count.txt
new_count=$(( $(cat ~/Ressources/Divscripts/Timers/count.txt) + (times * 3600) ))
if [ $? -ne 0 ]; then
    echo "Error: Unable to read count.txt or invalid input." >&2
    exit 1
fi
echo "$new_count" > ~/Ressources/Divscripts/Timers/count.txt

# Log the change in count
echo "ADDED ($times*3600) : $new_count at $(date)" >> ~/Ressources/Divscripts/Timers/timers.log

# Kill Allpurposetimer if it is running
if pgrep -x "Allpurposetimer.sh" >/dev/null; then
    pkill -f Allpurposetimer
fi

# Start the Allpurposetimer
if [ ! -f ~/Ressources/Divscripts/Timers/Allpurposetimer.sh ]; then
    echo "Error: Allpurposetimer.sh does not exist." >&2
    exit 1
fi
exec bash /home/user/Ressources/Divscripts/Timers/Launchtimer.sh
