; BestPerf Windows installer (NSIS) -- bundles the whole product into one Setup.exe.
; End user: double-click Setup.exe -> installs to the user profile, creates the numbered desktop
; entries, and registers an uninstaller in Add/Remove Programs.
;
; Cross-build on Mac/Linux:
;   makensis -DPAYLOAD=/tmp/fr_release/BestPerf-windows-x64 \
;            -DOUTFILE=/tmp/fr_release/BestPerf-Setup.exe scripts/bestperf_installer.nsi
;
; Source is kept ASCII-only so makensis Unicode mode never hits an encoding error; the built
; installer is still a Unicode PE, so it handles the Chinese shortcut filenames at runtime.

Unicode true
!include "MUI2.nsh"
!include "LogicLib.nsh"

!ifndef PAYLOAD
  !define PAYLOAD "/tmp/fr_release/BestPerf-windows-x64"
!endif
!ifndef OUTFILE
  !define OUTFILE "/tmp/fr_release/BestPerf-Setup.exe"
!endif

Name "BestPerf"
OutFile "${OUTFILE}"
RequestExecutionLevel user
InstallDir "$LOCALAPPDATA\BestPerf"
SetCompressor /SOLID lzma
BrandingText "BestPerf"

!define MUI_ABORTWARNING
!define MUI_FINISHPAGE_RUN
!define MUI_FINISHPAGE_RUN_TEXT "Launch BestPerf now"
!define MUI_FINISHPAGE_RUN_FUNCTION LaunchApp

!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH

!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES

!insertmacro MUI_LANGUAGE "SimpChinese"
!insertmacro MUI_LANGUAGE "English"

Function LaunchApp
  ExecShell "open" "$INSTDIR\BestPerf.bat"
FunctionEnd

Section "BestPerf" SecMain
  InitPluginsDir
  ; 1) unpack the embedded payload into a temp staging dir (auto-cleaned on exit)
  SetOutPath "$PLUGINSDIR\payload"
  File /r "${PAYLOAD}/*"

  ; 2) run the existing installer (bundled Python): copies to %LOCALAPPDATA%\BestPerf,
  ;    creates the numbered desktop entries, writes the install report.
  DetailPrint "Installing BestPerf to $INSTDIR ..."
  nsExec::ExecToLog '"$PLUGINSDIR\payload\runtime\python\python.exe" "$PLUGINSDIR\payload\scripts\bestperf_install.py" --root "$PLUGINSDIR\payload" --install-dir "$INSTDIR" --desktop-dir "$DESKTOP" --out-dir "$INSTDIR" --no-launch'
  Pop $0
  DetailPrint "installer exit code = $0"
  ${If} $0 != 0
    MessageBox MB_ICONEXCLAMATION "Install ran into a problem (exit code $0). See $INSTDIR\install_summary.latest.txt"
  ${EndIf}

  ; 3) uninstaller + Add/Remove Programs entry (per-user)
  WriteUninstaller "$INSTDIR\Uninstall.exe"
  !define ARP "Software\Microsoft\Windows\CurrentVersion\Uninstall\BestPerf"
  WriteRegStr   HKCU "${ARP}" "DisplayName"     "BestPerf (network capture and replay)"
  WriteRegStr   HKCU "${ARP}" "DisplayIcon"     "$INSTDIR\Uninstall.exe"
  WriteRegStr   HKCU "${ARP}" "UninstallString" '"$INSTDIR\Uninstall.exe"'
  WriteRegStr   HKCU "${ARP}" "InstallLocation" "$INSTDIR"
  WriteRegStr   HKCU "${ARP}" "Publisher"       "BestPerf"
  WriteRegDWORD HKCU "${ARP}" "NoModify" 1
  WriteRegDWORD HKCU "${ARP}" "NoRepair" 1
SectionEnd

Section "Uninstall"
  ; best-effort stop a running controller. The WINDOWTITLE filter misses the windowless / session-0
  ; controller, which then leaves the bundled python.exe holding $INSTDIR so RMDir fails -> also stop
  ; any python.exe running from under $INSTDIR (path-targeted, never the user's other python).
  nsExec::ExecToLog 'taskkill /F /IM python.exe /FI "WINDOWTITLE eq BestPerf*"'
  Pop $0
  nsExec::ExecToLog `powershell -NoProfile -Command "Get-Process python -ErrorAction SilentlyContinue | Where-Object { $$_.Path -like '$INSTDIR\*' } | Stop-Process -Force -ErrorAction SilentlyContinue"`
  Pop $0
  ; remove the inbound firewall rules opened via the "开放网络权限" helper (current + legacy BestPerf
  ; names); otherwise 39700-39899 / 8768 stay open forever after uninstall.
  nsExec::ExecToLog 'netsh advfirewall firewall delete rule name="BestPerf UDP"'
  Pop $0
  nsExec::ExecToLog 'netsh advfirewall firewall delete rule name="BestPerf TCP"'
  Pop $0
  nsExec::ExecToLog 'netsh advfirewall firewall delete rule name="BestPerf Console"'
  Pop $0
  nsExec::ExecToLog 'netsh advfirewall firewall delete rule name="BestPerf UDP"'
  Pop $0
  nsExec::ExecToLog 'netsh advfirewall firewall delete rule name="BestPerf TCP"'
  Pop $0
  nsExec::ExecToLog 'netsh advfirewall firewall delete rule name="BestPerf Console"'
  Pop $0
  ; remove the numbered desktop entries (wildcard matches the Chinese-prefixed names)
  Delete "$DESKTOP\*BestPerf*.cmd"
  SetOutPath "$TEMP"
  RMDir /r "$INSTDIR"
  DeleteRegKey HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\BestPerf"
SectionEnd
