#!/bin/sh

#==================================================
# This shell script is intended to be placed in
# /usr/bin/ and should be run to start a GNUmed
# client.
#
# $Source: /sources/gnumed/gnumed/gnumed/dists/Linux/gnumed,v $
# $Id: gnumed,v 1.9.2.1 2008/01/01 13:51:23 ncq Exp $
# license: GPL
# Karsten Hilbert, Sebastian Hilbert, Andreas Tille
#--------------------------------------------------

# for debugging this script
# set -x

OPTIONS=$@

# Verify the name of the script for the appendix "-debug"
# If it is symlinked to a file with this name use --debug as option
name=`basename $0`
if echo $name | grep -q -- "-debug" ; then
	name=`echo $name | sed 's/-debug//'`
	OPTIONS="$OPTIONS --debug"
fi

# the system-wide configuration file for backend profiles
SYSCONF="/etc/gnumed/gnumed-client.conf"
if [ ! -e ${SYSCONF} ] ; then
	echo "Global config file ${SYSCONF} missing."
	exit 1
fi

# There has to be a user config file (even if empty).
if [ ! -e ${HOME}/.gnumed/${name}.conf ] ; then
	touch ${HOME}/.gnumed/${name}.conf
fi

## Different ways to obtain the version number of the running Python interpreter
## store this here just for the purpose of education :-)
# PYVER=`python -V 2>&1 | sed 's/Python[[:space:]]\+\([0-9]\+\.[0-9]\+\).*/\1/'`
# PYVER=`pyversions -vd`
# PYVER=`python -c 'import platform; print platform.python_version()'`
PYVER=`python -c 'import sys; print sys.version[:3]'`

# this is where the gnumed.py Python script actually lives
GNUMEDDIR=/var/lib/python-support/python${PYVER}/Gnumed/wxpython

# check for explicit --conf-file option
CONFFILE=""
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 1
	fi
	CONFFILE="--conf-file=${CONFFILE}"
	OPTIONS=`echo $OPTIONS | sed -e 's/--conf-file[[:space:]]*=[[:space:]]*[^[:space:]]\+//'`
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/scripts/gnumed-startup-local.sh ] ; then
	. ${HOME}/.gnumed/scripts/gnumed-startup-local.sh
fi

# now run the client
python ${GNUMEDDIR}/gnumed.py ${CONFFILE} ${OPTIONS}

#==================================================
# $Log: gnumed,v $
# Revision 1.9.2.1  2008/01/01 13:51:23  ncq
# - pull in patch from Debian package touching user config file
#
# Revision 1.9  2007/09/24 18:37:45  ncq
# - fix startup script directory error as noticed by Andreas
#
# Revision 1.8  2007/05/22 13:49:52  ncq
# - exit with 1 when something goes wrong
#
# Revision 1.7  2007/04/27 13:30:07  ncq
# - better location for user-local shell include
#
# Revision 1.6  2007/04/05 17:21:18  ncq
# - pull in some logic from Debian startup script
# - cleanup for system-wide config file
#
# Revision 1.5  2007/03/26 17:18:04  ncq
# - no more --unicode-gettext
# - no more copying /etc/gnumed/gnumed.conf
#
# Revision 1.4  2005/07/11 16:55:27  ncq
# - allow systemwide startup extension shell script, too
#
# Revision 1.3  2005/07/11 16:53:03  ncq
# - include user-local startup shell script
# - use cp -R
#
# Revision 1.2  2005/07/11 16:46:41  ncq
# - make more dumb but more robust
#
