::==================================================================================================
::== COMMAND LINE UTILITIES / $MAPPDISTR IMPLEMENTATION; AUTHOR: SERGEI Y.KITAEV ===================
::==================================================================================================
@echo off
if /i not "%~1"=="(InternalCall)" goto INCORRECT_INTERNAL_CALL
if not defined _XAppDistr_IsNestedCall_ goto IS_NESTED_CALL_NOT_SPEC
::==================================================================================================
set errorlevel=0
set errorlevel=
::--------------------------------------------------------------------------------------------------
if not defined AppFolderName goto APP_FOLDER_NAME_NOT_SPEC
if /i not "!Action!"=="LNK" if not defined OnServerCommonInnerPath goto ON_SERVER_COMMON_INNER_PATH_NOT_SPEC

set ServerListFileDisplay=
if defined ServerListFile ( set ServerListFileDisplay=!ServerListFile!
) else if /i not "!Action!"=="LNK" set ServerListFileDisplay=^<Previous Error Scope^>
::--------------------------------------------------------------------------------------------------

call "%~dp0.assign_common_variables.cmd" "(InternalCall)"
if !errorlevel! neq 0 goto ERROR

::--------------------------------------------------------------------------------------------------

set $xappdistr.VersionFolderName.OrigValue=!$xappdistr.VersionFolderName!

if /i "!$xappdistr.VersionFolderName!"==":AUTO:" (
set $xappdistr.VersionFolderName=
for /d %%d in ( "*." ) do if /i not "%%d"=="!ReportFolderName!" set $xappdistr.VersionFolderName=%%d
if not defined $xappdistr.VersionFolderName (
if /i not "!Action!"=="REMOVE" goto CANT_DETERMINE_VERSION_FOLDER_NAME
set $xappdistr.VersionFolderName=???
))

set VersionFolderName=!$xappdistr.VersionFolderName!
if     defined VersionFolderName set AppSourceFolder=!VersionFolderName!
if not defined VersionFolderName set AppSourceFolder=!AppFolderName!

::--------------------------------------------------------------------------------------------------

set ShortcutName=!$xappdistr.ShortcutName!
set ShortcutDestinationFolder=!$xappdistr.ShortcutDestinationFolder!

set ShortcutFileName=
set ShortcutTxtFileName=
set PreviousShortcutFileName=
set TargetShortcutFile=
set PreviousShortcutFileInDestinationFolder=
set ShortcutTempFileInDestinationFolder=
if not defined ShortcutName goto END_SHORTCUT_PROPS

set ShortcutFileName=!ShortcutName!.lnk
set ShortcutTxtFileName=!ShortcutFileName!.txt
set ShortcutTempFileName=!ShortcutFileName!.tmp
if not defined ShortcutDestinationFolder goto END_SHORTCUT_PROPS

set PreviousShortcutFileName=#!ShortcutFileName!
set TargetShortcutFile=!ShortcutDestinationFolder!\!ShortcutFileName!
set PreviousShortcutFileInDestinationFolder=!ShortcutDestinationFolder!\!PreviousShortcutFileName!
set ShortcutTempFileInDestinationFolder=!ShortcutDestinationFolder!\!ShortcutTempFileName!

:END_SHORTCUT_PROPS

::--------------------------------------------------------------------------------------------------

set ReportFileName=~AppDistrReport.!AppFolderName!.txt
set ReportFile=!ReportFolder!\!ReportFileName!

set ErrorFilePattern=!ReportFolder!\*.Errors.txt
set LogFilePattern=!ReportFolder!\*.Log.txt

set BaseWindowTitle=!AppFolderName! /!Action!

::--------------------------------------------------------------------------------------------------

set VerboseMode=1
if "!_XAppDistr_IsNestedCall_!"=="1" set VerboseMode=0
if "!$xappdistr.AskConfirmation!"=="1" set VerboseMode=1
if "!$xappdistr.AskConfirmation!"=="0" set VerboseMode=0

::==================================================================================================

if /i not "!Action!"=="REMOVE" if not exist "!AppSourceFolder!\" goto APP_SORCE_FOLDER_NOT_FOUND
if /i "!Action!"=="LNK" (
if not defined $xappdistr.ShortcutName goto SHORTCUT_NAME_NOT_SPEC_FOR_LNK_ACTION
) else (
if defined ServerListFile if not exist "!ServerListFile!" goto SERVERS_LIST_FILE_NOT_FOUND
)
if /i not "!Action!"=="REMOVE" ( if defined ShortcutName (
if not exist "!ShortcutTxtFileName!" goto SHORTCUT_SOURCE_FILE_NOT_FOUND ))

::==================================================================================================

echo.
echo ώ Application folder name    : !AppFolderName!
echo ώ Version subfolder name     : !VersionFolderName!
echo ----------------------------------------------------------------------------------------------------
echo ώ On-server common inner path: !OnServerCommonInnerPath!
echo ώ App.servers list file      : !ServerListFileDisplay!
echo ----------------------------------------------------------------------------------------------------
echo ώ Action                     : !Action!
echo ----------------------------------------------------------------------------------------------------
echo ώ Task-kill EXE-name         : !TaskKillExeName!
echo ώ Open-files substring       : !OpenFilesSubstring!
echo ----------------------------------------------------------------------------------------------------
echo ώ Report file                : !ReportFile!
if /i not "!Action!"=="REMOVE" if /i not "!Action!"=="LNK" (
if defined PostUploadScriptFile (
echo ----------------------------------------------------------------------------------------------------
echo ώ Post-upload script file    : !PostUploadScriptFile!
echo ώ Post-upload script error
echo   affects failed servers list: !PostUploadScriptErrorAffectsFailedServerList!
))
if /i "!Action!"=="LNK" goto LNK_INFO
if not defined $xappdistr.ShortcutName if not defined $xappdistr.ShortcutDestinationFolder goto SKIP_LNK_INFO
if /i "!Action!"=="DISTRIBUTE" goto LNK_INFO
goto SKIP_LNK_INFO
:LNK_INFO
echo ----------------------------------------------------------------------------------------------------
echo ώ Shortcut name (base f.name^): !$xappdistr.ShortcutName!
echo ώ Shortcut destination folder: !$xappdistr.ShortcutDestinationFolder!
:SKIP_LNK_INFO

if "!$xappdistr.AskConfirmation!"=="1" goto CONFIRMATION_PROMPT
if "!$xappdistr.AskConfirmation!"=="0" goto NO_CONFIRMATION_PROMPT
if "!_XAppDistr_IsNestedCall_!"=="1"   goto NO_CONFIRMATION_PROMPT

:CONFIRMATION_PROMPT
echo.
echo ±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
echo ±± CONFIRM XCOPY APP.DISTRIBUTION MULTI-START: ±±± (DISTRIB.CONFIRMATION) ±±±±±±±±±±±±±±±±±±±±±±±±±±
echo.
call $CONFIRMPAUSE.cmd "0"
if !errorlevel! equ 2 EXIT 255
if !errorlevel! neq 0 goto ERROR
echo.
echo ΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔ
goto SKIP_HEADER

:NO_CONFIRMATION_PROMPT
echo.
echo ±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
echo ±± XCOPY APPLICATION DISTRIBUTION MULTI-START: ±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
:SKIP_HEADER

::==================================================================================================
set WasPublicShortcutCreated=0
set WasDistributionStarted=0
::==================================================================================================
call :DELETE_RO_FILE_VERBOSE "!ReportFile!"
if !errorlevel! neq 0 goto FAILURE
::==================================================================================================
if not defined ShortcutName goto END_SHORTCUT_CREATION
::--------------------------------------------------------------------------------------------------

call :DELETE_RO_FILE_VERBOSE "!ShortcutTempFileName!"
if !errorlevel! neq 0 goto FAILURE

if /i "!Action!"=="REMOVE" goto OLD_SHORTCUT_RENAMING

call :DELETE_RO_FILE_VERBOSE "!ShortcutFileName!"
if !errorlevel! neq 0 goto FAILURE

call :CALL_LNK_CONVERTER
if !errorlevel! neq 0 goto FAILURE

if /i "!Action!"=="LNK" goto SUCCESS_LITE
if not defined ShortcutDestinationFolder goto END_SHORTCUT_CREATION
if /i "!Action!"=="UPDATE" goto END_SHORTCUT_CREATION

::--------------------------------------------------------------------------------------------------

:OLD_SHORTCUT_RENAMING

if not exist "!TargetShortcutFile!"                   goto SKIP_OLD_SHORTCUT_RENAMING
if exist "!PreviousShortcutFileInDestinationFolder!"  goto SKIP_OLD_SHORTCUT_RENAMING

echo.
echo * Renaming: "!TargetShortcutFile!" ^>^> "!PreviousShortcutFileName!" ...
call :RESET_ERROR
ren "!TargetShortcutFile!" "!PreviousShortcutFileName!"
if !errorlevel! neq 0 goto FAILURE

:SKIP_OLD_SHORTCUT_RENAMING

::--------------------------------------------------------------------------------------------------

if /i "!Action!"=="REMOVE" goto END_SHORTCUT_CREATION

call :DELETE_RO_FILE_VERBOSE "!TargetShortcutFile!"
if !errorlevel! neq 0 goto FAILURE
call :DELETE_RO_FILE_VERBOSE "!ShortcutTempFileInDestinationFolder!"
if !errorlevel! neq 0 goto FAILURE
echo.
echo Copying "!ShortcutFileName!" to "!ShortcutDestinationFolder!\":
call :EXEC_CMD_NOLF_SUPPRESS_OUTPUT copy /b "!ShortcutFileName!" "!ShortcutTempFileInDestinationFolder!"
if !errorlevel! neq 0 goto FAILURE
call :EXEC_CMD_NOLF_SUPPRESS_OUTPUT attrib +r "!ShortcutTempFileInDestinationFolder!"
if !errorlevel! neq 0 goto FAILURE
call :EXEC_CMD_NOLF_SUPPRESS_OUTPUT ren "!ShortcutTempFileInDestinationFolder!" "!ShortcutFileName!"
if !errorlevel! neq 0 goto FAILURE

set WasPublicShortcutCreated=1
echo.
echo ΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ

:END_SHORTCUT_CREATION

::==================================================================================================

set ReportFileCreated=0

::--------------------------------------------------------------------------------------------------

if not defined ServerListFile goto PREVIOUS_ERRORS_SCOPE

call :RECREATE_REPORT_FOLDER
if !errorlevel! neq 0 goto FAILURE

call :RESET_ERROR
for /f "usebackq delims=" %%s in ("!ServerListFile!") do (
rem -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

set /a DistributionItemNo+=1

set ServerName=%%s

set ErrorFile=!ReportFolder!\!ServerName!.Errors.txt

call :CREATE_OPERATION_LOG_FILE "!ErrorFile!"
if !errorlevel! neq 0 goto FAILURE

call :RESET_ERROR
rem -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
)
if !errorlevel! neq 0 goto FAILURE

goto START_DISTRIBUTION

::==================================================================================================

:PREVIOUS_ERRORS_SCOPE

set ErrorFilesList=

call :RESET_ERROR
for %%f in ("!ErrorFilePattern!") do (
rem -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
set ErrorFilesList=!ErrorFilesList!;%%f
rem -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
)
if !errorlevel! neq 0 goto FAILURE

if not defined ErrorFilesList goto :START_DISTRIBUTION
call :RESET_ERROR
for %%f in ("!ErrorFilesList!") do (
rem -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
call :CREATE_OPERATION_LOG_FILE "%%f"
if !errorlevel! neq 0 goto FAILURE
rem -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
)
if !errorlevel! neq 0 goto FAILURE

::==================================================================================================

:START_DISTRIBUTION
set WasDistributionStarted=1

call :PREPARE_OUTPUT "!ReportFile!"
if !errorlevel! neq 0 goto FAILURE

call :PRINT_REPORT_HEADER >> "!ReportFile!"
if !errorlevel! neq 0 goto FAILURE

set ReportFileCreated=1

echo.
echo * Starting "!Action!" operations for "!AppFolderName!" app. ...

echo.>> "!ReportFile!"
if !errorlevel! neq 0 goto FAILURE
echo ΝΝΝ Started "!Action!" operations for "!AppFolderName!" app.: ΝΝΝ>> "!ReportFile!"
if !errorlevel! neq 0 goto FAILURE

set DistributionItemNo=0

call :RESET_ERROR
for %%f in ("!ErrorFilePattern!") do (
rem ------------------------------------------------------------------------------------------------

set /a DistributionItemNo+=1

call :EXTRACT_SERVER_NAME "%%f"
if !errorlevel! neq 0 goto FAILURE

set DestinationFolder=\\!ServerName!\!OnServerCommonInnerPath!\!AppFolderName!

if "!VerboseMode!"=="1" (
rem -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
echo.
echo !DistributionItemNo!^)
echo ΝΝ^> !DestinationFolder! ...
rem -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
)

if !errorlevel! neq 0 goto FAILURE
echo !DistributionItemNo!^)>> "!ReportFile!"
if !errorlevel! neq 0 goto FAILURE
echo ΝΝ^> !DestinationFolder!;>> "!ReportFile!"
if !errorlevel! neq 0 goto FAILURE

set Command="%~dp0process_single_server.cmd" "(InternalCall)" "!AppFolderName!" "!OnServerCommonInnerPath!" "!ServerName!" "!Action!" "!VersionFolderName!"

call :RESET_ERROR
start /min "[!ServerName!] - !BaseWindowTitle!" /I /D "!CD!" CMD /U /C "!Command!"
if !errorlevel! neq 0 (
rem -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
echo ## Can not start command batch.>> "!ReportFile!"
goto FAILURE
rem -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
)

call :RESET_ERROR
rem ------------------------------------------------------------------------------------------------
)
if !errorlevel! neq 0 goto FAILURE

echo.
echo ΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ

call :PRINT_REPORT_SPLITTER >> "!ReportFile!"
if !errorlevel! neq 0 goto FAILURE

echo.
echo * Waiting for completion of all started operations ...

:WAIT_COMPLETION
call :RESET_ERROR
for /f %%t in ('"TaskList /v | Find /i "!BaseWindowTitle!""') do (
rem -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
call :SLEEP "1"
if !errorlevel! neq 0 goto FAILURE
goto WAIT_COMPLETION
rem -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
)
if !errorlevel! neq 0 goto FAILURE

if not exist "!ErrorFilePattern!" goto PRE_SUCCESS

call :PRINT_REPORT_OPERATIONS_FAILED
if !errorlevel! neq 0 goto ERROR
call :PRINT_REPORT_OPERATIONS_FAILED >> "!ReportFile!"
goto FAILURE

::==================================================================================================

:PRE_SUCCESS
if not "!WasPublicShortcutCreated!"=="1" goto SUCCESS
echo New public shortcut was created: "!TargetShortcutFile!". >> "!ReportFile!"
if !errorlevel! neq 0 goto FAILURE
if not defined PreviousShortcutFileInDestinationFolder goto SUCCESS
call :DELETE_RO_FILE_VERBOSE "!PreviousShortcutFileInDestinationFolder!"
if !errorlevel! neq 0 goto FAILURE

:SUCCESS
call :PRINT_REPORT_OPERATIONS_SUCCESSED
if !errorlevel! neq 0 goto ERROR
call :PRINT_REPORT_OPERATIONS_SUCCESSED >> "!ReportFile!"
if !errorlevel! neq 0 goto ERROR

call :PRINT_REPORT_SUCCESS_FOOTER >> "!ReportFile!"
if !errorlevel! neq 0 goto ERROR

:SUCCESS_LITE
echo.
echo ±± APPLICATION DISTRIBUTION ΔΔ SUCCESS ±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
echo ±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
goto FINISH

::--------------------------------------------------------------------------------------------------

:FAILURE
if "!WasDistributionStarted!"=="1" (
call :RESET_ERROR
if "!ReportFileCreated!"=="1" call :PRINT_REPORT_FAILURE_FOOTER >> "!ReportFile!"
if !errorlevel! neq 0 goto ERROR )
echo.
echo ±± ## APPLICATION DISTRIBUTION ΔΔ FAILURE ±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
echo ±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
goto ERROR

::==================================================================================================

:INCORRECT_INTERNAL_CALL
echo.
echo ## Incorrect internal call of "%~nx0" ($XAppDistr implementation).
echo 
pause
EXIT 255

:IS_NESTED_CALL_NOT_SPEC
echo
echo ## Internal variable not specified: "_XAppDistr_IsNestedCall_".
echo 
pause
EXIT 255

:APP_FOLDER_NAME_NOT_SPEC
echo
echo ## Parameter-variable not specified: "AppFolderName".
goto ERROR

:ON_SERVER_COMMON_INNER_PATH_NOT_SPEC
echo
echo ## Parameter-variable not specified: "OnServerCommonInnerPath".
goto ERROR

:CANT_DETERMINE_VERSION_FOLDER_NAME
echo. ## Can not automatically determine name of version subfolder
echo. ## in the current directory. (:AUTO: keyword was specified.)
goto ERROR

:APP_SORCE_FOLDER_NOT_FOUND
echo.
echo ## Application source folder not found: "!AppSourceFolder!".
goto ERROR

:SERVERS_LIST_FILE_NOT_FOUND
echo.
echo ## Application servers list file not found: "!ServerListFile!".
goto ERROR

:SHORTCUT_NAME_NOT_SPEC_FOR_LNK_ACTION
echo.
echo ## Shortcut name was not specified for "LNK"-action.
echo ## Environment variable "$xappdistr.ShortcutName" not defined.
goto ERROR

:SHORTCUT_SOURCE_FILE_NOT_FOUND
echo.
echo ## Shortcut source file not fount: "!ShortcutTxtFileName!".
goto ERROR

::==================================================================================================

:PRINT_REPORT_HEADER
echo °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
echo °° APPLICATION DISTRIBUTION: °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
echo °° ΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔ °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
echo !DATE!
time /t
echo # Application folder name    : !AppFolderName!
echo # Version subfolder name     : !VersionFolderName!
echo # On-server common inner path: !OnServerCommonInnerPath!
echo # App.servers list file      : !ServerListFileDisplay!
echo # Action                     : !Action!
echo # Task-kill EXE-name         : !TaskKillExeName!
echo # Open-files substring       : !OpenFilesSubstring!
if /i not "!Action!"=="REMOVE" (
if defined PostUploadScriptFile (
echo # Post-upload script file    : !PostUploadScriptFile!
echo # Post-upload script error
echo   affects failed servers list: !PostUploadScriptErrorAffectsFailedServerList!
))
if /i "!Action!"=="LNK" goto PRH_LNK_INFO
if not defined ShortcutName if not defined ShortcutDestinationFolder goto PRH_SKIP_LNK_INFO
if /i "!Action!"=="DISTRIBUTE" goto PRH_LNK_INFO
goto PRH_SKIP_LNK_INFO
:PRH_LNK_INFO
echo # Shortcut name (base f.name^): !ShortcutName!
echo # Shortcut destination folder: !ShortcutDestinationFolder!
:PRH_SKIP_LNK_INFO
goto RETURN_SUCCESS

:PRINT_REPORT_SPLITTER
echo.
echo ΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ
goto RETURN_SUCCESS

::==================================================================================================

:PRINT_REPORT_OPERATIONS_SUCCESSED
echo.
echo All operations completed successfully.
echo See log files for details: "!LogFilePattern!".
goto RETURN_SUCCESS

:PRINT_REPORT_OPERATIONS_FAILED
echo.
echo ## One ore more distribution operations have failed.
echo ## See error files for details: "!ErrorFilePattern!".
echo ## See log files for details: "!LogFilePattern!".
goto RETURN_SUCCESS

::==================================================================================================

:PRINT_REPORT_SUCCESS_FOOTER
time /t
echo °° ΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔ °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
echo °° APPLICATION DISTRIBUTION ΔΔ SUCCESS °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
echo °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
goto RETURN_SUCCESS

:PRINT_REPORT_FAILURE_FOOTER
time /t
echo °° ΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔ °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
echo °° ## APPLICATION DISTRIBUTION ΔΔ FAILURE °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
echo °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
goto RETURN_SUCCESS

::==================================================================================================

:PRINT_OPERATION_LOG_HEADER
call :RESET_ERROR
echo °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
echo °° SINGLE APP.DISTRIBUTION OPERATION: °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
echo °° ΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔΔ °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
echo !DATE!
time /t
echo # Application Folder Name    : !AppFolderName!
echo # Version Subfolder Name     : !VersionFolderName!
echo # On Server Common Inner Path: !OnServerCommonInnerPath!
echo # Application Server Name    : %~n1
echo # Action                     : !Action!
echo # Task Kill Exe-Name         : !TaskKillExeName!
echo # Open Files Substring       : !OpenFilesSubstring!
if /i not "!Action!"=="REMOVE" (
if defined PostUploadScriptFile (
echo # Post Upload Script File    : !PostUploadScriptFile!
echo # Post Upload Script Error
echo   Affects Failed Servers List: !PostUploadScriptErrorAffectsFailedServerList!
))
goto :EOF

::==================================================================================================


::==================================================================================================
:RECREATE_REPORT_FOLDER
::--------------------------------------------------------------------------------------------------
echo.
echo * Recreating report folder ...
call :REMOVE_FOLDER "!ReportFolder!"
if !errorlevel! neq 0 goto RETURN_ERROR
mkdir "!ReportFolder!"
if !errorlevel! neq 0 goto RETURN_ERROR
::--------------------------------------------------------------------------------------------------
goto RETURN_SUCCESS
::==================================================================================================


::==================================================================================================
:CREATE_OPERATION_LOG_FILE
::--------------------------------------------------------------------------------------------------
verify # 2>nul
setlocal enableextensions disabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_SIMPLE_CMD_PROCESSING_RETURN_ERROR
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
set _P1_=%~1
set _FName1_=%~n1
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
verify # 2>nul
setlocal enableextensions enabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_EXTENDED_CMD_PROCESSING_RETURN_ERROR
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if exist "!_P1_!" del /q "!_P1_!"
if exist "!_P1_!" goto END_LOCAL_RETURN_ERROR

call :PREPARE_OUTPUT "!_P1_!"
if !errorlevel! neq 0 goto END_LOCAL_RETURN_ERROR

call :PRINT_OPERATION_LOG_HEADER "!_FName1_!" >> "!_P1_!"
if !errorlevel! neq 0 goto END_LOCAL_RETURN_ERROR

::--------------------------------------------------------------------------------------------------
goto CREATE_OPERATION_LOG_FILE_RETURN_SUCCESS
::==================================================================================================
:CREATE_OPERATION_LOG_FILE_RETURN_SUCCESS
endlocal
endlocal
goto RETURN_SUCCESS
:CREATE_OPERATION_LOG_FILE_RETURN_ERROR
endlocal
endlocal
goto RETURN_ERROR
::==================================================================================================


::==================================================================================================
:EXTRACT_SERVER_NAME
::--------------------------------------------------------------------------------------------------
call :RESET_ERROR
call :EXTRACT_SERVER_NAME_2 "%~n1"
goto :EOF
::--------------------------------------------------------------------------------------------------
:EXTRACT_SERVER_NAME_2
set ServerName=%~n1
goto :EOF
::==================================================================================================


::==================================================================================================
:CALL_LNK_CONVERTER
::--------------------------------------------------------------------------------------------------
verify # 2>nul
setlocal enableextensions enabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_EXTENDED_CMD_PROCESSING_RETURN_ERROR
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if not defined ShortcutTxtFileName goto CALL_LNK_CONVERTER_INCORRECT_CALL
set LnkConverter.OutputNormalWidth=79
rem set LnkConverter.SuppressUnicodeOutput=1
set LnkConverter.ReducedStartInfo=1
echo.
call $LNKCONVERT.cmd "!ShortcutTxtFileName!" /CKV /XDV
if !errorlevel! neq 0 goto END_LOCAL_RETURN_ERROR
goto END_LOCAL_RETURN_SUCCESS
::==================================================================================================
:CALL_LNK_CONVERTER_INCORRECT_CALL
echo.
echo ## Incorrect call of subroutine: "CALL_LNK_CONVERTER".
goto END_LOCAL_RETURN_ERROR
::==================================================================================================


::==================================================================================================
:SLEEP
::--------------------------------------------------------------------------------------------------
if "%~1"=="" goto SLEEP_INCORRECT_CALL
call :RESET_ERROR
timeout /t "%~1" /nobreak > nul
goto :EOF
::==================================================================================================
:SLEEP_INCORRECT_CALL
echo.
echo ## Incorrect call of subroutine: "SLEEP".
goto RETURN_ERROR
::==================================================================================================


::==================================================================================================
:DELETE_RO_FILE_VERBOSE
::--------------------------------------------------------------------------------------------------
verify # 2>nul
setlocal enableextensions disabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_SIMPLE_CMD_PROCESSING_RETURN_ERROR
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if "%~1"=="" goto DELETE_RO_FILE_VERBOSE_INCORRECT_CALL
if not exist "%~1" goto END_LOCAL_RETURN_SUCCESS
echo.
echo * Deleting file: "%~1" ...
call :RESET_ERROR
attrib -r -h "%~1"
if %errorlevel% equ 0 goto DELETE_RO_FILE_VERBOSE_DELETE
if not exist "%~1"  goto END_LOCAL_RETURN_SUCCESS
goto END_LOCAL_RETURN_ERROR
:DELETE_RO_FILE_VERBOSE_DELETE
del /q "%~1"
if not exist "%~1" goto END_LOCAL_RETURN_SUCCESS
goto END_LOCAL_RETURN_ERROR
::==================================================================================================
:DELETE_RO_FILE_VERBOSE_INCORRECT_CALL
echo.
echo ## Incorrect call of subroutine: "DELETE_RO_FILE_VERBOSE".
goto END_LOCAL_RETURN_ERROR
::==================================================================================================


::==================================================================================================
:REMOVE_FOLDER
::--------------------------------------------------------------------------------------------------
verify # 2>nul
setlocal enableextensions disabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_SIMPLE_CMD_PROCESSING_RETURN_ERROR
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if "%~1"=="" goto REMOVE_FOLDER_INCORRECT_CALL
if not exist "%~1" goto END_LOCAL_RETURN_SUCCESS
call :RESET_ERROR
attrib -r -h "%~1" /D /S
if %errorlevel% neq 0 goto END_LOCAL_RETURN_ERROR
if exist "%~1\" goto REMOVE_FOLDER_RMDIR
del /q "%~1"
if not exist "%~1" goto END_LOCAL_RETURN_SUCCESS
goto END_LOCAL_RETURN_ERROR
:REMOVE_FOLDER_RMDIR
rmdir /s /q "%~1"
if not exist "%~1" goto END_LOCAL_RETURN_SUCCESS
goto END_LOCAL_RETURN_ERROR
::==================================================================================================
:REMOVE_FOLDER_INCORRECT_CALL
echo.
echo ## Incorrect call of subroutine: "REMOVE_FOLDER".
goto END_LOCAL_RETURN_ERROR
::==================================================================================================


::==================================================================================================
:PREPARE_OUTPUT
::--------------------------------------------------------------------------------------------------
verify # 2>nul
setlocal enableextensions disabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_SIMPLE_CMD_PROCESSING_RETURN_ERROR
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
call $PREPARE_UNICODE_OUTPUT.cmd "%~1"
if %errorlevel% neq 0 goto END_LOCAL_RETURN_ERROR
goto END_LOCAL_RETURN_SUCCESS
::==================================================================================================


::==================================================================================================
:EXEC_CMD_NOLF_SUPPRESS_OUTPUT
::--------------------------------------------------------------------------------------------------
verify # 2>nul
setlocal enableextensions disabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_SIMPLE_CMD_PROCESSING_RETURN_ERROR
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
echo ^>^> %* ...
%* > nul
if %errorlevel% neq 0 goto END_LOCAL_RETURN_ERROR
goto END_LOCAL_RETURN_SUCCESS
::==================================================================================================


::==================================================================================================

:CANT_SWITCH_ON_SIMPLE_CMD_PROCESSING_RETURN_ERROR
echo.
echo ## Can not switch on simple command processing (without delayed expansion).
goto RETURN_ERROR

:CANT_SWITCH_ON_EXTENDED_CMD_PROCESSING_RETURN_ERROR
echo.
echo ## Can not switch on extended command processing.
goto RETURN_ERROR

::==================================================================================================

:RESET_ERROR
set errorlevel=0
set errorlevel=
goto :EOF

::==================================================================================================

:RETURN_SUCCESS
set errorlevel=0
set errorlevel=
goto :EOF

:RETURN_ERROR
verify # 2>nul
goto :EOF

:END_LOCAL_RETURN_SUCCESS
endlocal
goto RETURN_SUCCESS

:END_LOCAL_RETURN_ERROR
endlocal
goto RETURN_ERROR

::==================================================================================================

:ERROR
goto RETURN_ERROR

:FINISH
goto RETURN_SUCCESS

::==================================================================================================
