<?xml version="1.0" encoding="UTF-8"?>
<Wix
    xmlns="http://schemas.microsoft.com/wix/2006/wi"
    xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
  <Product
      Id="*"
      Name="NimbusBeaconNode"
      Language="1033"
      Version="1.0.0.0"
      Manufacturer="Status"
      UpgradeCode="7b226e1e-bb65-4bbf-a43a-dfbff394ea36">
    <Package
        InstallerVersion="200"
        Compressed="yes"
        InstallScope="perMachine" />

    <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
    <MediaTemplate
        CabinetTemplate="NBN_{0}.cab"
        EmbedCab="yes" />

    <Property
        Id="WIXUI_INSTALLDIR"
        Value="INSTALLFOLDER" />
    <UI>
      <UIRef Id="Nimbus_InstallDir" />
    </UI>
    <WixVariable
        Id="WixUILicenseRtf"
        Value="$(sys.CURRENTDIR)/nimbus-wix/LICENSE-APACHEv2.rtf" />
    <Feature
        Id="ProductFeature"
        Title="Nimbus Beacon Node"
        Level="1">
      <ComponentGroupRef Id="ProductComponents" />
    </Feature>
    <Property
        Id="Command"
        Value=" " />
    <SetProperty
        Id="Command"
        After="InstallFiles"
        Sequence='execute'
        Value="\&quot;[INSTALLFOLDER]nimbus_beacon_node.exe\&quot;" />

    <Property
        Id="CommandArgs"
        Value=" " />
    <SetProperty
        Id="CommandArgs"
        After="InstallFiles"
        Sequence='execute'
        Value="--run-as-service --network=\&quot;[NETWORK]\&quot; --web3-url=\&quot;[WURL]\&quot; --data-dir=\&quot;[%APPDATA]\Nimbus\BeaconNode\&quot; --log-file=\&quot;[%APPDATA]\Nimbus\BeaconNode\Nimbus.log\&quot;" />
    <CustomAction
        Id="SCEdit"
        Directory="INSTALLFOLDER"
        Execute="deferred"
        Impersonate="no"
        ExeCommand="sc.exe config NIMBUS_BEACON_NODE binpath=&quot;[Command] [CommandArgs]&quot;"
        Return="ignore" />
    <CustomAction
        Id="SCStart"
        Directory="INSTALLFOLDER"
        Execute="deferred"
        Impersonate="no"
        ExeCommand="sc.exe start NIMBUS_BEACON_NODE"
        Return="ignore" />
    <CustomAction
        Id="SCDelete"
        Directory="INSTALLFOLDER"
        Execute="deferred"
        Impersonate="no"
        ExeCommand="sc.exe delete NIMBUS_BEACON_NODE"
        Return="ignore" />
    <CustomAction
        Id="FolderPerms"
        Directory="INSTALLFOLDER"
        Execute="deferred"
        Impersonate="no"
        ExeCommand="icacls &quot;[%APPDATA]\Nimbus&quot; /inheritancelevel:e"
        Return="ignore" />
    <CustomAction
        Id="FolderPerms2"
        Directory="INSTALLFOLDER"
        Execute="deferred"
        Impersonate="no"
        ExeCommand="cmd.exe /c takeown /a /d Y /r /f \&quot;[%APPDATA]\Nimbus\&quot;"
        Return="ignore" />
    <CustomAction
        Id="FolderPerms3"
        Directory="INSTALLFOLDER"
        Execute="deferred"
        Impersonate="no"
        ExeCommand=" icacls \&quot;[%APPDATA]\Nimbus\&quot; /grant administrators:F"
        Return="ignore" />

    <InstallExecuteSequence>
      <Custom
          Action="SCEdit"
          Before="InstallFinalize">
            NOT REMOVE
      </Custom>
      <Custom
          Action="FolderPerms"
          After="SCEdit">
              NOT REMOVE
      </Custom>
      <Custom
          Action="FolderPerms2"
          After="FolderPerms">
              NOT REMOVE
      </Custom>
      <Custom
          Action="FolderPerms3"
          After="FolderPerms2">
              NOT REMOVE
      </Custom>
      <Custom
          Action="SCStart"
          After="SCEdit">
            NOT REMOVE AND (STARTAFTERINSTALL = "1")
      </Custom>
      <Custom
          Action="SCDelete"
          Before="InstallFinalize">
            Installed AND (REMOVE = "ALL")
      </Custom>
    </InstallExecuteSequence>
  </Product>

  <Fragment>
    <Directory
        Id="TARGETDIR"
        Name="SourceDir">
      <Directory Id="ProgramFilesFolder">
        <Directory
            Id="INSTALLFOLDER"
            Name="NimbusBeaconNode" />
      </Directory>
      <Directory Id="AppDataFolder">
        <Directory
            Id="NimbusFolder"
            Name="Nimbus"></Directory>
      </Directory>
    </Directory>
  </Fragment>

  <Fragment>
    <ComponentGroup
        Id="ProductComponents"
        Directory="INSTALLFOLDER">
      <Component
          Id="ProductComponent"
          Guid="fca4d113-ea07-4d47-b1e4-956b57d8eba9">
        <ServiceInstall
            DisplayName="Nimbus Beacon Node Service"
            Id="NBNS"
            ErrorControl="normal"
            Name="NIMBUS_BEACON_NODE"
            Start="auto"
            Type="ownProcess"
            Account="NT SERVICE\NIMBUS_BEACON_NODE" />
        <File Source="$(sys.CURRENTDIR)/build/nimbus_beacon_node.exe" />
        <CreateFolder Directory="NimbusFolder">
          <util:PermissionEx
              GenericAll="yes"
              Traverse="yes"
              User="NIMBUS_BEACON_NODE"
              Domain="NT SERVICE" />
          <util:PermissionEx
              Read="yes"
              GenericRead="yes"
              Traverse="yes"
              User="Users" />
          <util:PermissionEx
              User="Administrators"
              Traverse="yes"
              TakeOwnership="yes"
              GenericAll="yes" />
        </CreateFolder>
        <RemoveFolder
            Id="RemoveNimbusFolder"
            Directory="NimbusFolder"
            On="uninstall" />
      </Component>
    </ComponentGroup>
  </Fragment>
</Wix>