#!/bin/csh -f

# this script runs an application and then renice's it to 19.

if ( $1 == --help || $1 == -h ) then
    echo "RunCode (rc)"
    echo " "
    echo "written by Luke Abraham 2005/10/25"
    echo ""
    echo "useage:"
    echo ""
    echo "rc {input file stem}"
    echo ""
    echo "Will send code running and then re-nice it to 19"
    echo ""

# this just reads the file stem (i.e. 'test.input' -> 'test' etc.)
# just put this as $1 if you don't want this feature (removal of 
# .input etc)
set RUNFLAGS=`echo $1 | awk 'BEGIN{FS="."}{print $1}'`
# NOTE: all the below could be set from command line using $2, $3 etc.
# application name here e.g. myprog.exe
# path to application e.g. /home/abc123/bin/ (the final / *is* important)
# nice level you want to change it to, from 1->19

# run the application - this will keep the application running after the 
# xterm is closed
echo "nohup "${APP_PATH}${APPLICATION} ${RUNFLAGS} ">" ${RUNFLAGS}".screen &"

# work out the PID of the process just run. Will only return numbers 
# that have a NICe level set to 0, to avoid unnecessarily re-nicing 
# something already at the correct niceness (i.e. a process already run
# and reniced by this script
set PROG_NUM=`ps -o "NICE=%n %p %a" | grep "${APPLICATION} " | grep -v "grep" | awk '/NICE=  0/{print $3}'`

# now have the correct PID, re-nice
echo "renice" ${NICE_LEVEL} ${PROG_NUM}
renice ${NICE_LEVEL} ${PROG_NUM}