@echo off

:: Check if running with administrative privileges
>nul 2>&1 net session
if %errorLevel% neq 0 (
    echo You must run this script as an administrator.
    echo Right-click on the script and select "Run as administrator".
    pause
    exit /b
)

:: 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%"

title Menu Script

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

:MENU
cls
echo App Version 1.2.1
echo +----------------------------------+
echo ^|             Menu Options:        ^|
echo +----------------------------------+
echo ^|  1. Apply scaling to Teams       ^|
echo ^|  2. Apply Theme                  ^|
echo ^|  3. Enable Public Preview        ^|
echo ^|  4. DISABLE Public Preview       ^|
echo ^|  5. Open LocalState Folder       ^|
echo ^|  6. Customer XML Deployment      ^|
echo ^|  0. Exit                         ^|
echo +----------------------------------+

set /p choice=Enter your choice (1-6): 

IF "%choice%"=="1" (
    echo Applying scaling to Teams...
        if exist "C:\Users\Skype\AppData\Local\Packages\Microsoft.SkypeRoomSystem_8wekyb3d8bbwe\LocalState\SkypeSettings.xml" (
        echo SkypeSettings.xml already exists. Skipping XML operation.
        pause
    ) else (
        echo Attempting to copy Scaling XML...
        copy /Y "CustomSettings\ScalingXML\*" "C:\Users\Skype\AppData\Local\Packages\Microsoft.SkypeRoomSystem_8wekyb3d8bbwe\LocalState"
        echo Scaling XML copied successfully.
    )
    Powershell -ExecutionPolicy Unrestricted c:\Rigel\x64\scripts\provisioning\scriptlaunch.ps1 ApplyCurrentDisplayScaling.ps1
    pause
    goto RESTART_PROMPT
)

IF "%choice%"=="2" (
    call :APPLY_THEME
    goto RESTART_PROMPT
)

IF "%choice%"=="3" (
    echo Checking for existing SkypeSettings.xml...
    if exist "C:\Users\Skype\AppData\Local\Packages\Microsoft.SkypeRoomSystem_8wekyb3d8bbwe\LocalState\SkypeSettings.xml" (
        echo SkypeSettings.xml already exists. Cancelling operation.
        pause
    ) else (
        echo Attempting to enable Public Preview...
        copy /Y "CustomSettings\PublicPreviewEnable\*" "C:\Users\Skype\AppData\Local\Packages\Microsoft.SkypeRoomSystem_8wekyb3d8bbwe\LocalState"
        echo Public Preview enabled successfully.
        pause
    )
    goto RESTART_PROMPT
)

IF "%choice%"=="4" (
    echo Checking for existing SkypeSettings.xml...
    if exist "C:\Users\Skype\AppData\Local\Packages\Microsoft.SkypeRoomSystem_8wekyb3d8bbwe\LocalState\SkypeSettings.xml" (
        echo SkypeSettings.xml already exists. Cancelling operation.
        pause
    ) else (
        echo Attempting to disable Public Preview...
        copy /Y "CustomSettings\PublicPreviewDisable\*" "C:\Users\Skype\AppData\Local\Packages\Microsoft.SkypeRoomSystem_8wekyb3d8bbwe\LocalState"
        echo Public Preview disabled successfully.
        pause
    )
    goto RESTART_PROMPT
)

IF "%choice%"=="5" (
    echo Opening LocalState folder...
    start explorer "C:\Users\Skype\AppData\Local\Packages\Microsoft.SkypeRoomSystem_8wekyb3d8bbwe\LocalState"
    goto MENU
)

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

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

echo Invalid choice. Please try again.
pause
goto MENU

: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!...

    rem Set the destination path directly
    set "destination_path=C:\Users\Skype\AppData\Local\Packages\Microsoft.SkypeRoomSystem_8wekyb3d8bbwe\LocalState"

    copy /Y "Theming\!selected_theme!\*" "C:\Users\Skype\AppData\Local\Packages\Microsoft.SkypeRoomSystem_8wekyb3d8bbwe\LocalState"

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

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
    set "destination_path=C:\Users\Skype\AppData\Local\Packages\Microsoft.SkypeRoomSystem_8wekyb3d8bbwe\LocalState"

    copy /Y "CustomerConfigs\!selected_cconfig!\*" "C:\Users\Skype\AppData\Local\Packages\Microsoft.SkypeRoomSystem_8wekyb3d8bbwe\LocalState"

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

endlocal
goto MENU

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