@echo off
set windir=%~dp0
set cdfdir=%windir%..

set argC=0
set withinstalldir=0
set installdirt=%cd%
for %%x in (%*) do Set /A argC+=1
if %argC% GTR 0 (
  for %%y in (%*) do (
    if %%y==installdir (
      set withinstalldir=1
    ) else (
      set installdirt=%%y
    )
  )
)

set firstChar=%installdirt:~0,1%
set lastChar=%installdirt:~-1%
set firstChar=%firstChar:"=+%
set lastChar=%lastChar:"=+%
if "%firstChar%"=="+" (
  if "%lastChar%"=="+" (
    set installdir=%installdirt:"=%
  ) else (
    set installdir=%installdirt%
  )
) else (
  set installdir=%installdirt%
)

echo.
echo Making %installdir% directory...
mkdir "%installdir%" 2>NUL

echo.
echo Installing .txt, .bat, .ps1 files...
copy /Y %cdfdir%\CDF*.txt "%installdir%" >NUL
copy /Y %cdfdir%\CHANGES.txt "%installdir%" >NUL
copy /Y %cdfdir%\HOWTO*.txt "%installdir%" >NUL
copy /Y %cdfdir%\Release_notes.txt "%installdir%" >NUL
copy /Y %cdfdir%\Welcome.txt "%installdir%" >NUL
copy /Y %windir%setenv.bat "%installdir%" >NUL
copy /Y %windir%checkleapseconds.bat "%installdir%" >NUL
copy /Y %windir%*.ps1 "%installdir%" >NUL
copy /Y %windir%..\cdfjava\cdftools\CDFToolsDriver.jar "%installdir%" >NUL

echo.
echo Making bin, include, lib directories...
mkdir "%installdir%\bin" "%installdir%\include" "%installdir%\lib" 2>NUL

echo.
echo Installing tool programs...
copy /Y cdf2skt.exe "%installdir%\bin\skeletontable.exe" >NUL
copy /Y cdfcmp.exe "%installdir%\bin\cdfcompare.exe" >NUL
copy /Y cdfcvt.exe "%installdir%\bin\cdfconvert.exe" >NUL
copy /Y cdfdump.exe "%installdir%\bin\cdfdump.exe" >NUL
copy /Y cdfedit.exe "%installdir%\bin\cdfedit.exe" >NUL
copy /Y cdfexport.exe "%installdir%\bin\cdfexport.exe" >NUL
copy /Y cdfinq.exe "%installdir%\bin\cdfinquire.exe" >NUL
copy /Y cdfirsdump.exe "%installdir%\bin\cdfirsdump.exe" >NUL
copy /Y cdfleapsecondsinfo.exe "%installdir%\bin\cdfleapsecondsinfo.exe" >NUL
copy /Y cdfmerge.exe "%installdir%\bin\cdfmerge.exe" >NUL
copy /Y cdfstats.exe "%installdir%\bin\cdfstats.exe" >NUL
copy /Y cdfvalidate.exe "%installdir%\bin\cdfvalidate.exe" >NUL
copy /Y skt2cdf.exe "%installdir%\bin\skeletoncdf.exe" >NUL

echo.
echo Installing libraries, jar files...
copy /Y dllcdf.dll "%installdir%\bin" >NUL
copy /Y %windir%\pdcurses\x64\bin\pdcurses.dll "%installdir%\bin" >NUL
copy /Y dllcdf.lib "%installdir%\lib" >NUL
copy /Y libcdf.lib "%installdir%\lib" >NUL

copy /Y %cdfdir%\cdfjava\classes\cdfjava.jar "%installdir%\lib" >NUL
copy /Y %cdfdir%\cdfjava\classes\cdfjson.jar "%installdir%\lib" >NUL
copy /Y %cdfdir%\cdfjava\classes\cdfml.jar "%installdir%\lib" >NUL
copy /Y %cdfdir%\cdfjava\classes\gson-2.8.6.jar "%installdir%\lib" >NUL
copy /Y %cdfdir%\cdfjava\classes\javax.json-1.0.4.jar "%installdir%\lib" >NUL

echo.
echo Installing include files...
copy /Y %cdfdir%\src\include\cdf.h "%installdir%\include" >NUL
copy /Y %cdfdir%\src\include\cdfconfig.h "%installdir%\include" >NUL
copy /Y %cdfdir%\src\include\cdfdist.h "%installdir%\include" >NUL
copy /Y %cdfdir%\src\include\cdflib.h "%installdir%\include" >NUL
copy /Y %cdfdir%\src\include\cdflib64.h "%installdir%\include" >NUL
copy /Y %cdfdir%\src\include\cdftools.h "%installdir%\include" >NUL
copy /Y %cdfdir%\src\include\cdf.inc "%installdir%\include" >NUL

set cdfhelpdir="%installdir%\lib\cdf\help"

echo.
echo Making help directory...
mkdir "%cdfhelpdir%" 2>NUL

echo.
echo Installing help files...
for /R %cdfdir%\src\help %%f in (*.olh) do copy /Y %%f %cdfhelpdir%\ >NUL
for /R %cdfdir%\src\help %%f in (*.ilh) do copy /Y %%f %cdfhelpdir%\ >NUL

if exist cdfNativeLibrary.dll (
  echo.
  echo Installing jni files...
  copy /Y cdfNativeLibrary.dll "%installdir%\bin" >NUL
  copy /Y cdfNativeLibrary.lib "%installdir%\lib" >NUL
)

if exist dllcdfcsharp.dll (
  echo.
  echo Installing csharp files...
  copy /Y dllcdfcsharp.dll "%installdir%\bin" >NUL
  copy /Y dllcdfcsharp.lib "%installdir%\lib" >NUL
  copy /Y "%windir%\csharp\bin\CDFAPIs.dll" "%installdir%\bin" >NUL
  copy /Y "%windir%\csharp\bin\CDFConstants.dll" "%installdir%\bin" >NUL
  copy /Y "%windir%\csharp\bin\CDFException.dll" "%installdir%\bin" >NUL
  copy /Y "%windir%\csharp\bin\CDFUtils.dll" "%installdir%\bin" >NUL
)
