@echo off
setlocal
title 80hou Workbench Fix Tool

echo ==========================================
echo   80hou Workbench - One Click Fix
echo ==========================================
echo.

set "BASE=https://80houdashu.com"
set "D80="

for %%d in (C D E F G H) do (
  if not defined D80 if exist "%%d:\80hou\app\lt_server.py" set "D80=%%d:\80hou"
)
for %%d in (C D E F G H) do (
  if not defined D80 if exist "%%d:\80\app\lt_server.py" set "D80=%%d:\80"
)
if not defined D80 if exist "%USERPROFILE%\80hou\app\lt_server.py" set "D80=%USERPROFILE%\80hou"
if not defined D80 if exist "%USERPROFILE%\80\app\lt_server.py" set "D80=%USERPROFILE%\80"

if not defined D80 (
  echo [ERROR] Cannot find the workbench install folder.
  echo Looked for C:\80 D:\80 E:\80 F:\80 G:\80 H:\80 and \80hou variants.
  echo Please screenshot this window and send it to support.
  echo.
  pause
  exit /b 1
)

echo Install folder: %D80%
if not exist "%D80%\python\pythonw.exe" (
  echo [ERROR] %D80%\python\pythonw.exe not found - install is incomplete.
  pause
  exit /b 1
)
echo.

echo [1/12] lt_server.py  - license check fix
powershell -NoProfile -ExecutionPolicy Bypass -Command "try{Invoke-WebRequest -Uri '%BASE%/guide/lt_server_fix.txt' -OutFile '%D80%\app\lt_server.py' -UseBasicParsing; Write-Host '    OK'}catch{Write-Host '    FAILED:' $_.Exception.Message}"

echo [2/12] lt_audit.py  - stop the red banner on trial accounts
powershell -NoProfile -ExecutionPolicy Bypass -Command "try{Invoke-WebRequest -Uri '%BASE%/guide/lt_audit_fix.txt' -OutFile '%D80%\app\lt_audit.py' -UseBasicParsing; Write-Host '    OK'}catch{Write-Host '    FAILED:' $_.Exception.Message}"

echo [3/12] lt_ui.html  - no more redirect to download page
powershell -NoProfile -ExecutionPolicy Bypass -Command "try{Invoke-WebRequest -Uri '%BASE%/guide/ltui_fix.txt' -OutFile '%D80%\app\lt_ui.html' -UseBasicParsing; Write-Host '    OK'}catch{Write-Host '    FAILED:' $_.Exception.Message}"

echo [4/12] launcher.py  - shows the "pick what you need" page every first run
powershell -NoProfile -ExecutionPolicy Bypass -Command "try{Invoke-WebRequest -Uri '%BASE%/guide/launcher_fix.txt' -OutFile '%D80%\app\launcher.py' -UseBasicParsing; Write-Host '    OK'}catch{Write-Host '    FAILED:' $_.Exception.Message}"

echo [5/12] prov.py  - FIND python in the install folder (modules were failing to start on customer PCs)
powershell -NoProfile -ExecutionPolicy Bypass -Command "try{Invoke-WebRequest -Uri '%BASE%/guide/prov_fix.txt' -OutFile '%D80%\app\prov.py' -UseBasicParsing; Write-Host '    OK'}catch{Write-Host '    FAILED:' $_.Exception.Message}"

echo [6/12] sitecustomize.py  - lets modules import their own libraries
powershell -NoProfile -ExecutionPolicy Bypass -Command "try{Invoke-WebRequest -Uri '%BASE%/guide/sitecustomize_fix.txt' -OutFile '%D80%\app\sitecustomize.py' -UseBasicParsing; Write-Host '    OK'}catch{Write-Host '    FAILED:' $_.Exception.Message}"

powershell -NoProfile -ExecutionPolicy Bypass -Command "try{Invoke-WebRequest -Uri '%BASE%/guide/launcher_fix.txt' -OutFile '%D80%\app\launcher.py' -UseBasicParsing; Write-Host '    OK'}catch{Write-Host '    FAILED:' $_.Exception.Message}"
if exist "%D80%\app\_selected_models.json" del /f /q "%D80%\app\_selected_models.json" >nul 2>&1
echo     cleared old model choice - the picker will show up on next start

echo [7/12] python libraries (requests + Pillow) - your PC has NO global site-packages,
echo        so modules failed with: No module named requests / PIL
powershell -NoProfile -ExecutionPolicy Bypass -Command "try{Invoke-WebRequest -Uri '%BASE%/guide/libs.zip' -OutFile '%TEMP%\libs.zip' -UseBasicParsing; Write-Host '    downloaded'; if(Test-Path '%D80%\python\python311._pth'){Copy-Item '%D80%\python\python311._pth' '%D80%\python\python311._pth.bak' -Force}; New-Item -ItemType Directory -Force -Path '%D80%\python\Lib\site-packages' | Out-Null; Expand-Archive -Path '%TEMP%\libs.zip' -DestinationPath '%D80%\python' -Force; Write-Host '    installed into' '%D80%\python\Lib\site-packages'}catch{Write-Host '    FAILED:' $_.Exception.Message}"

echo [8/12] app pages + module paths - smart drive detection, no hardcoded letter
powershell -NoProfile -ExecutionPolicy Bypass -Command "try{Invoke-WebRequest -Uri '%BASE%/guide/appfix.zip' -OutFile '%TEMP%\appfix.zip' -UseBasicParsing; Write-Host '    downloaded'; Expand-Archive -Path '%TEMP%\appfix.zip' -DestinationPath '%D80%\app' -Force; Write-Host '    installed pages + modules'}catch{Write-Host '    FAILED:' $_.Exception.Message}"

echo [9/12] model sources  - hf-mirror.com is down, switch to modelscope
if not exist "%D80%\app\modules\music" mkdir "%D80%\app\modules\music" >nul 2>&1
if not exist "%D80%\app\modules\imgtools" mkdir "%D80%\app\modules\imgtools" >nul 2>&1
powershell -NoProfile -ExecutionPolicy Bypass -Command "try{Invoke-WebRequest -Uri '%BASE%/guide/models_music.json' -OutFile '%D80%\app\modules\music\models.json' -UseBasicParsing; Write-Host '    music OK'}catch{Write-Host '    music FAILED:' $_.Exception.Message}"
powershell -NoProfile -ExecutionPolicy Bypass -Command "try{Invoke-WebRequest -Uri '%BASE%/guide/models_imgtools.json' -OutFile '%D80%\app\modules\imgtools\models.json' -UseBasicParsing; Write-Host '    imgtools OK'}catch{Write-Host '    imgtools FAILED:' $_.Exception.Message}"

echo [10/12] restarting workbench ...
taskkill /f /im pythonw.exe >nul 2>&1
taskkill /f /im python.exe >nul 2>&1
timeout /t 3 /nobreak >nul
cd /d "%D80%"
start "" "%D80%\python\pythonw.exe" "%D80%\app\launcher.py"
timeout /t 12 /nobreak >nul

echo [11/12] starting module services: 8021 song / 8022 novel / 8024 drama / 8043 manju
if exist "%D80%\app\autostart\gw_watch.py" (
  start "" "%D80%\python\pythonw.exe" "%D80%\app\autostart\gw_watch.py"
  echo     gw_watch started
) else (
  echo     [WARN] autostart\gw_watch.py missing - module services will not start
)
echo     waiting 35s for the services to come up ...
timeout /t 35 /nobreak >nul

echo --- port check ---
for %%p in (8090 8091 8021 8022 8023 8024 8027 8029 8033 8034 8042 8043) do (
  netstat -ano | findstr ":%%p " | findstr LISTENING >nul 2>&1 && echo   port %%p  OK || echo   port %%p  DOWN
)

echo.
echo ==========================================
echo   DONE.
echo   1) open  http://127.0.0.1:8090   - should go straight in
echo   2) open  http://127.0.0.1:8091/log - should say: download all
echo   3) the red banner at the top must be GONE within 5 minutes
echo ==========================================

echo [12/12] writing a self-check report to your Desktop ...
powershell -NoProfile -ExecutionPolicy Bypass -Command "try{$r=Invoke-WebRequest -Uri 'http://127.0.0.1:8090/api/selftest' -UseBasicParsing -TimeoutSec 40; $r.Content | Out-File -FilePath '%USERPROFILE%\Desktop\80hou_check.txt' -Encoding UTF8; Write-Host '    saved: Desktop\80hou_check.txt'; Start-Process notepad '%USERPROFILE%\Desktop\80hou_check.txt'}catch{Write-Host '    FAILED:' $_.Exception.Message}"

pause
