﻿<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">

    <?define ProductVersion="$(env.CLI_VERSION)" ?>

    <?define ProductName = "promptflow" ?>
    <?define ProductDescription = "Command-line tools for prompt flow." ?>
    <?define ProductAuthor = "Microsoft Corporation" ?>
    <?define ProductResources = ".\resources\" ?>
    <?define UpgradeCode32 = "8b748161-e07a-48f2-8cdf-401480df4694" ?>

    <?if $(var.Platform) = "x64" ?>
    <?define PromptflowCliRegistryGuid = "0efd984f-9eec-425b-b230-a3994b69649a" ?>
    <?define PromptflowServiceGuid = "d4e99207-77be-4bdf-a430-b08632c5aa2b" ?>
    <?define PromptflowSystemPathGuid = "4c321045-d4e0-4446-bda4-8c19eaa42af1" ?>
    <?define ProgramFilesFolder = "ProgramFiles64Folder" ?>
    <?define RemovePromptflowFolderGuid = "ee843aa5-2b72-4958-be84-53dbac17efc7" ?>
    <?define UpgradeCode = "772aa21f-f8d4-4771-b910-1dbce3f1920c" ?>
    <?define Architecture = "64-bit" ?>

    <?elseif $(var.Platform) = "x86" ?>
    <?define PromptflowCliRegistryGuid = "7c2c792d-c395-44a1-8222-8e4ea006abb9" ?>
    <?define PromptflowServiceGuid = "f706b208-a15d-4ae7-9185-cfcc43656570" ?>
    <?define PromptflowSystemPathGuid = "9661fe6a-ff48-4e7c-a60d-fc34c2d06ef3" ?>
    <?define ProgramFilesFolder = "ProgramFilesFolder" ?>
    <?define RemovePromptflowFolderGuid = "588ca5e1-38c6-4659-8b38-762df7ed5b28" ?>
    <?define UpgradeCode = $(var.UpgradeCode32) ?>
    <?define Architecture = "32-bit" ?>

    <?else ?>
    <?error Unsupported platform "$(var.Platform)" ?>
    <?endif ?>

    <Product Id="*" Name="$(var.ProductName) ($(var.Architecture))" Language="1033" Version="$(var.ProductVersion)" Manufacturer="$(var.ProductAuthor)" UpgradeCode="$(var.UpgradeCode)">
        <Package InstallerVersion="200" Compressed="yes" InstallScope="perUser" />

        <Upgrade Id="$(var.UpgradeCode)">
          <UpgradeVersion Property="WIX_UPGRADE_DETECTED" Maximum="$(var.ProductVersion)" IncludeMaximum="no" MigrateFeatures="yes" />
          <UpgradeVersion Property="WIX_DOWNGRADE_DETECTED" Minimum="$(var.ProductVersion)" IncludeMinimum="no" OnlyDetect="yes" />
        </Upgrade>
        <InstallExecuteSequence>
          <RemoveExistingProducts After="InstallExecute" />
        </InstallExecuteSequence>


        <!-- New product architectures should upgrade the original x86 product - even of the same version. -->
        <?if $(var.UpgradeCode) != $(var.UpgradeCode32) ?>
        <Upgrade Id="$(var.UpgradeCode32)">
          <UpgradeVersion Property="WIX_X86_UPGRADE_DETECTED" Maximum="$(var.ProductVersion)" IncludeMaximum="yes" MigrateFeatures="yes" />
          <UpgradeVersion Property="WIX_X86_DOWNGRADE_DETECTED" Minimum="$(var.ProductVersion)" IncludeMinimum="no" OnlyDetect="yes" />
        </Upgrade>
        <Condition Message="A newer version of $(var.ProductName) is already installed.">NOT (WIX_DOWNGRADE_DETECTED OR WIX_X86_DOWNGRADE_DETECTED)</Condition>
        <?else ?>
        <Condition Message="A newer version of $(var.ProductName) is already installed.">NOT WIX_DOWNGRADE_DETECTED</Condition>
        <?endif ?>

        <Media Id="1" Cabinet="promptflow.cab" EmbedCab="yes" CompressionLevel="high" />

        <Icon Id="PromptflowIcon" SourceFile="$(var.ProductResources)logo32.ico" />

        <Property Id="ARPPRODUCTICON" Value="PromptflowIcon" />
        <Property Id="ARPHELPLINK" Value="https://microsoft.github.io/promptflow/how-to-guides/quick-start.html" />
        <Property Id="ARPURLINFOABOUT" Value="https://microsoft.github.io/promptflow/how-to-guides/quick-start.html" />
        <Property Id="ARPURLUPDATEINFO" Value="https://microsoft.github.io/promptflow/how-to-guides/quick-start.html" />
        <Property Id="MSIFASTINSTALL" Value="7" />
        <Property Id="ApplicationFolderName" Value="promptflow" />
        <Property Id="WixAppFolder" Value="WixPerUserFolder" />

        <Feature Id="ProductFeature" Title="promptflow" Level="1" AllowAdvertise="no">
            <ComponentGroupRef Id="ProductComponents" />
        </Feature>

        <!--Custom action to propagate path env variable change-->
        <CustomActionRef Id="WixBroadcastEnvironmentChange" />

            <!-- User Interface -->
        <WixVariable Id="WixUILicenseRtf" Value="$(var.ProductResources)CLI_LICENSE.rtf"/>

        <UIRef Id="WixUI_ErrorProgressText"/>

        <!-- Show message to restart any terminals only if the PATH is changed -->
        <CustomAction Id="Set_WIXUI_EXITDIALOGOPTIONALTEXT" Property="WIXUI_EXITDIALOGOPTIONALTEXT" Value="Please close and reopen any active terminal window to use prompt flow." />
        <InstallUISequence>
          <Custom Action="Set_WIXUI_EXITDIALOGOPTIONALTEXT" After="CostFinalize">NOT Installed AND NOT WIX_UPGRADE_DETECTED</Custom>
        </InstallUISequence>

        <CustomAction Id="StartPromptFlowService"
            Directory="APPLICATIONFOLDER"
            Execute="deferred"
            ExeCommand="wscript.exe promptflow_service.vbs"
            Return="asyncNoWait" />
        <InstallExecuteSequence>
            <Custom Action="StartPromptFlowService" Before="InstallFinalize">NOT Installed OR WIX_UPGRADE_DETECTED</Custom>
        </InstallExecuteSequence>
    </Product>

    <Fragment>
        <Directory Id="TARGETDIR" Name="SourceDir">
            <Directory Id="$(var.ProgramFilesFolder)">
                <Directory Id="APPLICATIONFOLDER" Name="promptflow" />
            </Directory>
            <Directory Id="StartupFolder" />
        </Directory>

        <UIRef Id="WixUI_Advanced" />
    </Fragment>

    <Fragment>
        <ComponentGroup Id="PromptflowCliSettingsGroup">
            <Component Id="RemovePromptflowFolder" Directory="APPLICATIONFOLDER" Guid="$(var.RemovePromptflowFolderGuid)">
                <RemoveFolder Id="APPLICATIONFOLDER" On="uninstall" />
            </Component>

            <Component Id="PromptflowSystemPath" Directory="APPLICATIONFOLDER" Guid="$(var.PromptflowSystemPathGuid)">
                <Environment Id="PromptflowAddedToPATH"
                     Name="PATH"
                     Value="[APPLICATIONFOLDER]"
                     Permanent="no"
                     Part="first"
                     Action="set"
                     System="no" />
                <CreateFolder />
            </Component>

            <Component Id="promptflow_service.vbs" Directory="APPLICATIONFOLDER" Guid="$(var.PromptflowServiceGuid)">
                <File Id="promptflow_service.vbs" Source="scripts\promptflow_service.vbs" KeyPath="yes" Checksum="yes"/>
            </Component>

            <Component Id="ApplicationShortcut" Directory="StartupFolder" Guid="$(var.PromptflowCliRegistryGuid)">
                <Shortcut Id="ApplicationStartMenuShortcut"
                          Name="Prompt flow service"
                          Description="Prompt Flow Service"
                          Target="[#promptflow_service.vbs]"
                          WorkingDirectory="APPLICATIONFOLDER"
                          Advertise="no">
                          <Icon Id="PromptflowServiceIcon" SourceFile="$(var.ProductResources)logo32.ico" />
                </Shortcut>
                <RemoveFile Id="CleanUpShortCut" Directory="StartupFolder" Name="Prompt flow service" On="uninstall"/>
                <RegistryKey Root="HKCU" Key="Software\Microsoft\$(var.ProductName)" Action="createAndRemoveOnUninstall">
                    <RegistryValue Name="installed" Type="integer" Value="1" />
                    <RegistryValue Name="version" Type="string" Value="$(var.ProductVersion)" KeyPath="yes"/>
                </RegistryKey>
            </Component>


        </ComponentGroup>

        <ComponentGroup Id="ProductComponents">
          <ComponentGroupRef Id="PromptflowCliComponentGroup"/>
          <ComponentGroupRef Id="PromptflowCliSettingsGroup"/>
        </ComponentGroup>
    </Fragment>
</Wix>
