

# Create patches to copyfiles.bat for QMS builds with gisinternals package
# Create build_routino_add.bat for compiling routino

# Run on x64 Native tools command prompt: "cmake -PCopyFilesGis.cfg"

cmake_policy(SET CMP0011 NEW)

set(PKG QMS)

set(N "\n")
set(BSL \\ )
set(SL / )

message(NOTICE "Using basic user build settings for package ${PKG}.")

# get basic QMS build info
include("QMSUser.cfg" OPTIONAL)

message(NOTICE "Loaded basic QMS user configuration.")

set(OUTF ${QMSDIR}/${QMSSRC}/msvc_64/CopyFilesGis_add.bat)
set(OUTFR ${QMSDIR}/${QMSSRC}/msvc_64/build_routino_add.bat)

message(NOTICE "Output files created:${N}  ${QMSDIR}/${QMSSRC}/msvc_64/CopyFilesGis_add.bat${N}  ${QMSDIR}/${QMSSRC}/msvc_64/build_routino_add.bat")

       
# https://cmake.org/cmake/help/latest/command/string.html     
# https://cmake.org/cmake/help/latest/command/file.html

file(REMOVE ${OUTF})
file(APPEND ${OUTF}  ${N} "rem Environment variable replacements created with CopyFilesGis.cfg using QMSUser.cfg" ${N}${N})

file(REMOVE ${OUTFR})
file(APPEND ${OUTFR} ${N} "rem Environment variable replacements created with CopyFilesGis.cfg using QMSUser.cfg" ${N}${N})

# adjust environment to gisinternals package config
set(VARS

"set QMSI_QMS_PATH=${QMSDIR}"
"set QMSI_GIS_PATH=${QMSDIR}/gisinternals/${GISINTERNALS_VER}_${GISDATE}/release-${GISINTERNALS_VER}-x64"

"set QMSI_QT_PATH=\"${QTBASE}/${QT5_VER}/msvc${QT_MSVC64_VER}\""
"set QMSI_VCREDIST_PATH=\"c:\\Program Files\\Microsoft Visual Studio\\${MSVC_YEAR}\\Community\\VC\\Redist\\MSVC\\${MSVC_VER}\""
"set QMSI_ROUT_PATH=\"${QMSDIR}\\routino/routino-${ROUTINO_VER}\\install\""

# DLLs from here are necessary for planetsplitter.exe
"set QMSI_MGW6_PATH=\"${MSYS2_DIR}\\ucrt64\\bin\""
"set QMSI_QUAZIP_PATH=\"${QMSDIR}/quazip/quazip-${QUAZIP_VER}/install\""
"set QMSI_BUILD_PATH=\"${QMSDIR}\\${QMSSRC}\\build\""
)

# adjusted copy statements added to end of copyfiles.bat
set(VARSX

" "
"cd Files"
"mkdir data"
"xcopy %QMSI_GIS_PATH%\\bin\\gdal-data data /s /i"
"copy  %QMSI_GIS_PATH%\\bin\\proj_${PROJ_VER}_${PROJ_SUBVER}.dll"
"xcopy %QMSI_GIS_PATH%\\bin\\*.dll /I /EXCLUDE:..\\gisexclude.txt"

##################### not yet copied - might be missing! Needs additional entry in basic config!
# d:\QtProjects\QMS\lzma\xz-5.4.0\install\bin\liblzma.dll

"mkdir gdalplugins"
"xcopy %QMSI_GIS_PATH%\\bin\\gdal\\plugins gdalplugins /s /i /EXCLUDE:..\\gisexclude.txt"

"copy %QMSI_GIS_PATH%\\bin\\curl-ca-bundle.crt"
"copy %QMSI_GIS_PATH%\\bin\\gdal\\apps\\*.exe"
"copy %QMSI_GIS_PATH%\\bin\\proj${PROJ_VER}\\apps\\*.exe"
"copy %QMSI_GIS_PATH%\\bin\\curl.exe"
"copy %QMSI_GIS_PATH%\\bin\\openssl.exe"
"copy %QMSI_GIS_PATH%\\bin\\sqlite3.exe"

"mkdir share\\proj"

#################### needs more files from original folder, possibly from proj-data-1.11.tar.gz, too.
"xcopy %QMSI_GIS_PATH%\\bin\\proj${PROJ_VER}\\share share\\proj /s /i"

)

# routino replacements:
set(VARSR

"set ROUT_SRC_PATH=\"${QMSDIR}\\routino\\routino-${ROUTINO_VER}\""
"set ROUT_PKG_PATH=\"${QMSDIR}\\routino\\routino-${ROUTINO_VER}\\install\""

"set PATH=${MSYS2_DIR}\\ucrt64\\bin\;%PATH%"
)

foreach(X ${VARS})
    string(REPLACE ${SL} ${BSL} H ${X})
    file(APPEND ${OUTF} "${H}" ${N})
endforeach()

foreach(X ${VARSX})
    file(APPEND ${OUTF} "${X}" ${N})
endforeach()   

foreach(X ${VARSR})
    file(APPEND ${OUTFR} "${X}" ${N})
endforeach()  

message(NOTICE "End of run.")