#!/bin/bash
######################################################################################
#
# This script removes unnecessary packages and files needed for
# a NeXtMidas no-graphical build.
#
# The thin build is designed to provide NeXtMidas Java library
# code access and also NeXtMidas commands related to BLUE file
# processing and needed to support a standard NeXtMidas Build.
#
# NOTE: The lucene jar dependencies must be separated first
#       Before running this script.
#
# Author: Zachary Matthews
# Since:  NeXtMidas 4.1.3
# Note:   Experimental version first appeared in NeXtMidas 4.0.0
#         and was in NMROOT/swap/thinNXM/thin_nextmidas.sh
#
######################################################################################


DEP_HAMCREST=`echo $((16#0004))` # hex to decimal
DEP_JUNIT=`echo $((16#0008))` # hex to decimal
DEP_BCEL=`echo $((16#0020))` # hex to decimal
DEP_COMMONS_LANG3=`echo $((16#0040))` # hex to decimal
DEP_FOR_STRIP=$(($DEP_HAMCREST + $DEP_JUNIT + $DEP_BCEL + $DEP_COMMONS_LANG3))

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

cmdsCnf=${NMROOT}/nxm/sys/cfg/commands.cnf
testDir=${NMROOT}/nxm/sys/test
mcrDir=${NMROOT}/nxm/sys/mcr
expDir=${NMROOT}/nxm/sys/exp

############################################
# Separate lib-libg graphical dependencies #
############################################
${no_graphics_nxm_dir}/lib2libg-modularity/adjust-lib-libg-dependencies.sh split
exit_code=$?
if [ ${exit_code} != 0 ] ; then
  echo ""
  echo "Something went wrong when adjusting lib-libg dependencies, got exit code ${exit_code}, aborting strip-graphics script..."
  exit 1
fi

############################################################################################
# Modify the Environment intrinsic to use nxm.sys.lib.Theme in place of nxm.sys.libg.Theme #
############################################################################################
echo ""
echo "Modifying the Environment intrinsic to use nxm.sys.lib.Theme..."
sed -i 's/import nxm\.sys\.libg\.Theme;/import nxm\.sys\.lib\.Theme;/' ${NMROOT}/nxm/sys/intr/Environment.java

##################################################################
# Switch to the "thin" version of commands.cnf & test_config.tbl #
##################################################################
echo ""
echo "Switching to LIBNXM version of commands.cnf and test_config.tbl..."
cp ${no_graphics_nxm_dir}/commands.cnf ${cmdsCnf}
cp ${no_graphics_nxm_dir}/test_config.tbl ${testDir}/test_config.tbl

####################################
# Remove unnecessary full packages #
####################################
echo ""
echo "Removing all other option trees besides sys, as well as unnecessary packages within sys..."

# Remove NeXtMidas option tree packages that do not include sys
origDir=${PWD}
cd ${NMROOT}/nxm
rm -rf audio/ database/ dsp/ hwc/ minio/ net/ nettest/ ucl/ vnc/ webvnc/ xbc/

# Remove unnecessary nxm.sys packages
cd ${NMROOT}/nxm/sys
rm -rf docs/ host/ libg/ libh/ libxm/ osgi/

# Remove unnecessary nxm.sys.jar files
cd ${NMROOT}/nxm/sys/jars
rm -f osgi.core.jar

cd ${origDir}

# ########################################################
# Update dependency flag, output the new dependency flag #
# to current_dependency_flag_file.txt, in hex format,    #
# then update both classpath files.                      #
# ########################################################
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
depFlag=$((${depFlag}&${DEP_FOR_STRIP}))

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

########################################################
# Remove unnecessary classes under nxm.sys.net package #
########################################################

# nxm.sys.net classes to keep
sysNetClassesToKeep=(NetUtil)

# Remove all nxm.sys.net classs except the ones being kept
for sysNetFile in ${NMROOT}/nxm/sys/net/* ;
do
  for classToKeep in ${sysNetClassesToKeep[@]} ;
  do
    if [[ ${sysNetFile} =~ ${classToKeep} ]] ; then
      continue 2
    fi
  done

  rm -f ${sysNetFile}
done

################################################################
# Remove unnecessary unit tests and update unittest_config.tbl #
################################################################
echo ""
echo "Removing unnecessary classes from sys.net..."

# remove selective lib tests that have libg dependencies
junitLibTestsToRemove=(CommandTest DatagramDataTest GPrimitiveTest RTPICEPacketsCacheTest RTPHeaderTest)
for junitLibTest in ${junitLibTestsToRemove[@]} ;
do
  rm -rf ${testDir}/junit/lib/${junitLibTest}.*
done

# remove all tests in packages in which corresponding source packages were removed
rm -rf ${testDir}/junit/libg/*
rm -rf ${testDir}/junit/net/*

# all current primitive tests get removed
rm -rf ${testDir}/junit/prim/*

# modify unittest_config.tbl to exclude junit tests that were removed above
sed -i 's/\(^\(libg\|net\|prim\).*\)/!\1/' ${testDir}/junit/unittest_config.tbl
sed -i 's/\(^lib.\(CommandTest\|DatagramDataTest\|GPrimitiveTest\|RTPICEPacketsCacheTest\|RTPHeaderTest\)\)/!\1/' ${testDir}/junit/unittest_config.tbl

##############################################################################
# Remove unnecessary primitives, intrinsics, and associated macro test cases #
##############################################################################
${no_graphics_nxm_dir}/remove-cmds-test-cases.sh
exit_code=$?
if [ ${exit_code} != 0 ] ; then
  echo ""
  echo "Something went wrong when removing source code and tests for commands, got exit code ${exit_code}, aborting strip-graphics script..."
  exit 1
fi

###########################################################
# Remove unnecessary additional non-junit java test files #
###########################################################
echo ""
echo "Removing unnecessary additional files from sys.test..."

# remove these test files
miscJavaTestFilesToRemove=(LayerKmlTestHelper *plot* *Plot* testbeanpipehangbean test_widget_wrapper test_panel_jsetup test_panel_jsetup_no_pane0 test_panel_jsetup_wb test_panel_jsetup_jpanel test_list_jsetup test_dragbox_highres_fft test_jnuanes TestLayerDrawable TestNeXtMidas testprescalerhelper t5pathgenerator)

# Remove source files and other files directly associated with source files with these file extensions
fileExtensionsToRemove=(java class form html)

# Remove all test files having to do with plot with specified file extensions to remove
for javaTestFile in ${miscJavaTestFilesToRemove[@]} ;
do
  for fileExt in ${fileExtensionsToRemove[@]} ;
  do
    rm -f ${testDir}/${javaTestFile}.${fileExt}
  done
done

# Remove these macro tests since they depend on the specified Java files mentioned above
miscMacroTestFilesToRemove=(test_midasbean test_dragbox_highres_fft test_jnuanes test_convert test_datalist_256ascii test_nextmidas)

for macroTestFile in ${miscMacroTestFilesToRemove[@]} ;
do
  rm -f ${testDir}/${macroTestFile}.m*
done

# Remove kml files
kmlFilesToRemove=(kmlLinkTest.kml kmlNetTest* kmlPolyFillTest*.kml kmlStyledDescription.kml kmlTest_bad.kml kmlTestLeft.kml kmlTestNoCoordinates.kml kmlTestRight.kml kmlTestSmall.kml kmz_test.kmz test_linestring.kml test_model.kml test_point.kml)

for kmlFile in ${kmlFilesToRemove[@]} ;
do
  rm -f ${testDir}/${kmlFile}
done

# Remove miscellaneous files in the test area
miscTestFilesToRemove=(chirped_1000.tmp exp*_plot_master.exp map_types_and_data_demo.m* runNativeCodeJar.* staticlayers.tbl *ctrlc*_plot*.m* perf_plot.m* plot_keyobject_setup.tbl test_plot_autoscale*.tmp test_box.m* test_plot_coneflags5000.png test_plot_layerwms_configure.inc test_plot_tblfile.tbl test_sd360_loop.* test_shapefile_getshapesnear.m* test_snapper* test_xcut.png timecodevisual.m*)

for testFile in ${miscTestFilesToRemove[@]} ;
do
  rm -f ${testDir}/${testFile}
done

###################################################################
# Remove unnecessary additional miscellaneous files from mcr area #
###################################################################
echo ""
echo "Removing unnecessary additional files from sys.mcr..."

miscMcrFilesToRemove=(buildopt_libxm.mm checktimekeywords.m* *.txt)

for mcrFile in ${miscMcrFilesToRemove[@]} ;
do
  rm -f ${mcrDir}/${mcrFile}
done

###################################################################
# Remove unnecessary additional miscellaneous files from exp area #
###################################################################
echo ""
echo "Removing unnecessary additional files from sys.exp..."

miscExpFilesToRemove=(checktimekeywords.exp)

for expFile in ${miscExpFilesToRemove[@]} ;
do
  rm -f ${expDir}/${expFile}
done
