<?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 Name='{app}{x64}' Id='*' UpgradeCode='{upgrade_code}' Language='1033' Codepage='1252' Version='{version}' Manufacturer='Kovid Goyal'>
				
    <Package Id='*' Keywords='Installer' Description="{app} Installer"
        Comments='{app} is a registered trademark of Kovid Goyal' Manufacturer='Kovid Goyal'
        InstallerVersion='300' Languages='1033' Compressed='yes'
        SummaryCodepage='1252' />

    <!-- 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'/>

    <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>
    <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>

    <Directory Id='TARGETDIR' Name='SourceDir'>
        <Directory Id='{ProgramFilesFolder}' Name='PFiles'>
            <!-- The name must be calibre on 32 bit to ensure
                that the component guids dont change compared to previous msis.
                However, on 64 bit it must be Calibre2 otherwise by default it
                will install to C:\Program Files\calibre -->
            <Directory Id='APPLICATIONFOLDER' Name="{appfolder}" />
        </Directory>
        <Directory Id="ProgramMenuFolder">
            <Directory Id="ApplicationProgramsFolder" Name="{app}{x64} - E-book Management"/>
        </Directory>
        <Directory Id="DesktopFolder" Name="Desktop"/>
    </Directory>

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

    <DirectoryRef Id="APPLICATIONFOLDER">
        {app_components}
        <Component Id="AddToPath" Guid="*">
            <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" Guid="*">
            <RegistryValue Root="HKLM" Key="Software\{app}{x64}\Installer" Name="InstallPath" Type="string" Value="[APPLICATIONFOLDER]" KeyPath="yes"/>
        </Component>
    </DirectoryRef>

    <DirectoryRef Id="ApplicationProgramsFolder">
        <Component Id="StartMenuShortcuts" Guid="*">
            <Shortcut Id="s1" Name="{app}{x64} - E-book management"
                Description="Manage your e-book collection and download news"
                Target="[#{exe_map[calibre]}]"
                WorkingDirectory="APPLICATIONROOTDIRECTORY" />
            <Shortcut Id="s2" Name="E-book viewer{x64}"
                Description="Viewer for all the major e-book formats"
                Target="[#{exe_map[ebook-viewer]}]"
                WorkingDirectory="APPLICATIONROOTDIRECTORY" />
            <Shortcut Id="s4" Name="Edit E-book{x64}"
                Description="Edit e-books"
                Target="[#{exe_map[ebook-edit]}]"
                WorkingDirectory="APPLICATIONROOTDIRECTORY" />
            <Shortcut Id="s3" Name="LRF viewer{x64}"
                Description="Viewer for LRF format e-books"
                Target="[#{exe_map[lrfviewer]}]"
                WorkingDirectory="APPLICATIONROOTDIRECTORY" />
            <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>

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


    <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}" Absent="disallow">
            <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>

    <!-- 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" />

    <Condition
        Message="This application is only supported on {minverhuman}, or higher.">
        <![CDATA[Installed OR (VersionNT >= {minver})]]>
    </Condition>
    <!-- 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">NEWPRODUCTFOUND</Custom>
        {fix_wix}
        <RemoveExistingProducts After="InstallFinalize" />
    </InstallExecuteSequence>
    <InstallUISequence>
        <Custom Action="PreventDowngrading" After="FindRelatedProducts">NEWPRODUCTFOUND</Custom>
        {fix_wix}
    </InstallUISequence>

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

    </UI>

    <!-- 
    Set default folder name and allow only per machine installs.
    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. This is no longer necessary
    (i.e. per user installs should work) but left this way as I
    dont want to deal with the complications 
    -->
    <Property Id="ApplicationFolderName" Value="Calibre2" />
    <Property Id="WixAppFolder" Value="WixPerMachineFolder" />
    <Property Id="ALLUSERS" Value="1" />
    <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" BinaryKey="WixCA" DllEntry="WixShellExec" Impersonate="yes"/>

</Product>
</Wix>
