@echo off
setlocal enabledelayedexpansion
cd %NMROOT%
@rem Copy the files needed for gradle build to root project
@rem In bash, this uses link instead of copying them.
@rem to make a link in windows, the user has to be admin
copy gradle\build.gradle build.gradle /Y
copy gradle\common.gradle common.gradle /Y
copy gradle\settings.gradle settings.gradle /Y
copy gradle\gradle.properties gradle.properties /Y

if not exist backuplegacy mkdir backuplegacy
copy .classpath backuplegacy /Y
xcopy /E /I /Y  .settings backuplegacy\.settings

if exist .project move .project backuplegacy
for /f  %%G in ('dir /b /a:d "nxm"') do (
  if /I "%%G" == "" ( set cont=false )
  if /I %%G == xbc (
    move nxm\%%G %cd%
    tar -cf xbc.tar %%G
    rmdir /s /q %%G
    set cont=false
  )
  if not defined cont (set cont=true)
  @rem This could be change to just check if cont is defined. If it is not defined, we need to run the conversion.
  if /I !cont! == true (
    @rem Make the directories
    if not exist %%G\src\main\midas mkdir %%G\src\main\midas
    if not exist %%G\src\test\midas mkdir %%G\src\test\midas
    if not exist %%G\src\main\resources mkdir %%G\src\main\resources
    if not exist %%G\src\test\resources  mkdir %%G\src\test\resources
    copy %cd%\gradle\%%G.gradle %cd%\%%G\build.gradle /Y
    @rem copy all directories/files to the gradle directory structure destination
    for /f %%e in ('dir /b /a:d "nxm/%%G"') do (
      set result=false
      @rem Check to see if these are resource direcotries
      if "%%e" == "cfg"  set result=true
      if "%%e" == "dat"  set result=true
      if "%%e" == "docs" set result=true
      if "%%e" == "exp"  set result=true
      if "%%e" == "help" set result=true
      if "%%e" == "rmif" set result=true
      if  !result! == true (
        @rem copy the directories to test resources and main resources
        xcopy /y /i /s /e nxm\%%G\%%e %%G\src\main\resources\nxm\%%G\%%e
        xcopy /y /i /s /e nxm\%%G\%%e %%G\src\test\resources\nxm\%%G\%%e
      )
      if !result! == false (
        if %%e == test (
          set result=true
          @rem These files need to go to src/test/midas
          xcopy /y /i /s /e nxm\%%G\%%e %%G\src\test\midas\nxm\%%G\%%e
          if %%G == sys  (
            @rem This file is needed in src/main/midas
            xcopy /y nxm\sys\test\junit\NMTestCase.java sys\src\main\midas\nxm\sys\test\junit\
          )
        )
      )
      if !result! == false (
        @rem These files need to go to src/main/midas
        xcopy /y /i /s /e nxm\%%G\%%e %%G\src\main\midas\nxm\%%G\%%e
      )
    )
    @rem move the files that are not in directories over. These are most like resources so I will put them in the resources folder for now. May need to expand on this
    @rem example is version.txt
    for /f %%e in ('dir /b /a-d "nxm/%%G"') do (
      copy nxm\%%G\%%e %%G\src\main\resources\nxm\%%G\%%e /Y
    )
    if %%G == sys (
      @rem Need to copy the tools folder into sys
      rem for /f %%H in ('dir /b /s  "tools"') do xcopy %%e "%%G\src\main\midas" /Y
      xcopy /y /i /s /e "tools" "%%G\src\main\midas\tools"
    )
  )
)

rmdir /s /q nxm

@rem gradle wrapper --gradle-version 4.10
@rem gradlew build -x test
@rem gradlew eclipse