#! /bin/bash


function usage {
   echo "usage: xstimeout MINUTES"
   echo "Set xscreensaver timeout and restart the daemon"
   echo
   echo "MINUTES must be a number between 1 and 720"
   echo
   echo "xscreensaver current timeout setting:"
   echo "$(grep timeout ~/.xscreensaver)"
   exit 1
}


# No args at all
if [ "$1" = "" ] ; then usage ; fi

# Argument is within allowed range
if [[ $1 -lt 1 ]] || [[ $1 -gt 720 ]] ; then usage ; fi

# Convert the total minutes value into hours and minutes values
hours=$[ $1 / 60 ]
minutes=$[ $1 % 60 ]

# Format the hours and minutes into a colon-separated string
delay=$(printf "%02d:%02d:00" $hours $minutes)

echo "Setting xscreensaver timeout to $delay"

# Place the new delay time into xscreensaver's config file
sed -i -e "s/\(timeout:	\).*/\1$delay/" ~/.xscreensaver

# Restart the daemon
xscreensaver-command -restart
