@echo off
setlocal EnableExtensions EnableDelayedExpansion
title ZhiPinTou Plugin Installer
echo.
echo ===== ZPT-INSTALLER-v459 =====
echo If you do not see this line, you opened an OLD bat file.
echo.

:: No admin required: install goes to LocalAppData only
chcp 936 >nul

set "EXT_NAME=ZhiPinTou"
set "INSTALL_DIR=%LOCALAPPDATA%\BossJDHelper\extension"
set "DOWNLOAD_DIR=%LOCALAPPDATA%\BossJDHelper\download"
set "PROFILE_DIR=%LOCALAPPDATA%\BossJDHelper\BrowserProfile"
set "ZIP_FILE=%DOWNLOAD_DIR%\manifest.zip"
set "SITE_URL=https://www.boss.xiaozuwo.top"
set "ZIP_URL=%SITE_URL%/manifest.zip?v=4.5.9"
set "ZIP_URL_FALLBACK=%SITE_URL%/manifest.zip"
set "BOSS_URL=https://www.zhipin.com"
set "SHORTCUT_NAME=ZhiPinTou.lnk"
set "STEP=0"
set "TOTAL_STEPS=6"

echo.
echo ========================================
echo   %EXT_NAME% - one-click install
echo   Site: %SITE_URL%
echo   Zip : %ZIP_URL%
echo ========================================
echo.
echo Tip: Double-click this file, or run: cmd /c ZhiPinTou-Install.bat
echo.

call :step "Check environment"
call :check_env
if errorlevel 1 goto :fail

call :step "Download extension zip"
call :download_zip
if errorlevel 1 goto :fail

call :step "Extract to local folder"
call :extract_zip
if errorlevel 1 goto :fail

call :step "Verify extension files"
call :verify_extension
if errorlevel 1 goto :fail

call :step "Create desktop shortcut"
call :create_shortcut

call :step "Launch browser and open Boss"
call :launch_browser

echo.
echo [OK] All done.
echo   Extension: %INSTALL_DIR%
echo   Shortcut : %USERPROFILE%\Desktop\%SHORTCUT_NAME%
echo   Later use the desktop shortcut (not normal browser icon).
echo.
goto :end

:fail
echo.
echo [FAIL] Install not finished. Check [ERR] lines above.
echo   Common: network / antivirus / browser still running / bad zip.
echo   Manual: %ZIP_URL%
echo.
goto :end

:end
pause
endlocal
exit /b 0

:step
set /a STEP+=1
echo.
echo ----------------------------------------
echo [STEP !STEP!/%TOTAL_STEPS%] %~1
echo ----------------------------------------
exit /b 0

:log_info
echo [INFO] %~1
exit /b 0

:log_ok
echo [OK] %~1
exit /b 0

:log_warn
echo [WARN] %~1
exit /b 0

:log_err
echo [ERR] %~1
exit /b 0

:check_env
if not exist "%DOWNLOAD_DIR%" mkdir "%DOWNLOAD_DIR%" 2>nul
if not exist "%INSTALL_DIR%" mkdir "%INSTALL_DIR%" 2>nul
if not exist "%PROFILE_DIR%" mkdir "%PROFILE_DIR%" 2>nul
call :log_info "Download dir: %DOWNLOAD_DIR%"
call :log_info "Install dir : %INSTALL_DIR%"

set "BROWSER_EXE="
set "BROWSER_NAME="

for %%P in (
  "%ProgramFiles%\Microsoft\Edge\Application\msedge.exe"
  "%ProgramFiles(x86)%\Microsoft\Edge\Application\msedge.exe"
  "%LocalAppData%\Microsoft\Edge\Application\msedge.exe"
) do (
  if exist "%%~P" (
    set "BROWSER_EXE=%%~P"
    set "BROWSER_NAME=Edge"
  )
)

if not defined BROWSER_EXE (
  for %%P in (
    "%ProgramFiles%\Google\Chrome\Application\chrome.exe"
    "%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe"
    "%LocalAppData%\Google\Chrome\Application\chrome.exe"
  ) do (
    if exist "%%~P" (
      set "BROWSER_EXE=%%~P"
      set "BROWSER_NAME=Chrome"
    )
  )
)

if not defined BROWSER_EXE (
  call :log_err "Edge/Chrome not found. Install a browser first."
  exit /b 1
)

call :log_info "Browser: !BROWSER_NAME! - !BROWSER_EXE!"
call :log_ok "Environment OK"
exit /b 0

:download_zip
if exist "%ZIP_FILE%" del /f /q "%ZIP_FILE%" 2>nul
call :log_info "Downloading: %ZIP_URL%"

set "DL_OK=0"
where curl.exe >nul 2>nul
if not errorlevel 1 (
  curl.exe -L --fail --retry 3 --connect-timeout 20 -o "%ZIP_FILE%" "%ZIP_URL%"
  if not errorlevel 1 if exist "%ZIP_FILE%" set "DL_OK=1"
  if "!DL_OK!"=="0" (
    call :log_warn "Primary URL failed, try fallback..."
    curl.exe -L --fail --retry 3 --connect-timeout 20 -o "%ZIP_FILE%" "%ZIP_URL_FALLBACK%"
    if not errorlevel 1 if exist "%ZIP_FILE%" set "DL_OK=1"
  )
)

if "!DL_OK!"=="0" (
  call :log_info "Trying bitsadmin download..."
  bitsadmin /transfer zpt_dl /download /priority foreground "%ZIP_URL%" "%ZIP_FILE%" >nul
  if exist "%ZIP_FILE%" set "DL_OK=1"
)

if "!DL_OK!"=="0" (
  call :log_info "Trying certutil download..."
  certutil -urlcache -split -f "%ZIP_URL%" "%ZIP_FILE%" >nul
  if exist "%ZIP_FILE%" set "DL_OK=1"
)

if not exist "%ZIP_FILE%" (
  call :log_err "Download failed. Open in browser: %SITE_URL%/manifest.zip"
  exit /b 1
)

for %%F in ("%ZIP_FILE%") do set "ZIP_SIZE=%%~zF"
if "!ZIP_SIZE!"=="0" (
  call :log_err "Downloaded zip size is 0."
  exit /b 1
)
if !ZIP_SIZE! LSS 1000 (
  call :log_err "Downloaded zip too small (!ZIP_SIZE! bytes)."
  exit /b 1
)

call :log_ok "Saved %ZIP_FILE% (!ZIP_SIZE! bytes)"
exit /b 0

:extract_zip
call :log_info "Clear old files: %INSTALL_DIR%"
if exist "%INSTALL_DIR%" rmdir /s /q "%INSTALL_DIR%" 2>nul
mkdir "%INSTALL_DIR%" 2>nul

call :log_info "Extracting with tar..."
where tar.exe >nul 2>nul
if errorlevel 1 (
  call :log_err "tar.exe not found (need Windows 10+)."
  exit /b 1
)

tar.exe -xf "%ZIP_FILE%" -C "%INSTALL_DIR%"
if errorlevel 1 (
  call :log_err "Extract failed. Zip may be corrupt."
  exit /b 1
)

if not exist "%INSTALL_DIR%\manifest.json" (
  call :log_warn "manifest.json not in root, search subfolder..."
  for /d %%D in ("%INSTALL_DIR%\*") do (
    if exist "%%D\manifest.json" (
      call :log_info "Found: %%D"
      xcopy /e /i /y "%%D\*" "%INSTALL_DIR%\" >nul
      rmdir /s /q "%%D" 2>nul
    )
  )
)

if not exist "%INSTALL_DIR%\manifest.json" (
  call :log_err "manifest.json missing after extract."
  dir /b "%INSTALL_DIR%"
  exit /b 1
)

call :log_ok "Extract OK"
exit /b 0

:verify_extension
set "EXT_VER=unknown"
for /f "tokens=2 delims=:," %%A in ('findstr /i /c:"\"version\"" "%INSTALL_DIR%\manifest.json"') do (
  set "RAW=%%~A"
  set "RAW=!RAW: =!"
  set "RAW=!RAW:"=!"
  if not "!RAW!"=="" set "EXT_VER=!RAW!"
)

call :log_info "Name: %EXT_NAME%"
call :log_info "Version: !EXT_VER!"
call :log_info "Path: %INSTALL_DIR%\manifest.json"

if not exist "%INSTALL_DIR%\background.js" call :log_warn "missing background.js"
if not exist "%INSTALL_DIR%\content.js" call :log_warn "missing content.js"
if not exist "%INSTALL_DIR%\popup.html" call :log_warn "missing popup.html"
if exist "%INSTALL_DIR%\popup-quota.js" call :log_info "popup-quota.js present"

call :log_ok "Verify OK"
exit /b 0

:create_shortcut
set "DESKTOP=%USERPROFILE%\Desktop"
if not exist "%DESKTOP%" if exist "%USERPROFILE%\×ÀÃæ" set "DESKTOP=%USERPROFILE%\×ÀÃæ"
if not exist "%DESKTOP%" set "DESKTOP=%USERPROFILE%\Desktop"

set "LNK=%DESKTOP%\%SHORTCUT_NAME%"
set "VBS=%TEMP%\zpt_shortcut.vbs"
call :log_info "Shortcut: %LNK%"

> "%VBS%"  echo Set o = CreateObject("WScript.Shell"^)
>>"%VBS%" echo Set s = o.CreateShortcut("%LNK%"^)
>>"%VBS%" echo s.TargetPath = "!BROWSER_EXE!"
>>"%VBS%" echo q = Chr(34^)
>>"%VBS%" echo s.Arguments = "--user-data-dir=" ^& q ^& "%PROFILE_DIR%" ^& q ^& " --disable-extensions-except=" ^& q ^& "%INSTALL_DIR%" ^& q ^& " --load-extension=" ^& q ^& "%INSTALL_DIR%" ^& q ^& " " ^& q ^& "%BOSS_URL%" ^& q
>>"%VBS%" echo s.WorkingDirectory = "%PROFILE_DIR%"
>>"%VBS%" echo s.Description = "ZhiPinTou Boss Helper"
>>"%VBS%" echo s.Save

cscript //nologo "%VBS%"
set "SC_ERR=!errorlevel!"
del "%VBS%" >nul 2>&1

if not "!SC_ERR!"=="0" (
  call :log_warn "Shortcut create failed. You can still launch from this script."
) else if not exist "%LNK%" (
  call :log_warn "Shortcut file not found after create."
) else (
  call :log_ok "Desktop shortcut created"
)
exit /b 0

:launch_browser
set "PROC=msedge.exe"
if /I "!BROWSER_NAME!"=="Chrome" set "PROC=chrome.exe"

tasklist /FI "IMAGENAME eq !PROC!" 2>nul | find /I "!PROC!" >nul
if not errorlevel 1 (
  call :log_warn "!BROWSER_NAME! is running, closing it..."
  taskkill /F /IM !PROC! >nul 2>nul
  timeout /t 2 /nobreak >nul
)

call :log_info "Starting !BROWSER_NAME!..."
call :log_info "Load extension: %INSTALL_DIR%"
call :log_info "Open: %BOSS_URL%"

start "" "!BROWSER_EXE!" --user-data-dir="%PROFILE_DIR%" --disable-extensions-except="%INSTALL_DIR%" --load-extension="%INSTALL_DIR%" "%BOSS_URL%"

timeout /t 2 /nobreak >nul
call :log_ok "!BROWSER_NAME! started"
call :log_info "If icon missing, fully quit browser and use desktop shortcut."
exit /b 0

