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

:PROCESS_AUXILIARIES
::==================================================================================================
verify # 2>nul
setlocal enableextensions disabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_SIMPLE_CMD_PROCESSING
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
set _ScriptFolderPath_=%~dp0
set _ScriptFileName_=%~nx0
SHIFT
if %errorlevel% neq 0 goto ERROR
if "%~1"==""   goto INCORRECT_BATCHPROC_CALL
if "%~1"=="\"  goto INCORRECT_BATCHPROC_CALL
if "%~1"=="\\" goto INCORRECT_BATCHPROC_CALL
if "%~2"==""   goto INCORRECT_BATCHPROC_CALL
if "%~2"=="\"  goto INCORRECT_BATCHPROC_CALL
if "%~2"=="\\" goto INCORRECT_BATCHPROC_CALL
set SourceFileBaseName=%~nx1
set DestinationFolder=%~2
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
verify # 2>nul
setlocal enableextensions enabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_EXTENDED_CMD_PROCESSING
::==================================================================================================
set SourceFile_ICO=!SourceFileBaseName!.ico
set SourceFile_CMD=!SourceFileBaseName!.cmd
set SourceFilesX_CMD=!SourceFileBaseName!.*.cmd
::--------------------------------------------------------------------------------------------------
if not exist "!SourceFile_ICO!" set SourceFile_ICO=
if not exist "!SourceFile_CMD!" set SourceFile_CMD=
if not exist "!SourceFilesX_CMD!" set SourceFilesX_CMD=
::--------------------------------------------------------------------------------------------------
if not defined SourceFile_ICO if not defined SourceFile_CMD if not defined SourceFilesX_CMD goto FINISH
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
set HasErrors=
echo.
call :PROCESS_SINGLE_FILE "SourceFile_ICO"
if !errorlevel! neq 0 set HasErrors=1
call :PROCESS_SINGLE_FILE "SourceFile_CMD"
if !errorlevel! neq 0 set HasErrors=1
call :PROCESS_MULTIPLE_FILES "SourceFilesX_CMD"
if !errorlevel! neq 0 set HasErrors=1
if "!HasErrors!"=="1" goto ERROR
::--------------------------------------------------------------------------------------------------
goto FINISH
::==================================================================================================

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

:CANT_SWITCH_ON_EXTENDED_CMD_PROCESSING
echo.
echo ## Can not switch on extended command processing.
goto ERROR

:INCORRECT_BATCHPROC_CALL
echo.
echo ## Incorrect call or procedural batch: "%_ScriptFileName_%".
goto ERROR

::==================================================================================================
:PROCESS_SINGLE_FILE
::--------------------------------------------------------------------------------------------------
verify # 2>nul
setlocal enableextensions enabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_EXTENDED_CMD_PROCESSING_RETURN_ERROR
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if "%~1"==""                     goto PROCESS_SINGLE_FILE_INCORRECT_CALL
if not defined DestinationFolder goto PROCESS_SINGLE_FILE_INCORRECT_CALL
set SourceFileName=!%~1!
if not defined SourceFileName goto END_LOCAL_RETURN_SUCCESS
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
set TargetPath=!DestinationFolder!\!SourceFileName!
set TargetTempPath=!TargetPath!.tmp
echo === Uploading auxiliary file "!SourceFileName!": ===
attrib -h "!TargetTempPath!" > nul 2>&1
if exist "!TargetTempPath!" (
call :EXEC_CMD_NOLF del /f /q "!TargetTempPath!"
if exist "!TargetTempPath!" goto PROCESS_SINGLE_FILE_FAILURE )
call :EXEC_CMD_NOLF copy /b /y "!SourceFileName!" "!TargetTempPath!"
if !errorlevel! neq 0 goto PROCESS_SINGLE_FILE_FAILURE
attrib -h "!TargetPath!" > nul 2>&1
if exist "!TargetPath!" (
call :EXEC_CMD_NOLF del /f /q "!TargetPath!"
if exist "!TargetPath!" goto PROCESS_SINGLE_FILE_FAILURE )
call :EXEC_CMD_NOLF ren "!TargetTempPath!" "!SourceFileName!"
if !errorlevel! equ 0 goto END_LOCAL_RETURN_SUCCESS
call :SLEEP "1"
call :EXEC_CMD_NOLF ren "!TargetTempPath!" "!SourceFileName!"
if !errorlevel! equ 0 goto END_LOCAL_RETURN_SUCCESS
:PROCESS_SINGLE_FILE_FAILURE
echo ## Auxiliary file uploading has failed.
echo ## Source file: "!SourceFileName!".
echo ## Target path: "!TargetPath!".
goto END_LOCAL_RETURN_ERROR
::==================================================================================================
:PROCESS_SINGLE_FILE_INCORRECT_CALL
echo.
echo ## Incorrect call of subroutine: "PROCESS_SINGLE_FILE".
goto END_LOCAL_RETURN_ERROR
::==================================================================================================

::==================================================================================================
:PROCESS_MULTIPLE_FILES
::--------------------------------------------------------------------------------------------------
verify # 2>nul
setlocal enableextensions enabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_EXTENDED_CMD_PROCESSING_RETURN_ERROR
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if "%~1"==""                     goto PROCESS_MULTIPLE_FILES_INCORRECT_CALL
if not defined DestinationFolder goto PROCESS_MULTIPLE_FILES_INCORRECT_CALL
set SourceFileNames=!%~1!
if not defined SourceFileNames goto END_LOCAL_RETURN_SUCCESS
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
set TargetPaths=!DestinationFolder!\!SourceFileNames!
set TargetTempPaths=!TargetPaths!.tmp
echo === Uploading auxiliary special files "!SourceFileNames!": ===
attrib -h "!TargetTempPaths!" > nul 2>&1
if exist "!TargetTempPaths!" (
call :EXEC_CMD_NOLF del /f /q "!TargetTempPaths!"
if exist "!TargetTempPaths!" goto PROCESS_SINGLE_FILE_FAILURE )
call :EXEC_CMD_NOLF copy /b /y "!SourceFileNames!" "!TargetTempPaths!"
if !errorlevel! neq 0 goto PROCESS_MULTIPLE_FILES_FAILURE
attrib -h "!TargetPaths!" > nul 2>&1
if exist "!TargetPaths!" (
call :EXEC_CMD_NOLF del /f /q "!TargetPaths!"
if exist "!TargetPaths!" goto PROCESS_MULTIPLE_FILES_FAILURE )
call :SLEEP "1"
set errorlevel=0
set errorlevel=
for %%f in ( "!TargetTempPaths!" ) do (
rem -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
call :EXEC_CMD_NOLF ren "%%f" "%%~nf"
if !errorlevel! neq 0 goto PROCESS_MULTIPLE_FILES_FAILURE
rem -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
)
if !errorlevel! equ 0 goto END_LOCAL_RETURN_SUCCESS
:PROCESS_MULTIPLE_FILES_FAILURE
echo ## Auxiliary special files uploading has failed.
echo ## Source files: "!SourceFileNames!".
echo ## Target paths: "!TargetPaths!".
goto END_LOCAL_RETURN_ERROR
::==================================================================================================
:PROCESS_MULTIPLE_FILES_INCORRECT_CALL
echo.
echo ## Incorrect call of subroutine: "PROCESS_MULTIPLE_FILES".
goto END_LOCAL_RETURN_ERROR
::==================================================================================================


::==================================================================================================
: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
set errorlevel=0
set errorlevel=
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

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

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

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

:RETURN_ERROR
verify # 2>nul
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
endlocal
endlocal
goto RETURN_ERROR

:FINISH
endlocal
endlocal
goto RETURN_SUCCESS

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