#!/bin/bash
######################################################################################
#
# This script removes the source code and test cases associated with
# commands removed from the LIBNXM version of commands.cnf with !-
#
# Author: Ryan McCall
# Since:  NeXtMidas 4.1.3
# Note:   Experimental version first appeared in NeXtMidas 4.0.0
#         and was in NMROOT/swap/thinNXM/remove_cmds_test_cases.sh
#
######################################################################################

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
primDir=${NMROOT}/nxm/sys/prim
intrDir=${NMROOT}/nxm/sys/intr
mcrDir=${NMROOT}/nxm/sys/mcr
hlpDir=${NMROOT}/nxm/sys/hlp
expDir=${NMROOT}/nxm/sys/exp

# grep:
#     ^ = Start of line
#     !- = Regex to search for (! may need to be escaped)
# sed:
#     s/regex/replace/options
#     -e match following regular expression (can use multiple)
#     ^!\- = Start of line regex - same as grep\
#     \(...\) = Add contents to stack
#     [^\s]* = Match consecutive non-whitespace characters
#     \s.* = Match 1 whitespace character and remainder of the line
#     ^!\-\([^\s]*\)\s.* = Match start of line followed by !- and then followed by any number of non-whitespace
#                         characters, which are pushed to a stack, and then followed by a whitespace and any
#                                         other characters
#     \1 = Pop 1st element in regex stack
#     s/\*// = Replace all '*' characters with ''
# tr:
#     'A-Z' match uppercase
#     'a-z' replace with lowercase
#
# Note: Each each removed command is resulting in two entries in the array (command name portion and rest of line)
# Until this is addressed, this script uses 'index' to skip over every second entry

cmdList=$(grep '^!-' ${cmdsCnf} | sed -e 's/^!\-\([^\s]*\)\s.*/\1/' -e 's/\*//g' | tr 'A-Z' 'a-z')

echo ""
echo "Removing Source code and tests for commands associated with lib-only commands.cnf..."
declare -i index=0
for cmd in $(echo ${cmdList}) ; do
  if [ ${index} -eq 0 ] ; then
    capitalizedCMD=${cmd^}
    rm -f ${testDir}/test_${cmd}_*.m*
    rm -f ${testDir}/test_${cmd}.m*
    rm -f ${primDir}/${cmd}.*
    rm -f ${primDir}/${cmd}\$*
    rm -f ${intrDir}/${capitalizedCMD}.*
    rm -f ${intrDir}/${capitalizedCMD}\$*
    rm -f ${mcrDir}/${cmd}.*
    rm -f ${mcrDir}/${cmd}_*
    rm -f ${hlpDir}/${cmd}.*
    rm -f ${expDir}/${cmd}.*
  fi
  index=(${index}+1)%2
done
