#!/bin/csh NeXtMidas shell instantiation script must be sourced.
#
# @author  Jeff Schoen, J. Czechowski IV, Neon Ngo
#

################################################################################
# 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.
#

if ( ! $?NM_BOOT_CP_APPEND ) then
  set nm_boot_classpath = ${NMROOT}/tools
else
  set nm_boot_classpath = "${NMROOT}/tools:${NM_BOOT_CP_APPEND}"
endif

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

set nm_fips = "-Dcom.redhat.fips=false -Dredhat.crypto-policies=false"

# If the tools.jar exists, it is added, otherwise it is not
set nm_reflective_access = ""
set nm_native_access = ""
if ( -f ${JAVAHOME}/lib/tools.jar ) then
  set nm_javac_classpath = "${nm_boot_classpath}:${NMROOT}:${JAVAHOME}/lib/tools.jar"
else
  set nm_javac_classpath = "${nm_boot_classpath}:${NMROOT}"

  # set this value as this means java is greater than 1.8 to keep reflective access quiet.
  # java.util.logging keeps LogUtilites quiet. java.security keeps SSLUtilites quiet
  set 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"

  # NXM-4244 since NeXtMidas 4.2.2
  set javaVer = `${JAVAHOME}/bin/java -version |& awk -F '"' '/version/ {print $2}'`
  set javaVerStr = `echo ${javaVer} | sed 's/-/_/' | awk -F '_' '{print $1}'`
  set javaMajVer = `echo ${javaVerStr} | awk -F '.' '{print $1}'`
  if ( $javaMajVer >= 21 ) then
    set nm_native_access = "--enable-native-access=ALL-UNNAMED"
  endif

  unset javaVer
  unset javaVerStr
  unset javaMajVer
endif

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

unset nm_sys_javac_flags
unset nm_fips
unset nm_reflective_access
unset nm_native_access

################################################################################
# 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 ("$args" =~ make*) then
  ${NMROOT}/os/unix/nmjdkswap.sh
  ${NMROOT}/os/unix/nmcpdepflags.sh

  # begin BUG 1850: workaround for tcsh 6.14/16 (with sm option) bug that causes
  #                 "args: Subscript out of range." for ${args[2-]} syntax
  set makefunc   = ""
  set optiontree = ""
  if ($#args >= 2) then
    set makefunc = "${args[2]}"
  endif
  if ($#args >= 3) then
    set optiontree = "${args[3]}"
  endif
  # end of changes for BUG 1850

  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
    endif

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

    goto DONE
  else if ("$makefunc" == "boot" || "$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
    foreach file ( nmbat nmbld nmedit nmmake nmsf ipcclean )
      if ( ! -x $NM_OS/$file ) chmod a+x $NM_OS/$file
    end
    foreach file ( jvm.b nm.b )
      if ( -x $NMROOT/nxm/sys/host/$file && -f $NMROOT/nxm/sys/host/$file ) chmod a+x $NMROOT/nxm/sys/host/$file
    end

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

    # 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
    endif

    goto DONE
  endif
endif
if ("$args" =~ linkopt* || "$args" =~ optlink*) then
  if ($#args != 3) then
    echo "Syntax:  nm linkopt <optname> <treename>"
  endif
  set optname  = "${args[2]}"
  set treename = "${args[3]}"
  mkdir $treename/nxm
  ln -sfn ".." $treename/nxm/$optname
  ln -sfn $treename ${optname}xxx
  ln -sfn $cwd/${optname}xxx $NMROOT/nxm/$optname
  goto DONE
endif

################################################################################
# Catch the special cases handled by the OS scripts
if ("$args" =~ end) then
  source $NM_OS/nm$args
  goto DONE
endif

if ("$args" =~ edit*) then
  $NM_OS/nm$args
  goto DONE
endif

if ( ! -e ${NMROOT}/nxm/sys/lib/Shell.class ) then
  echo "NeXtMidas has not been built yet - see $NMROOT/readme.txt (nm make all)"
  goto DONE
endif

# 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
  goto SHELL
endif

################################################################################
SHELL:
  if ( -e $NM_SF.bat) \rm -f $NM_SF.bat

  $nm_java_cmd -classpath "${nm_boot_classpath}" -DSHELL=nxm.sys.lib.Shell NmClassLoader "$args"

  if ( -e $NM_SF.bat) then
    source $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 ( ! ${?NM_SF} ) goto DONE

    \rm $NM_SF.bat

    # if args is undefined (e.g. from nmd), set it to blank to prevent "args: Undefined variable."
    if ( ! $?args ) set args = ""

    # only re-enter NeXtMidas shell if args is empty (since 3.1.2 for BUG 2600)
    if ( "$args" == "" ) goto SHELL

    goto DONE
  endif

################################################################################
DONE:
# cleanup local variables
  unset args
  unset nm_boot_classpath
  unset nm_javac_classpath
  unset nm_java_cmd
  unset nm_javac_cmd

# Set the status code from the build code of the last NeXtMidas build
  if ( ${?NM_PID} ) then
    set cmd_status_file = ${HOME}/nm_last_cmd_status_${NM_PID}.txt
    if ( -f ${cmd_status_file} ) then
      set cmd_status = `cat $cmd_status_file`
      rm -f $cmd_status_file
      unset cmd_status_file
      exit $cmd_status
    else
      unset cmd_status_file
    endif
  endif
