#!/bin/bash
######################################################################################
#
# Unittest.java has nxm.sys.test and JUnit dependencies.
# This moves Unittest.java from nxm.sys.intr to nxm.sys.test.junit
# and updates its package and imports accordingly
#
# Author: Zachary Matthews
# Since:  NeXtMidas 4.1.3
# Note:   Experimental version first appeared in NeXtMidas 4.0.0
#         and was in NMROOT/swap/unittest/unittest_move.sh
#
######################################################################################

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

intrDir=${NMROOT}/nxm/sys/intr
junitDir=${NMROOT}/nxm/sys/test/junit
gradleDir=${NMROOT}/gradle

if [ ! -f ${junitDir}/Unittest.java ] ; then
  echo ""
  echo "Moving original Unittest class into nxm.sys.test.junit ...."
  mv ${intrDir}/Unittest.java ${junitDir}/
  sed -i 's/package nxm\.sys\.intr;/package nxm\.sys\.test.junit;/' ${junitDir}/Unittest.java
  sed -i 's/import nxm\.sys\.test\.junit\.NMTestCase;/\/\/import nxm\.sys\.test\.junit\.NMTestCase;/' ${junitDir}/Unittest.java
  cp ${unittest_script_dir}/Unittest.java.txt ${intrDir}/Unittest.java
  rm -f ${intrDir}/Unittest*.class
  sed -i 's/\(.*\)\(it\.path\.contains(\"NmClassLoader\.java\") || \)\(.*\)\( && !it\.path\.contains(\"NMTestCase\")\)\(.*\)/\1\3\5/' ${gradleDir}/common.gradle
  sed -i 's/\(((\)\(.*\)\(it\.path\.contains(\"NMTestCase\.java\")) || \)\(.*\)/(\2\4/' ${gradleDir}/common.gradle
else
  echo ""
  echo "Moving original Unittest class back into nxm.sys.intr ...."
  mv ${junitDir}/Unittest.java ${intrDir}/
  sed -i 's/package nxm\.sys\.test\.junit;/package nxm\.sys\.intr;/' ${intrDir}/Unittest.java
  sed -i 's/\/\/import nxm\.sys\.test\.junit\.NMTestCase;/import nxm\.sys\.test\.junit\.NMTestCase;/' ${intrDir}/Unittest.java
  rm -f ${junitDir}/Unittest*.class ${intrDir}/Unittest*.class
  sed -i 's/\(.*\)\(File\.separator+\"test\") || \)\(.*\)\(File\.separator+\"bin\"))\)\(.*\)/\1\2it\.path\.contains(\"NmClassLoader\.java\") || \3\4 \&\& !it\.path\.contains(\"NMTestCase\")\5/' ${gradleDir}/common.gradle
  sed -i 's/\((\)\(.*\)\(File\.separator+\"nmopts\") || \)\(.*\)/((\2\3it\.path\.contains(\"NMTestCase\.java\")) || \4/' ${gradleDir}/common.gradle
fi
