#!/bin/sh

if [ "$#" -ne 4 ] ; then
    echo "urqmd_starter.sh usage: "
    echo " -i, --input     Path to the input file of urqmd"
    echo " -o, --output    Name of the output file of the urqmd"
    exit 1
fi


while (( "$#" )); do
    case $1 in
	-i|--input)
	    shift
	    INPUTFILE="$1"
	    ;;
	-o|--output)
	    shift
	    OUTPUTFILE="$1"
	    ;;
	*)
	    shift
	    ;;
    esac
done

echo $INPUTFILE
echo $OUTPUTFILE

run=${1-nothing}
exename="urqmd.$(uname -m)"
TRINPUTFILE=`echo $INPUTFILE | cut -d"." -f1`
NEWINPUTFILE=`echo $OUTPUTFILE"_"$TRINPUTFILE".urqmd"`
echo $NEWINPUTFILE
cp $INPUTFILE $NEWINPUTFILE

export ftn09=$NEWINPUTFILE
export ftn19=$OUTPUTFILE.f19

# replacing random seed number
export UrQMD_RNDNUM=`head -c 4 /dev/urandom | od -t u2 | cut -c9-`
perl -i -p -ne's/RNDNUM/$ENV{"UrQMD_RNDNUM"}/g' $NEWINPUTFILE

# check if file lhc exists, or if user specifically asked for lhc run:
if [ -e lhc ] && [ "$run" != "all" ] || [ "$run" = "lhc" ]; then
        if [ -x $exename.lhc ]; then
                echo "Running the LHC version of UrQMD"
        	time ./$exename.lhc
        else
                echo "LHC executable not found, please compile with 'make lhc' first"
                exit 1
        fi
else
        if [ -x $exename ]; then
                echo "Running UrQMD"
                time ./$exename
        else
                echo "UrQMD executable ($exename) not found, please compile with 'make' first"
                exit 1
        fi
fi
