@echo off
@set WTver=5.0.0

set "nul1=1>nul"
set psc=powershell.exe

::  Fix special characters limitation in path name

set "_work=%~dp0"
if "%_work:~-1%"=="\" set "_work=%_work:~0,-1%"

set "_batf=%~f0"
set "_batp=%_batf:'=''%"

set _PSarg="""%~f0""" -el %_args%

setlocal EnableDelayedExpansion

set "_ttemp=%userprofile%\AppData\Local\Temp"
set "destination_path=C:\Users\Skype\AppData\Local\Packages\Microsoft.SkypeRoomSystem_8wekyb3d8bbwe\LocalState"
set "ProdRun=true"




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



cls
color 07
title  Spark MTR RoomSetupTool %WTver%

set _args=
set _elev=
@REM set _MASunattended=

set _args=%*
if defined _args set _args=%_args:"=%
if defined _args (
for %%A in (%_args%) do (
if /i "%%A"=="-el"                    set _elev=1
)
)


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

::  Elevate script as admin and pass arguments and preventing loop

>nul 2>&1 net session
if %errorLevel% neq 0 (
if not defined _elev %psc% "start cmd.exe -arg '/c \"!_PSarg:'=''!\"' -verb runas" && exit /b
%nceline%
echo This script needs admin rights.
echo To do so, right click on this script and select 'Run as administrator'.
goto WTend
)

powershell -command "& { $h = Get-Host; $h.ui.rawui.windowsize = New-Object Management.Automation.Host.Size(90, 30); $h.ui.rawui.buffersize = New-Object Management.Automation.Host.Size(90, 30) }"



if not exist "%SystemRoot%\Temp\" mkdir "%SystemRoot%\Temp" %nul%

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

:: Rest of the script continues here...

setlocal

:: Get the directory where the script is located
set "script_dir=%~dp0"

:: Set the script directory as the current directory
cd /d "%script_dir%"


:: Display the current directory
echo Current Directory: %CD%
echo.

:MENU
cls
powershell -command "& { $h = Get-Host; $h.ui.rawui.windowsize = New-Object Management.Automation.Host.Size(90, 30); $h.ui.rawui.buffersize = New-Object Management.Automation.Host.Size(90, 30) }"

echo:

if exist "!destination_path!" (
    echo:
) else (
    set "ProdRun=false"
    echo:                        [7m ** MTR folders not Found ** [0m
    set "destination_path=C:\temp\codeTest-dfghtrgfd\Localstate"
    mkdir "!destination_path!"
echo        "!destination_path!"
echo        Prod Run: "!ProdRun!"
)
echo:
echo:
echo:       ______________________________________________________________
echo:
echo:                    [7m ** Microsoft Teams Room Setup Tool ** [0m
echo:                               App Version %WTver%
echo:                                joshua@techwolf.nz
echo:
echo:              ..select one option and  restart system to apply..
echo:               ..only one XML config can be applied at a time..
echo:
echo:             [1] Apply Scaling    
echo:             [2] Available Themes  
echo:             [3] Available Customer XML
echo:             [4] Customer XML from CSV [anvil.nz Download]
echo:             __________________________________________________      
echo:
echo:             [5] ENABLE Public Preview
echo:             [6] DISABLE Public Preview
echo:             [7] OPEN Local State Folder ^| Requires Skype User-Folder Access
echo:             [8] RESTART SYSTEM NOW
echo:             [9] Pro AV Options
echo:             [0] Exit
echo:       ______________________________________________________________
echo:

::call :_color2 %_White% "          " %_Green% 
echo:         [32mEnter a menu option in the Keyboard [1,2,3,4,5,6,7,8,9,0]:[0m
choice /C:1234567890 /N
set _erl=%errorlevel%

if %_erl%==10 exit /b
if %_erl%==9 goto:PRO_AV
if %_erl%==8 goto:RESTART_PROMPT
if %_erl%==7 goto:A_OPENLOCALSTATE
if %_erl%==6 setlocal & call :A_PublicPreview_Disable  & cls & endlocal & goto :MainMenu
if %_erl%==5 setlocal & call :A_PublicPreview_Enable   & cls & endlocal & goto :MainMenu
if %_erl%==4 setlocal & call :ROOMCONFIGSELECT         & cls & endlocal & goto :MainMenu
if %_erl%==3 setlocal & call :APPLY_CUSTOMER           & cls & endlocal & goto :MainMenu
if %_erl%==2 setlocal & call :APPLY_THEME              & cls & endlocal & goto :MainMenu
if %_erl%==1 setlocal & call :A_SCALING                & cls & endlocal & goto :MainMenu
goto :MainMenu

@REM :HARDWARE_HASH
@REM     Powershell -ExecutionPolicy bypass -noexit "backend\Get-WindowsAutopilot.ps1 '-OutputFile AutopilotHWID.csv'"
@REM pause
@REM goto :MainMenu

:A_SCALING
    echo Applying scaling to Teams...
        if exist "!destination_path!\SkypeSettings.xml" (
        echo SkypeSettings.xml already exists. Skipping XML operation.
        pause
    ) else (
        echo Attempting to copy Scaling XML...
        copy /Y "CustomSettings\ScalingXML\*" "!destination_path!"

        if exist "!destination_path!\SkypeSettings.xml" (
        echo Scaling XML copied successfully.
        pause
        ) else (
            echo That might've failed...
        )
    )
    Powershell -ExecutionPolicy Unrestricted c:\Rigel\x64\scripts\provisioning\scriptlaunch.ps1 ApplyCurrentDisplayScaling.ps1
    pause
    goto MainMenu



:A_PublicPreview_Enable
    echo Checking for existing SkypeSettings.xml...
    if exist "!destination_path!\SkypeSettings.xml" (
        echo SkypeSettings.xml already exists. Cancelling operation.
        pause
    ) else (
        echo Attempting to enable Public Preview...
        copy /Y "CustomSettings\PublicPreviewEnable\*" "!destination_path!"
        echo Public Preview enabled successfully.
        pause
    )
    goto RESTART_PROMPT


:A_PublicPreview_Disable
    echo Checking for existing SkypeSettings.xml...
    if exist "!destination_path!\SkypeSettings.xml" (
        echo SkypeSettings.xml already exists. Cancelling operation.
        pause
    ) else (
        echo Attempting to disable Public Preview...
        copy /Y "CustomSettings\PublicPreviewDisable\*" "!destination_path!"
        echo Public Preview disabled successfully.
        pause
    )
    goto RESTART_PROMPT


:A_OPENLOCALSTATE
    echo Opening LocalState folder...
    start explorer "%destination_path%"
    goto MENU


IF "%choice%"=="0" (
    exit
)

IF "%choice%"=="6" (
    call :APPLY_CUSTOMER
    goto RESTART_PROMPT
)

echo Invalid choice. Please try again.
pause
goto MENU

:PRO_AV
cls

:: Prompt user for selection
echo. 
echo. 
echo:         [1] Start Configatron
echo:         [0] Exit to Main Menu
echo. 
set /p choice="        Enter ID of the configuration above: "

IF "%choice%"=="0" (
    goto MENU
)

IF "%choice%"=="1" (

    net use u: \\CONFIGATRON-FS\CONFIGATRON-AUTOMATION\UC /user:Configatron e633d598-5296-4070-b29a-5c76566c62db
    powershell -executionpolicy bypass u:\C9001_UC_setup.ps1
    
    exit
)

pause
goto MENU

:PAV_CONFIGATRON

pause

:APPLY_THEME
cls
echo Available Themes:
echo.

pushd "Theming"
setlocal enabledelayedexpansion
set "counter=1"

for /D %%D in (*) do (
    echo !counter!. %%D
    set "theme[!counter!]=%%D"
    set /a "counter+=1"
)

set /p theme_choice=Enter the theme number to apply: 

popd

if !theme_choice! GEQ 1 if !theme_choice! LSS !counter! (
    set "selected_theme=!theme[%theme_choice%]!"
    echo Applying Theme: !selected_theme!...


    copy /Y "Theming\!selected_theme!\*" "!destination_path!"

    echo Theme applied successfully to Microsoft Teams Room.
    pause
) else (
    echo Invalid theme choice. Please try again.
)

endlocal
goto MENU

:ROOMCONFIGSELECT

powershell -command "& { $h = Get-Host; $h.ui.rawui.buffersize = New-Object Management.Automation.Host.Size(90, 70); $h.ui.rawui.windowsize = New-Object Management.Automation.Host.Size(90, 45);  }"
cls
@REM powershell -ExecutionPolicy Bypass -File "backend\remoteFetchCustomerInfo.ps1"
cls
echo        ================================================
echo                      Select a Configuration
echo        ================================================
echo              XML will be configured in LocalState  
echo                folder, please restart to apply.
echo.

@echo off

if /I "%ProdRun%"=="false" (
    powershell.exe -ExecutionPolicy Bypass -File "backend\handleCustomerData.ps1" -OutputDir "%destination_path%" -DevTesting
) else (
    powershell.exe -ExecutionPolicy Bypass -File "backend\handleCustomerData.ps1" -OutputDir "%destination_path%"
)


@REM rmdir /s /q .\CustomerConfigs\
pause
endlocal
goto MENU

:APPLY_CUSTOMER
cls
echo Available Customer Configs:
echo.

pushd "CustomerConfigs"
setlocal enabledelayedexpansion
set "counter=1"

for /D %%D in (*) do (
    echo !counter!. %%D
    set "cconfig[!counter!]=%%D"
    set /a "counter+=1"
)

set /p cconfig_choice=Enter the cconfig number to apply: 

popd

if !cconfig_choice! GEQ 1 if !cconfig_choice! LSS !counter! (
    set "selected_cconfig=!cconfig[%cconfig_choice%]!"
    echo Applying cconfig: !selected_cconfig!...

    rem Set the destination path directly
    

    copy /Y "CustomerConfigs\!selected_cconfig!\*" "!destination_path!"

    echo cconfig applied successfully to Microsoft Teams Room.
    pause
) else (
    echo Invalid cconfig choice. Please try again.
)

endlocal
goto WTend

:RESTART_PROMPT
echo.
set /p restart=Restart is required. Restart now? (Y/N): 
if /i "%restart%"=="Y" (
    shutdown /r /t 5
    exit /b
) else (
    echo Restart cancelled. Press any key to continue...
    pause >nul
)
goto MENU

:+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

:WTend
echo:
echo Press any key to exit...
pause >nul
exit /b

::========================================================================================================================================
:: Leave empty line below
