::==================================================================================================
::== COMMAND LINE UTILITIES / $MAPPDISTR IMPLEMENTATION; AUTHOR: SERGEI Y.KITAEV ===================
::==================================================================================================
@echo off
if "%~1"=="(InternalCall)" goto UPLOAD_APPLICATION
echo.
echo ## Incorrect internal call of "%~nx0" ($XAppDistr implementation).
echo 
pause
EXIT 255

:UPLOAD_APPLICATION
set PostUploadScriptError=0
::==================================================================================================
if not defined AppFolderName           goto APP_FOLDER_NAME_NOT_SPEC
if not defined ServerName              goto SERVER_NAME_NOT_SPEC
if not defined Action                  goto ACTION_NOT_SPEC
if not defined DestinationFolder       goto DESTINATION_FOLDER_NOT_SPEC
if not defined UpperFolderPathOnServer goto UPPER_FOLDER_PATH_ON_SERVER_NOT_SPEC
::--------------------------------------------------------------------------------------------------
if /i "!Action!"=="DISTRIBUTE"         goto ACTION_OK
if /i "!Action!"=="UPDATE"             goto ACTION_OK
if /i "!Action!"=="REMOVE"             goto ACTION_OK
goto INCORRECT_VALUE_ACTION
:ACTION_OK
::--------------------------------------------------------------------------------------------------

set AppFolderTargerPath=!DestinationFolder!

set AppOrVersionFolderTargerPath=!AppFolderTargerPath!
if defined VersionFolderName set AppOrVersionFolderTargerPath=!AppFolderTargerPath!\!VersionFolderName!

set TempFolderName_Upload=$AppDistr.!AppFolderName!.Upload
set TempFolderName_Update=$AppDistr.!AppFolderName!.Update
set TempFolderName_Removal=$AppDistr.!AppFolderName!.Removal

set TempFolderPath_Upload=!UpperFolderPathOnServer!\!TempFolderName_Upload!
set TempFolderPath_Update=!UpperFolderPathOnServer!\!TempFolderName_Update!
set TempFolderPath_Removal=!UpperFolderPathOnServer!\!TempFolderName_Removal!

if not defined VersionFolderName goto CLEAR_VERSION_PROPS
if /i "Action"=="REMOVE"         goto CLEAR_VERSION_PROPS

set VersionTempFolderName_Intermediate=$AppDistr.Version.Intermediate
set VersionFolderSourcePath=!VersionFolderName!
set VersionFolderTargetPath=!AppFolderTargerPath!\!VersionFolderName!
set VersionTempFolderPath_Upload=!TempFolderPath_Upload!\!VersionTempFolderName_Intermediate!
set VersionTempFolderPath_Update=!TempFolderPath_Update!\!VersionTempFolderName_Intermediate!
set VersionTempFolderPath_Update_OrigVerFolderName=!TempFolderPath_Update!\!VersionFolderName!
set VersionTempFolderPath_InAppFolder=!AppFolderTargerPath!\!VersionTempFolderName_Intermediate!
goto END_VERSION_PROPS
:CLEAR_VERSION_PROPS
set VersionTempFolderName_Intermediate=
set VersionFolderSourcePath=
set VersionFolderTargetPath=
set VersionTempFolderPath_Upload=
set VersionTempFolderPath_Update=
set VersionTempFolderPath_Update_OrigVerFolderName=
set VersionTempFolderPath_InAppFolder=
:END_VERSION_PROPS
::--------------------------------------------------------------------------------------------------

if /i "!Action!"=="DISTRIBUTE" echo ÍÍÍ Distributing "!AppFolderName!" app.: ÍÍÍ
if /i "!Action!"=="UPDATE"     echo ÍÍÍ Updating "!AppFolderName!" app.: ÍÍÍ
if /i "!Action!"=="REMOVE"     echo ÍÍÍ Removing "!AppFolderName!" app.: ÍÍÍ
if !errorlevel! neq 0 goto ERROR
time /t
if !errorlevel! neq 0 goto ERROR
echo ÍÍ^> "!DestinationFolder!":
if !errorlevel! neq 0 goto ERROR
echo.

rem echo ˆáªãááâ¢¥­­ ï ®è¨¡ª  ®â« ¤ª¨ ¢ "%~dpnx0".
rem goto FAILURE

if /i "!Action!"=="DISTRIBUTE" goto INSTALL_APPLICATION
if /i "!Action!"=="UPDATE"     goto UPDATE_APPLICATION
::==================================================================================================
call :REMOVE_APP_FOLDER
if !errorlevel! neq 0 goto FAILURE
call :REMOVE_UPLOAD_FOLDER
if !errorlevel! neq 0 goto FAILURE
call :REMOVE_UPDATE_FOLDER
if !errorlevel! neq 0 goto FAILURE
goto COMPLETE
::==================================================================================================
:INSTALL_APPLICATION

call :RESET_ERROR
if     defined VersionFolderName call :UPLOAD_VERSION_FOLDER
if not defined VersionFolderName call :UPLOAD_APP_FOLDER
if !errorlevel! neq 0 goto FAILURE

call :REMOVE_UPDATE_FOLDER
if !errorlevel! neq 0 goto FAILURE

goto POSTUPLOAD_ACTIONS
::==================================================================================================
:UPDATE_APPLICATION
if not exist "!AppOrVersionFolderTargerPath!\" goto INSTALL_APPLICATION

call :RESET_ERROR
if     defined VersionFolderName call :UPDATE_VERSION_FOLDER
if not defined VersionFolderName call :UPDATE_APP_FOLDER
if !errorlevel! neq 0 goto FAILURE

call :REMOVE_UPLOAD_FOLDER
if !errorlevel! neq 0 goto FAILURE
::==================================================================================================
:POSTUPLOAD_ACTIONS
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if defined VersionFolderName (
call :PROCESS_AUXILIARIES "!AppFolderName!" "!AppFolderTargerPath!"
if !errorlevel! neq 0 goto FAILURE )

if not defined PostUploadScriptFile goto COMPLETE

echo * Executing post-upload script ...
call :RESET_ERROR
call !PostUploadScriptFile! "!AppFolderName!" "!ServerName!" "!AppOrVersionFolderTargerPath!"
if !errorlevel! neq 0 goto POST_UPLOAD_COMMAND_FAILURE

::--------------------------------------------------------------------------------------------------
:COMPLETE
if /i "!Action!"=="REMOVE"       goto COMPLETE_WITH_OK
if defined PostUploadScriptFile  goto COMPLETE_WITH_OK
goto FINISH
:COMPLETE_WITH_OK
echo ## OK
goto FINISH
::==================================================================================================
:FAILURE
echo ## *** ERROR ***
goto ERROR
::==================================================================================================
:POST_UPLOAD_COMMAND_FAILURE
echo ## *** ERROR (POST-UPLOAD COMMAND) ***
endlocal
set PostUploadScriptError=1
goto RETURN_ERROR
::==================================================================================================

:APP_FOLDER_NAME_NOT_SPEC
echo.
echo ## Internal variable-parameter was not specitied: "AppFolderName".
goto ERROR

:SERVER_NAME_NOT_SPEC
echo.
echo ## Internal variable-parameter was not specitied: "ServerName".
goto ERROR

:ACTION_NOT_SPEC
echo.
echo ## Internal variable-parameter was not specitied: "Action".
goto ERROR

:DESTINATION_FOLDER_NOT_SPEC
echo.
echo ## Internal variable-parameter was not specitied: "DestinationFolder".
goto ERROR

:UPPER_FOLDER_PATH_ON_SERVER_NOT_SPEC
echo.
echo ## Internal variable-parameter was not specitied: "UpperFolderPathOnServer".
goto ERROR

:INCORRECT_VALUE_ACTION
echo.
echo ## Incorrect value of "Action" (variable-parameter): "!Action!".
goto ERROR

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


::==================================================================================================
:UPLOAD_APP_FOLDER
::--------------------------------------------------------------------------------------------------
call :REMOVE_UPLOAD_FOLDER
if !errorlevel! neq 0 goto RETURN_ERROR
call :COPY_FOLDER "!AppFolderName!" "!TempFolderPath_Upload!"
if !errorlevel! neq 0 goto RETURN_ERROR
call :REMOVE_APP_FOLDER
if !errorlevel! neq 0 goto RETURN_ERROR
call :RENAME_FOLDER_PERSISTENT "!TempFolderPath_Upload!" "!AppFolderName!"
goto :EOF
::==================================================================================================

::==================================================================================================
:UPLOAD_VERSION_FOLDER
::--------------------------------------------------------------------------------------------------
if not defined VersionFolderName goto UPLOAD_VERSION_FOLDER_INCORRECT_CALL

call :REMOVE_UPLOAD_FOLDER
if !errorlevel! neq 0 goto RETURN_ERROR
call :COPY_FOLDER "!VersionFolderSourcePath!" "!VersionTempFolderPath_Upload!"
if !errorlevel! neq 0 goto RETURN_ERROR
call :RENAME_FOLDER_PERSISTENT "!VersionTempFolderPath_Upload!" "!VersionFolderName!"
if !errorlevel! neq 0 goto RETURN_ERROR

if not exist "!AppFolderTargerPath!" goto UPLOAD_VERSION_FOLDER_RENAME_TO_TARGET
call :REMOVE_FOLDER "!TempFolderPath_Removal!"
if !errorlevel! neq 0 goto RETURN_ERROR
call :RENAME_FOLDER_PERSISTENT_SE "!AppFolderTargerPath!" "!TempFolderName_Removal!"
if !errorlevel! equ 0 goto UPLOAD_VERSION_FOLDER_RENAME_TO_TARGET

if exist "!AppFolderTargerPath!\" goto UPLOAD_VERSION_FOLDER_VER_ONLY
call :DELETE_READONLY_FILE "!AppFolderTargerPath!"
if !errorlevel! neq 0 goto RETURN_ERROR

:UPLOAD_VERSION_FOLDER_RENAME_TO_TARGET
call :RENAME_FOLDER_PERSISTENT "!TempFolderPath_Upload!" "!AppFolderName!"
if !errorlevel! neq 0 goto RETURN_ERROR

call :REMOVE_FOLDER_PERSISTENT "!TempFolderPath_Removal!"
goto :EOF
::--------------------------------------------------------------------------------------------------
:UPLOAD_VERSION_FOLDER_VER_ONLY
if not exist "!VersionFolderTargetPath!"  goto UPLOAD_VERSION_FOLDER_EXPOSE_VERFOLDER
if     exist "!VersionFolderTargetPath!\" goto UPLOAD_VERSION_FOLDER_REMOVE_VERFOLDER

call :DELETE_READONLY_FILE "!VersionFolderTargetPath!"
if !errorlevel! neq 0 goto RETURN_ERROR
goto UPLOAD_VERSION_FOLDER_EXPOSE_VERFOLDER

:UPLOAD_VERSION_FOLDER_REMOVE_VERFOLDER
call :MOVE_FOLDER_PERSISTENT "!VersionFolderTargetPath!" "!TempFolderPath_Removal!"
rem if !errorlevel! neq 0 goto RETURN_ERROR
goto RETURN_SUCCESS

:UPLOAD_VERSION_FOLDER_EXPOSE_VERFOLDER
call :MOVE_FOLDER_PERSISTENT "!VersionTempFolderPath_Upload!" "!VersionFolderTargetPath!"
if !errorlevel! neq 0 goto RETURN_ERROR

call :REMOVE_FOLDER_PERSISTENT "!TempFolderPath_Removal!"
if !errorlevel! neq 0 goto RETURN_ERROR

call :REMOVE_UPLOAD_FOLDER
goto :EOF
::==================================================================================================
:UPLOAD_VERSION_FOLDER_INCORRECT_CALL
echo.
echo ## Incorrect call of subroutine: "UPLOAD_VERSION_FOLDER".
goto RETURN_ERROR
::==================================================================================================


::==================================================================================================
:UPDATE_APP_FOLDER
::--------------------------------------------------------------------------------------------------
call :RENAME_APP_FOLDER_TO_UPDATE
if !errorlevel! neq 0 goto RETURN_ERROR
call :COPY_FOLDER_UPDATE "!AppFolderName!" "!TempFolderPath_Update!"
if !errorlevel! neq 0 goto RETURN_ERROR
call :RENAME_FOLDER_PERSISTENT "!TempFolderPath_Update!" "!AppFolderName!"
goto :EOF
::==================================================================================================

::==================================================================================================
:UPDATE_VERSION_FOLDER
::--------------------------------------------------------------------------------------------------
if not defined VersionFolderName goto UPDATE_VERSION_FOLDER_INCORRECT_CALL
call :RENAME_APP_FOLDER_TO_UPDATE
if !errorlevel! neq 0 goto RETURN_ERROR
call :REMOVE_FOLDER_PERSISTENT "!VersionTempFolderPath_Update!"
if !errorlevel! neq 0 goto RETURN_ERROR
call :RENAME_FOLDER_PERSISTENT "!VersionTempFolderPath_Update_OrigVerFolderName!" "!VersionTempFolderName_Intermediate!"
if !errorlevel! neq 0 goto RETURN_ERROR
call :COPY_FOLDER_UPDATE "!VersionFolderSourcePath!" "!VersionTempFolderPath_Update!"
if !errorlevel! neq 0 goto RETURN_ERROR
call :CREATE_FOLDER "!AppFolderTargerPath!"
if !errorlevel! neq 0 goto RETURN_ERROR
call :MOVE_FOLDER_PERSISTENT "!VersionTempFolderPath_Update!" "!VersionFolderTargetPath!"
if !errorlevel! neq 0 goto RETURN_ERROR
call :REMOVE_UPDATE_FOLDER
goto :EOF
::==================================================================================================
:UPDATE_VERSION_FOLDER_INCORRECT_CALL
echo.
echo ## Incorrect call of subroutine: "UPDATE_VERSION_FOLDER".
goto RETURN_ERROR
::==================================================================================================


::==================================================================================================
:REMOVE_APP_FOLDER
::--------------------------------------------------------------------------------------------------
call :REMOVE_FOLDER "!TempFolderPath_Removal!"
if !errorlevel! neq 0 goto RETURN_ERROR

if not exist "!AppFolderTargerPath!"   goto RETURN_SUCCESS
if     exist "!AppFolderTargerPath!\"  goto REMOVE_APP_FOLDER_REMOVE

call :DELETE_READONLY_FILE "!AppFolderTargerPath!"
if !errorlevel! neq 0 goto RETURN_ERROR
goto RETURN_SUCCESS

:REMOVE_APP_FOLDER_REMOVE
call :RENAME_FOLDER_PERSISTENT "!AppFolderTargerPath!" "!TempFolderName_Removal!"
if !errorlevel! equ 0 goto REMOVE_APP_FOLDER_CLEANUP
call :RESET_CONNECTIONS
if !errorlevel! neq 0 goto RETURN_ERROR
if exist "!AppFolderTargerPath!\" (
call :RENAME_FOLDER_PERSISTENT "!AppFolderTargerPath!" "!TempFolderName_Removal!" "1"
if !errorlevel! neq 0 goto RETURN_ERROR )

:REMOVE_APP_FOLDER_CLEANUP
call :REMOVE_FOLDER_PERSISTENT "!TempFolderPath_Removal!"
goto :EOF
::==================================================================================================


::==================================================================================================
:RENAME_APP_FOLDER_TO_UPDATE
::--------------------------------------------------------------------------------------------------
call :REMOVE_FOLDER "!TempFolderPath_Removal!"
if !errorlevel! neq 0 goto RETURN_ERROR
call :REMOVE_UPDATE_FOLDER
if !errorlevel! neq 0 goto RETURN_ERROR
call :RENAME_FOLDER_PERSISTENT "!AppFolderTargerPath!" "!TempFolderName_Update!"
if !errorlevel! equ 0 goto RETURN_SUCCESS
call :RESET_CONNECTIONS
if !errorlevel! neq 0 goto RETURN_ERROR
if exist "!AppFolderTargerPath!\" (
call :RENAME_FOLDER_PERSISTENT "!AppFolderTargerPath!" "!TempFolderName_Update!" "1"
if !errorlevel! neq 0 goto RETURN_ERROR )
::--------------------------------------------------------------------------------------------------
goto RETURN_SUCCESS
::==================================================================================================


::==================================================================================================
:REMOVE_UPLOAD_FOLDER
::--------------------------------------------------------------------------------------------------
if not exist "!TempFolderPath_Upload!" goto RETURN_SUCCESS
if exist "!TempFolderPath_Upload!\" goto REMOVE_UPLOAD_FOLDER_REMOVE

call :DELETE_READONLY_FILE "!TempFolderPath_Upload!"
if !errorlevel! neq 0 goto RETURN_ERROR
goto RETURN_SUCCESS

:REMOVE_UPLOAD_FOLDER_REMOVE
call :REMOVE_FOLDER "!TempFolderPath_Removal!"
if !errorlevel! neq 0 goto RETURN_ERROR
call :RENAME_FOLDER_PERSISTENT "!TempFolderPath_Upload!" "!TempFolderName_Removal!"
if !errorlevel! neq 0 goto RETURN_ERROR
call :REMOVE_FOLDER_PERSISTENT "!TempFolderPath_Removal!"
goto :EOF
::==================================================================================================

::==================================================================================================
:REMOVE_UPDATE_FOLDER
::--------------------------------------------------------------------------------------------------
if not exist "!TempFolderPath_Update!" goto RETURN_SUCCESS
if exist "!TempFolderPath_Update!\" goto REMOVE_UPDATE_FOLDER_REMOVE

call :DELETE_READONLY_FILE "!TempFolderPath_Update!"
if !errorlevel! neq 0 goto RETURN_ERROR
goto RETURN_SUCCESS

:REMOVE_UPDATE_FOLDER_REMOVE
call :REMOVE_FOLDER "!TempFolderPath_Removal!"
if !errorlevel! neq 0 goto RETURN_ERROR
call :RENAME_FOLDER_PERSISTENT "!TempFolderPath_Update!" "!TempFolderName_Removal!"
if !errorlevel! neq 0 goto RETURN_ERROR
call :REMOVE_FOLDER_PERSISTENT "!TempFolderPath_Removal!"
goto :EOF
::==================================================================================================


::==================================================================================================
:RESET_CONNECTIONS
::--------------------------------------------------------------------------------------------------
if not defined TaskKillExeName goto RESET_CONNECTIONS_DISCONNECT_FILES
for /f %%t in ('"TaskList /s "!ServerName!" | Find /i "!TaskKillExeName!""') do (
call :EXEC_CMD_NOLF TaskKill /s "!ServerName!" /im "!TaskKillExeName!"
rem echo * Killing task: "!TaskKillExeName!" (on "!ServerName!"^) ...
rem TaskKill /s "!ServerName!" /im "!TaskKillExeName!"
goto RESET_CONNECTIONS_DISCONNECT_FILES
)
:RESET_CONNECTIONS_DISCONNECT_FILES
if not defined OpenFilesSubstring goto RETURN_SUCCESS
for /f "tokens=2 delims=," %%i in ('"OpenFiles /query /v /fo CSV /s "!ServerName!" | Find /i "!OpenFilesSubstring!""') do (
call :EXEC_CMD_NOLF OpenFiles /disconnect /s "!ServerName!" /o * /id "%%i"
rem echo * Disconnecting open file: "%%i" (on "!ServerName!"^) ...
rem OpenFiles /disconnect /s "!ServerName!" /o * /id "%%i"
)
::--------------------------------------------------------------------------------------------------
goto RETURN_SUCCESS
::==================================================================================================


::==================================================================================================
:CREATE_FOLDER
::--------------------------------------------------------------------------------------------------
verify # 2>nul
setlocal enableextensions disabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_SIMPLE_CMD_PROCESSING_RETURN_ERROR
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if "%~1"==""   goto CREATE_FOLDER_INCORRECT_CALL
if "%~1"=="\"  goto CREATE_FOLDER_INCORRECT_CALL
if "%~1"=="\\" goto CREATE_FOLDER_INCORRECT_CALL
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
echo * Creating folder: "%~1" ...
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
call :RESET_ERROR
mkdir "%~1"
if %errorlevel% equ 0 goto END_LOCAL_RETURN_SUCCESS
echo.
echo ## Folder creation has failed.
echo ## Path: "%~1".
goto END_LOCAL_RETURN_ERROR
::==================================================================================================
:CREATE_FOLDER_INCORRECT_CALL
echo.
echo ## Incorrect call of subroutine: "CREATE_FOLDER".
goto END_LOCAL_RETURN_ERROR
::==================================================================================================


::==================================================================================================
:COPY_FOLDER
::--------------------------------------------------------------------------------------------------
verify # 2>nul
setlocal enableextensions disabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_SIMPLE_CMD_PROCESSING_RETURN_ERROR
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if "%~1"==""   goto COPY_FOLDER_INCORRECT_CALL
if "%~1"=="\"  goto COPY_FOLDER_INCORRECT_CALL
if "%~1"=="\\" goto COPY_FOLDER_INCORRECT_CALL
if "%~2"==""   goto COPY_FOLDER_INCORRECT_CALL
if "%~2"=="\"  goto COPY_FOLDER_INCORRECT_CALL
if "%~2"=="\\" goto COPY_FOLDER_INCORRECT_CALL
if not exist "%~1\" goto COPY_FOLDER_SOURCE_NOT_EXIST
if exist "%~2" goto COPY_FOLDER_TARGET_ALREADY_EXISTS
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
echo * XCOPY: "%~1" ^>^> "%~2" ...
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
call :RESET_ERROR
xcopy /q /i /e /k /y "%~1" "%~2"
if %errorlevel% equ 0 goto END_LOCAL_RETURN_SUCCESS
echo.
echo ## XCopy operation has failed.
echo ## Source path: "%~1".
echo ## Target path: "%~2".
goto END_LOCAL_RETURN_ERROR
::==================================================================================================
:COPY_FOLDER_INCORRECT_CALL
echo.
echo ## Incorrect call of subroutine: "COPY_FOLDER".
goto END_LOCAL_RETURN_ERROR
::--------------------------------------------------------------------------------------------------
:COPY_FOLDER_SOURCE_NOT_EXIST
echo.
echo ## Source folder not found: "%~1" (folder copying).
goto END_LOCAL_RETURN_ERROR
::--------------------------------------------------------------------------------------------------
:COPY_FOLDER_TARGET_ALREADY_EXISTS
echo.
echo ## Filename allready exists: "%~2" (folder copying).
goto END_LOCAL_RETURN_ERROR
::==================================================================================================

::==================================================================================================
:COPY_FOLDER_UPDATE
::--------------------------------------------------------------------------------------------------
verify # 2>nul
setlocal enableextensions disabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_SIMPLE_CMD_PROCESSING_RETURN_ERROR
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if "%~1"==""   goto COPY_FOLDER_UPDATE_INCORRECT_CALL
if "%~1"=="\"  goto COPY_FOLDER_UPDATE_INCORRECT_CALL
if "%~1"=="\\" goto COPY_FOLDER_UPDATE_INCORRECT_CALL
if "%~2"==""   goto COPY_FOLDER_UPDATE_INCORRECT_CALL
if "%~2"=="\"  goto COPY_FOLDER_UPDATE_INCORRECT_CALL
if "%~2"=="\\" goto COPY_FOLDER_UPDATE_INCORRECT_CALL
if not exist "%~1\" goto COPY_FOLDER_UPDATE_SOURCE_NOT_EXISTS
if not exist "%~2\" goto COPY_FOLDER_UPDATE_TARGET_NOT_EXISTS
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
echo * XCOPY update: "%~1" ^>^> "%~2" ...
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
call :RESET_ERROR
xcopy /q /i /e /k /d /y "%~1" "%~2"
if %errorlevel% equ 0 goto END_LOCAL_RETURN_SUCCESS
echo.
echo ## XCopy update operation has failed.
echo ## Source path: "%~1".
echo ## Target path: "%~2".
goto END_LOCAL_RETURN_ERROR
::==================================================================================================
:COPY_FOLDER_UPDATE_INCORRECT_CALL
echo.
echo ## Incorrect call of subroutine: "COPY_FOLDER_UPDATE".
goto END_LOCAL_RETURN_ERROR
::--------------------------------------------------------------------------------------------------
:COPY_FOLDER_UPDATE_SOURCE_NOT_EXISTS
echo.
echo ## Source folder not found: "%~1" (folder update).
goto END_LOCAL_RETURN_ERROR
:COPY_FOLDER_UPDATE_TARGET_NOT_EXISTS
echo.
echo ## Target folder not exists: "%~2" (folder update).
goto END_LOCAL_RETURN_ERROR
::==================================================================================================


::==================================================================================================
:DELETE_READONLY_FILE
::--------------------------------------------------------------------------------------------------
verify # 2>nul
setlocal enableextensions disabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_SIMPLE_CMD_PROCESSING_RETURN_ERROR
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if "%~1"==""   goto DELETE_READONLY_FILE_INCORRECT_CALL
if "%~1"=="\"  goto DELETE_READONLY_FILE_INCORRECT_CALL
if "%~1"=="\\" goto DELETE_READONLY_FILE_INCORRECT_CALL
if not exist "%~1" goto END_LOCAL_RETURN_SUCCESS
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
echo * Deleting file: "%~1" ...
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
attrib -r -h "%~1"
del /f /q "%~1"
if not exist "%~1" goto END_LOCAL_RETURN_SUCCESS
echo.
echo ## File removal has failed.
echo ## Path: "%~1".
goto END_LOCAL_RETURN_ERROR
::==================================================================================================
:DELETE_READONLY_FILE_INCORRECT_CALL
echo.
echo ## Incorrect call of subroutine: "DELETE_READONLY_FILE".
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 "%~1"=="\"  goto REMOVE_FOLDER_INCORRECT_CALL
if "%~1"=="\\" goto REMOVE_FOLDER_INCORRECT_CALL
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if not exist "%~1" goto END_LOCAL_RETURN_SUCCESS
echo * Removing folder: "%~1" ...
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
call :RESET_ERROR
attrib -r -h "%~1" /D /S
if %errorlevel% neq 0 goto REMOVE_FOLDER_FAILURE
rmdir /s /q "%~1" > nul 2>&1
if not exist "%~1" goto END_LOCAL_RETURN_SUCCESS
del /q "%~1" > nul 2>&1
if not exist "%~1" goto END_LOCAL_RETURN_SUCCESS
rmdir /s /q "%~1"
if not exist "%~1" goto END_LOCAL_RETURN_SUCCESS
:REMOVE_FOLDER_FAILURE
echo.
echo ## Folder removal has failed.
echo ## Path: "%~1".
goto END_LOCAL_RETURN_ERROR
::==================================================================================================
:REMOVE_FOLDER_INCORRECT_CALL
echo.
echo ## Incorrect call of subroutine: "REMOVE_FOLDER".
goto END_LOCAL_RETURN_ERROR
::==================================================================================================


::==================================================================================================
:REMOVE_FOLDER_PERSISTENT
::--------------------------------------------------------------------------------------------------
verify # 2>nul
setlocal enableextensions disabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_SIMPLE_CMD_PROCESSING_RETURN_ERROR
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
call "%~dp0.remove_folder_persistent.cmd" "(InternalCall)" "%~1"
if %errorlevel% neq 0 goto END_LOCAL_RETURN_ERROR
goto END_LOCAL_RETURN_SUCCESS
::==================================================================================================

::==================================================================================================
:RENAME_FOLDER_PERSISTENT
::--------------------------------------------------------------------------------------------------
verify # 2>nul
setlocal enableextensions disabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_SIMPLE_CMD_PROCESSING_RETURN_ERROR
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if "%~3"=="1" if not exist "%~1\" if exist "%~2\" goto END_LOCAL_RETURN_SUCCESS
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
call "%~dp0.rename_folder_persistent.cmd" "(InternalCall)" "%~1" "%~2"
if %errorlevel% neq 0 goto END_LOCAL_RETURN_ERROR
goto END_LOCAL_RETURN_SUCCESS
::==================================================================================================

::==================================================================================================
:RENAME_FOLDER_PERSISTENT_SE
::--------------------------------------------------------------------------------------------------
verify # 2>nul
setlocal enableextensions disabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_SIMPLE_CMD_PROCESSING_RETURN_ERROR
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if "%~3"=="1" if not exist "%~1\" if exist "%~2\" goto END_LOCAL_RETURN_SUCCESS
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
call "%~dp0.rename_folder_persistent_se.cmd" "(InternalCall)" "%~1" "%~2"
if %errorlevel% neq 0 goto END_LOCAL_RETURN_ERROR
goto END_LOCAL_RETURN_SUCCESS
::==================================================================================================

::==================================================================================================
:MOVE_FOLDER_PERSISTENT
::--------------------------------------------------------------------------------------------------
verify # 2>nul
setlocal enableextensions disabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_SIMPLE_CMD_PROCESSING_RETURN_ERROR
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
call "%~dp0.move_folder_persistent.cmd" "(InternalCall)" "%~1" "%~2" "%~3"
if %errorlevel% neq 0 goto END_LOCAL_RETURN_ERROR
goto END_LOCAL_RETURN_SUCCESS
::==================================================================================================


::==================================================================================================
:PROCESS_AUXILIARIES
::--------------------------------------------------------------------------------------------------
verify # 2>nul
setlocal enableextensions disabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_SIMPLE_CMD_PROCESSING_RETURN_ERROR
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
call "%~dp0.process_auxiliaries.cmd" "(InternalCall)" "%~1" "%~2"
if %errorlevel% neq 0 goto END_LOCAL_RETURN_ERROR
goto END_LOCAL_RETURN_SUCCESS
::==================================================================================================


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


::==================================================================================================
: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
::==================================================================================================


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

:CANT_SWITCH_ON_SIMPLE_CMD_PROCESSING_RETURN_ERROR
echo.
echo ## Can not switch on simple command processing (without delayed expansion).
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 END_LOCAL_RETURN_ERROR

:FINISH
goto END_LOCAL_RETURN_SUCCESS

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