@echo off

set CONFIG=GAPS.ini

if not [%1]==[] if not [%1]==[GAPS.ini] (
  if not [%TASKS%]==[] goto :eof
  if not [%1]==[0] set LOGFILE=1
)
cd /d "%~dp0.."
set TASKS="%CD%"
cd /d "%~dp0"

if [%LOGFILE%]==[1] (
  echo Bitte DOS-Fenster geoeffnet lassen!
  echo Protokolldatei: %TASKS:~1,-1%\logs\%~nx1.log

  call :cfg %1 1> %TASKS%\logs\%~nx1.log 2>&1
  exit 0
)

:cfg
echo %DATE% %TIME:~0,5% Tasks-Konsole gestartet
echo.
echo == CONFIG ==

if "%PROGRAMFILES(X86)%"=="" ( 
  set PROG=x86
  set PROGRAMPATH="%PROGRAMFILES%"
) else (
  set PROG=x64
  set PROGRAMPATH="%PROGRAMFILES(X86)%"
)

if not exist ..\%CONFIG% (
  echo * Konfiguration "%CONFIG%" initialisieren
  call :init %PROGRAMPATH% > ..\%CONFIG%
)
echo * Konfiguration "%CONFIG%" laden
for /F "delims=" %%i in (..\%CONFIG%) do set %%i>nul

call teste_verzeichnis.bat %JAVA%
call teste_verzeichnis.bat %COGNOS%
call teste_verzeichnis.bat %PHP%

call teste_verzeichnis.bat %PORTAL%
call teste_verzeichnis.bat %XML%
call teste_verzeichnis.bat %STARTER%

set SYSTEM_DIR="%PORTAL:~1,-1%\System\%SYSTEM:~1,-1%"
if [%PROT%]==[] set PROT=%SYSTEM_DIR%\logs
call teste_verzeichnis.bat %PROT%

if [%LOGFILE%]==[1] (
  del /F /Q %TASKS%\logs\*.*.log 1> nul 2> nul
  del /F /Q %PROT%\*.*.log 1> nul 2> nul
)

if [%KUNDE%]==[] (
  echo KUNDE="Neuer Kunde" >> ..\%CONFIG%
  set KUNDE="Neuer Kunde"
)
if [%KUNDE%]==["Autohaus XY"] echo   !! Bitte Kundenname in %CONFIG% anpassen !!
if [%KUNDE%]==["Neuer Kunde"] echo   !! Bitte Kundenname in %CONFIG% anpassen !!
if %STATUSBERICHT%=="" echo   ! Bitte E-Mailadresse fuer Statusbericht angeben !

if [%SMTP_HOST%]==[""] echo   ! SMTP-Konfiguration bitte anpassen !

if [%SMTP_PORT%]==[] (
  call :smtp >> ..\%CONFIG%
  echo   !! SMTP-Konfiguration in %CONFIG% ergaenzt; bitte anpassen !!
)

if [%EXCEL%]==[] (
  call :excel %PROGRAMPATH% >> ..\%CONFIG%
)
call teste_verzeichnis.bat %EXCEL%


md %TASKS%\logs 1> nul 2> nul
call datum.bat
call config_backup.bat
robocopy /MIR /NJH /NJS /NDL %C11_SRV%\deployment %SYSTEM_DIR%\Sicherung
echo.
service_status.exe
echo.
for /F usebackq %%i in (`type version.txt`) do set VERSION=%%i>NUL
echo Version:       %VERSION%
echo Lizenznehmer:  %KUNDE:~1,-1%
echo Benutzer:      %USERDOMAIN%\%USERNAME%
%PHP%\php Tools\run.php systeminfo
echo GAPS-Portal:   %GAPS_URL:~1,-1%
echo.
echo hilfe.bat fuer Befehlsuebersicht
echo.
if [%LOGFILE%]==[1] (
  echo == %~nx1 ==
  call %1
)
if [%LOGFILE%]==[1] (
  %PHP%\php Tools\run.php update %BETA%
  echo %date% %time:~0,5% Aktualisierung beendet.
  call fehlermeldung.bat %~nx1
  exit 0
)
goto :eof

:init
echo JAVA="%~1\Java\j2re1.4.1_01\bin"
echo COGNOS="%~1\Cognos\cer5\bin"
echo PHP="%~1\GCBS\GCPS\PHP5"
call :excel %1
echo.
cd ..
echo XML="%CD%\config"
cd ..\..
echo PORTAL="%CD%\Portal"
echo STARTER="%CD%\GCStarter"
echo.
echo KUNDE="Neuer Kunde"
echo STATUSBERICHT=""
:smtp
echo.
echo CAT_U=""
echo CAT_P=""
echo.
echo GAPS_XML="Gaps.xml"
echo GAPS_TITEL="GAPS"
echo.
echo SMTP_HOST=""
echo SMTP_PORT="25"
echo SMTP_SSL="N"
echo SMTP_FROM="Global Cube"
echo SMTP_USER=""
echo SMTP_PW=""

cd /d "%~dp0"
goto :eof

:excel
set EXCEL=""
dir /b "%~1\Microsoft Office\Office11\excel.exe" 1> nul 2> nul && set EXCEL="%~1\Microsoft Office\Office11"
dir /b "%~1\Microsoft Office\Office12\excel.exe" 1> nul 2> nul && set EXCEL="%~1\Microsoft Office\Office12"
dir /b "%~1\Microsoft Office\Office13\excel.exe" 1> nul 2> nul && set EXCEL="%~1\Microsoft Office\Office13"
dir /b "%~1\Microsoft Office\Office14\excel.exe" 1> nul 2> nul && set EXCEL="%~1\Microsoft Office\Office14"
echo EXCEL=%EXCEL%
