#!/bin/bash
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
#
# Copyright IBM Corporation © 2005, 2006, 2007
#
# Author: John Kacur <jkacur at ca.ibm.com>


# dslimit = dynamic soft cpu limit.
#
# Usage: dslimit [seconds | unlimited] [prog [program parameters]]
#
# This program allows you to quickly change the soft cpu time limit
# without modifying the default limit in your current shell
#
# Example 1
# This example runs cmvc without any cpu time limit in the background
# dslimit unlimited cmvc &
#
# Example 2
# This example runs a java program with a 60 second timeout
# dslimit 60 javaprog
#

usage() {
       echo "dslimit [seconds | unlimited] [prog [program parameters]]"
       echo
       echo "dslimit without options will print the current soft cpu time limit"
       echo
       echo "If you don't specify a timeout, the program will run"
       echo "with the default soft cpu timeout."
       echo
       echo "dslimit seconds prog will run the program with a soft cpu time"
       echo "specified in seconds"
       exit 0
}

if [ "$1" = "-h" ] || [ "$1" = "--h" ] || [ "$1" = "-help" ]; then
       usage
fi

if [ $# -lt 1 ]; then
       ulimit -St
elif [ $# -eq 1 ]; then
       ($@)
else
       (ulimit -St $1; shift; $@)
fi

exit 0

