@echo off
setlocal

REM -- Credential Management -- (Pre-register to avoid 'net use' hanging)
REM cmdkey /add:192.168.11.81 /user:zgqq /pass:1995826zgQ 
REM cmdkey /add:192.168.11.50 /user:debian /pass:zgqq

REM -- Configuration Area -- 
REM Define total number of mappings
set MAPPINGS_COUNT=3

REM Mapping #1
set SHARE_1=\\192.168.11.89\shared
set DRIVE_1=Z:
set USER_1=zgqq
set PASS_1=zgqq

REM Mapping #2
set SHARE_2=\\192.168.11.81\machines
set DRIVE_2=Y:
set USER_2=zgqq
set PASS_2=1995826zgQ


REM Mapping #3
set SHARE_3=\\192.168.11.81\machines_cold
set DRIVE_3=X:
set USER_3=zgqq
set PASS_3=1995826zgQ


REM To add more entries, copy the four lines above, change to SHARE_4...PASS_4, and increase MAPPINGS_COUNT accordingly
REM ----------

REM -- Main Loop: Iterate through mappings by index -- 
for /L %%i in (1,1,%MAPPINGS_COUNT%) do (
    call :map_share "%%SHARE_%%i%%" "%%DRIVE_%%i%%" "%%USER_%%i%%" "%%PASS_%%i%%"
)

REM Continue to file copying section
goto :copy_files

:map_share
REM Parameters: %~1 = share, %~2 = drive, %~3 = user, %~4 = pass
set "share=%~1"
set "drive=%~2"
set "user=%~3"
set "pass=%~4"

REM First delete existing mapping (attempt regardless of existence)
net use %drive% /delete /y >nul 2>&1

REM Then create new mapping and make it persistent
net use %drive% "%share%" "%pass%" /user:"%user%" /persistent:yes >nul 2>&1

REM Print result with timestamp
if errorlevel 1 (
    echo [%date% %time%] Mount failed: %share% to %drive%
) else (
    echo [%date% %time%] Mount successful: %share% to %drive%
)

goto :eof

:copy_files
echo Copying files from Z:\git-repo\oh-my-windows\win11common...

set "DESKTOP=%USERPROFILE%\Desktop\win11common"

@REM xcopy /Y /E /I "Z:\git-repo\oh-my-windows\win11common\*" "%~dp0"
xcopy /Y /E /I "Z:\git-repos\oh-my-windows\win11common\*" "%DESKTOP%"

echo to %DESKTOP%

if errorlevel 1 (
    echo Copy failed with error code %errorlevel%
) else (
    echo Files copied successfully
)

timeout /t 3 /nobreak >nul
