#!/bin/sh NeXtMidas shell instantiation script must be sourced.
#
# @author  J. Czechowski IV
# @since NeXtMidas 3.5.4
#

################################################################################
# Convention is to user all lower case for local variables
#
# nm_java_cmd   Used to run Java, configured for use with NeXtMidas (containing
#               appropriate JVM flags and options. This is used to run java and
#               "boot-up" NeXtMidas by configuring and starting the required
#               class loader and then running the NeXtMidas shell (set using
#               -DSHELL=<shell class>).
#
# nm_javac_cmd  Used to compile the Java files in the $NMROOT/tools directory
#               which are used to build the rest of NeXtMidas.
#               This has the NeXtMidas boot-strap build / compile options.
#               Again, there should be NO NEED to CHANGE this since it only
#               affects files built under $NMROOT/tools.
#
# NOTE:  The flags in nm_javac_cmd are used for boot strap build of NeXtMidas
#        and should not be modified. To configure your option tree's target JVM
#        and/or compile options, set java.version and/or java.flags.append
#        in <option_tree>/cfg/build.props. See $NMROOT/nxm/sys/cfg/build.props
#        for default values.
#

ECLIPSE_DEBUG=0

nm_reflective_access=""
if [ -z "${NM_BOOT_CP_APPEND}" ]; then
  nm_boot_classpath=${NMROOT}/tools
  #if the tools.jar exists, it is added to the classpath Since 3.8.1
  if [ -e ${JAVAHOME}/lib/tools.jar ]; then
    nm_javac_classpath=${NMROOT}/tools:${NMROOT}:${JAVAHOME}/lib/tools.jar
  else
    nm_javac_classpath=${NMROOT}/tools:${NMROOT}
    nm_reflective_access="--add-opens java.logging/java.util.logging=ALL-UNNAMED --add-opens java.base/java.security=ALL-UNNAMED --add-opens java.base/sun.security.util=ALL-UNNAMED --add-opens java.base/sun.security.pkcs=ALL-UNNAMED"
  fi
else
  nm_boot_classpath="${NMROOT}/tools:${NM_BOOT_CP_APPEND}"
    #if the tools.jar exists, it is added to the classpath Since 3.8.1
  if [ -e ${JAVAHOME}/lib/tools.jar ]; then
    nm_javac_classpath="${NMROOT}/tools:${NM_BOOT_CP_APPEND}:${NMROOT}:${JAVAHOME}/lib/tools.jar"
  else
    nm_javac_classpath="${NMROOT}/tools:${NM_BOOT_CP_APPEND}:${NMROOT}"
    nm_reflective_access="--add-opens java.logging/java.util.logging=ALL-UNNAMED --add-opens java.base/java.security=ALL-UNNAMED --add-opens java.base/sun.security.util=ALL-UNNAMED --add-opens java.base/sun.security.pkcs=ALL-UNNAMED"
  fi
fi

# get NeXtMidas baseline (SYS) javac flags for bootstrap build ($NMROOT/tools/*.java)
nm_sys_javac_flags=`grep "^SYS.java.flags.prepend" ${NMROOT}/nxm/sys/cfg/build.props | cut -d " " -f 3-`

nm_java_cmd="${JAVAHOME}/bin/java ${NM_JVM_FLAGS} ${nm_reflective_access}"
nm_javac_cmd="${JAVAHOME}/bin/javac -g -Xlint ${nm_sys_javac_flags}"

unset nm_sys_javac_flags

################################################################################
# Build NeXtMidas (boot-strap)
# BUG 1607: only run the boot-strap build when building ALL or SYS option tree
#           or when making zip archive for distribution or when cleaning under $NMROOT
#           if option tree is empty, it defaults to ALL
if [ "$1" == "make" ]; then
  ${NMROOT}/os/unix/nmjdkswap.sh

  makefunc="$2"
  optiontree="$3"

  if [[ "$makefunc" =~ zip* || "$makefunc" =~ jar* || "$makefunc" =~ dist* || "$makefunc" =~ clean* ]]; then
    if [[ ! -e ${NMROOT}/tools/NmMake.class || ! -e ${NMROOT}/tools/NmClassLoader.class ]]; then
      $nm_javac_cmd -classpath "${nm_javac_classpath}" ${NMROOT}/tools/NmMake.java ${NMROOT}/tools/NmClassLoader.java
    fi

    # pass nm make [zip|jar|dist|clean] directly to tools.NmMake
    $nm_java_cmd -classpath "${nm_boot_classpath}" -DSHELL=tools.NmMake NmClassLoader "$*"
    return 0

  elif [[ "$makefunc" == "boot" || -z "$optiontree" || "$optiontree" == "all" || "$optiontree" == "sys" ]]; then
    echo "Compiling Tools"
    $nm_javac_cmd -classpath "${nm_javac_classpath}" ${NMROOT}/tools/*.java

    # make sure NeXtMidas OS scripts are executable
    for file in nmbat nmbld nmedit nmmake nmsf ipcclean ; do
      if [ ! -x $NM_OS/$file ]; then
        chmod a+x $NM_OS/$file
      fi
    done
    for file in jvm.b nm.b; do
      if [[ ! -x $NMROOT/nxm/sys/host/$file && -f $NMROOT/nxm/sys/host/$file ]]; then
        chmod a+x $NMROOT/nxm/sys/host/$file
      fi
    done

    # Build everything
    $nm_java_cmd -classpath "${nm_boot_classpath}" -DSHELL=tools.NmMake NmClassLoader "$*"

    # Reload the nmstartup.mm file (if Shell is built now)
    if [ -e ${NMROOT}/nxm/sys/lib/Shell.class ]; then
      $nm_java_cmd -classpath "${nm_boot_classpath}" -DSHELL=nxm.sys.lib.Shell -DCONFIGURE=TRUE NmClassLoader
    fi

    # Set the status code from the build code of the last NeXtMidas MAKE
    if [ ! -z ${NM_PID} ] ; then
      cmd_status_file=${HOME}/nm_last_cmd_status_${NM_PID}.txt
      if [ -f $cmd_status_file ] ; then
        cmd_status=`cat $cmd_status_file`
        rm -f $cmd_status_file
        unset cmd_status_file
        return $cmd_status
      else
        unset cmd_status_file
        return 0
      fi
    else
      return 0
    fi
  fi
fi

################################################################################
# Catch the special cases handled by the OS scripts
if [ "$1" == "end" ]; then
  source $NM_OS/nmend
  return 0
fi

if [ ! -e ${NMROOT}/nxm/sys/lib/Shell.class ]; then
  echo "NeXtMidas has not been built yet - see $NMROOT/readme.txt (nm make all)"
  return 0
fi

# Note: Unconfigured IDEs (e.g. Netbeans/Eclipse) may delete NmClassloader.class
#       as it is not in the "tools" package, but is located in that directory.
#       So just compile this one file and continue to run the shell.
if [ ! -e ${NMROOT}/tools/NmClassLoader.class ]; then
  echo "Compiling NmClassLoader (.class file was deleted?)"
  $nm_javac_cmd -classpath "${nm_javac_classpath}" ${NMROOT}/tools/NmClassLoader.java
  return 0
fi

################################################################################
while [ 1 == 1 ]; do
  if [ -e $NM_SF.bat ]; then
    \rm -f $NM_SF.bat
  fi
  if [ "$ECLIPSE_DEBUG" == "1" ]; then
    echo "Eclipse Debug"
    $nm_java_cmd -classpath "${nm_boot_classpath}" -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9002 -DSHELL=nxm.sys.lib.Shell NmClassLoader "$*"
  else
    $nm_java_cmd -classpath "${nm_boot_classpath}" -DSHELL=nxm.sys.lib.Shell NmClassLoader "$*"
  fi

  if [ -e $NM_SF.bat ]; then
    . $NM_SF.bat

    # if NM_SF var is no longer set (usually due to running nmend - which also
    # removes $NM_SF.bat), then goto DONE to prevent "NM_SF: Undefined variable." message
    if [ -z "$NM_SF" ]; then
      return 0
    fi

    \rm $NM_SF.bat

    # only re-enter NeXtMidas shell if args is empty (since 3.1.2 for BUG 2600)
    if [ -n "$*" ]; then
      return 0
    fi
  else
    return 0
  fi
done

################################################################################
