@echo off
setlocal DisableDelayedExpansion

rem Brotech Azure Upload Tool v2.0
rem Uploads one file or the contents of one directory to Azure Blob Storage.
rem Authentication: AzCopy login/managed identity, or AZURE_UPLOAD_SAS in the environment.
rem No credentials belong in this file.

set "SOURCE=%~1"
set "CLIENT=%~2"
set "REMOTE_PATH=%~3"
set "STORAGE_ACCOUNT=%AZURE_STORAGE_ACCOUNT%"
set "CONTAINER=%AZURE_STORAGE_CONTAINER%"
set "SAS_TOKEN=%AZURE_UPLOAD_SAS%"
set "OVERWRITE=false"

if "%SOURCE%"=="" goto :usage
if "%CLIENT%"=="" goto :usage
if not defined STORAGE_ACCOUNT (
    echo ERROR: AZURE_STORAGE_ACCOUNT is not set.
    exit /b 2
)
if not defined CONTAINER (
    echo ERROR: AZURE_STORAGE_CONTAINER is not set.
    exit /b 2
)
if not exist "%SOURCE%" (
    echo ERROR: Source does not exist.
    exit /b 2
)

rem Validate URL path components and Azure naming rules without echoing credentials.
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "$ErrorActionPreference='Stop'; $a=$env:STORAGE_ACCOUNT; $c=$env:CONTAINER; $n=$env:CLIENT; $p=$env:REMOTE_PATH; $s=$env:SAS_TOKEN; if ($a -notmatch '^[a-z0-9]{3,24}$') { exit 1 }; if ($c -notmatch '^(?=.{3,63}$)(?!-)[a-z0-9-]+(?<!-)$' -or $c -match '--') { exit 1 }; foreach ($x in @($n,$p)) { if ($x -and ($x -notmatch '^[A-Za-z0-9][A-Za-z0-9._/-]*$' -or $x -match '(^|/)\.\.?(/|$)' -or $x -match '//')) { exit 1 } }; if ($s -and ($s.StartsWith('?') -or $s -match '[\r\n]' -or $s.IndexOf([char]34) -ge 0)) { exit 1 }"
if errorlevel 1 (
    echo ERROR: Invalid storage name, container, client/path, or SAS token format.
    exit /b 2
)

if /i "%AZURE_UPLOAD_OVERWRITE%"=="true" set "OVERWRITE=true"

set "AZCOPY=%~dp0azcopy.exe"
if exist "%AZCOPY%" goto :azcopy_found
where azcopy.exe >nul 2>&1
if errorlevel 1 (
    echo ERROR: AzCopy was not found beside this script or on PATH.
    exit /b 3
)
set "AZCOPY=azcopy.exe"
:azcopy_found

set "BASE_PATH=%CLIENT%"
if not "%REMOTE_PATH%"=="" set "BASE_PATH=%BASE_PATH%/%REMOTE_PATH%"
set "DEST_BASE=https://%STORAGE_ACCOUNT%.blob.core.windows.net/%CONTAINER%/%BASE_PATH%"

set "IS_DIR=0"
if exist "%SOURCE%\*" set "IS_DIR=1"

echo Azure Blob upload
echo Source: %SOURCE%
echo Destination: %DEST_BASE%
echo Overwrite existing blobs: %OVERWRITE%

if "%IS_DIR%"=="1" goto :upload_directory

for %%F in ("%SOURCE%") do set "FILENAME=%%~nxF"
set "DEST_URL=%DEST_BASE%/%FILENAME%"
if defined SAS_TOKEN set "DEST_URL=%DEST_URL%?%SAS_TOKEN%"
"%AZCOPY%" copy "%SOURCE%" "%DEST_URL%" --overwrite=%OVERWRITE%
set "RESULT=%ERRORLEVEL%"
goto :finish

:upload_directory
set "DEST_URL=%DEST_BASE%/"
if defined SAS_TOKEN set "DEST_URL=%DEST_URL%?%SAS_TOKEN%"
"%AZCOPY%" copy "%SOURCE%\*" "%DEST_URL%" --recursive=true --overwrite=%OVERWRITE%
set "RESULT=%ERRORLEVEL%"

:finish
if not "%RESULT%"=="0" (
    echo ERROR: AzCopy failed with exit code %RESULT%.
    exit /b %RESULT%
)
echo Upload completed. Destination identifiers do not grant public access.
exit /b 0

:usage
echo Usage: azure-upload-v2.bat "source-file-or-folder" client [remote-path]
echo Set AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_CONTAINER first.
echo Authenticate AzCopy with Microsoft Entra ID, or set AZURE_UPLOAD_SAS securely.
exit /b 2
