@echo off
net session >nul 2>&1
if %errorLevel% == 0 (
    goto :AdminExecute
) else (
    echo Requesting administrative privileges...
    goto :UACPrompt
)

:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B

:AdminExecute
setlocal enableextensions

set "rclonePath=%~dp0rclone.exe"
set "confPath=%~dp0rclone.conf"
set "serviceNameM=3DFF-Mount"
set "serviceNameD=3DFF-DLNA"
set "remoteName=3DFF"
set "logFileD=%~dp0DLNAlog.txt"
set "logFileM=%~dp0Mountlog.txt"
set "cacheDirM=%~dp0cache_mount"
set "cacheDirD=%~dp0cache_dlna"

REM Create cache directories if they don't exist
if not exist "%cacheDirM%" mkdir "%cacheDirM%"
if not exist "%cacheDirD%" mkdir "%cacheDirD%"

REM Check if WinFsp is installed for mounting
set "winfspInstalled="
if exist "C:\Program Files (x86)\WinFsp\bin\winfsp-x64.sys" (
    echo WinFsp is already installed.
    set "winfspInstalled=true"
    pause
    cls
)

echo This will set up 3DFlickFix as a Windows service
echo.
echo A mounted drive makes it easy to view and download the collection right from Windows File Explorer.
echo.
echo A DLNA server makes it easy to stream on a phone, TV, or VR headset.
echo.
pause
cls

echo Please choose an option
echo 1. Mount the collection as a drive
echo 2. Set up a DLNA server on your local network
echo 3. Both (Mount as drive and set up DLNA server)
choice /C 123 /N /M "Enter your choice (1/2/3): "
set choice=%errorlevel%

if "%choice%"=="1" (
    goto MountDrive
) else if "%choice%"=="2" (
    goto SetupDLNA
) else if "%choice%"=="3" (
    goto BothOptions
) else (
    echo Invalid choice. Exiting.
    exit /b 1
)

:MountDrive
REM Mount a drive
if "%winfspInstalled%" NEQ "true" (
    echo WinFsp is not installed. Downloading and installing...
    powershell -Command "(New-Object System.Net.WebClient).DownloadFile('https://github.com/winfsp/winfsp/releases/download/v2.0/winfsp-2.0.23075.msi', '%~dp0winfsp-2.0.23075.msi')"
    msiexec /i "%~dp0winfsp-2.0.23075.msi" /quiet /qn /norestart
)
call :RemoveService %serviceNameM%
set /p "mountpoint=What do you want to name the mountpoint? (folder name) "
%~dp0nssm.exe install %serviceNameM% "%rclonePath%" mount %remoteName%: %mountpoint% --dir-cache-time 72h --drive-chunk-size 64M --log-level INFO --vfs-read-chunk-size 32M --vfs-read-chunk-size-limit off --config "%confPath%" --log-file "%logFileM%" --vfs-cache-mode full --cache-dir "%cacheDirM%"
%~dp0nssm.exe set %serviceNameM% Start SERVICE_AUTO_START
%~dp0nssm.exe start %serviceNameM%
echo %serviceNameM% mounted to "%~dp0%mountpoint%"
PAUSE
goto END

:SetupDLNA
call :RemoveService %serviceNameD%
echo Installing DLNA server %serviceNameD% ...
%~dp0nssm.exe install %serviceNameD% %rclonePath% "serve dlna %remoteName%: --dir-cache-time 72h --drive-chunk-size 64M --log-level INFO --vfs-read-chunk-size 32M --vfs-read-chunk-size-limit off --config "%confPath%" --log-file "%logFileD%" --vfs-cache-mode full --cache-dir "%cacheDirD%""
%~dp0nssm.exe set %serviceNameD% Start SERVICE_AUTO_START
%~dp0nssm.exe start %serviceNameD%
echo DLNA server %serviceNameD% set up for %remoteName%
PAUSE
goto END

:BothOptions
call :MountDrive
call :SetupDLNA
goto END

:RemoveService
%~dp0nssm.exe status %1 >nul 2>&1
if '%errorlevel%'=='0' (
    %~dp0nssm.exe stop %1 >nul 2>&1
    %~dp0nssm.exe remove %1 confirm >nul 2>&1
    cls
)
exit /b

:END
endlocal
CLS
ECHO Script Completed Successfully
PAUSE