#!/bin/bash
# This will create soft links to the original code instead of moving it. Since 3.9.3
(
# 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 incase a revert is needed
mkdir backuplegacy
# Link the files needed for gradle build to root project directlry
ln -s gradle/build.gradle
ln -s gradle/common.gradle
ln -s gradle/settings.gradle
ln -s gradle/gradle.properties
mv ./.classpath gradle/.classpath.orig
cp gradle/.classpathlinks ./.classpath
cp -r .settings backuplegacy
cp .classpath backuplegacy
for optDir in nxm/* ; do
  optName=$(basename "${optDir}")
  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/nxm/${optName}
  mkdir -p ${optName}/src/test/midas/nxm/${optName}
  mkdir -p ${optName}/src/main/resources/nxm/${optName}
  mkdir -p ${optName}/src/test/resources/nxm/${optName}
  # We can link gradle files to the source
  ln -s ../gradle/${optName}.gradle ${optName}/build.gradle
  # copy all directories/files to the gradle directory structure destination
  for optSubDir in ${optDir}/* ; do
    baseDirName=$(basename "$optSubDir")
    if [ "${optSubDir}" == "${optDir}/test" ]; then
      # copy all test files to src/test/midas
      ln -s ../../../../../../${optSubDir} ${optName}/src/test/midas/${optSubDir}
      if [ "${optName}" == "sys" ]; then
        mkdir -p ${optName}/src/main/midas/${optSubDir}/junit
        # Need the one test file in src/main/midas in order to run unittest from the jar
        ln -s ../../../../../../../../${optSubDir}/junit/NMTestCase.java ${optName}/src/main/midas/${optSubDir}/junit
      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
      ln -s ../../../../../../${optSubDir} ${optName}/src/main/resources/${optSubDir}
      ln -s ../../../../../../${optSubDir} ${optName}/src/test/resources/${optSubDir}
    elif [ "${baseDirName}" == 'jars' ]; then
      ln -s ../../../../../../${optSubDir} ${optName}/src/main/midas/${optSubDir}
    else
      # These are the main java files, they go to src/main/midas
      ln -s ../../../../../../${optSubDir} ${optName}/src/main/midas/${optSubDir}
    fi
  done
  if [ "${optName}" == "sys" ]; then
    # Need to copy the tools folder into sys
    ln -s ../../../../tools ${optName}/src/main/midas
  fi
  mkdir backuplegacy/${optName}
  cp -r ${optSubDir} backuplegacy/${optName}
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
)