#!/bin/sh
# This script OMShell-terminal is a start script. It launches the con-
# sole based program OMShell-terminal. 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-terminal should be an interim solution. The hard
# coded directory OMWorkspace isn't very elegant. The user should create
# his or her own workspace directory during the start, if using the
# the GNOME or Debian menu.
#
# The script OMShell-terminal 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's 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-terminal  from  Your  home
   directory:
   
   ${HOME}
   
   Please  create a sub  directory and change into the new created
   sub  directory. In the sub  directory  launch  OMShell-terminal
   again.
   
   Creating and  changing the sub  directory is necessary, because
   OMShell-terminal creates temporary files during the runtime. If
   You not created a sub directory, OMShell-terminal would clutter
   Your home directory with temporary files.
   
   Thanks
   "
   exit 0
fi
                              
# Set OpenModelica OMShell-terminal environment variables.
export OPENMODELICAHOME=/usr/lib/omc
export OPENMODELICALIBRARY=/usr/share/omlibrary/modelicalib/
export QTHOME=${OPENMODELICAHOME}/lib

# Check if verbose flag is set then run console based OMShell-terminal.
if [ ! -z "$1" ] && [ "$1" == -verbose ]
then
  # start OMShell-terminal from console and start child process (omc).
  # Do not start OMShell-terminal, if launched from user's home directory.
  ( ${OPENMODELICAHOME}/bin/omc +d=interactiveCorba; ) &
  ${OPENMODELICAHOME}/bin/OMShell-terminal -noserv -corba
elif [ -z "$1" ] && [ "${PPROC}" != "${SHELL}" ]
then
  # start OMShell-terminal from menu and start omc indirect via
  # OMShell-terminal. Run omc in mode +d=interactiveCorba.
  # Current working directory is OMWorkspace.
  ${OPENMODELICAHOME}/bin/OMShell-terminal -corba
else
  # start OMShell-terminal from console and start omc indirect.
  # Do not start OMShell-terminal, if launched from user's home directory.
  ${OPENMODELICAHOME}/bin/OMShell-terminal $*
fi
