<?xml version="1.0" encoding="UTF-8"?>
<!--
  AyaanScript+ Windows MSI installer.
  Built from Linux with wixl (msitools).
  On install:
    1. Drops install-ayaanscript.ps1 to LOCALAPPDATA\AyaanScriptPlus
    2. Runs the bootstrap PowerShell script which:
       (a) Installs rustup if cargo is not already present
       (b) Runs cargo install ayaan force
    3. Adds shortcut to Start Menu

  MSI format gives us higher AV/SmartScreen reputation than custom .exe,
  proper uninstall via Add/Remove Programs, and standard behavior.
-->
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Product
      Id="*"
      Name="AyaanScript+"
      Language="1033"
      Version="1.36.0"
      Manufacturer="Ayaan"
      UpgradeCode="B5E14C8A-3F2C-4B0A-9F1D-AA12001CDEF1">

    <Package
        InstallerVersion="200"
        Compressed="yes"
        InstallScope="perUser"
        InstallPrivileges="limited"
        Description="AyaanScript+ scripting language and ASPKG package manager"
        Manufacturer="Ayaan" />

    <!--
      No MajorUpgrade element on purpose: MSI does no version comparison,
      no "newer version already installed" check, no downgrade block.
      Each install is independent. Cargo install force flag handles the
      actual ayaan binary replacement.
    -->

    <MediaTemplate EmbedCab="yes" CompressionLevel="high" />

    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="LocalAppDataFolder">
        <Directory Id="INSTALLDIR" Name="AyaanScriptPlus">
          <Component Id="cmpBootstrap" Guid="A1B2C3D4-1111-2222-3333-444455556666">
            <File Id="filBootstrap"
                  Source="install-ayaanscript.ps1"
                  Name="install-ayaanscript.ps1"
                  KeyPath="yes" />
            <File Id="filRunCmd"
                  Source="run-bootstrap.cmd"
                  Name="run-bootstrap.cmd" />
            <RemoveFolder Id="rfInstall" On="uninstall" />
            <RegistryValue
                Root="HKCU"
                Key="Software\AyaanScript+"
                Name="installed"
                Type="integer"
                Value="1" />
          </Component>
        </Directory>
      </Directory>

      <Directory Id="ProgramMenuFolder">
        <Directory Id="AppShortcutFolder" Name="AyaanScript+">
          <Component Id="cmpShortcuts" Guid="A1B2C3D4-7777-8888-9999-AAAABBBBCCCC">
            <Shortcut Id="scAyaan"
                      Name="AyaanScript+ REPL"
                      Description="Launch the AyaanScript+ REPL"
                      Target="[%USERPROFILE]\.cargo\bin\ayaan.exe"
                      WorkingDirectory="PersonalFolder" />
            <Shortcut Id="scAspkg"
                      Name="ASPKG package manager"
                      Description="Run the ASPKG package manager"
                      Target="[%USERPROFILE]\.cargo\bin\aspkg.exe"
                      WorkingDirectory="PersonalFolder" />
            <RemoveFolder Id="rfShortcut" On="uninstall" />
            <RegistryValue
                Root="HKCU"
                Key="Software\AyaanScript+"
                Name="shortcuts"
                Type="integer"
                Value="1"
                KeyPath="yes" />
          </Component>
        </Directory>
      </Directory>
    </Directory>

    <Feature Id="featMain" Title="AyaanScript+" Level="1">
      <ComponentRef Id="cmpBootstrap" />
      <ComponentRef Id="cmpShortcuts" />
    </Feature>

    <!--
      Bootstrap runs in a VISIBLE PowerShell window so the user sees progress.
      rustup download takes a couple of minutes; silent execution would look
      frozen. The start command detaches the console window from the MSI
      engine and stays open via NoExit until the user closes it.
    -->
    <CustomAction
        Id="caRunBootstrap"
        FileKey="filRunCmd"
        ExeCommand=""
        Execute="deferred"
        Impersonate="yes"
        Return="ignore" />

    <InstallExecuteSequence>
      <Custom Action="caRunBootstrap" After="InstallFiles">NOT REMOVE</Custom>
    </InstallExecuteSequence>
  </Product>
</Wix>
