#!/bin/bash
######################################################################################
#
# This script is the master script used for full preparation for a
# NeXtMidas no-graphical build with jar footprint reduction.
# This script does the following steps:
#   1. Move Unittest.java into nxm.sys.test.junit.
#   2. Move nxm.sys.dat into nxm.sys.test.
#   3. Separate jar dependencies.
#   4. Call strip-graphics script to remove unnecessary packages and files.
#
# Usage: ${0} [nkvpair/ftp] [nkvpair/ftp]
#
# Author: Zachary Matthews
# Since:  NeXtMidas 4.1.3
# Note:   Experimental version first appeared in NeXtMidas 4.0.0
#         and was in NMROOT/swap/master_swap_thin.sh
#
######################################################################################

function usage() {
  echo ""
  echo "This is the master script used for full preparation for a NeXtMidas no-graphical build with jar footprint reduction."
  echo "By default, ftp will be disabled and kvpair enabled. These can be overridden with optional arguments, each with two possible choices:"
  echo "  1. nkvpair - kvpair will be disabled."
  echo "  2. ftp - ftp will be enabled."
  echo ""
  echo "Usage: ${0} [nkvpair/ftp] [nkvpair/ftp]"
}

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

# Set default modes (kvpair enabled and ftp disabled)
kvpairArg=kvpair
ftpArg=nftp

if [[ "$@" =~ "--help" ]] ; then
  usage
  exit 0
fi

# Read in arguments
for arg in "$@"
do
  if [ "${arg}" == "nkvpair" ] ; then
    kvpairArg=${arg}
  elif [ "${arg}" == "ftp" ] ; then
    ftpArg=${arg}
  else
    echo "Invalid optional argument ${arg}"
    usage
    exit 1
  fi
done

# Move intr.Unittest.java into test.junit
if [ ! -f ${NMROOT}/nxm/sys/test/junit/Unittest.java ] ; then
  ${libnxm_dir}/jar-footprint/jar-dependencies/junit/move-unittest.sh
  exit_code=$?
  if [ ${exit_code} != 0 ] ; then
    echo ""
    echo "Something went wrong when moving the UNITTEST intrinsic, got exit code ${exit_code}, aborting make-libnxm script..."
    exit 1
  fi
else
  echo ""
  echo "nxm.sys.test.junit.Unittest.java already exists, no unittest swapping needed"
fi

# Move nxm.sys.dat into nxm.sys.test.dat
if [ ! -d ${NMROOT}/nxm/sys/test/dat ] ; then
  ${libnxm_dir}/jar-footprint/dat/move-dat.sh
  exit_code=$?
  if [ ${exit_code} != 0 ] ; then
    echo ""
    echo "Something went wrong when moving the dat/ directory, got exit code ${exit_code}, aborting make-libnxm script..."
    exit 1
  fi
else
  echo ""
  echo "nxm/sys/test/dat already exists, no dat swapping needed"
fi

# Separate jar dependencies
${libnxm_dir}/jar-footprint/jar-dependencies/adjust-jar-dependencies.sh ${kvpairArg} ${ftpArg} nhelp
exit_code=$?
if [ ${exit_code} != 0 ] ; then
  echo ""
  echo "Something went wrong when separating the jar dependencies, got exit code ${exit_code}, aborting make-libnxm script..."
  exit 1
fi

# Call the strip-graphics script to strip down NeXtMidas in preparation for a no-graphical build
${libnxm_dir}/no-graphics-nxm/strip-graphics.sh
exit_code=$?
if [ ${exit_code} != 0 ] ; then
  echo ""
  echo "Something went wrong when stripping graphics from NeXtMidas, got exit code ${exit_code}, aborting make-libnxm script..."
  exit 1
fi
