#!/bin/bash
(
# get command line args
for i in "$@"
do
case $i in
    --softlink|--sl|-softlink|-sl) softlink=true ;;
    --jars) includejars=true ;;
    *) echo "Option $1 not recognized" ;;
esac
shift
done

# Get into the proper directory to run the commands
if [[ -z "${NMROOT}" ]]; then
  echo "WARNING: NMROOT is not defined, using script working directory"
  NMROOT=$(dirname "$0")
  echo $NMROOT
fi
cd $NMROOT

# Make backup directory in case a revert is needed
mkdir backuplegacy

# Link the files needed for gradle build to root project directly
if [ $softlink ]; then 
  ln -s gradle/build.gradle      build.gradle
  ln -s gradle/common.gradle     common.gradle
  ln -s gradle/settings.gradle   settings.gradle
  ln -s gradle/gradle.properties gradle.properties
else
  # or copy all the files
  cp gradle/build.gradle .
  cp gradle/common.gradle .
  cp gradle/settings.gradle .
  cp gradle/gradle.properties .
fi 
mv .project backuplegacy
cp -r .settings backuplegacy
cp .classpath backuplegacy
for d in nxm/* ; do
  optName=$(basename "$d")
  if [ "${optName}" == "xbc" ]; then
    tar -cf xbc.tar nxm/${optName}
    continue
  fi
  if [ "${optName}" == "*" ]; then
    continue
  fi
  # Make the directories
  mkdir -p "${optName}/src/main/midas"
  mkdir -p "${optName}/src/test/midas"
  mkdir -p "${optName}/src/main/resources"
  mkdir -p "${optName}/src/test/resources"
  # We can link gradle files to the source
  if [ $softlink ]; then 
    ln -s ${NMROOT}/gradle/${optName}.gradle ${optName}/build.gradle
  else
    cp gradle/${optName}.gradle ${optName}/build.gradle
  fi
  # cp "gradle/${optName}.gradle" "${optName}/build.gradle"
  # copy all directories/files to the gradle directory structure destination
  for optSubDir in $d/* ; do
    baseDirName=$(basename "$optSubDir")
    if [ "${optSubDir}" == "${d}/test" ]; then
      # copy all test files to src/test/midas
      cp -r --parents "${optSubDir}" "$optName/src/test/midas"
      if [ "${optName}" == "sys" ]; then
        # Need the one test file in src/main/midas in order to run unittest from the jar
        cp --parents "${d}/test/junit/NMTestCase.java" "$optName/src/main/midas"
      fi
    elif [ "${baseDirName}" == 'cfg' ] || [ "${baseDirName}" == 'dat' ] || [ "${baseDirName}" == 'docs' ] || [ "${baseDirName}" == 'exp' ] || [ "${baseDirName}" == 'help' ] || [ "${baseDirName}" == 'rmif' ]; then
      # all of these files need to go to src/main/resources and src/test/resoures so they are available for both tests and regular use
      cp -r --parents "${optSubDir}" "$optName/src/main/resources"
      cp -r --parents "${optSubDir}" "$optName/src/test/resources" 
    elif [ "${baseDirName}" == 'jars' ]; then
      if [ "$includejars" == "true" ]; then
        cp -r --parents "${optSubDir}" "$optName/src/main/midas"
      elif [ "${optName}" == "sys" ]; then
        cp -r --parents "${optSubDir}/wbp-support.jar" "$optName/src/main/midas"
        cp -r --parents "${optSubDir}/bcel-6.9.0.jar" "$optName/src/main/midas"
        cp -r --parents "${optSubDir}/commons-lang3-3.14.0.jar" "$optName/src/main/midas"
      elif [ "${optName}" == "audio" ]; then
        cp -r --parents "${optSubDir}/jspeex.jar" "$optName/src/main/midas"
      fi
      mkdir backuplegacy/$optName
      cp -r ${optSubDir} backuplegacy/${optName}
    else
      # These are the main java files, they go to src/main/midas
      cp -r --parents "${optSubDir}" "$optName/src/main/midas"
    fi
  done
  if [ "${optName}" == "sys" ]; then
    # Need to copy the tools folder into sys
    cp -r --parents "tools" "$optName/src/main/midas"
  fi
done

# This will remove the nxm folder from git tracking if that is being used. This is for development purposes only
if [ -d ".git" ]; then
  git ls-files -z nxm/ | xargs -0 git update-index --assume-unchanged
fi

# tar up the current nxm folder for backup and then remove it.
# tar -cf backupnxm.tar nxm/xbc && rm -r nxm
# tar -cf backupnxm.tar nxm
rm -r nxm

# build all the code
# gradle wrapper --gradle-version 4.10
# ./gradlew build  -x test
# ./gradlew eclipse
gradle build -x test
gradle eclipse
)
