#!/bin/sh

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

# step into run directory
cd "$1"

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

# 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 ./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
    exec xinit  ${DUECA_EXECUTABLE} 1>normal.log 2>error.log $XOPTS
else
    exec xinit  $2 1>normal.log 2>error.log $XOPTS
fi
