#!/bin/bash

#==================================================
# 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.14.2.1 2008/08/31 22:21:22 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


## 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
# Debian: /var/lib/python-support/python${PYVER}/Gnumed/wxpython
# SuSE/PCLinuxOS: /usr/lib/python${PYVER}/site-packages/Gnumed/wxpython
#	if [ -f /etc/SuSE-release ] ; then
#	if [ -f /etc/version ] ; then
#		grep -q PCLinuxOS /etc/version
#		if [ $? -eq 0 ] ; then
GNUMED_SCRIPT_PATH_CANDIDATES=\
"/var/lib/python-support/python${PYVER}/Gnumed/wxpython"\
" /usr/lib/python${PYVER}/site-packages/Gnumed/wxpython"\
" /usr/lib64/python${PYVER}/site-packages/Gnumed/wxpython"

GM_SCRIPT=""
for GNUMED_SCRIPT_PATH in ${GNUMED_SCRIPT_PATH_CANDIDATES} ; do
	if test -f ${GNUMED_SCRIPT_PATH}/gnumed.py ; then
		GM_SCRIPT="${GNUMED_SCRIPT_PATH}/gnumed.py"
		break
	fi
done

if test -z "${GM_SCRIPT}" ; then
	echo "Cannot find \"gnumed.py\" in any of:"
	for GNUMED_SCRIPT_PATH in ${GNUMED_SCRIPT_PATH_CANDIDATES} ; do
		echo " ${GNUMED_SCRIPT_PATH}"
	done
	exit 1
fi


# 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 ${GNUMED_SCRIPT_PATH}/gnumed.py ${CONFFILE} ${OPTIONS}

#==================================================
# $Log: gnumed,v $
# Revision 1.14.2.1  2008/08/31 22:21:22  ncq
# - properly search for gnumed.py in list of paths
#
# Revision 1.14  2008/08/01 10:32:41  ncq
# - /bin/sh -> /bin/bash
#
# Revision 1.13  2008/07/22 13:55:58  ncq
# - better way of finding gnumed.py as suggested by Ruthard Baudach
#
# Revision 1.12  2008/03/29 16:23:11  ncq
# - whitespace fix
#
# Revision 1.11  2008/02/25 17:44:55  ncq
# - cleanup
# - support a few pathes for gnumed.py, included from downstream packages
#
# Revision 1.10  2008/01/01 13:53:35  ncq
# - should not need touching user config file anymore
#
# 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
#
