#!/bin/bash
#auto suspend computer if idle for the indicated amount of minutes... BY PPC, for use with antiX Linux, full GPL license. Please keep this atribution
#Requirements: xprintidle, yad

#Make sure xprintidle is installed, if not ask if the user wants to to install it:
if dpkg -s "xprintidle" >/dev/null 2>&1; then
	# package is installed, continue with script
     echo xprintidle is installed, continuing...
     else
		# package is not installed, try to install it before continuing with script
		yad --center --title=$"Suspend computer if idle for..." --borders=20  --width=400 --text=$"The package xprintidle is not installed.\n It is required to detect how long the system has been idle. \n Do you want to install it now?" --button=$"OK":1 --button=$" x ":2 
		foo=$?
		#This next line zeroes the file that is used to flag the end of the install process (in the end of the follwoing if statement, the "; echo 1 > /tmp/finished1" part changes that's file's content, signaling that the process is over and the main script can continue running:
		echo 0 > /tmp/finished1
			if [[ $foo -eq 1 ]]; then x-terminal-emulator -e /bin/bash -c "gksu 'apt update' && gksu 'apt install -y xprintidle'; echo 1 > /tmp/finished1"
			fi
		#wait until install process is finished (this waits until the "sudo apt install" process running in paralel echoes the value 1 to the /tmp/finished1 file:
		finished=$(cat /tmp/finished1)
		until [ $finished -gt 0 ]
		do
		finished=$(cat /tmp/finished1)
		done
fi	

#Final check, to confirm if xprintidle is installed (if it is not, exit the script):
if ! command -v xprintidle &> /dev/null; then
    echo "Error: 'xprintidle' command not found. Exiting script."
    exit 1
fi

#Get value given in the terminal:
inputed_period="$1"

#Set default value:
default_gui_period=15

#If no value was given in the terminal, using yad, ask user to input a value as use it, removing the trailing pipe at the end
if [ -z "$inputed_period" ]; then
	echo No value was given, opening the GUI to you can select the value...
	inputed_period=$(yad --center --title=$"Suspend computer if idle for..." --borders=20  --width=400 --form   --field=$"(Select number of minutes:):NUM" $default_gui_period    --range="0:600" | sed 's/.$//')
fi

#IF value is missing or 0, exit
if [ -z "$inputed_period" ] || [ "$inputed_period" -eq 0 ]; then
    echo "Variable is empty or zero. Exiting..."
    killall -q suspend_if_idle
    exit 1
fi

#Perform main loop
chosen_period=$(($inputed_period))
chosen_period_in_seconds=$((chosen_period * 60))
echo The system will wait for  $chosen_period minute\(s\), or $chosen_period_in_seconds seconds, of inativity before trying to suspend...
echo IMPORTANT: in order for stop that from happening, close this terminal window.

while true; do
    # Get idle time in milliseconds using xprintidle
    idle_time=$(xprintidle)

    # Convert idle time to seconds
    idle_seconds=$((idle_time / 1000))

    # Check if idle time is greater than 60 seconds
    if [ "$idle_seconds" -gt $chosen_period_in_seconds ]; then
        # Suspend the computer
        echo suspending due to inactivity...
        desktop-session-exit -S
    fi

    # Sleep for a short duration before restarting loop
    sleep 5
done
