#!/bin/sh NeXtMidas shell initialization script must be sourced
#
# @author  J. Czechowski IV
# @since   NeXtMidas 3.5.4
# @version $Id: nmstart.sh,v 1.4 2015/08/21 20:13:20 paq Exp $
#

# check if NMROOT directory exists
if [ -z "$NMROOT" ]; then
  echo "ERROR: [nmstart] Specified NMROOT path does not exist: $NMROOT"
  return 1
elif [[ ! -d $NMROOT/os/unix || ! -d $NMROOT/nxm/sys ]]; then
  # $NMROOT/nxm/sys or $NMROOT/os/unix does not exists
  echo "ERROR: [nmstart] Specified NMROOT path is invalid: $NMROOT"
  return 1
fi

# clean up from any previous sessions by running the (current version of) nmend
#set args = ""
if [ -n "$NM_PID" ]; then
 if [ "$NM_PID" == "$$" ]; then
  . $NMROOT/os/unix/nmend.sh
 fi
fi

# see which java home is defined (JAVAHOME then JAVA_HOME)
if [ -z "$JAVAHOME" ]; then
  if [ -n "$JAVA_HOME" ]; then
    export JAVAHOME=${JAVA_HOME}
  else
    # neither JAVAHOME nor JAVA_HOME are defined
    echo "ERROR: [nmstart] JAVAHOME is not defined!"
    return 1
  fi
fi
# check if JAVAHOME  directory exist
if [ ! -d $JAVAHOME ]; then
  echo "ERROR: [nmstart] Specified JAVAHOME path does not exist: $JAVAHOME"
  return 1
fi

export NM_JB="$JAVAHOME/bin"
export NM_JAVA="$NM_JB/java"
export NM_JAVAC="$NM_JB/javac"
export NM_OS="$NMROOT/os/unix"
export NM_OPT=sys
export NM_DIR=lib
export NM_PID=$$

#setenv NM_SF  ~/nmsf_$NM_PID
# Per BUG 1368, look for NMHISTORY_DIR for saving nmsf files
if [ -z "$NMHISTORY_DIR" ]; then
  # default behavior if NMHISTORY_DIR is not defined
  export NM_SF=~/nmsf_$NM_PID
else
  if [ ! -d "$NMHISTORY_DIR" ]; then
    echo "NMHISTORY_DIR $NMHISTORY_DIR defined but does not exist, using ~/"
    export NM_SF=~/nmsf_$NM_PID
  else
    echo "Setting NMHISTORY_DIR to $NMHISTORY_DIR"
    export NM_SF=${NMHISTORY_DIR}/nmsf_$NM_PID
  fi
fi

# Set common defines for Java. In sh (unlike csh) these can all be combined since
# an undefined variable simply resolves to the empty string in sh.
export NM_JVM_FLAGS="-DNMROOT=$NMROOT/ -DNMPID=$NM_PID -DNMSF=$NM_SF \
${NM_USER_JVM_FLAGS} \
-DCLASSPATH=${NM_USER_CP} \
-DCLASSPATHAFTERSYS=${NM_USER_CP_AFTER_SYS} \
-DCLASSPATHPREPEND=${NM_USER_CP_PREPEND} \
-DCOMPLETIONAUXEXCLUDE=${NM_COMPLETION_AUX_EXCLUDE} \
-DNMSTARTUP.USER=${NM_STARTUP_MACRO-default}"

export NM_PROFILER_FLAGS="${NM_USER_PROFILER_FLAGS}"

# setup and start session
alias nm='. $NM_OS/nm.sh $*'
alias nmprofile='. $NM_OS/nmprofile.sh $*'
alias nmd='. $NM_OS/nmdir.sh $*'
alias nmend='. $NM_OS/nmend.sh $*'

/bin/rm -f $NM_SF.msf
/bin/rm -f $NM_SF.bat

if [ -z "$NMOPTS" ]; then
  export NMOPTS=$NMROOT/opts
fi

# check if Java exists
if [ ! -e  $NM_JAVA ]; then
  echo "ERROR: [nmstart] Cannot find Java at: $NM_JAVA"
  echo "       [nmstart] Current JAVAHOME is: $JAVAHOME"
fi

# check if Java compiler (javac) exists (but only print warning if it doesn't)
if [ ! -e  $NM_JAVAC ]; then
  echo "WARN: [nmstart] Cannot find Java compiler at: $NM_JAVAC"
  echo "      [nmstart] You will not be able to compile any Java classes."
fi

# Print out the Copyright notice... do it manually if NeXtMidas has not been
# compiled yet.
if [ ! -e ${NMROOT}/nxm/sys/lib/Shell.class ]; then
  cat $NMROOT/nxm/sys/version.txt
  echo "NeXtMidas has not been built yet - see $NMROOT/readme.txt (nm make all)"
else
  # run %nmstartup and then exit
  . $NM_OS/nm.sh -DCONFIGURE=TRUE
fi