@ECHO off 
setlocal enableextensions enabledelayedexpansion
REM Set input variables
SET DOCU_BASEDIR=%1
SET TARGET_PLATFORM=%2
SET TARGET_LANGUAGE=%3

REM Set documentation base dir if not passed in
IF [%1] NEQ [] ] GOTO CONTINUE

SET SCRIPT_DIR=%~dp0
SET DOCU_BASEDIR=%SCRIPT_DIR%..\Docs\Vision

IF NOT EXIST "%DOCU_BASEDIR%" (
    ECHO.
    ECHO ERROR:
    ECHO Please put the script into the 'setup' subdirectory of the SDK root and run it from there, or pass the Vision documentation basedir as a parameter! 
    SET ERRORLEVEL=1
    GOTO exitError
)

:CONTINUE
IF [%2] EQU [] SET TARGET_PLATFORM=ios
IF [%3] EQU [] SET TARGET_LANGUAGE=english

REM make lowercase
CALL :toLower TARGET_PLATFORM
CALL :toLower TARGET_LANGUAGE

REM Trim quotes around the input
FOR /f "useback tokens=*" %%a IN ('%DOCU_BASEDIR%') DO SET DOCU_BASEDIR=%%~a
FOR /f "useback tokens=*" %%a IN ('%TARGET_PLATFORM%') DO SET TARGET_PLATFORM=%%~a
FOR /f "useback tokens=*" %%a IN ('%TARGET_LANGUAGE%') DO SET TARGET_LANGUAGE=%%~a

REM Remove trailing backslash or slash
IF %DOCU_BASEDIR:~-1%==\ SET DOCU_BASEDIR=%DOCU_BASEDIR:~0,-1%
IF %DOCU_BASEDIR:~-1%==/ SET DOCU_BASEDIR=%DOCU_BASEDIR:~0,-1%

REM Get Windows major version
FOR /f "tokens=2 delims=[]" %%x IN ('ver') DO SET WINVER=%%x
SET WINVER=%WINVER:Version =%
FOR /f "tokens=1 delims=." %%x IN ("%WINVER%") DO SET WINVERMAJOR=%%x
IF "%WINVERMAJOR%" lss "6" (GOTO WINXP) ELSE (GOTO WINVISTAPLUS)

REM ===================================================================================================================
REM On Windows XP, we can't set symbolic links, so we have to copy
:WINXP
ECHO This is a Windows XP system - we have to copy the manuals
GOTO performCopy

REM ===================================================================================================================
REM On Windows Vista and later, symbolic links are available, so we can save disk space
:WINVISTAPLUS
ECHO This is a Windows Vista or later system - we can create symbolic links to the manuals
GOTO performCopy

REM ===================================================================================================================

:performCopy


call:copyFile "windows\VisionEngine_Introduction.chm" "ios\VisionEngine_Introduction.chm" "Windows English"

call:copyFile "windows\VisionEngine_StartupGuide.chm" "ios\VisionEngine_StartupGuide.chm" "Windows English"

call:copyFile "windows\vForgeAPI.chm" "ios\vForgeAPI.chm" "Windows English"

call:copyFile "windows\VisionEngine_Common.chm" "ios\VisionEngine_Common.chm" "Windows English"

call:copyFile "windows\VisionEngine_Tutorials.chm" "ios\VisionEngine_Tutorials.chm" "Windows English"

call:copyFile "windows\Geom2API.chm" "ios\Geom2API.chm" "Windows English"

call:copyFile "windows\ScriptingAPI.chm" "ios\ScriptingAPI.chm" "Windows English"

call:copyFile "windows\HavokSdk_ProgrammersManual.chm" "ios\HavokSdk_ProgrammersManual.chm" "Windows English"

call:copyFile "windows\VisionEngine_ReleaseNotes.chm" "ios\VisionEngine_ReleaseNotes.chm" "Windows English"

call:copyFile "windows\VisionEngine_ArtistManual.chm" "ios\VisionEngine_ArtistManual.chm" "Windows English"


GOTO exitSuccess

REM ===================================================================================================================

:exitError
ECHO.
ECHO Setup was not successful.
ECHO.
REM uncomment this to keep the log on screen when run manually
REM pause
exit %ERRORLEVEL%

:exitSuccess
ECHO.
ECHO Setup was successful.
ECHO.
exit 0

:toLower
:: Convert contents of a variable to lower case.
FOR %%i IN ("A=a" "B=b" "C=c" "D=d" "E=e" "F=f" "G=g" "H=h" "I=i" "J=j" "K=k" "L=l" "M=m" "N=n" "O=o" "P=p" "Q=q" "R=r" "S=s" "T=t" "U=u" "V=v" "W=w" "X=x" "Y=y" "Z=z") DO CALL SET "%1=%%%1:%%~i%%"
GOTO:EOF


:copyFile
:: Copy or link a documentation file from the file it's depending on
REM Check if the source file exists
SET SOURCE_FILE=%~1
SET TARGET_FILE=%~2
SET SOURCE_PACKAGE=%~3

IF NOT EXIST "%DOCU_BASEDIR%\%SOURCE_FILE%" call:FILE_NOT_FOUND "%SOURCE_PACKAGE%"

IF "%WINVERMAJOR%" lss "6" ( GOTO WINXPCOPY )

:WINVISTAPLUSLINK
REM mklink fails if the file already exists.
IF EXIST "%DOCU_BASEDIR%\%TARGET_FILE%" del "%DOCU_BASEDIR%\%TARGET_FILE%"
mklink "%DOCU_BASEDIR%\%TARGET_FILE%" "%DOCU_BASEDIR%\%SOURCE_FILE%"
IF %ERRORLEVEL% NEQ 0 ( 
  ECHO.
  ECHO Linking the documentation file failed. Falling back to copying.
  ECHO.
  GOTO WINXPCOPY
) ELSE (
  GOTO:EOF
)  

:WINXPCOPY
REM if mklink failed, it still retains a handle on the file. by deleting it,
REM we free that handle as well.
IF EXIST "%DOCU_BASEDIR%\%TARGET_FILE%" del "%DOCU_BASEDIR%\%TARGET_FILE%"
xcopy /Y /f "%DOCU_BASEDIR%\%SOURCE_FILE%" "%DOCU_BASEDIR%\%TARGET_FILE%*"
IF %ERRORLEVEL% NEQ 0 ( 
  ECHO.
  ECHO Copying the documentation file failed. Exiting.
  ECHO.
  GOTO exitError 
) ELSE (
  GOTO:EOF
)

:FILE_NOT_FOUND
SET SOURCE_PACKAGE=%~1
ECHO.
ECHO ERROR:
ECHO Please install the %SOURCE_PACKAGE% documentation package and run the installer again to complete the setup!
SET ERRORLEVEL=1
GOTO exitError