#!/bin/sh

# source any local wishes
if [ -r /etc/dueca/profile ]; then
    . /etc/dueca/profile
fi

# step into run folder
cd "$1"

# remove scratch file
rm -f dueca.scratch

# start up clean, no window manager
if test -z "$2"; then
    if [ -x ./runscript ]; then
        DUECA_EXECUTABLE=./runscript
    elif [ -x ./dueca_run.x ]; then
        dueca-archivelogfiles
        DUECA_EXECUTABLE=./dueca_run.x
    elif [ -x ../../../build/dueca_run.x ]; then
        dueca-archivelogfiles
        DUECA_EXECUTABLE=../../../build/dueca_run.x
    elif [ -x ../../../dueca_run.x ]; then
        dueca-archivelogfiles
        DUECA_EXECUTABLE=../../../dueca_run.x
    else
        cat <<EOF >error.log
Cannot find ./runscript, ./dueca_run.x, ../../../build/dueca_run.x or ../../../dueca_run.x
from folder `pwd`
EOF
        exit 1
    fi
    ${DUECA_EXECUTABLE} </dev/null 1>normal.log 2>error.log
else
    # custom script. Assumes dueca-archivelogfiles
    $2 </dev/null 1>normal.log 2>error.log
fi
