#!/bin/sh
# This script OMShell is a start script. It launches the Qt based pro-
# gram OMShell. Before launching it creates a sub directory in users
# home directory, if launched from GNOME or Debian menu. If launched
# from a console it doesn't create a sub directory in the users home
# directory. The user get a hint, how he or she should start the
# script from the console.
#
# The script OMShell should be an interim solution until the Qt based
# program OMShell has a setting menu.
#
# The script OMShell based on the work of William Spinelli's
# package_builder and changed by Robert Wotzlaw.

OMDIR=OMWorkspace                        # OpenModelica Workspace
PPROC=`which $(ps -p ${PPID} -o comm=)`  # Prog name with path from 
                                         # parent process ID

# No OMWorkspace in user home dir and not started from terminal console
# or console (bash), then create dir OMWorkspace and change in dir.
if [ ! -e ${HOME}/${OMDIR} ] && [ "${PPROC}" != "${SHELL}" ]
then
   mkdir ${HOME}/${OMDIR}
   cd ${HOME}/${OMDIR}

# OMWorkspace exist and not started from terminal console
# or console (bash), then change directory.
elif [ -d ${HOME}/${OMDIR} ] && [ "${PPROC}" != "${SHELL}" ]
then
   cd ${HOME}/${OMDIR}

# Started from terminal console or console (bash) and current dir is
# user home dir, then give a hint and exit.
elif [ "${PPROC}" = "${SHELL}" ] && [ "`pwd`" = "${HOME}" ]
then
   clear
   echo "   
   Dear user ${USER},
   
   You launched  OpenModelica  OMShell from  Your  home directory:
   
   ${HOME}
   
   Please  create a sub  directory and change into the new created
   sub  directory.  In the sub  directory  launch  OMShell  again.
   
   Creating and  changing the sub  directory is necessary, because
   OMShell creates temporary files during the runtime.  If You not
   created a sub directory, OMShell would clutter Your home direc-
   tory with temporary files.
   
   Thanks
   "
   exit 0
fi

# Set OpenModelica OMShell environment variables.
export OPENMODELICAHOME=/usr/lib/omc
export OPENMODELICALIBRARY=/usr/share/omlibrary/modelicalib/
export QTHOME=${OPENMODELICAHOME}/lib

# Run the Qt based OMShell.
exec ${OPENMODELICAHOME}/bin/OMShell $*
