setlocal enableextensions enabledelayedexpansion
@echo off

@rem check to see if the parameter for the opt path was provided
if  [%1] == [] (
  echo Please provide the directory path of the option tree to be converted
  exit /B 1
)

@rem check to see if the option tree has a build.gradle. Chances are that it has been converted
if not exist "%1" (
  echo The directory path provided does not exist, please check your input
  exit /B 1
)

@rem check to see if the option tree has a build.gradle. Chances are that it has been converted
if exist "%1/build.gradle" (
  echo This directory seems to be converted already, exiting
  exit /B 1
)

@rem Get the name of the option tree
for %%I in (%1) do set optName=%%~nxI
rem echo %optName%

@rem Get the nextmidas directory saved so we can get the default gradle files needed
set nextmidasDir=%cd%
rem echo %nextmidasDir%

rem if relative, convert it to absolute path
set inputPath=%~dp1%optName%
@rem cd into the  
cd %inputPath%
rem echo %cd%

@rem get working directory path
set workingDir=%cd%

@rem get parent directory
cd ..
set parentDir=%cd%
@rem create temporary directory for option tree that is in gradle structure
@rem give option of having outputPath of option tree
set outputPath=%2

if "%outputPath%" == "" set outputPath=%parentDir%\temp\%optName%
echo %outputPath%
rem echo %outputPath%
if not exist %outputPath%\src\main\midas mkdir %outputPath%\src\main\midas
if not exist %outputPath%\src\test\midas mkdir %outputPath%\src\test\midas
if not exist %outputPath%\src\main\resources mkdir %outputPath%\src\main\resources
if not exist %outputPath%\src\test\resources  mkdir %outputPath%\src\test\resources
copy %nextmidasDir%\gradle\%optName%.gradle %outputPath%\build.gradle /Y

@rem copy all directories and files to the gradle directory structure
@rem move up to the direcotry that holds the nxm folder
cd ..

@rem loop through all the FOLDERS to be migrated
for /f  %%G in ('dir /b /a:d "nxm/%optName%"') do (  
  set isResource=false
  echo %%G
  if "%%G"=="cfg"   set isResource=true
  if "%%G"=="dat"   set isResource=true
  if "%%G"=="docs"  set isResource=true
  if "%%G"=="exp"   set isResource=true
  if "%%G"=="help"  set isResource=true
  if "%%G"=="rmif"  set isResource=true
  if !isResource!==true (
    xcopy /y /i /s /e "nxm\%optName%\%%G" "%outputPath%\src\main\resources\nxm\%optName%\%%G"
    xcopy /y /i /s /e "nxm\%optName%\%%G" "%outputPath%\src\test\resources\nxm\%optName%\%%G"
  )
  if %%G==test xcopy /y /i /s /e "nxm\%optName%\%%G" "%outputPath%\src\test\midas\nxm\%optName%\%%G"
  if not !isResource!==true xcopy /y /i /s /e "nxm\%optName%\%%G" "%outputPath%\src\main\midas\nxm\%optName%\%%G"
)
@rem loop through all FILES to be migrated
for /f  %%G in ('dir /b /a-d "nxm/%optName%"') do (  
  copy "nxm\%optName%\%%G"  "%outputPath%\src\main\midas\nxm\%optName%\%%G" /Y
)

pushd %parentDir 
powershell.exe -nologo -noprofile -command "& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::CreateFromDirectory('%inputPath%', '%inputPath%.zip'); }"
popd
echo  %outputPath% %inputPath%
rmdir /S /Q "%inputPath%"
rem echo "xcopy /y/i/s/e %outputPath% %1"
xcopy /y/i/s/e %outputPath% %inputPath%
rmdir /S /Q %parentDir%\temp

@rem if the option tree is not in NMROOT we have to add the external location to the settings.gradle
if %nextmidasDir% == %outputPath%  goto complete
find "include ':%optName%'" %nextmidasDir%\settings.gradle 
if %errorlevel% equ 1 goto add_include
echo "this project is already included"
goto done_include
:add_include
  echo include ':%optName%' >> %nextmidasDir%\settings.gradle 
:done_include
find "project(':%optName%').projectDir = new File('%outputPath:\=/%')" %nextmidasDir%\settings.gradle 
if %errorlevel% equ 1 goto add_dir_to_project
echo "%optName% already has a folder path defined"
goto done_add_dir
:add_dir_to_project
  echo project(':%optName%').projectDir = new File('%inputPath:\=/%') >> %nextmidasDir%\settings.gradle
:done_add_dir

:complete