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

    <?include variables.wxi ?>

    <Product Id="*" UpgradeCode="124623D9-35D6-4D2E-9474-2ADACC8BABBB" Name="$(var.Name)" Manufacturer="$(var.Team)" Version="$(var.Version)" Language="1033">

        <Package Id="*" Compressed="yes" InstallerVersion="405" InstallPrivileges="elevated" InstallScope="perMachine" />

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

        <!--
        Schedule="afterInstallInitialize"
            Removes the installed product first then installs the new version, if the installation of the new version
            fails, Windows Installer also rolls back the removal of the installed product, in other words, reinstalls it.

        AllowDowngrades="yes"
            When set to yes, any version can be installed over any other version.

        Further info:
            http://wixtoolset.org/documentation/manual/v3/xsd/wix/majorupgrade.html
        -->
        <MajorUpgrade
            Schedule="afterInstallInitialize"
            AllowDowngrades="yes"
        />

        <!-- ========================================= Add/Remove Programs ========================================= -->

        <Icon Id="app.ico" SourceFile="$(var.AppIcon)" />
        <Property Id="ARPPRODUCTICON" Value="app.ico" />
        <Property Id='ARPCOMMENTS' Value="$(var.AppComments)" />
        <Property Id='ARPCONTACT' Value="$(var.AppContact)" />
        <Property Id='ARPREADME' Value="$(var.AppReadme)" />
        <Property Id="ARPHELPLINK" Value="$(var.AppHelpLink)" />
        <Property Id="ARPURLINFOABOUT" Value="$(var.AppSupportLink)" />
        <Property Id="ARPURLUPDATEINFO" Value="$(var.AppUpdateInfoLink)" />
        <SetProperty Id="ARPINSTALLLOCATION" Value="[INSTALLDIR]" After="CostFinalize" />

        <!-- ================================================ Files ================================================ -->

        <?if $(sys.BUILDARCH)="x64"?>
            <?define ProgramFilesFolder="ProgramFiles64Folder" ?>
        <?elseif $(sys.BUILDARCH)="x86"?>
            <?define ProgramFilesFolder="ProgramFilesFolder" ?>
        <?endif?>

        <Directory Id="TARGETDIR" Name="SourceDir">
            <Merge Id="VCRedist" SourceFile="$(var.VCRedistPath)\$(var.VCRedistFile)" DiskId="1" Language="0" />
            <Directory Id="$(var.ProgramFilesFolder)">
                <Directory Id="INSTALLDIR" Name="$(var.Name)">
                    <!-- Save the install location to the Registry -->
                    <Component Id="RegistryInstallLocation">
                        <RegistryValue Root="HKLM" Key="Software\$(var.Name)" Name="InstallLocation" Value="[INSTALLDIR]" Type="string" />
                    </Component>

                    <Directory Id="styles" Name="styles">
                        <Component><File Source="$(var.QtPath)\plugins\styles\qwindowsvistastyle.dll" /></Component>
                    </Directory>
                    <Directory Id="bearer" Name="bearer">
                        <Component><File Source="$(var.QtPath)\plugins\bearer\qgenericbearer.dll" /></Component>
                    </Directory>
                    <Directory Id="extensions" Name="extensions">
                        <Component><File Source="$(var.SQLitePath)\math.dll" /></Component>
                        <Component><File Source="$(var.SQLitePath)\formats.dll" /></Component>
                        <Component><File Source="$(var.SQLitePath)\fileio.dll" /></Component>
                    </Directory>
                    <Directory Id="imageformats" Name="imageformats">
                        <Component><File Source="$(var.QtPath)\plugins\imageformats\qgif.dll" /></Component>
                        <Component><File Source="$(var.QtPath)\plugins\imageformats\qicns.dll" /></Component>
                        <Component><File Source="$(var.QtPath)\plugins\imageformats\qico.dll" /></Component>
                        <Component><File Source="$(var.QtPath)\plugins\imageformats\qjpeg.dll" /></Component>
                        <Component><File Source="$(var.QtPath)\plugins\imageformats\qsvg.dll" /></Component>
                        <Component><File Source="$(var.QtPath)\plugins\imageformats\qtga.dll" /></Component>
                        <Component><File Source="$(var.QtPath)\plugins\imageformats\qtiff.dll" /></Component>
                        <Component><File Source="$(var.QtPath)\plugins\imageformats\qwbmp.dll" /></Component>
                        <Component><File Source="$(var.QtPath)\plugins\imageformats\qwebp.dll" /></Component>
                    </Directory>
                    <Directory Id="platforms" Name="platforms">
                        <Component><File Source="$(var.QtPath)\plugins\platforms\qwindows.dll" /></Component>
                    </Directory>
                    <Directory Id="printsupport" Name="printsupport">
                        <Component><File Source="$(var.QtPath)\plugins\printsupport\windowsprintersupport.dll" /></Component>
                    </Directory>
                    <Directory Id="licenses" Name="licenses">
                        <Component><File Source="..\..\LICENSE" /></Component>
                        <Component><File Source="..\..\LICENSE-PLUGINS" /></Component>
                    </Directory>
                    <Component><File Source="$(var.QtPath)\bin\Qt5Concurrent.dll" /></Component>
                    <Component><File Source="$(var.QtPath)\bin\Qt5Core.dll" /></Component>
                    <Component><File Source="$(var.QtPath)\bin\Qt5Gui.dll" /></Component>
                    <Component><File Source="$(var.QtPath)\bin\Qt5Network.dll" /></Component>
                    <Component><File Source="$(var.QtPath)\bin\Qt5PrintSupport.dll" /></Component>
                    <Component><File Source="$(var.QtPath)\bin\Qt5Widgets.dll" /></Component>
                    <Component><File Source="$(var.QtPath)\bin\Qt5Xml.dll" /></Component>
                    <Component><File Source="$(var.QtPath)\bin\Qt5Svg.dll" /></Component>
		    <!-- OpenSSL 1.1.1 -->
                    <?if $(sys.BUILDARCH)="x64"?>
                        <Component><File Id="libcrypto.dll" Source="$(var.OpenSSLPath)\libcrypto-1_1-x64.dll" /></Component>
                        <Component><File Id="libssl.dll" Source="$(var.OpenSSLPath)\libssl-1_1-x64.dll" /></Component>
                    <?elseif $(sys.BUILDARCH)="x86"?>
                        <Component><File Id="libcrypto.dll" Source="$(var.OpenSSLPath)\libcrypto-1_1.dll" /></Component>
                        <Component><File Id="libssl.dll" Source="$(var.OpenSSLPath)\libssl-1_1.dll" /></Component>
                    <?endif?>
                    <Component><File Source="$(var.SQLCipherPath)\sqlcipher.dll" /></Component>
                    <Component><File Source="$(var.SQLitePath)\sqlite3.dll" /></Component>
                    <Component><File Source="$(var.SQLiteExePath)\DB Browser for SQLite.exe" Checksum="yes" /></Component>
                    <Component><File Source="$(var.SQLCipherExePath)\DB Browser for SQLCipher.exe" Checksum="yes" /></Component>
                </Directory>
            </Directory>
            <Directory Id="DesktopFolder">
                <Component Id="SQLiteDesktopShortcut" Transitive="yes">
                    <Condition>SHORTCUT_SQLITE_DESKTOP</Condition>
                    <Shortcut Id="SLDSC" Name="DB Browser (SQLite)" Description="$(var.AppComments)" Target="[INSTALLDIR]DB Browser for SQLite.exe" WorkingDirectory="INSTALLDIR" />
                    <RegistryValue Root="HKLM" Key="Software\$(var.Name)" Name="SQLiteDesktopShortcut" Type="integer" Value="1" KeyPath="yes"/>
                </Component>
                <Component Id="SQLCipherDesktopShortcut" Transitive="yes">
                    <Condition>SHORTCUT_SQLCIPHER_DESKTOP</Condition>
                    <Shortcut Id="SCDSC" Name="DB Browser (SQLCipher)" Description="$(var.AppComments)" Target="[INSTALLDIR]DB Browser for SQLCipher.exe" WorkingDirectory="INSTALLDIR" />
                    <RegistryValue Root="HKLM" Key="Software\$(var.Name)" Name="SQLCipherDesktopShortcut" Type="integer" Value="1" KeyPath="yes"/>
                </Component>
            </Directory>
            <Directory Id="ProgramMenuFolder">
                <Component Id="SQLiteProgramMenuShortcut" Transitive="yes">
                    <Condition>SHORTCUT_SQLITE_PROGRAMMENU</Condition>
                    <Shortcut Id="SLPMSC" Name="DB Browser (SQLite)" Description="$(var.AppComments)" Target="[INSTALLDIR]DB Browser for SQLite.exe" WorkingDirectory="INSTALLDIR" />
                    <RegistryValue Root="HKLM" Key="Software\$(var.Name)" Name="SQLiteProgramMenuShortcut" Type="integer" Value="1" KeyPath="yes"/>
                </Component>
                <Component Id="SQLCipherProgramMenuShortcut" Transitive="yes">
                    <Condition>SHORTCUT_SQLCIPHER_PROGRAMMENU</Condition>
                    <Shortcut Id="SCPMSC" Name="DB Browser (SQLCipher)" Description="$(var.AppComments)" Target="[INSTALLDIR]DB Browser for SQLCipher.exe" WorkingDirectory="INSTALLDIR" />
                    <RegistryValue Root="HKLM" Key="Software\$(var.Name)" Name="SQLCipherProgramMenuShortcut" Type="integer" Value="1" KeyPath="yes"/>
                </Component>
            </Directory>
        </Directory>

        <!-- ============================================== Features =============================================== -->

        <Feature Id="Complete" Title="$(var.Name)" Description="The complete package" ConfigurableDirectory="INSTALLDIR" AllowAdvertise="no" Display="expand">

            <Feature Id="VCRedist" Title="Visual C++ Runtime" AllowAdvertise="no" Display="hidden">
                <MergeRef Id="VCRedist"/>
            </Feature>

            <Feature Id="Extensions" Title="SQLite Extensions" Description="SQLite runtime loadable extensions" AllowAdvertise="no" Display="expand">
                <Feature Id="MathExtension" Title="Math" Description="Provide mathematical and string extension functions for SQL queries." AllowAdvertise="no">
                    <ComponentRef Id="math.dll" />
                </Feature>
                <Feature Id="FormatsExtension" Title="Formats" Description="Provide additional field display formats." AllowAdvertise="no">
                    <ComponentRef Id="formats.dll" />
                </Feature>
                <Feature Id="FileioExtension" Title="Fileio" Description="Implements SQL functions readfile(), writefile(), and eponymous virtual type 'fsdir'." AllowAdvertise="no">
                    <ComponentRef Id="fileio.dll" />
                </Feature>
            </Feature>

            <!-- Registry -->
            <ComponentRef Id="RegistryInstallLocation" />

            <!-- Shortcuts -->
            <ComponentRef Id="SQLiteDesktopShortcut" />
            <ComponentRef Id="SQLiteProgramMenuShortcut" />
            <ComponentRef Id="SQLCipherDesktopShortcut" />
            <ComponentRef Id="SQLCipherProgramMenuShortcut" />

            <!-- Translations -->
            <ComponentGroupRef Id="TranslationsGroup" />

            <!-- Bearer -->
            <ComponentRef Id="qgenericbearer.dll" />
            <!-- Plugins -->
            <ComponentRef Id="qgif.dll" />
            <ComponentRef Id="qicns.dll" />
            <ComponentRef Id="qico.dll" />
            <ComponentRef Id="qjpeg.dll" />
            <ComponentRef Id="qsvg.dll" />
            <ComponentRef Id="qtga.dll" />
            <ComponentRef Id="qtiff.dll" />
            <ComponentRef Id="qwbmp.dll" />
            <ComponentRef Id="qwebp.dll" />
            <!-- Platforms -->
            <ComponentRef Id="qwindows.dll" />
            <!-- Windows Vista Style -->
                <ComponentRef Id="qwindowsvistastyle.dll" />
            <!-- Printer -->
            <ComponentRef Id="windowsprintersupport.dll" />
            <!-- Licenses -->
            <ComponentRef Id="LICENSE" />
            <ComponentRef Id="LICENSE_PLUGINS" />
            <!-- Qt -->
            <ComponentRef Id="Qt5Concurrent.dll" />
            <ComponentRef Id="Qt5Core.dll" />
            <ComponentRef Id="Qt5Gui.dll" />
            <ComponentRef Id="Qt5Network.dll" />
            <ComponentRef Id="Qt5PrintSupport.dll" />
            <ComponentRef Id="Qt5Widgets.dll" />
            <ComponentRef Id="Qt5Xml.dll" />
            <ComponentRef Id="Qt5Svg.dll" />
            <!-- OpenSSL -->
            <ComponentRef Id="libcrypto.dll" />
            <ComponentRef Id="libssl.dll" />
            <!-- SQLite & SQLCipher -->
            <ComponentRef Id="sqlcipher.dll" />
            <ComponentRef Id="sqlite3.dll" />
            <!-- Application -->
            <ComponentRef Id="DB_Browser_for_SQLite.exe" />
            <ComponentRef Id="DB_Browser_for_SQLCipher.exe" />
        </Feature>

    <!-- ============================================= Installation UI ============================================= -->

        <!-- License file -->
        <WixVariable Id="WixUILicenseRtf" Value="license.rtf" />
        <!-- Banner image -->
        <WixVariable Id="WixUIBannerBmp" Value="banner.bmp" />
        <!-- Background image -->
        <WixVariable Id="WixUIDialogBmp" Value="background.bmp" />

        <UI>
            <!-- Standard Dialog Set -->
            <UIRef Id="WixUI_FeatureTree" />

            <!-- Shortcuts Dialog -->
            <DialogRef Id="ShortcutsDialog" />

            <!-- Show the Shortcuts dialog after "LicenseAgreementDlg" and before "CustomizeDlg" dialog -->
            <Publish Dialog="LicenseAgreementDlg" Control="Next" Event="NewDialog" Value="ShortcutsDialog">LicenseAccepted = "1"</Publish>
            <Publish Dialog="CustomizeDlg" Control="Back" Event="NewDialog" Value="ShortcutsDialog" Order="2">NOT Installed</Publish>

            <!-- Restore the previous settings for the shortcuts -->
            <Property Id="SHORTCUT_SQLITE_DESKTOP" Secure="yes">
                <RegistrySearch Id="RS_SC_SL_DT" Root="HKLM" Key="Software\$(var.Name)" Name="SQLiteDesktopShortcut" Type="raw" />
            </Property>
            <Property Id="SHORTCUT_SQLITE_PROGRAMMENU" Secure="yes">
                <RegistrySearch Id="RS_SC_SL_PM" Root="HKLM" Key="Software\$(var.Name)" Name="SQLiteProgramMenuShortcut" Type="raw" />
            </Property>
            <Property Id="SHORTCUT_SQLCIPHER_DESKTOP" Secure="yes">
                <RegistrySearch Id="RS_SC_SC_DT" Root="HKLM" Key="Software\$(var.Name)" Name="SQLCipherDesktopShortcut" Type="raw" />
            </Property>
            <Property Id="SHORTCUT_SQLCIPHER_PROGRAMMENU" Secure="yes">
                <RegistrySearch Id="RS_SC_SC_PM" Root="HKLM" Key="Software\$(var.Name)" Name="SQLCipherProgramMenuShortcut" Type="raw" />
            </Property>

            <!-- Optional text, shown at the end of installation -->
            <Property Id="WIXUI_EXITDIALOGOPTIONALTEXT" Value="Thank you for installing $(var.Name)." />
        </UI>

    <!-- ===================================== Run the application after setup ===================================== -->
    <!--   http://wixtoolset.org/documentation/manual/v3/howtos/ui_and_localization/run_program_after_install.html   -->

        <!-- Step 1: Add the checkbox, and select it, at the end of installation -->
        <!--
        <Property Id="WIXUI_EXITDIALOGOPTIONALCHECKBOXTEXT" Value="Run $(var.Name)" />
        <Property Id="WIXUI_EXITDIALOGOPTIONALCHECKBOX" Value="1" />
        -->

        <!-- Step 2: Include the custom Action -->
        <!--
        <Property Id="WixShellExecTarget" Value="[#DB_Browser_for_SQLite.exe]" />
        <CustomAction Id="LaunchApplication" BinaryKey="WixCA" DllEntry="WixShellExec" Impersonate="yes" />
        -->

        <!-- Step 3: Trigger the custom action -->
        <!--
        <UI><Publish Control="Finish" Dialog="ExitDialog" Event="DoAction" Value="LaunchApplication">WIXUI_EXITDIALOGOPTIONALCHECKBOX = 1 and NOT Installed</Publish></UI>
        -->

    <!-- ================================================= Logging ================================================= -->
    <!-- Enable verbose logging during installation. The log file is named MSI*.LOG and located in %TEMP% folder.    -->

        <Property Id="MsiLogging" Value="voicewarmupx" />

    <!-- ======================================== Remove old NSIS installer ======================================== -->
    <!--
        When the application is installed using the NSIS installer, the installer writes a key in the registry. We will
        use this fact to check if an older version (up to v3.10.1) of the application is installed. If found, we will
        remove it before installing the application with the new MSI installer.

        NSIS installer writes the following key "HKLM\Software\<Company>\<Product>" in the registry after installation.
        So the right key to search for is "HKLM\Software\DB Browser for SQLite Team\DB Browser for SQLite".

        But the installer itself is a 32-bit application, and it can't write to the 64-bit registry. This works without
        a problem on 32-bit Windows. On 64-bit Winodws however, the key is written in the 32-bit registry, under the
        "Wow6432Node" key.

        Therefore, the `Win64="no"` attribute is needed to disable searching in the 64-bit registry. On 32-bit Windows
        the right key is searched, and on 64-bit Windows the same key is searched but under the "Wow6432Node" key.
    -->

        <!-- Search the registry to find the location of NSIS installation folder, and set "NSIS_INSTALLDIR" to its path. -->
        <Property Id="NSIS_INSTALLDIR">
            <RegistrySearch Id="RS_NSIS" Root="HKLM" Key="Software\DB Browser for SQLite Team\DB Browser for SQLite" Win64="no" Type="raw" />
        </Property>

        <!-- If registry search was successful, "NSIS_UNINSTALLER" property will hold the full path to "Uninstall.exe" -->
        <SetProperty Id="NSIS_UNINSTALLER" Value="[NSIS_INSTALLDIR]\Uninstall.exe" After="AppSearch">NSIS_INSTALLDIR</SetProperty>

        <!-- Run "Uninstall.exe /S" to silently remove the older version of the application. -->
        <CustomAction Id="CA_NSIS_UNINSTALL" Property="NSIS_UNINSTALLER" ExeCommand="/S" Execute="deferred" Impersonate="no" />

        <!-- Schedule the custom action above to run after "RemoveExistingProducts". -->
        <InstallExecuteSequence>
            <Custom Action='CA_NSIS_UNINSTALL' After='RemoveExistingProducts'>NSIS_INSTALLDIR</Custom>
        </InstallExecuteSequence>

    </Product>

</Wix>
