<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs" xmlns:util="http://wixtoolset.org/schemas/v4/wxs/util" xmlns:ui="http://wixtoolset.org/schemas/v4/wxs/ui">

    <Package Name="{app}{x64}" UpgradeCode="{upgrade_code}" Language="1033" Codepage="1252" Version="{version}" Manufacturer="Kovid Goyal" InstallerVersion="300" Scope="perMachine">

    <SummaryInformation Keywords="Installer" Description="{app} Installer" Manufacturer="Kovid Goyal" />

    <!-- Disable creation of system restore points on calibre installs. Speeds
         up the install. We dont need system restore since we dont install any
         system DLLs/components anyway (apart from start menu entries) -->
    <Property Id="MSIFASTINSTALL" Value="3" />

    <Media Id="1" Cabinet="{app}.cab" CompressionLevel="{compression}" EmbedCab="yes" />
    <!-- The following line ensures that DLLs are replaced even if
        their version is the same as before or they dont have versions.
        Microsoft's brain dead installer will otherwise use file dates to
        determine whether to install a file or not. Simply not robust. And
        since we dont install any system files whatsoever, we can never replace
        a system file with an older version. This way the calibre install
        should always result in a consistent set of files being present in the
        installation folder, though of course, with Microsoft there are no
        guarantees of anything. -->
    <Property Id="REINSTALLMODE" Value="amus" />

    <!-- get the windows build number -->
    <Property Id="WINDOWSBUILDNUMBER" Secure="yes">
        <RegistrySearch Id="BuildNumberSearch" Root="HKLM" Key="SOFTWARE\Microsoft\Windows NT\CurrentVersion" Name="CurrentBuildNumber" Type="raw" />
    </Property>

    <Upgrade Id="{upgrade_code}">
        <UpgradeVersion Maximum="{version}" IncludeMaximum="yes" OnlyDetect="no" Language="1033" MigrateFeatures="yes" Property="OLDPRODUCTFOUND" />
        <UpgradeVersion Minimum="{version}" IncludeMinimum="no" OnlyDetect="yes" Language="1033" Property="NEWPRODUCTFOUND" />
    </Upgrade>
    <Upgrade Id="BEB2A80D-E902-4DAD-ADF9-8BD2DA42CFE1">
        <UpgradeVersion Maximum="{version}" IncludeMaximum="yes" OnlyDetect="no" Language="1033" MigrateFeatures="yes" Property="THIRTYTWOBITINSTALLFOUND" />
    </Upgrade>
    <CustomAction Id="PreventDowngrading" Error="Newer version of {app} already installed. If you want to downgrade you must uninstall {app} first." />

    <Property Id="APPLICATIONFOLDER">
        <RegistrySearch Id="calibreInstDir" Type="raw" Root="HKLM" Key="Software\{app}{x64}\Installer" Name="InstallPath" />
    </Property>



    <Icon Id="main_icon" SourceFile="{main_icon}" />
    <!-- <Icon Id="viewer_icon" SourceFile="{viewer_icon}"/> -->
    <!-- <Icon Id="editor_icon" SourceFile="{editor_icon}"/> -->

    <StandardDirectory Id="ProgramFiles64Folder">
        <Directory Id="APPLICATIONFOLDER" Name="Calibre2" />
    </StandardDirectory>
    <StandardDirectory Id="ProgramMenuFolder">
        <Directory Id="ApplicationProgramsFolder" Name="{app}{x64} - E-book Management" />
    </StandardDirectory>
    <StandardDirectory Id="DesktopFolder" />

    <DirectoryRef Id="APPLICATIONFOLDER">
            {app_components}
        <Component Id="AddToPath">
            <Environment Id="UpdatePath" Name="PATH" Action="set" System="yes" Part="last" Value="[APPLICATIONFOLDER]" />
            <RegistryValue Root="HKCU" Key="Software\Microsoft\{app}{x64}" Name="system_path_updated" Type="integer" Value="1" KeyPath="yes" />
        </Component>
        <Component Id="RememberInstallDir">
            <RegistryValue Root="HKLM" Key="Software\{app}{x64}\Installer" Name="InstallPath" Type="string" Value="[APPLICATIONFOLDER]" KeyPath="yes" />
        </Component>
		<Component Id="calibreProtocol">
			<RegistryKey Root="HKCR" Key="calibre" ForceDeleteOnUninstall="yes">
				<RegistryValue Type="string" Value="URL:calibre Protocol" KeyPath="yes" />
				<RegistryValue Type="string" Name="URL Protocol" Value="" />
				<RegistryKey Key="DefaultIcon">
					<RegistryValue Type="string" Value="[APPLICATIONFOLDER]calibre.exe" />
				</RegistryKey>
				<RegistryKey Key="shell\open\command">
					<RegistryValue Type="string" Value="&quot;[APPLICATIONFOLDER]calibre.exe&quot; &quot;%1&quot;" />
				</RegistryKey>
			</RegistryKey>
        </Component>
    </DirectoryRef>

    <DirectoryRef Id="ApplicationProgramsFolder">
        <Component Id="StartMenuShortcuts">
            <Shortcut Id="s1" Name="{app}{x64} - E-book management" Description="Manage your e-book collection, read or edit your e-books and download news" Target="[#{exe_map[calibre]}]" WorkingDirectory="APPLICATIONFOLDER">
				<ShortcutProperty Key="System.AppUserModel.ID" Value="{main_app_uid}"/>
			</Shortcut>
            <Shortcut Id="s2" Name="E-book viewer{x64}" Description="Viewer for all the major e-book formats" Target="[#{exe_map[ebook-viewer]}]" WorkingDirectory="APPLICATIONFOLDER">
				<ShortcutProperty Key="System.AppUserModel.ID" Value="{viewer_app_uid}"/>
			</Shortcut>
            <Shortcut Id="s4" Name="Edit E-book{x64}" Description="Edit e-books" Target="[#{exe_map[ebook-edit]}]" WorkingDirectory="APPLICATIONFOLDER">
				<ShortcutProperty Key="System.AppUserModel.ID" Value="{editor_app_uid}"/>
			</Shortcut>
            <Shortcut Id="s3" Name="LRF viewer{x64}" Description="Viewer for LRF format e-books" Target="[#{exe_map[lrfviewer]}]" WorkingDirectory="APPLICATIONFOLDER" />
            <util:InternetShortcut Id="OnlineDocumentationShortcut" Name="User Manual" Type="url" Target="https://manual.calibre-ebook.com" />
            <util:InternetShortcut Id="GetInvolvedS" Name="Get Involved" Type="url" Target="https://calibre-ebook.com/get-involved" />

            <RemoveFolder Id="ApplicationProgramsFolder" On="uninstall" />
            <RegistryValue Root="HKCU" Key="Software\Microsoft\{app}{x64}" Name="start_menu_shortcuts_installed" Type="integer" Value="1" KeyPath="yes" />
        </Component>
    </DirectoryRef>

    <StandardDirectory Id="DesktopFolder">
        <Component Id="DesktopShortcut">
            <Shortcut Id="ds1" Name="{app}{x64} - E-book management" Description="Manage your e-book collection, read or edit your e-books and download news" Target="[#{exe_map[calibre]}]" WorkingDirectory="APPLICATIONFOLDER" />
            <RegistryValue Root="HKCU" Key="Software\Microsoft\{app}{x64}" Name="desktop_shortcut_installed" Type="integer" Value="1" KeyPath="yes" />
        </Component>
    </StandardDirectory>


    <Feature Id="Complete" Title="{app}" Display="expand" Level="1" ConfigurableDirectory="APPLICATIONFOLDER">

        <Feature Id="MainApplication" Title="Program Files" Level="1" Description="All the files needed to run {app}" AllowAbsent="no">
            <ComponentRef Id="RememberInstallDir" />
        </Feature>

        <Feature Id="FSMS" Title="Start menu shortcuts" Level="1" Description="Program shortcuts installed in the Start Menu">
            <ComponentRef Id="StartMenuShortcuts" />
        </Feature>

        <Feature Id="DS" Title="Shortcut on desktop" Level="1" Description="Shortcut to {app} on your desktop">
            <ComponentRef Id="DesktopShortcut" />
        </Feature>

        <Feature Id="FAddToPath" Title="Add install directory to path" Level="1" Description="Add installation directory to PATH. Makes using command line tools easier">
            <ComponentRef Id="AddToPath" />
        </Feature>

		<Feature Id="FcalibreProtocol" Title="Register calibre:// URLs" Level="1" Description="Register to handle URLs starting with calibre://">
			<ComponentRef Id="calibreProtocol" />
		</Feature>
    </Feature>

    <!-- Add icon to entry in Add/Remove programs -->
    <Property Id="ARPPRODUCTICON" Value="main_icon" />
    <Property Id="ARPURLINFOABOUT" Value="https://calibre-ebook.com" />
    <Property Id="ARPHELPLINK" Value="https://calibre-ebook.com/help" />
    <Property Id="ARPURLUPDATEINFO" Value="https://calibre-ebook.com/download_windows" />
    <SetProperty Id="ARPINSTALLLOCATION" Value="[APPLICATIONFOLDER]" After="CostFinalize" />

    <!-- See https://en.wikipedia.org/wiki/Windows_10_version_history for build numbers -->

    <Launch Condition="Installed OR (WINDOWSBUILDNUMBER &gt;= 10240)" Message="calibre requires Windows 10 or newer." />
    <!-- On 64 bit installers there is a bug in WiX that causes the
        WixSetDefaultPerMachineFolder action to incorrectly set
        APPLICATIONFOLDER to the x86 value, so we override it. See
        http://stackoverflow.com/questions/5479790/wix-how-to-override-c-program-files-x86-on-x64-machine-in-wixui-advanced-s
    -->
    <CustomAction Id="OverwriteWixSetDefaultPerMachineFolder" Property="WixPerMachineFolder" Value="[APPLICATIONFOLDER]" Execute="immediate" />

    <InstallExecuteSequence>
        <Custom Action="PreventDowngrading" After="FindRelatedProducts" Condition="NEWPRODUCTFOUND" />
        <Custom Action="OverwriteWixSetDefaultPerMachineFolder" After="WixSetDefaultPerMachineFolder" />
        <RemoveExistingProducts After="InstallFinalize" />
    </InstallExecuteSequence>
    <InstallUISequence>
        <Custom Action="PreventDowngrading" After="FindRelatedProducts" Condition="NEWPRODUCTFOUND" />
        <Custom Action="OverwriteWixSetDefaultPerMachineFolder" After="WixSetDefaultPerMachineFolder" />
    </InstallUISequence>

    <UI>
        <ui:WixUI Id="WixUI_Advanced" />
        <UIRef Id="WixUI_ErrorProgressText" />
        <Publish Dialog="ExitDialog" Control="Finish" Event="DoAction" Value="LaunchApplication" Condition="WIXUI_EXITDIALOGOPTIONALCHECKBOX = 1 and NOT Installed" />

    </UI>

    <!--
        Set default folder name. For a per-machine installation, the default
        installation location will be [ProgramFilesFolder][ApplicationFolderName]
        and the user will be able to change it in the setup UI.
    -->
    <Property Id="ApplicationFolderName" Value="Calibre2" />
    <Property Id="WixAppFolder" Value="WixPerMachineFolder" />
    <WixVariable Id="WixUISupportPerUser" Value="0" />

    <!-- Add option to launch calibre after install -->
    <Property Id="WIXUI_EXITDIALOGOPTIONALCHECKBOXTEXT" Value="Launch {app}" />
    <Property Id="WIXUI_EXITDIALOGOPTIONALCHECKBOX" Value="1" />
    <Property Id="WixShellExecTarget" Value="[#{exe_map[calibre]}]" />
    <CustomAction Id="LaunchApplication" DllEntry="WixShellExec" Impersonate="yes" BinaryRef="Wix4UtilCA_X86" />

    <!-- UI -->
    <WixVariable Id="WixUILicenseRtf" Value="{license}" />
    <WixVariable Id="WixUIBannerBmp" Value="{banner}" />
    <WixVariable Id="WixUIDialogBmp" Value="{dialog}" />

    </Package>
</Wix>
