#!/bin/sh
OPTIONS=$@

name=`basename $0`
CONFSOURCE=/etc/gnumed/${name}.conf

# set -x

# check for explicite conffile option
if echo $OPTIONS | grep -q -- "--conf-file[[:space:]]*=" ; then
    CONFFILE=`echo $OPTIONS | sed -e 's/^.*--conf-file[[:space:]]*=[[:space:]]*\([^[:space:]]\+\).*/\1/'`
    if [ ! -s ${CONFFILE} ] ; then
        echo "Given config file ${CONFFILE} missing."
	exit
    fi
    CONFFILE="--conf-file=${CONFFILE}"
    OPTIONS=`echo $OPTIONS | sed -e 's/--conf-file[[:space:]]*=[[:space:]]*[^[:space:]]\+//'`
else
    # copy global config file to user directory because /etc/gnumed is not writable
    if [ ! -s ${HOME}/.gnumed/${name}.conf ] ; then
        if [ ! -e ${CONFSOURCE} ] ; then
	    echo "Global config file ${CONFSOURCE} missing."
	    exit
	fi
	if [ ! -d ${HOME}/.gnumed ] ; then
	    mkdir -p ${HOME}/.gnumed
	fi
        cp -a ${CONFSOURCE} ${HOME}/.gnumed/${name}.conf
    fi
    CONFFILE="--conf-file=${HOME}/.gnumed/${name}.conf"
fi

# check for explicite --unicode-gettext option
if echo $OPTIONS | grep -q -- "--unicode-gettext[[:space:]]*=" ; then
    ## just for debugging
    echo $OPTIONS
else
    OPTIONS="--unicode-gettext=0 $OPTIONS"
fi

# source systemwide startup extension shell script if it exists
if [ -r /etc/gnumed/gnumed-startup-local.sh ] ; then
    . /etc/gnumed/gnumed-startup-local.sh
fi

# source local startup extension shell script if it exists
if [ -r ${HOME}/.gnumed/gnumed-startup-local.sh ] ; then
    . ${HOME}/.gnumed/gnumed-startup-local.sh
fi

python2.3 /usr/lib/python2.3/site-packages/Gnumed/wxpython/gnumed.py ${CONFFILE} ${OPTIONS}
