@echo off
setlocal EnableDelayedExpansion

:: Get current username
set "username=%USERNAME%"

:: Check if W: is reachable
if not exist "W:\%username%\" (
    echo ERROR: Network drive W: is not reachable.
    echo Please connect to the network drive and try again.
    pause
    exit /b 1
)

:: Get drive the script is running from (W: or C:)
set "curdrive=%~d0"

:: Full path to the folder containing the script
set "scriptdir=%~dp0"
set "scriptdir=%scriptdir:~0,-1%"

:: Define base paths (Documents included for local)
set "wbase=W:\%username%"
set "cbase=C:\Users\%username%\Documents"

:: Verify scriptdir starts with either wbase or cbase
echo %scriptdir% | findstr /I /B /C:"%wbase%" >nul
if errorlevel 1 (
    echo %scriptdir% | findstr /I /B /C:"%cbase%" >nul
    if errorlevel 1 (
        echo ERROR: This script should be run from a directory inside %wbase% or %cbase% 1>&2
        pause
        exit /b 1
    )
)

:: Determine relative path from user directory
if "%curdrive%"=="W:" (
    set "relative=!scriptdir:%wbase%\=!"
) else (
    set "relative=!scriptdir:%cbase%\=!"
)


:: Define source and destination paths using the same relative path
set "networkdir=%wbase%\%relative%"
set "localdir=%cbase%\%relative%"

:: Extract the final folder name for messages
for %%I in ("%scriptdir%") do set "labfolder=%%~nxI"

:: ===========================
:: Determine mode (upload/download)
:: ===========================
if "%curdrive%"=="W:" (
    set "action=Download"
    set "opposite=Upload"
    set "sourcedir=%networkdir%"
    set "destdir=%localdir%"
    set "sourceLabel=network"
    set "destLabel=local"
    set "destDrive=C:"
) else if "%curdrive%"=="C:" (
    set "action=Upload"
    set "opposite=Download"
    set "sourcedir=%localdir%"
    set "destdir=%networkdir%"
    set "sourceLabel=local"
    set "destLabel=network"
    set "destDrive=W:"
) else (
    echo ERROR: This script must be run from C: or W:
    echo Detected drive: %curdrive%
    pause
    exit /b 1
)

:: ===========================
:: User confirmation
:: ===========================
echo.
echo You are about to %action% %labfolder% and its contents
echo from %sourceLabel% directory: %sourcedir%
echo to %destLabel% directory:     %destdir%
echo.
echo Please confirm that you have closed and saved your project files before continuing.
echo.
echo If you intended to %opposite% from the %destLabel% drive %destDrive% to the %sourceLabel% drive %curdrive%,
echo then please cancel and run this script from %destdir%
echo.

if exist "%destdir%\*" (
    echo WARNING:
    echo A copy already exists on %destDrive% and will be backed up to:
    echo   "%destdir%_backup"
    echo.
)

set /p userChoice="Enter y to continue, or n to cancel: "

if /I not "!userChoice!"=="y" (
    echo Operation cancelled.
    echo.
    echo Have a great rest of your day!
    echo.
    pause
    exit /b 0
)

:: ===========================
:: Backup destination if it exists
:: ===========================

if exist "%destdir%\*" (
    echo Backing up "%destdir%" to "%destdir%_backup"...
    xcopy "%destdir%" "%destdir%_backup" /E /I /H /Y
    rmdir /S /Q "%destdir%"
)

:: ===========================
:: Copy
:: ===========================

echo %action%ing from "%sourcedir%" to "%destdir%" ...
xcopy "%sourcedir%" "%destdir%" /E /I /H /Y

if !errorlevel! equ 0 (
    echo %action% completed successfully.
) else (
    echo Error during %action%.
)

echo.
echo When you close this script, I will open file explorer to show you the copied project directory...
echo Have a great rest of your day!
echo.
pause

start "" explorer "%destdir%"

endlocal
