#!/bin/bash
######################################################################################
#
# This script removes dependencies via swapping between a few NeXtMidas swap modes:
#   1. kvpair/nkvpair - As of Jdk 8+, if KeyValuePair's are being used to create
#                       lambda expressions, then the bcel jar file is needed.
#                       Otherwise, the bcel jar file is not needed.
#   2. ftp/nftp       - If FtpResource is being used, keep the commons-net jar.
#                       Otherwise, the commons-net jar is not needed.
#   3. help/nhelp     - If both the CLI functionality for the HELP command and the
#                       graphics functionalities for the HELP command are being used,
#                       then the lucene jar file is needed. Otherwise, if only the
#                       CLI functionality for the HELP command is being used,
#                       then the lucene jar file is not needed.
#
# This script takes in swap mode arguments (in any order), and based on each argument,
# if the corresponding set of txt files can be found within the swap parent directory
# of NMROOT, those files will be copied over into NMROOT as java files. These
# new java files will replace the current corresponding java files in NMROOT
# with the new corresponding java files.
#
# Usage: ${0} <swapArg1> [swapArg2] [swapArg3]
#    eg: ${0} nhelp kvpair nftp
#
# Author: Zachary Matthews
# Since:  NeXtMidas 4.1.3
# Note:   Experimental version first appeared in NeXtMidas 4.0.0
#         and was in NMROOT/swap/swap.sh
#
######################################################################################

DEP_LUCENE=`echo $((16#0001))` # hex to decimal
DEP_COMMONS_NET=`echo $((16#0002))` # hex to decimal
DEP_BCEL=`echo $((16#0020))` # hex to decimal
DEP_COMMONS_LANG3=`echo $((16#0040))` # hex to decimal

if [ $# -lt 1 ] ; then
  echo ""
  echo "Usage: ${0} <swapArg1> [swapArg2] [swapArg3]"
  echo "   eg: ${0} nhelp kvpair nftp"
  exit 1
fi

swap_parent_dir=`realpath ${0}` ; swap_parent_dir=`dirname ${swap_parent_dir}`
NMROOT_TMP=`realpath ${0}`
for i in {1..4}
do
  NMROOT_TMP=`dirname ${NMROOT_TMP}`
done
NMROOT=${NMROOT_TMP}

# Get the current dependency flag in hex format, and then convert it to decimal format
current_dependency_flag_file=${NMROOT}/current_dependency_flag.txt
depFlagHex=`head -n 1 ${current_dependency_flag_file}` # original flag as hex
depFlag=`echo $((16#${depFlagHex:2}))` # hex to decimal

# **********************************************************************
# Check swap-mode arguments:
# 1. If not valid argument, skip it.
# 2. If valid argument, update dependency flag, then proceed
#    to code swapping.
# **********************************************************************
jarDependenciesDir=${NMROOT}/libnxm/jar-footprint/jar-dependencies
for arg in "$@"
do
  case ${arg} in
    help)    swap_mode_dir=${jarDependenciesDir}/lucene/help-graphics;  depFlag=$((${depFlag}|DEP_LUCENE));;
    nhelp)   swap_mode_dir=${jarDependenciesDir}/lucene/nhelp-graphics; depFlag=$((${depFlag}&~DEP_LUCENE));;
    ftp)     swap_mode_dir=${jarDependenciesDir}/commons-net/ftp;       depFlag=$((${depFlag}|DEP_COMMONS_NET));;
    nftp)    swap_mode_dir=${jarDependenciesDir}/commons-net/nftp;      depFlag=$((${depFlag}&~DEP_COMMONS_NET));;
    kvpair)  swap_mode_dir=${jarDependenciesDir}/bcel/kvpair;           depFlag=$((${depFlag}|DEP_BCEL|DEP_COMMONS_LANG3));;
    nkvpair) swap_mode_dir=${jarDependenciesDir}/bcel/nkvpair;          depFlag=$((${depFlag}&~(DEP_BCEL|DEP_COMMONS_LANG3)));;
    *)       echo "Swap mode ${arg} not recognized"; continue;;
  esac

  # **********************************************************************
  # Copy all files within the swap subdirectory to the temp directory,
  # change them to java files, and move the java files into NMROOT
  # to replace the corresponding files currently in NMROOT
  # **********************************************************************
  swap_mode_dir_name=`basename ${swap_mode_dir}`
  echo ""
  echo "Swapping files for ${swap_mode_dir_name}..."
  cd ${swap_mode_dir}
  files_to_copy=`find . -type f -name "*.txt"` # assuming text files here in mode directory
  for file in ${files_to_copy}
  do
    opt_tree=`cut -d '/' -f2 <<< ${file}`           # get top-level option tree
    text_file=${file:2}                             # name of the text file without "./"
    java_file=${text_file::-4}.java                 # name of the java-file version of the text file
    cp --parents ${text_file} /tmp                  # preserve original parent directory structure (sys/...) 
    mv /tmp/${text_file} ${NMROOT}/nxm/${java_file} # convert files to java files as they're being moved over; for 'mv', files are overwritten in NMROOT by default
  done
  rm -rf /tmp/${opt_tree} # remove /tmp/<option_tree>
done

# **********************************************************************
# Output the new dependency flag to current_dependency_flag_file.txt
# in hex format, then update both classpath files.
# **********************************************************************
echo 0x`printf '%x\n' ${depFlag}` > ${current_dependency_flag_file} # write out dependency flag in hex

# Update .classpath and .classpathlinks based on flag in current_dependency_flag.txt
${NMROOT}/libnxm/update-classpath.sh
