#!/bin/sh

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

# step into run directory
cd "$1"

# keep old logs
# keep old logs
LOGFILES="
error.log
normal.log
dueca.messagelog
dueca.timinglog
dueca.channels
dueca.objects
dueca.activities
dueca.activitylog
dueca.channelreadinfo
dueca.channelwriteinfo
dueca.netload
dueca.nettiming"

dueca-archivelogfiles

# remove junk that might still be in the way
rm -f dueca.scratch $LOGFILES

# determine whether X server instructions are included
XOPTS=""
if test -n "$3"; then
    XOPTS="-- $3"
fi

# start up with clean X
if test -z "$2"; then
    if [ -x ./dueca_run.x ]; then
        DUECA_EXECUTABLE=./dueca_run.x
    elif [ -x ../../../build/dueca_run.x ]; then
        DUECA_EXECUTABLE=../../../build/dueca_run.x
    elif [ -x ../../../dueca_run.x ]; then
        DUECA_EXECUTABLE=../../../dueca_run.x
    else
        cat <<EOF >error.log
Cannot find ../../../build/dueca_run.x or ../../../dueca_run.x
from folder `pwd`
EOF
        exit 1
    fi
    exec xinit  ${DUECA_EXECUTABLE} 1>normal.log 2>error.log $XOPTS
else
    exec xinit  $2 1>normal.log 2>error.log $XOPTS
fi
