; RemoteLink Agent NSIS Installer
; Requires: NSIS 3.x, the dist/ folder from PyInstaller build
;
; Build: makensis installer.nsi
; Silent install: RemoteLink-Setup.exe /S
; Silent install + enroll: RemoteLink-Setup.exe /S /SERVER=https://myserver.com /ENROLL=mytoken

!define APP_NAME "RemoteLink Agent"
!define APP_VERSION "1.0.0"
!define APP_PUBLISHER "RemoteLink"
!define APP_URL "https://remotelink.example.com"
!define INSTALL_DIR "$PROGRAMFILES64\RemoteLink"
!define SERVICE_EXE "remotelink-agent-service.exe"
!define AGENT_EXE "remotelink-agent.exe"
!define REG_KEY "Software\RemoteLink\Agent"
!define UNINSTALL_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\RemoteLinkAgent"

; Installer settings
Name "${APP_NAME} ${APP_VERSION}"
OutFile "RemoteLink-Setup.exe"
InstallDir "${INSTALL_DIR}"
InstallDirRegKey HKLM "${REG_KEY}" "InstallDir"
RequestExecutionLevel admin
SetCompressor /SOLID lzma

; Command-line parameters
Var ServerURL
Var EnrollToken

; Modern UI
!include "MUI2.nsh"
!include "LogicLib.nsh"
!include "nsProcess.nsh"

!define MUI_ABORTWARNING
!define MUI_ICON "assets\icon.ico"
!define MUI_UNICON "assets\icon.ico"

!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH

!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES

!insertmacro MUI_LANGUAGE "English"

; ── Functions ─────────────────────────────────────────────────────────────────

Function .onInit
    ; Parse command-line switches /SERVER= and /ENROLL=
    ${GetParameters} $R0
    ${GetOptions} $R0 "/SERVER=" $ServerURL
    ${GetOptions} $R0 "/ENROLL=" $EnrollToken

    ; Silent mode: skip UI if /S passed
    IfSilent 0 +2
    SetSilent silent
FunctionEnd

; ── Main install section ──────────────────────────────────────────────────────

Section "Install" SEC_MAIN
    SetOutPath "${INSTALL_DIR}"

    ; Stop existing service if running
    nsExec::ExecToLog '"${INSTALL_DIR}\${SERVICE_EXE}" stop'
    nsExec::ExecToLog '"${INSTALL_DIR}\${SERVICE_EXE}" remove'
    Sleep 1000

    ; Copy files from PyInstaller dist/
    File "dist\remotelink-agent.exe"
    File "dist\remotelink-agent-service.exe"

    ; Write registry
    WriteRegStr HKLM "${REG_KEY}" "InstallDir" "${INSTALL_DIR}"
    WriteRegStr HKLM "${REG_KEY}" "Version" "${APP_VERSION}"

    ; Run enrollment if tokens were provided (silent mass-deploy)
    ${If} $ServerURL != ""
    ${AndIf} $EnrollToken != ""
        DetailPrint "Enrolling with server $ServerURL…"
        nsExec::ExecToLog '"${INSTALL_DIR}\${AGENT_EXE}" --server "$ServerURL" --enroll "$EnrollToken"'
    ${EndIf}

    ; Install + start Windows service
    DetailPrint "Installing Windows service…"
    nsExec::ExecToLog '"${INSTALL_DIR}\${SERVICE_EXE}" install'
    nsExec::ExecToLog '"${INSTALL_DIR}\${SERVICE_EXE}" start'

    ; Create uninstaller
    WriteUninstaller "${INSTALL_DIR}\Uninstall.exe"
    WriteRegStr HKLM "${UNINSTALL_KEY}" "DisplayName" "${APP_NAME}"
    WriteRegStr HKLM "${UNINSTALL_KEY}" "UninstallString" "${INSTALL_DIR}\Uninstall.exe"
    WriteRegStr HKLM "${UNINSTALL_KEY}" "DisplayVersion" "${APP_VERSION}"
    WriteRegStr HKLM "${UNINSTALL_KEY}" "Publisher" "${APP_PUBLISHER}"
    WriteRegDWORD HKLM "${UNINSTALL_KEY}" "NoModify" 1
    WriteRegDWORD HKLM "${UNINSTALL_KEY}" "NoRepair" 1
SectionEnd

; ── Uninstall section ─────────────────────────────────────────────────────────

Section "Uninstall"
    nsExec::ExecToLog '"${INSTALL_DIR}\${SERVICE_EXE}" stop'
    nsExec::ExecToLog '"${INSTALL_DIR}\${SERVICE_EXE}" remove'
    Sleep 1000

    Delete "${INSTALL_DIR}\remotelink-agent.exe"
    Delete "${INSTALL_DIR}\remotelink-agent-service.exe"
    Delete "${INSTALL_DIR}\Uninstall.exe"
    RMDir "${INSTALL_DIR}"

    DeleteRegKey HKLM "${REG_KEY}"
    DeleteRegKey HKLM "${UNINSTALL_KEY}"
SectionEnd
