<?xml version="1.0" encoding="UTF-8"?>
<?ifndef Version ?>
<?define Version = "0.0.0.0" ?>
<?endif?>
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs">
  <Package Name="Vantage Agent" Manufacturer="Vantage"
           Version="$(var.Version)" UpgradeCode="7d1e6d2c-2a5f-4b3e-9c3a-8a1b2c3d4e5f"
           Scope="perMachine">
    <MajorUpgrade DowngradeErrorMessage="A newer version is already installed."
                  Schedule="afterInstallInitialize" />
    <MediaTemplate EmbedCab="yes" />

    <!-- Public properties settable via msiexec: SERVERID, TOKEN, SERVERURL -->
    <Property Id="SERVERID" Secure="yes" />
    <Property Id="TOKEN" Secure="yes" />
    <Property Id="SERVERURL" Secure="yes" />

    <StandardDirectory Id="ProgramFiles64Folder">
      <Directory Id="INSTALLDIR" Name="Vantage">
        <Component Id="AgentExe" Guid="*">
          <File Id="AgentExe" Source="vantage-agent-windows-amd64.exe" Name="vantage-agent.exe" KeyPath="yes" />
        </Component>
        <Component Id="NssmExe" Guid="*">
          <File Id="NssmExe" Source="nssm.exe" Name="nssm.exe" KeyPath="yes" />
        </Component>
        <Component Id="SetupScript" Guid="*">
          <File Id="SetupScript" Source="setup.ps1" Name="setup.ps1" KeyPath="yes" />
        </Component>
      </Directory>
    </StandardDirectory>

    <Feature Id="Main">
      <ComponentRef Id="AgentExe" />
      <ComponentRef Id="NssmExe" />
      <ComponentRef Id="SetupScript" />
    </Feature>

    <!-- Write config.yaml, then install + start the service via nssm.
         Implemented as sequenced CustomActions running a helper script.

         Deferred CustomActions run out-of-process (and with Impersonate="no",
         as SYSTEM) with NO access to the installer property table, so
         "[SERVERID]"/"[TOKEN]"/"[SERVERURL]"/"[INSTALLDIR]" would resolve to
         empty strings if referenced directly on the deferred action. The fix
         is the standard CustomActionData marshaling pattern: an immediate
         SetProperty (type 51) with the SAME Id as the deferred CustomAction
         runs first (while property values are still visible) and resolves
         the formatted string; the deferred Directory/ExeCommand CustomAction
         that shares that Id then receives the resolved string back as its
         CustomActionData, referenced here as "[WriteConfig]". This avoids
         pulling in the WixToolset.Util extension (WixQuietExec64) purely to
         get CustomActionData plumbing.

         NOTE: this only builds/validates the MSI's XML in CI - it has not
         been verified with a real install on Windows. Needs a smoke test
         (msiexec /i, confirm C:\ProgramData\Vantage\config.yaml or similar
         is written with the correct values, and the service starts) on an
         actual Windows machine before this is trusted in production. -->
    <SetProperty Id="WriteConfig"
      Before="WriteConfig" Sequence="execute" Condition="NOT Installed"
      Value='cmd.exe /c powershell -ExecutionPolicy Bypass -File "[INSTALLDIR]setup.ps1" -ServerId "[SERVERID]" -Token "[TOKEN]" -ServerUrl "[SERVERURL]"' />

    <CustomAction Id="WriteConfig" Directory="INSTALLDIR" ExeCommand="[WriteConfig]"
      Execute="deferred" Impersonate="no" Return="check" />

    <!-- Teardown on uninstall: stop + remove the service BEFORE RemoveFiles
         deletes nssm.exe/setup.ps1. Same CustomActionData marshaling pattern
         as WriteConfig. REMOVE="ALL" = full uninstall (not a component-level
         repair/modify). -->
    <SetProperty Id="RemoveService"
      Before="RemoveService" Sequence="execute" Condition="REMOVE=&quot;ALL&quot;"
      Value='cmd.exe /c powershell -ExecutionPolicy Bypass -File "[INSTALLDIR]setup.ps1" -Uninstall' />

    <CustomAction Id="RemoveService" Directory="INSTALLDIR" ExeCommand="[RemoveService]"
      Execute="deferred" Impersonate="no" Return="ignore" />

    <InstallExecuteSequence>
      <Custom Action="WriteConfig" After="InstallFiles" Condition="NOT Installed" />
      <Custom Action="RemoveService" Before="RemoveFiles" Condition="REMOVE=&quot;ALL&quot;" />
    </InstallExecuteSequence>
  </Package>
</Wix>
