@echo off
setlocal EnableDelayedExpansion
chcp 65001 > nul 2>&1

:: ============================================================
:: Avigation Cloud — Windows Setup
::
:: What this script does:
::   1. Downloads official Nextcloud 34.0.1 from nextcloud.com
::   2. Verifies SHA256 (certutil — built into Windows)
::   3. Installs Nextcloud silently (msiexec /passive)
::   4. Creates desktop shortcut pointing to Avigation Cloud
::   5. Launches Nextcloud against cloud.avigation.cloud
::
:: No custom Nextcloud binary. Official Nextcloud + server address only.
:: Source: support.avigation.cloud/packages/avigation/windows/Avigation-Cloud-Setup.cmd
:: ============================================================

set "NC_VERSION=34.0.1"
set "NC_URL=https://download.nextcloud.com/desktop/releases/Windows/Nextcloud-34.0.1-x64.msi"
set "NC_SHA256=e81c052832383508c67136395b9304bfdd8bf7a4dbdd9b86a1bd4dd22272b485"
set "BRAND_URL=https://cloud.avigation.cloud"
set "BRAND_NAME=Avigation Cloud"
set "MSI_FILE=%TEMP%\Nextcloud-%NC_VERSION%-x64.msi"

echo.
echo  Avigation Cloud -- Windows Setup
echo  =================================
echo  Using official Nextcloud %NC_VERSION% from nextcloud.com
echo  Server address: %BRAND_URL%
echo.

:: ---- Admin check ----
net session > nul 2>&1
if errorlevel 1 (
    echo  NOTE: Installing for current user (no admin required).
    echo.
)

:: ---- 1. Download MSI ----
echo  [1/4] Downloading Nextcloud %NC_VERSION% (~112 MB)...
powershell -NoProfile -NonInteractive -Command ^
  "Invoke-WebRequest -Uri '%NC_URL%' -OutFile '%MSI_FILE%' -UseBasicParsing" 2>&1
if errorlevel 1 (
    echo.
    echo  ERROR: Download failed.
    echo  Check your internet connection and try again.
    goto :ERROR
)
echo  Download complete.

:: ---- 2. Verify SHA256 ----
echo  [2/4] Verifying SHA256...
for /f "skip=1 tokens=* delims=" %%H in (
    'certutil -hashfile "%MSI_FILE%" SHA256 2^>nul'
) do (
    if not defined _RAW_HASH (
        set "_RAW_HASH=%%H"
    )
)
set "_ACTUAL_HASH="
for %%C in (!_RAW_HASH!) do set "_ACTUAL_HASH=!_ACTUAL_HASH!%%C"

if /i "!_ACTUAL_HASH!" NEQ "%NC_SHA256%" (
    echo.
    echo  ERROR: SHA256 mismatch. The file may be corrupted or tampered with.
    echo  Expected: %NC_SHA256%
    echo  Actual:   !_ACTUAL_HASH!
    del "%MSI_FILE%" 2>nul
    goto :ERROR
)
echo  SHA256 OK.

:: ---- 3. Install ----
echo  [3/4] Installing Nextcloud (this may take a moment)...
msiexec /passive /i "%MSI_FILE%" SKIPAUTOUPDATE=1 DO_NOT_SCHEDULE_REBOOT=1
set "MSI_EXIT=%ERRORLEVEL%"
del "%MSI_FILE%" 2>nul

if "%MSI_EXIT%" NEQ "0" (
    if "%MSI_EXIT%" NEQ "3010" (
        echo.
        echo  ERROR: Installation failed (code %MSI_EXIT%).
        goto :ERROR
    )
)
echo  Installation complete.

:: ---- 4. Locate nextcloud.exe ----
echo  [4/4] Creating shortcut for %BRAND_NAME%...
set "NC_EXE="
for %%P in (
    "%LOCALAPPDATA%\Programs\Nextcloud\nextcloud.exe"
    "%ProgramFiles%\Nextcloud\nextcloud.exe"
    "%ProgramFiles(x86)%\Nextcloud\nextcloud.exe"
) do (
    if exist %%~P (
        if not defined NC_EXE set "NC_EXE=%%~P"
    )
)

if not defined NC_EXE (
    echo.
    echo  NOTE: Could not locate nextcloud.exe after installation.
    echo  Launch Nextcloud manually and enter: %BRAND_URL%
    goto :DONE
)

:: ---- 5. Desktop shortcut ----
powershell -NoProfile -NonInteractive -Command ^
  "$ws = New-Object -ComObject WScript.Shell;" ^
  "$sc = $ws.CreateShortcut([System.IO.Path]::Combine([System.Environment]::GetFolderPath('Desktop'), '%BRAND_NAME%.lnk'));" ^
  "$sc.TargetPath = '%NC_EXE%';" ^
  "$sc.Arguments = '--overrideserverurl %BRAND_URL%';" ^
  "$sc.Description = 'Start %BRAND_NAME%';" ^
  "$sc.Save();" 2>nul
echo  Desktop shortcut created.

:: ---- 6. Launch against Avigation Cloud ----
echo.
echo  Starting %BRAND_NAME%...
echo  Your browser will open for sign-in -- use your Avigation account.
echo.
start "" "%NC_EXE%" --overrideserverurl "%BRAND_URL%"
goto :DONE

:ERROR
echo.
echo  Setup aborted. Contact support.avigation.cloud for help.
echo.
pause
exit /b 1

:DONE
echo  %BRAND_NAME% is ready for sign-in.
timeout /t 4 > nul
exit /b 0
