#!/bin/sh
#
# Wrapper script used to launch other long running scripts that need sudo
# Otherwise the long running script will require sudo after the grace window has passed.
#
# This script takes a script as an argument
if [ "$1" = "" ]
  then 
    echo "Usage: $0 commans args ..."
    exit 1
  fi
# Invalidate users cached creds
sudo -k 
# If gnome sudo is installed use it
if [ -x /usr/bin/gksudo ]
  then
    gksudo true 
  else
    sudo true
fi
#  takes authentication once from user as stdin, feeds to devnull
sudo -S true < /dev/null || exit 1

# Repeat every 30 seconds to keep the cache aliave, and fork to background
(while sudo -S true < /dev/null ; do sudo -v ; sleep 30; done) &

# Run the remainder of the arguments to this program
"$@"

# kill the background while loop once the main program finishes
kill %1
# Invalidate users cached creds
sudo -k
