:: Copyright (c) 2009, SAS Institute Inc., Cary, N.C., USA
:: All rights reserved.

@echo off

setlocal enabledelayedexpansion

set updateserver=true
set updateclient=false

call :findtemp
if not "%ERRORLEVEL%"=="0" exit /b %ERRORLEVEL%

echo.
call :userprompt
if not "%ERRORLEVEL%"=="0" exit /b %ERRORLEVEL%
echo.

call :findsasroot
if not "%ERRORLEVEL%"=="0" exit /b %ERRORLEVEL%

call :finddepotroot
if not "%ERRORLEVEL%"=="0" exit /b %ERRORLEVEL%

call :isrequired
if not "%ERRORLEVEL%"=="0" exit /b %ERRORLEVEL%

if "%type%"=="depot" (
   call :findjava
   if not "%ERRORLEVEL%"=="0" exit /b %ERRORLEVEL%

   echo Extracting the Java Runtime Environment...
   call :extractjre
   if not "%ERRORLEVEL%"=="0" exit /b %ERRORLEVEL%

   echo Preparing the SAS Deployment Registry...
   call :initregistry
   if not "%ERRORLEVEL%"=="0" exit /b %ERRORLEVEL%

   echo Populating the SAS Deployment Registry...
   call :updateregistry
   if not "%ERRORLEVEL%"=="0" exit /b %ERRORLEVEL%

   echo Generating SAS Software Configuration File...
   call :sasswconfig
   if not "%ERRORLEVEL%"=="0" exit /b %ERRORLEVEL%
)

if "%type%"=="sashome" (
   echo Populating the SAS Deployment Registry...
   call :updateregistry
   if not "%ERRORLEVEL%"=="0" exit /b %ERRORLEVEL%
)

echo.
echo Done.

exit /b 0

:findtemp
   set tempdir=

   if "%tmp%"=="" (
      echo Unable to secure a temporary work directory on account of TMP being undefined
      exit /b -1
   )

   for /l %%a in (1,1,99) do (
      set tempdir=%tmp%\temp_%%a
      if not exist "!tempdir!" exit /b 0

      set tempdir=
   )

   echo Unable to secure a temporary work directory within TMP
   exit /b -1

:userprompt
   set type=
   set root=

   if "%updateserver%"=="true" if "%updateclient%"=="true" (
      echo Enter the root of your SAS Software Depot for Client Users or Client SAS Home.
   )

   if "%updateserver%"=="true" if not "%updateclient%"=="true" (
      echo Enter the root of your SAS Software Depot for Client Users.
   )

   if not "%updateserver%"=="true" if "%updateclient%"=="true" (
      echo Enter the root of your Client SAS Home.
   )

   echo To exit this utility, type 'Q' followed by the Enter key.
   echo.
   echo    For example:

   if "%updateserver%"=="true" (
      echo       C:\SAS Software Depot
      echo       \\machine\share\SAS Software Depot
   )

   if "%updateclient%"=="true" (
      echo       C:\Program Files\SAS
   )

   :userprompt_prompt
      echo.

      set /p root=">>> "

      set root=%root:"=%

      if "%root%"=="" goto :userprompt_prompt

      if "%root%"=="q" exit /b -1
      if "%root%"=="Q" exit /b -1

      for /f "tokens=* delims= " %%a in ("%root%\") do set root=%%~fa
      set root=%root:~0,-1%

      if "%updateserver%"=="true" (
         if exist "%root%\sassd.txt" (
            if exist "%root%\sassw.config" (
               echo This script has already been run against the specified SAS Software Depot

               exit /b -1
            )

            set type=depot

            exit /b 0
         )
      )

      if "%updateclient%"=="true" (
         if not exist "%root%\sassd.txt" if exist "%root%\sassw.config" (
            set type=sashome

            exit /b 0
         )
      )

      echo The location specified is not valid
      goto :userprompt_prompt

:finddepotroot
   set depotroot= 

   if exist "%root%\sassd.txt" (
      set depotroot=%root%

      exit /b 0
   )

   if exist "%root%\SASFoundation\9.2\nls\en\SASV9.CFG" (
      for /f "usebackq tokens=1,2,* delims= " %%a in ("%root%\SASFoundation\9.2\nls\en\SASV9.CFG") do (
         if "%%a"=="-SET" if "%%b"=="sasroot" (
            set depotroot=%%~fc
            for /f "tokens=* delims= " %%a in ("!depotroot!\..\..\..") do set depotroot=%%~fa

            if exist "!depotroot!" exit /b 0

            set depotroot=
            echo Unable to access the SAS Software Depot for Client Users
            exit /b -1
         )
      )
   )

   echo Unable to locate the SAS Software Depot root
   exit /b -1

:findsasroot
   set sasroot=

   if exist "%root%\sassd.txt" (
      for /f %%a in ('dir "%root%\products\sasinstall__92210__*" /b') do (
         set sasroot=%root%\products\%%a\sas

         exit /b 0
      )
   )

   if exist "%root%\SASFoundation\9.2" (
      set sasroot=%root%\SASFoundation\9.2

      exit /b 0
   )

   echo Unable to locate SASROOT
   exit /b -1

:isrequired
   find "client" "%depotroot%\sassd.txt" >nul
   if not "%ERRORLEVEL%"=="0" (
      echo This is not a Client SAS Deployment
      exit /b -1
   )

   if not exist "%depotroot%\products\sasinstall__92210__*" (
      echo The directory specified does not contain SAS Foundation 9.21
      exit /b -1
   )

   if "%type%"=="sashome" if not exist "%depotroot%\sassw.config" (
      echo Please update your SAS Software Depot prior to updating this SAS Home
      exit /b -1
   )

   exit /b 0

:findjava
   set deploywiz=
   set javaruntime=

   set deploymntreg=
   set jre=

   if exist "%root%\setup.dat" (
      for /f "usebackq tokens=1,2 delims==" %%a in ("%root%\setup.dat") do (
         if "%%a"=="DEPLOYWIZ_PRT_PKG" set deploywiz=%%b
         if "%%a"=="JAVARUNTIME_WIN_PKG" set javaruntime=%%b
      )
   )

   if "%deploywiz%"=="" (
      echo Unable to locate the SAS Deployment Wizard
      exit /b -1
   )

   if "%javaruntime%"=="" (
      echo Unable to locate the Java Runtime Environment
      exit /b -1
   )
   
   set deploymntreg=%root%\%deploywiz:/=\%\deploywiz\deploymntreg\sas.tools.deploymntreg.jar
   set jre=%root%\%javaruntime:/=\%\jre.exe

   if not exist "%deploymntreg%" (
      echo Unable to locate the Deployment Registry
      exit /b -1
   )

   if not exist "%jre%" (
      echo Unable to locate the Java Runtime Environment archive
      exit /b -1
   )

   exit /b 0

:extractjre
   set currentdir=%cd%

   mkdir "%tempdir%"
   cd /d "%tempdir%"
   "%jre%" >nul

   cd /d "%currentdir%"

   xcopy "%tempdir%" "%root%\jre" /e /i /q /h /y >nul
   rd /s /q "%tempdir%"

   exit /b 0

:initregistry
   if not exist "%root%\deploymntreg" mkdir "%root%\deploymntreg"
   copy /y "%deploymntreg%" "%root%\deploymntreg\" >nul

   set host=win
   if "%PROCESSOR_ARCHITECTURE%"=="AMD64" set host=wx6

   echo default.host=%host% > "%root%\deploymntreg\registry.properties"

   "%root%\jre\bin\java.exe" -jar "%root%\deploymntreg\sas.tools.deploymntreg.jar" setSASHome "%root%" >nul
   "%root%\jre\bin\java.exe" -jar "%root%\deploymntreg\sas.tools.deploymntreg.jar" setInstallUser "%USERDOMAIN%\%USERNAME%" >nul

   exit /b 0

:updateregistry
   set order=
   set sasinstall=

   for /f %%a in ('dir "%depotroot%\order_data" /b') do set order=%%a

   if "%order%"=="" (
      echo Unable to determine the order number
      exit /b -1
   )

   for /f %%a in ('dir "%depotroot%\products\sasinstall__92210_*" /b') do set sasinstall=%%a

   if "%sasinstall%"=="" (
      echo Unable to locate SAS Foundation in the SAS Software Depot for Client Users
      exit /b -1
   )

   mkdir "%tempdir%"

   echo ^<deploymentRegistry^> > "%tempdir%\registry.xml"

   for /f %%a in ('dir "%sasroot%\core\sasinst\data" /b') do (
      set file=%depotroot%\order_data\%order%\%sasinstall%\sas\core\sasinst\data\%%a

      set code=
      set swhr=
      set host=
      set lang=
      set name=

      for /f "usebackq tokens=1,2 delims==" %%a in ("!file!") do (
         if "%%a"=="AltSku" (
            for /f "tokens=1,2,3,4 delims=_" %%a in ("%%b") do (
               if not "%%a"=="" if not "%%b"=="" if not "%%c"=="" if not "%%d"=="" (
                  set code=%%a
                  set swhr=%%b
                  set host=%%c
                  set lang=%%d
               )
            )
         )

         if "%%a"=="Component" (
            for /f "tokens=2 delims=:" %%a in ("%%b") do (
               call :updateregistry_trim_name %%a
            )
         )
      )

      if not "!code!"=="" if not "!swhr!"=="" if not "!host!"=="" if not "!lang!"=="" if not "!name!"=="" (
         if "%type%"=="depot" if "!code!"=="sasinstall" (
            call :updateregistry_add_product "!code!" "!swhr!" "!host!" "en" "SAS Foundation"
            if exist "%sasroot%\nls\1d" call :updateregistry_add_product "!code!" "!swhr!" "!host!" "1d" "SAS Foundation"
         )

         if not "!lang!"=="xx" (
            if "!lang!"=="07" call :updateregistry_add_product "!code!" "!swhr!" "!host!" "!lang!" "!name!"
            if exist "%sasroot%\nls\!lang!" call :updateregistry_add_product "!code!" "!swhr!" "!host!" "!lang!" "!name!"
         )
      )
   )

   echo ^</deploymentRegistry^> >> "%tempdir%\registry.xml"

   "%depotroot%\jre\bin\java.exe" -jar "%root%\deploymntreg\sas.tools.deploymntreg.jar" --file "%tempdir%\registry.xml" >nul

   rd /s /q "%tempdir%"

   exit /b 0

   :updateregistry_trim_name
      set name=%*

      exit /b 0

   :updateregistry_add_product
      set code=%~1
      set swhr=%~2
      set host=%~3
      set lang=%~4
      set name=%~5

      set version=9.21

      if "!code!"=="accelmva"   if "!swhr!"=="92110" set version=1.5
      if "!code!"=="sashpf"     if "!swhr!"=="92110" set version=3.1
      if "!code!"=="r3"         if "!swhr!"=="92110" set version=4.3
      if "!code!"=="spdsclient" if "!swhr!"=="92110" set version=4.4
      if "!code!"=="spdsclient" if "!swhr!"=="92200" set version=4.5
      if "!code!"=="assist"     if "!swhr!"=="92110" set version=9.2
      if "!code!"=="inttech"    if "!swhr!"=="92110" set version=9.2
      if "!code!"=="securessl"  if "!swhr!"=="92110" set version=9.2
      if "!code!"=="tablesrvtk" if "!swhr!"=="92110" set version=9.2
      if "!code!"=="test"       if "!swhr!"=="92210" set version=9.2
      if "!code!"=="warehouse"  if "!swhr!"=="92210" set version=9.2

      if "!lang!"=="en" (
         echo ^<execute method="setInstallLoc" param1="!code!" param2="!version!" param3="!host!" param4="%sasroot%"/^> >> "%tempdir%\registry.xml"
         echo ^<execute method="setInstallDisplayName" param1="!code!" param2="!version!" param3="!host!" param4="!name!"/^> >> "%tempdir%\registry.xml"

         echo ^<execute method="setInstallOrderNum" param1="!code!" param2="!version!" param3="!host!" param4="%order%"/^> >> "%tempdir%\registry.xml"

         echo ^<execute method="setMaintDisplayName" param1="!code!" param2="!version!" param3="!host!" param4="0" param5="!version!"/^> >> "%tempdir%\registry.xml"
         echo ^<execute method="setMaintLevel" param1="!code!" param2="!version!" param3="!host!" param4="0"/^> >> "%tempdir%\registry.xml"
      )

      echo ^<execute method="addInstallLanguage" param1="!code!" param2="!version!" param3="!host!" param4="!lang!"/^> >> "%tempdir%\registry.xml"

      if "%type%"=="depot" (
         echo ^<execute method="addInstallLanguage" param1="sasinstall" param2="9.21" param3="!host!" param4="!lang!"/^> >> "%tempdir%\registry.xml"
      )

      exit /b 0

:sasswconfig
   echo [properties] > "%root%\sassw.config"

   echo JREHOME=%root%\jre\bin\java.exe >> "%root%\sassw.config"
   echo SASHOME=%root% >> "%root%\sassw.config"
   echo DPLMTREGLOC=%root%\deploymntreg >> "%depotroot%\sassw.config"

   exit /b 0