@echo off

set _PROGNAME=%~0

rem Set start folder:
set _Start=%~d0%~p0
echo Start Folder : %_Start%

set _AdminOnly=0
set _Args=
set _Env=
set _Port=
set _User=admin
set _Pass=admin
set _UserPass_Are_Default=1
set _Check=0

shift
if "%~0" == "" goto EXIT_ON_ERROR_COLLECT_PATH_NOT_GIVEN

:next
if "x%~0x" == "x-collect_pathx" goto get_collect_path
if "x%~0x" == "x-admin_onlyx" goto get_admin_only
if "x%~0x" == "x-hostnamex" goto get_host
if "x%~0x" == "x-portx" goto get_port
if "x%~0x" == "x-Envx" goto get_env
if "x%~0x" == "x-applis_default_credential_HSS_Databasex" goto get_cred_default
if "x%~0x" == "x-applis_credential_HSS_Databasex" goto get_credential
if "%~0" == "" goto Step_1
goto EXIT_ON_INVALID_PARAMETER

:get_collect_path
shift
if "%~0" == "" goto EXIT_ON_ERROR_COLLECT_PATH_NOT_GIVEN
echo "%~0"
set _Workdir=%~0

if not "%~0" == "" shift
goto next 

:get_host
set _Args=%_Args% --hostname
shift
if "%~0" == "" goto EXIT_ON_ERROR_HOST_NOT_GIVEN
set _Args=%_Args% %~0
if not "%~0" == "" shift
goto next 

:get_port
set _Args=%_Args% --port
shift
if "%~0" == "" goto EXIT_ON_ERROR_PORT_NOT_GIVEN
set _Args=%_Args% %~0
set _Port=%~0
if not "%~0" == "" shift
goto next 

:get_env
shift
if "%~0" == "" goto EXIT_ON_ERROR_ENV_NOT_GIVEN
set _Env=%~0
if not "%~0" == "" shift
goto next

:get_cred_default
echo admin:admin
exit /b 0

:get_credential
shift
if "%~0" == "" goto EXIT_ON_ERROR_IDENT_NOT_GIVEN
set IDENTS=%~0
for /f "usebackq tokens=1* delims=:" %%i in (`echo %IDENTS%`) do set _User=%%i && set _Pass=%%j && set _UserPass_Are_Default=0
shift
if "%~0" == "" goto next
if not "%~0" == "-check" goto next
set _Check=1
shift
goto next


:get_admin_only
set _AdminOnly=1
if not "%~0" == "" shift
goto next 

rem goto Step_1

rem =====================================================================

:EXIT_ON_INVALID_PARAMETER
echo invalid option: "%~0" 
:EXIT_ON_ERROR_COLLECT_PATH_NOT_GIVEN
:EXIT_ON_ERROR_WRONG_COLLECT_PATH_GIVEN
:EXIT_ON_ERROR_HOST_NOT_GIVEN
:EXIT_ON_ERROR_PORT_NOT_GIVEN
:EXIT_ON_ERROR_ENV_NOT_GIVEN
echo usage : %_PROGNAME% -collect_path "path" -hostname "host" -port "port" -Env "env" -admin_only
exit /b -99
rem exit -99

rem =====================================================================


:Step_1
rem treat check credential test and exit if applies..
if %_Check% == 0 goto Step_1_bis

rem "%_Start%hyperstream_admin" -u %_User% -p %_Pass% %_Args% --cmd info | findstr "HyperStream version"
for /f "usebackq tokens=1 delims=:" %%i in (`"%_Start%hyperstream_admin" -u %_User% -p %_Pass% %_Args% --cmd info`) do set hss_info=%%i && goto Break
:Break
for /f "usebackq tokens=1,2*" %%i in (`echo %hss_info%`) do set HyperStream=%%i && set version=%%j && set garbage=%%k
if not "%HyperStream%" == "HyperStream " goto EXIT_ON_ERROR_CREDENT
if not "%version%" == "version " goto EXIT_ON_ERROR_CREDENT
echo OK
exit /b 0
:EXIT_ON_ERROR_CREDENT
echo KO
exit /b 17 

:Step_1_bis
echo _SetCurrentStep=1,HSS Collect

rem Warning, by default, the step start with a 60 seconds timeout.
rem We should reset it on a regulat basis regarding the step done

rem Reset the timeout for 1 minute
echo _SetTimeout=60

rem =====================================================================

:Step_2
echo _SetCurrentStep=2,Create directories

if not exist "%_Workdir%" goto EXIT_ON_ERROR_WRONG_COLLECT_PATH_GIVEN

if not exist "%_Workdir%\HSS_Adm"  md "%_Workdir%\HSS_Adm" >nul
if not exist "%_Workdir%\HSS_Info" md "%_Workdir%\HSS_Info" >nul

echo.
echo Target dir is %_Workdir%
echo.
echo _FinishCurrentStep=2,OK

rem =====================================================================

if %_AdminOnly% == 1 goto Step_4
:Step_3
rem Reset the timeout for 1 minute
echo _SetTimeout=60

echo _SetCurrentStep=2,Copy Logs
copy /Y "%_Start%..\HyperStream_Server_InstallLog.log" "%_Workdir%\HSS_Adm\" >nul
mkdir "%_Workdir%\HSS_Adm\log"
mkdir "%_Workdir%\HSS_Adm\conf"
copy /Y "%_Start%..\log\log_"* "%_Workdir%\HSS_Adm\log\" >nul
copy /Y "%_Start%..\log\hss_"* "%_Workdir%\HSS_Adm\log\" >nul
copy /Y "%_Start%..\conf\"* "%_Workdir%\HSS_Adm\conf\" >nul
echo _FinishCurrentStep=2,OK

rem =====================================================================

:Step_4
echo _SetCurrentStep=2,Retreive port from portnum.txt

if "%_Env%" == "" goto :Step_4_OK
if not "%_Port%" == "" goto suite

set  portfile=%_Start:bin\=conf%
for /f "usebackq" %%G IN ("%portfile%\portnum.txt") DO (set port=%%G) && goto Break
:Break
set _Args=%_Args% --port %port%

:suite
echo Port provided on Command Line

:Step_4_OK
echo _FinishCurrentStep=2,OK

rem =====================================================================

echo _SetCurrentStep=2,Collect information with hyperstream_admin

rem Folowing command can be started by guest, using hardcoded password
echo _SetCurrentStep=3,Collect "env all" with user guest
"%_Start%hyperstream_admin" --cmd "env all" -u guest -p guest %_Args% >> "%_Workdir%\HSS_Info\HSS_env.txt"
echo _FinishCurrentStep=3,OK

echo _SetCurrentStep=3,Collect "info" with user guest
"%_Start%hyperstream_admin" --cmd info -u guest -p guest %_Args% >> "%_Workdir%\HSS_Info\HSS_info.txt"
echo _FinishCurrentStep=3,OK

echo _SetCurrentStep=3,Collect "info_AER" with user guest
"%_Start%hyperstream_admin" --cmd info_AER -u guest -p guest %_Args% >> "%_Workdir%\Summary.txt"
echo _FinishCurrentStep=3,OK

echo _SetCurrentStep=3,Collect "server_sys" with user guest
"%_Start%hyperstream_admin" --cmd server_sys -u guest -p guest %_Args% >> "%_Workdir%\HSS_Info\HSS_sys.txt"
echo _FinishCurrentStep=3,OK

echo _SetCurrentStep=3,Collect "file_AER" with user guest
"%_Start%hyperstream_admin" --cmd file_AER -u guest -p guest %_Args% >> "%_Workdir%\HSS_Info\HSS_file.txt"
echo _FinishCurrentStep=3,OK

echo _SetCurrentStep=3,Collect "license_info" with user guest
"%_Start%hyperstream_admin" --cmd license_info -u guest -p guest %_Args% >> "%_Workdir%\HSS_Info\HSS_license.txt"
echo _FinishCurrentStep=3,OK

echo _SetCurrentStep=3,Collect "slist" with user guest
"%_Start%hyperstream_admin" --cmd slist -u guest -p guest %_Args% >> "%_Workdir%\HSS_Info\HSS_ServerList.txt"
echo _FinishCurrentStep=3,OK

echo _SetCurrentStep=3,Collect "service list" with user guest
"%_Start%hyperstream_admin" --cmd "service list" -u guest -p guest %_Args% >> "%_Workdir%\HSS_Info\HSS_ServiceList.txt"
echo _FinishCurrentStep=3,OK

echo _SetCurrentStep=3,Collect "list_repli" with user guest
"%_Start%hyperstream_admin" --cmd "list_repli" -u guest -p guest %_Args% >> "%_Workdir%\HSS_Info\HSS_Repli.txt"
echo _FinishCurrentStep=3,OK

echo _SetCurrentStep=3,Collect "repli_info" with user guest
"%_Start%hyperstream_admin" --cmd repli_info -u guest -p guest %_Args% >> "%_Workdir%\HSS_Info\HSS_Repli_info.txt"
echo _FinishCurrentStep=3,OK

echo _SetCurrentStep=3,Collect "ls_stat" with user guest
"%_Start%hyperstream_admin" --cmd "ls_stat" -u guest -p guest %_Args% >> "%_Workdir%\HSS_Info\HSS_HostStat.txt"
echo _FinishCurrentStep=3,OK

echo _SetCurrentStep=3,Collect ""rdmini" with user guest
"%_Start%hyperstream_admin" --cmd "rdmini" -u guest -p guest %_Args% >> "%_Workdir%\HSS_Info\HSS_rdmserver.ini.txt"
echo _FinishCurrentStep=3,OK

echo _SetCurrentStep=3,Collect "list_from_app %%" with user guest 
"%_Start%hyperstream_admin" --cmd "list_from_app %%" -u guest -p guest %_Args% >> "%_Workdir%\HSS_Info\HSS_StreamList.txt"
echo _FinishCurrentStep=3,OK

echo _SetCurrentStep=3,Collect "dump_stream_table" with user guest 
"%_Start%hyperstream_admin" --cmd "exec_sql select * from dedup_stream" -u guest -p guest %_Args% >> "%_Workdir%\HSS_Info\HSS_StreamList2.txt"
echo _FinishCurrentStep=3,OK

echo _SetCurrentStep=3,Collect "client" with user guest
"%_Start%hyperstream_admin" --cmd "client" -u guest -p guest %_Args% >> "%_Workdir%\HSS_Info\HSS_Client.txt"
echo _FinishCurrentStep=3,OK

echo _SetCurrentStep=3,Collect "host" with user guest
"%_Start%hyperstream_admin" --cmd "host" -u guest -p guest %_Args% >> "%_Workdir%\HSS_Info\HSS_Host.txt"
echo _FinishCurrentStep=3,OK

echo _SetCurrentStep=3,Collect "app" with user guest
"%_Start%hyperstream_admin" --cmd "app" -u guest -p guest %_Args% >> "%_Workdir%\HSS_Info\HSS_App.txt"
echo _FinishCurrentStep=3,OK

echo _SetCurrentStep=3,Collect "requirement" with user guest
"%_Start%hyperstream_admin" --cmd "wsget2 /requirement.xml?upgrade=1" -u guest -p guest %_Args% >> "%_Workdir%\HSS_Info\HSS_Requirement.txt"
echo _FinishCurrentStep=3,OK

echo _SetCurrentStep=3,Collect "list_bad_stream" with user guest
"%_Start%hyperstream_admin" --cmd "list_bad_stream" -u guest -p guest %_Args% >> "%_Workdir%\HSS_Info\HSS_ListBadStream.txt"

echo _SetCurrentStep=3,Collect "boot_info" with user guest
"%_Start%hyperstream_admin" --cmd "boot_info" -u guest -p guest %_Args% >> "%_Workdir%\HSS_Info\HSS_BootInfo.txt"

if %_AdminOnly% == 0 goto Step_5
echo _SetCurrentStep=3,Collect "get_log" with user guest
"%_Start%hyperstream_admin" --cmd "get_log" -u guest -p guest %_Args% >> "%_Workdir%\HSS_Info\HSS_Log.txt"
echo _FinishCurrentStep=3,OK

:Step_5


echo _FinishCurrentStep=3,OK

echo _FinishCurrentStep=2,OK

echo _FinishCurrentStep=1,OK


:plugins
call "%_Start%hsx_env_report.cmd"

goto std_end

:std_end
