#!/bin/bash
#######################################################################################
# This version allows (... hopefully ...) an external option tree to be converted
# even if the script is outside NMROOT and with a non-standard option tree structure.
# This script assumes NMROOT is defined, but will use the script directory as a backup
# arguments:
#   $1 - <location on disk>/nxm/optiontree OR <location on disk>/optiontree
# TODO - add option for output directory.
#######################################################################################

#######################################################################################
# Check to make sure Option Tree path was specified AND is not already converted
#######################################################################################
if [[ $# -eq 0 ]] ; then
    echo "please pass the Option Tree path for conversion ex: /home/user/optiontrees/nxm/<option_tree>"
    exit 1
fi
if [ -f $1/build.gradle ]; then
    echo "This directory seems to be converted already, exiting"
    exit 1
fi

# Save off `pwd` so we can end up back there
original_pwd=`pwd`
echo "Original pwd ${original_pwd}"

#######################################################################################
# Check if NMROOT is defined ... If not WARN and set to script working directory
#######################################################################################
if [[ -z "${NMROOT}" ]]; then
  echo "WARNING: NMROOT is not defined, using script working directory"
  NMROOT=$(dirname "$0")
  echo $NMROOT
fi

#######################################################################################
# Get the Option Tree directory, full path, & parent directory.
# Set up temporary output directory
#######################################################################################
# Use basename to grab the option tree name / folder (not full path)
optName=$(basename "$1")
echo "Converting option tree: ${optName}"

# use readlink to get the full path
# -m --canonicalize-missing (follow every symlink and do not require existence)
optPath=$(readlink -m $1)
echo "Actual full path to Option tree: ${optPath}"

# get parent directory of option tree directory
parentDir="$(dirname $optPath)"
echo "Parent directory of option tree: ${parentDir}"

# get name of parent directory (not full path)
parentName=$(basename "$parentDir")
echo "Name of Parent directory of option tree: ${parentName}"

# make temporary output path
outputPath="${parentDir}/temp/${optName}"
echo "Intermediate Output path of new option tree = ${outputPath}"

###############################################################################################
# Go to the option tree and create gradle structure (in temporary location)
###############################################################################################
cd $optPath
# Create output directory and make parents as needed
mkdir -p "$outputPath/src/main/midas"
mkdir -p "$outputPath/src/test/midas"
mkdir -p "$outputPath/src/main/resources"
mkdir -p "$outputPath/src/test/resources"

#  Get to the level of the parent of the parent (usually nxm) folder.
# The standard structure of the option tree location is /path/to/folder/nxm/optname
cd ../../

# Copy source code to correct gradle structure location
parentNXM=`pwd`
echo "Now working in parent of nxm or equivalent: ${parentNXM} and copying over source code"
for optSubDir in $parentName/$optName/* ; do
  baseDirName=$(basename "$optSubDir")
  if [ "$baseDirName" = "test" ]; then
    cp -r --parents "${optSubDir}" "$outputPath/src/test/midas"
  elif [ "${baseDirName}" = 'cfg' ] || [ "${baseDirName}" = 'dat' ] || [ "${baseDirName}" = 'docs' ] || [ "${baseDirName}" = 'exp' ] || [ "${baseDirName}" = 'help' ] || [ "${baseDirName}" = 'rmif' ]; then
    cp -r --parents "${optSubDir}" "$outputPath/src/main/resources"
  else
    cp -r --parents "${optSubDir}" "$outputPath/src/main/midas"
  fi
done

# Copy from $NMROOT needed gradle files
echo "Copying over build.gradle to ${outputPath}/build.gradle"
cp "$NMROOT/gradle/generic.gradle" "${outputPath}/build.gradle"
cp "$NMROOT/gradle/settings.gradle" "${parentDir}"
cp "$NMROOT/gradle/common.gradle" "${parentDir}"
cp "$NMROOT/gradle/build.gradle" "${parentDir}"
echo "${optName}.version=0.0.0" > "${parentDir}/gradle.properties"

###############################################################################################
# Now tar original as backup and move gradle structure to proper location
###############################################################################################
echo "Copy to temporary structure is complete, backing up and deleting original"

# tar the original code as a backup and delete the original code
cd $parentName
tar -cf ${optName}.tar $optName
if [ ! $? -eq 0 ]; then 
  echo "unable to backup the previous codebase, exiting"
  exit 1
fi
rm -r $optPath
# move the temp code to the new location
mv $outputPath $optPath
rm -r "${parentDir}/temp"

# handle the non-standard directory structure that is not <location on disk>/nxm/optiontree
# get subdirectory name to be nxm like it should be
if [ "$parentName" != "nxm" ]; then
  echo "Making final adjustment for non-standard directory structure that was not <location on disk>/nxm/optiontree"
  wrongMain=$optPath/src/main/midas/$parentName
  goodMain=$optPath/src/main/midas/nxm
  wrongTest=$optPath/src/test/midas/$parentName
  goodTest=$optPath/src/test/midas/nxm
  wrongResources=$optPath/src/main/resources/$parentName
  goodResources=$optPath/src/main/resources/nxm
  if [[ -d $wrongMain ]]; then
    echo "moving $wrongMain to $goodMain"
    mv $wrongMain $goodMain
  fi
  if [[ -d $wrongTest ]]; then
    echo "moving $wrongTest to $goodTest"
    mv $wrongTest $goodTest
  fi
  if [[ -d $wrongResources ]]; then
    echo "moving $wrongResources to $goodResources"
    mv $wrongResources $goodResources
  fi
fi

echo "***********Gradle conversion is complete************"




