@echo off
setlocal EnableExtensions
title Quant Desk Installer
set "PROFILE=%QD_PROFILE%"
if "%PROFILE%"=="" set "PROFILE=quant-desk"
set "STORE=%LOCALAPPDATA%\QuantDesk"
if not exist "%STORE%" mkdir "%STORE%"
set "TGZ=%STORE%\dsh-quant-desk-0.1.0.tgz"
set "URL=https://www.jzlh999.com/download/dsh-quant-desk-0.1.0.tgz"
set "SHA=4896A64B7737BAC7394E2C2BF91C4962A785D0EAD8F890DD5DC47D3B9BBE0C4F"
set "DSH=%LOCALAPPDATA%\Programs\DeepSeek Harness\resources\runtime\cli\bin\dsh.cmd"
set "DSH_CMD="%DSH%""

if exist "%DSH%" goto found
where dsh >nul 2>nul
if errorlevel 1 goto nodsh
set "DSH_CMD=dsh"
goto found

:nodsh
echo.
echo  DeepSeek Harness was not found on this computer.
echo.
echo  1. Install DeepSeek Harness 0.2.1-rc.2 first:
echo     https://download.deepseek.com/dsh-desk/bin/win-x64/deepseek-harness-0.2.1-rc.2-win-x64.exe
echo  2. Then run this installer again.
echo.
if not defined QD_NOPAUSE pause
exit /b 1

:found
echo.
echo  Quant Desk installer
echo  --------------------
echo  Profile: %PROFILE%
echo.
echo  [1/4] Downloading...
powershell -NoProfile -ExecutionPolicy Bypass -Command "Invoke-WebRequest -Uri '%URL%' -OutFile '%TGZ%'"
if errorlevel 1 goto failed

echo  [2/4] Checking file...
certutil -hashfile "%TGZ%" SHA256 | findstr /i "%SHA%" >nul
if errorlevel 1 (
  echo.
  echo  File check failed. Delete the file and run again.
  if not defined QD_NOPAUSE pause
  exit /b 1
)

echo  [3/4] Preparing profile...
if not exist "%USERPROFILE%\.dsh\profiles\%PROFILE%\package.json" (
  call %DSH_CMD% --profile %PROFILE% --from-default-profile web --dump-config >nul 2>&1
  if errorlevel 1 goto failed
)

echo  [4/4] Installing plugins...
call %DSH_CMD% plugin --profile %PROFILE% add @v587d/capital-generation@2.9.1
if errorlevel 1 goto failed
call %DSH_CMD% plugin --profile %PROFILE% add "%TGZ%"
if errorlevel 1 goto failed

> "%USERPROFILE%\Desktop\Launch Quant Desk.cmd" (
  echo @echo off
  echo %DSH_CMD% --profile %PROFILE%
)
echo.
echo  Done.
echo  A shortcut was created on your Desktop: "Launch Quant Desk.cmd"
echo  Double-click it to open Quant Desk.
echo.
if not defined QD_NOPAUSE pause
exit /b 0

:failed
echo.
echo  Install failed. Copy the error above and send it to the author.
echo.
if not defined QD_NOPAUSE pause
exit /b 1
