<?xml version="1.0" encoding="UTF-8"?>
<!--
  WiX 4 installer for RMM Agent (PHP edition)
  Build: wix build RmmAgent.wxs -o RmmAgent.msi
-->
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs">

  <Package Name="RMM Agent (PHP)"
           Manufacturer="RMM Internal"
           Version="1.0.0.0"
           UpgradeCode="e8f4b5c6-d7e8-4f5a-9b0c-2d3e4f5a6b7c"
           Scope="perMachine">

    <SummaryInformation Description="RMM Agent (PHP edition)" />

    <MajorUpgrade DowngradeErrorMessage="Une version plus récente de [ProductName] est déjà installée." />
    <MediaTemplate EmbedCab="yes" />

    <Property Id="ENROLLMENT_TOKEN" Secure="yes" />
    <Property Id="SERVER_URL" Secure="yes" Value="https://votre-domaine.com" />

    <Feature Id="Main" Title="RMM Agent" Level="1">
      <ComponentRef Id="AgentExe" />
      <ComponentRef Id="AppSettings" />
      <ComponentRef Id="ServiceInstall" />
      <ComponentRef Id="DataConfig" />
    </Feature>

    <StandardDirectory Id="ProgramFiles64Folder">
      <Directory Id="INSTALLFOLDER" Name="RmmAgent" />
    </StandardDirectory>

    <StandardDirectory Id="CommonAppDataFolder">
      <Directory Id="DataFolder" Name="RmmAgent" />
    </StandardDirectory>

    <DirectoryRef Id="INSTALLFOLDER">
      <Component Id="AgentExe" Bitness="always64">
        <File Id="RmmAgentExe" Source="..\RmmAgent\bin\Release\net8.0-windows\win-x64\publish\RmmAgent.exe" KeyPath="yes" />
      </Component>

      <Component Id="AppSettings" Bitness="always64">
        <File Id="AppSettingsJson" Source="..\RmmAgent\appsettings.json" KeyPath="yes" />
      </Component>

      <Component Id="ServiceInstall" Bitness="always64" Guid="*">
        <File Id="ServiceFile" Source="..\RmmAgent\bin\Release\net8.0-windows\win-x64\publish\RmmAgent.exe" KeyPath="yes" />
        <ServiceInstall
          Id="RmmAgentService"
          Type="ownProcess"
          Name="RmmAgent"
          DisplayName="RMM Agent"
          Description="Agent de monitoring du parc informatique (PHP edition)"
          Start="auto"
          ErrorControl="normal"
          Account="LocalSystem"
          Interactive="no" />
        <ServiceControl
          Id="StartRmmAgent"
          Start="install"
          Stop="both"
          Remove="uninstall"
          Name="RmmAgent"
          Wait="yes" />
      </Component>
    </DirectoryRef>

    <DirectoryRef Id="DataFolder">
      <Component Id="DataConfig" Guid="*" Bitness="always64">
        <CreateFolder>
          <PermissionEx Sddl="O:BAG:BAD:P(A;;FA;;;SY)(A;;FA;;;BA)(A;;0x1200a9;;;BU)" />
        </CreateFolder>
        <File Id="DataAppSettings"
              Source="..\installer\runtime-appsettings-template.json"
              Name="appsettings.json"
              KeyPath="yes" />
      </Component>
    </DirectoryRef>

    <CustomAction
      Id="ConfigureAgent"
      Directory="DataFolder"
      ExeCommand="cmd.exe /c &quot;powershell -NoProfile -ExecutionPolicy Bypass -Command &quot;&quot;(Get-Content '[DataFolder]appsettings.json') -replace '__TOKEN__','[ENROLLMENT_TOKEN]' -replace '__SERVER__','[SERVER_URL]' | Set-Content '[DataFolder]appsettings.json'&quot;&quot;"
      Execute="deferred"
      Impersonate="no"
      Return="check" />

    <InstallExecuteSequence>
      <Custom Action="ConfigureAgent" Before="StartServices" Condition="NOT Installed" />
    </InstallExecuteSequence>

  </Package>
</Wix>
