@echo off

echo %date%:%time%:executing manager upgrade script > logs/MANAGER-UPGRADER-SCRIPT.log

copy /Y upgrade.me upgrade.me.org
cmd /c "type upgrade.me.org&&echo :fail"  > upgrade.me.txt
copy /Y upgrade.me.txt upgrade.me

echo %date%:%time%:stopping motadata service >> logs/MANAGER-UPGRADER-SCRIPT.log
set SERVICE_NAME=motadata-manager_manage

sc query "%SERVICE_NAME%" >nul 2>&1
IF %ERRORLEVEL% EQU 0 (
    echo Service "%SERVICE_NAME%" exists. stopping AIOPS >> logs/MANAGER-UPGRADER-SCRIPT.log
     net stop "Motadata AIOPS"

) ELSE (
    echo Service "%SERVICE_NAME%" not exists. stopping Agent >> logs/MANAGER-UPGRADER-SCRIPT.log
    net stop "Motadata Agent"
    taskkill /f /im motadata-manager.exe
)

echo %date%:%time%:Waiting for service to stop completely... >> logs/MANAGER-UPGRADER-SCRIPT.log
ping localhost -n 10 > nul

echo %date%:%time%:taking backup >> logs/MANAGER-UPGRADER-SCRIPT.log

if not exist backup mkdir backup
if exist backup\manager-artifacts rmdir /Q /S backup\manager-artifacts
if not exist backup\manager-artifacts mkdir backup\manager-artifacts

copy /Y motadata-manager.exe backup\manager-artifacts\
xcopy /E /I /Y lib backup\manager-artifacts\lib

echo %date%:%time%:updating manager artifacts >> logs/MANAGER-UPGRADER-SCRIPT.log

del /F /Q motadata-manager.exe
copy /Y downloads\manager-artifacts\windows\motadata-manager.exe .

if exist "downloads\manager-artifacts\lib" (
    echo %date%:%time%:updating lib >> logs/MANAGER-UPGRADER-SCRIPT.log
    rmdir /Q /S lib
    xcopy /E /I /Y downloads\manager-artifacts\lib lib
) else (
    echo %date%:%time%:lib not exists >> logs/MANAGER-UPGRADER-SCRIPT.log
)

xcopy /E /I /Y downloads\manager-artifacts\javaplugindir javaplugindir 


@REM set SERVICE_NAME=motadata-manager_manage

echo %date%:%time%:Checking if service "%SERVICE_NAME%" exists... >> logs/MANAGER-UPGRADER-SCRIPT.log

REM Query service
sc query "%SERVICE_NAME%" >nul 2>&1
IF %ERRORLEVEL% EQU 0 (
    echo Service "%SERVICE_NAME%" exists. Do nothing..

) ELSE (
    echo %date%:%time%:Service "%SERVICE_NAME%" does not exist. installation of new services >> logs/MANAGER-UPGRADER-SCRIPT.log
    sc delete "motadata-manager_m.exe"
    ping localhost -n 5 > nul
    REM Install the new service
    echo %date%:%time%:Installing service... >> logs/MANAGER-UPGRADER-SCRIPT.log
    copy /Y downloads\manager-artifacts\windows\motadata-agent-manage.exe .
    copy /Y downloads\manager-artifacts\windows\motadata-agent-manage.xml .
    copy /Y downloads\manager-artifacts\windows\motadata-manager_manage.exe .
    copy /Y downloads\manager-artifacts\windows\motadata-manager_manage.xml .
    copy /Y downloads\manager-artifacts\windows\motadata-agent-stop.bat .
    motadata-manager_manage.exe install
    motadata-agent-manage.exe install
    ping localhost -n 5 > nul
    echo %date%:%time%:Installation of new services completed >> logs/MANAGER-UPGRADER-SCRIPT.log
)


echo %date%:%time%:starting motadata service >> logs/MANAGER-UPGRADER-SCRIPT.log

net start "Motadata AIOPS"
ping localhost -n 5 > nul

tasklist | findstr "motadata-manager" | find /c /v "" > SERVICE_STATUS
set /p SERVICESTATUS= < SERVICE_STATUS
del /F /Q SERVICE_STATUS

if %SERVICESTATUS% geq 1 (
echo %date%:%time%:motadata service started successfully >> logs/MANAGER-UPGRADER-SCRIPT.log

cmd /c "type upgrade.me.org&&echo :succeed"  > upgrade.me.txt
copy /Y upgrade.me.txt upgrade.me
del /F /Q upgrade.me.txt
del /F /Q upgrade.me.org

) else (

echo %date%:%time%:motadata manager upgrade failed >> logs/MANAGER-UPGRADER-SCRIPT.log

net stop "Motadata AIOPS"
taskkill /f /im motadata-manager.exe
del /F /Q motadata-manager.exe
rmdir /Q /S lib

echo %date%:%time%:restoring manager artifacts >> logs/MANAGER-UPGRADER-SCRIPT.log

copy /Y backup\manager-artifacts\motadata-manager.exe .
xcopy /E /I /Y backup\manager-artifacts\lib lib

echo %date%:%time%:starting motadata service >> logs/MANAGER-UPGRADER-SCRIPT.log

net start "Motadata AIOPS"
del /F /Q upgrade.me.txt
del /F /Q upgrade.me.org
)
