<?xml version="1.0" encoding="UTF-8"?>
<!--
  <copyright company="Cloudbase Solutions Srl">
    Copyright 2015 Cloudbase Solutions Srl

    Licensed under the Apache License, Version 2.0 (the "License"); you may
    not use this file except in compliance with the License.You may obtain
    a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the
    License for the specific language governing permissions and limitations
    under the License.
  </copyright>
-->
<!--
  Predefined variables with regards of the platform we are building for
-->
<?if $(var.Platform) = x64 ?>
  <?define Win64 = "yes" ?>
  <?define PlatformProgramFilesFolder = "ProgramFiles64Folder" ?>
<?else ?>
  <?define Win64 = "no" ?>
  <?define PlatformProgramFilesFolder = "ProgramFilesFolder" ?>
<?endif ?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
     xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
  <Product Id="{0DF60ED5-6C22-49A0-913E-77412D35C400}" Name="Open vSwitch for Hyper-V" Language="1033" Version="$(var.Version)"
           Manufacturer="The Linux Foundation&#169;" UpgradeCode="da802b12-433d-4742-a7ae-783aa0c48222">
    <Package InstallerVersion="405" Compressed="yes" InstallScope="perMachine" InstallPrivileges="elevated" Platform="x64" />

    <Property Id="Description" Value="Open vSwitch Extension" />
    <Property Id="DriverID" Value="{583CC151-73EC-4A6A-8B47-578297AD7623}" />
    <Property Id="DriverVersion" Value="$(var.Version)" />
    <Property Id="ExtensionType" Value="Forwarding" />
    <Property Id="BUILDNUMBER" Secure="yes">
      <RegistrySearch Id="Test" Root="HKLM" Key="SOFTWARE\Microsoft\Windows NT\CurrentVersion" Name="CurrentBuild" Type="raw" />
    </Property>

    <Condition Message="This application is only supported on Windows 2012 or higher.">
       <![CDATA[Installed OR (VersionNT >= 602)]]>
    </Condition>

    <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
    <MediaTemplate EmbedCab="yes" CompressionLevel="mszip" />

    <Feature Id="OpenvSwitchCLI" Title="Open vSwitch CLI" Level="1" Absent="disallow" InstallDefault="local" TypicalDefault="install" AllowAdvertise="no"
             Description="Installs the Open vSwitch command line tools."
             ConfigurableDirectory="INSTALLDIR" Display="expand">
      <ComponentGroupRef Id="BinariesComponentGroup" />
      <ComponentGroupRef Id="SymbolsComponentGroup" />
      <ComponentRef Id="OpenvSwitchEnvVars" />

      <Feature Id="VC140Redistx86" Title="Visual C++ 14 Runtime x86" AllowAdvertise="no" Display="hidden" Level="1"
               Absent="disallow" InstallDefault="followParent" TypicalDefault="install">
        <MergeRef Id="VC140Redistx86" />
      </Feature>
      <Feature Id="VC140Redistx64" Title="Visual C++ 14 Runtime x64" AllowAdvertise="no" Display="hidden" Level="1"
               Absent="disallow" InstallDefault="followParent" TypicalDefault="install">
        <MergeRef Id="VC140Redistx64" />
      </Feature>
    </Feature>

    <Feature Id="OpenvSwitchDriver" Title="Open vSwitch Hyper-V driver" Level="1" Absent="allow" InstallDefault="local" TypicalDefault="install" AllowAdvertise="no"
             Description="Installs the Open vSwitch Hyper-V switch extension driver." Display="expand">
      <ComponentRef Id="OpenvSwitchDriver_Win8" />
      <ComponentRef Id="OpenvSwitchDriver_Win8.1" />
      <ComponentRef Id="OpenvSwitchDriver_Win10" />
      <ComponentRef Id="ConfDir" />
      <ComponentRef Id="LogsDir" />
      <ComponentRef Id="AppDataDir" />
      <ComponentRef Id="OvsdbServerService" />
      <ComponentRef Id="OvsVSwitchdService" />
      <ComponentRef Id="OpenvSwitchPSModule" />
    </Feature>

    <UIRef Id="MyWixUI_FeatureTree" />
    <UIRef Id="WixUI_ErrorProgressText" />

    <WixVariable Id="WixUIBannerBmp" Value="images\bannrbmp.bmp" />
    <WixVariable Id="WixUIDialogBmp" Value="images\dlgbmp.bmp" />
    <WixVariable Id="WixUILicenseRtf" Value="License.rtf" />

    <InstallExecuteSequence>
      <Custom Action="InstallOpenVSwitchDriver_Prop" After="CostFinalize"><![CDATA[REMOVE <> "ALL" AND (&OpenvSwitchDriver = 3)]]></Custom>
      <Custom Action="InstallOpenVSwitchDriver" Before="ChangeOvsVSwitchdService" ><![CDATA[REMOVE <> "ALL" AND (&OpenvSwitchDriver = 3)]]></Custom>

      <Custom Action="UninstallOpenVSwitchDriver_Prop" After="CostFinalize"><![CDATA[(&OpenvSwitchDriver=2) AND (!OpenvSwitchDriver=3)]]></Custom>
      <Custom Action="UninstallOpenVSwitchDriver" Before="InstallFinalize" ><![CDATA[(&OpenvSwitchDriver=2) AND (!OpenvSwitchDriver=3)]]></Custom>

      <Custom Action="GenerateConfDB_Prop" After="CostFinalize"><![CDATA[REMOVE <> "ALL" AND (&OpenvSwitchDriver = 3)]]></Custom>
      <Custom Action="GenerateConfDB" Before="ChangeOvsdbServerService" ><![CDATA[REMOVE <> "ALL" AND (&OpenvSwitchDriver = 3)]]></Custom>

      <Custom Action="StartOvsdbServerService_Prop" After="CostFinalize"><![CDATA[NOT Installed AND (&OpenvSwitchDriver = 3)]]></Custom>
      <Custom Action="RestartOvsdbServerService_Prop" After="CostFinalize"><![CDATA[Installed AND REMOVE <> "ALL" AND (&OpenvSwitchDriver = 3)]]></Custom>
      <Custom Action="ChangeOvsdbServerService" Before="ChangeOvsVSwitchdService"><![CDATA[REMOVE <> "ALL" AND (&OpenvSwitchDriver = 3)]]></Custom>

      <Custom Action="InitializeDB_Prop" After="CostFinalize"><![CDATA[REMOVE <> "ALL" AND (&OpenvSwitchDriver = 3)]]></Custom>
      <Custom Action="InitializeDB" After="ChangeOvsdbServerService" ><![CDATA[REMOVE <> "ALL" AND (&OpenvSwitchDriver = 3)]]></Custom>

      <Custom Action="AddTriggerToOvsVswitchdService_Prop" After="CostFinalize"><![CDATA[NOT Installed AND (&OpenvSwitchDriver = 3)]]></Custom>
      <Custom Action="AddTriggerToOvsVswitchdService" Before="ChangeOvsVSwitchdService"><![CDATA[NOT Installed AND (&OpenvSwitchDriver = 3)]]></Custom>
      <Custom Action="StartOvsVSwitchdService_Prop" After="CostFinalize"><![CDATA[NOT Installed AND (&OpenvSwitchDriver = 3)]]></Custom>
      <Custom Action="RestartOvsVSwitchdService_Prop" After="CostFinalize"><![CDATA[Installed AND REMOVE <> "ALL" AND (&OpenvSwitchDriver = 3)]]></Custom>
      <Custom Action="ChangeOvsVSwitchdService" Before="InstallFinalize"><![CDATA[REMOVE <> "ALL" AND (&OpenvSwitchDriver = 3)]]></Custom>
    </InstallExecuteSequence>
  </Product>

  <Fragment>
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="$(var.PlatformProgramFilesFolder)">
        <Directory Id="INSTALLDIR" Name="Open vSwitch" />
      </Directory>
      <Directory Id="CommonAppDataFolder">
        <Directory Id="APPDATADIR" Name="openvswitch">
          <Component Id="AppDataDir" Guid="{9FAA3CD1-2234-4D6E-A974-D3D48A40773F}">
            <CreateFolder />
          </Component>
        </Directory>
      </Directory>
    </Directory>

    <DirectoryRef Id="INSTALLDIR">
      <Directory Id="BINARIESDIR" Name="bin" />
    </DirectoryRef>

    <DirectoryRef Id="INSTALLDIR">
      <Directory Id="SYMBOLSDIR" Name="symbols" />
    </DirectoryRef>

    <DirectoryRef Id="INSTALLDIR">
      <Directory Id="DRIVERDIR" Name="driver" />
    </DirectoryRef>

    <DirectoryRef Id="INSTALLDIR">
      <Directory Id="LOGSDIR" Name="logs">
        <Component Id="LogsDir" Guid="{CA78C399-6E2C-4D39-88FB-1F58AC9DF8AC}">
          <CreateFolder />
        </Component>
      </Directory>
    </DirectoryRef>

    <DirectoryRef Id="INSTALLDIR">
      <Directory Id="CONFDIR" Name="conf" />
    </DirectoryRef>
  </Fragment>

  <Fragment>
    <DirectoryRef Id="TARGETDIR">
      <Directory Id="System64Folder" Name="System64Folder">
        <Directory Id="WindowsPowerShell" Name="WindowsPowerShell">
          <Directory Id="v1.0" Name="v1.0">
            <Directory Id="Modules" Name="Modules">
              <Directory Id="OVSPSMODULEDIR" Name="OVS" />
            </Directory>
          </Directory>
        </Directory>
      </Directory>
    </DirectoryRef>

    <Component Id="OpenvSwitchPSModule" Directory="OVSPSMODULEDIR" Guid="{91819E0F-36F9-44C1-BCA3-973AE991E386}" Win64="yes">
      <File Id="OVS.psm1" Source="Services\OVS.psm1" Checksum="yes" KeyPath="yes" />
    </Component>
  </Fragment>

  <Fragment>
    <DirectoryRef Id="TARGETDIR">
      <Component Id="OpenvSwitchEnvVars" Guid="{4FCCA9D3-77BE-4283-815E-E3E63D4BF763}">
        <Environment Id="PATH" Name="PATH" Value="[BINARIESDIR]" Permanent="no" Part="last" Action="set" System="yes" />
        <Environment Id="OVS_RUNDIR" Name="OVS_RUNDIR" Value="[APPDATADIR]" Permanent="no" Part="all" Action="set" System="yes" />
        <Environment Id="OVS_PKGDATADIR" Name="OVS_PKGDATADIR" Value="[DRIVERDIR]" Permanent="no" Part="all" Action="set" System="yes" />
        <Environment Id="OVS_LOGDIR" Name="OVS_LOGDIR" Value="[LOGSDIR]" Permanent="no" Part="all" Action="set" System="yes" />
        <Environment Id="OVS_SYSCONFDIR" Name="OVS_SYSCONFDIR" Value="[CONFDIR]" Permanent="no" Part="all" Action="set" System="yes" />
        <Environment Id="OVS_BINDIR" Name="OVS_BINDIR" Value="[BINARIESDIR]" Permanent="no" Part="all" Action="set" System="yes" />
      </Component>
    </DirectoryRef>
  </Fragment>

  <Fragment>
    <Component Id="ConfDir" Directory="CONFDIR" Guid="{6DB7BC99-FF12-41E2-9EAA-A98A579BE27B}">
      <File Id="vswitch.ovsschema" Source="Services\vswitch.ovsschema" Checksum="yes" KeyPath="yes" />
      <RemoveFile Id="removeConfDb" Name="conf.db" On="install" />
      <RemoveFile Id="removeConfDbLock" Name=".conf.db.~lock~" On="install" />
    </Component>

    <Component Id="OvsdbServerService" Directory="BINARIESDIR" Guid="{280201D5-35E7-45D6-83B9-293F1A4F7F0E}">
      <File Id="ovsdbserver.exe" Source="Services\ovsdb-server.exe" Checksum="yes" />
      <ServiceInstall
                Arguments='--log-file="[LOGSDIR]ovsdb-server.log" --pidfile --service --service-monitor --unixctl="[APPDATADIR]ovsdb-server.ctl" --remote=punix:"[APPDATADIR]db.sock" "[CONFDIR]conf.db"'
                Id="OvsdbServerServiceInstaller"
                Type="ownProcess"
                Vital="yes"
                Name="ovsdb-server"
                DisplayName="Open vSwitch DB Service"
                Description="Open vSwitch DB Service"
                Start="auto"
                Account="LocalSystem"
                ErrorControl="ignore"
                Interactive="no">
        <ServiceDependency Id="vmms" />
      </ServiceInstall>
      <!-- Start service at the end of the setup, not here -->
      <ServiceControl
        Id="OvsdbServerServiceStartService"
        Stop="both"
        Remove="uninstall"
        Name="ovsdb-server"
        Wait="yes" />
      <util:ServiceConfig
        ServiceName="ovsdb-server"
        FirstFailureActionType="restart"
        SecondFailureActionType="restart"
        ThirdFailureActionType="restart"
        RestartServiceDelayInSeconds="1" />
    </Component>

    <Component Id="OvsVSwitchdService" Directory="BINARIESDIR" Guid="{A2ED5531-B2D7-4CD3-8ADE-B525A8BEB957}">
      <File Id="ovsvswitchd.exe" Source="Services\ovs-vswitchd.exe" Checksum="yes" />
      <ServiceInstall
                Arguments='--log-file="[LOGSDIR]ovs-vswitchd.log" unix:"[APPDATADIR]db.sock" --unixctl="[APPDATADIR]ovs-vswitchd.ctl" --pidfile --service --service-monitor'
                Id="OvsVSwitchdServiceServiceInstaller"
                Type="ownProcess"
                Vital="yes"
                Name="ovs-vswitchd"
                DisplayName="Open vSwitch Service"
                Description="Open vSwitch Service"
                Start="demand"
                Account="LocalSystem"
                ErrorControl="ignore"
                Interactive="no">
        <ServiceDependency Id="ovsdb-server" />
      </ServiceInstall>
      <!-- Start service at the end of the setup, not here -->
      <ServiceControl
        Id="OvsVSwitchdServiceServiceStartService"
        Stop="both"
        Remove="uninstall"
        Name="ovs-vswitchd"
        Wait="yes" />
      <util:ServiceConfig
        ServiceName="ovs-vswitchd"
        FirstFailureActionType="restart"
        SecondFailureActionType="restart"
        ThirdFailureActionType="restart"
        RestartServiceDelayInSeconds="1" />
    </Component>
  </Fragment>

  <Fragment>
    <DirectoryRef Id="DRIVERDIR">
      <Component Id='OpenvSwitchDriver_Win8' Guid='{7A1E2446-8196-4738-8362-5CFD55896A7B}'>
        <Condition><![CDATA[VersionNT = "602"]]></Condition>
          <File Id='win8_ovsext.sys' Name='ovsext.sys' DiskId='1' Source='Driver\Win8\ovsext.sys' Checksum='yes' KeyPath='yes' />
          <File Id='win8_ovsext.inf' Name='ovsext.inf' DiskId='1' Source='Driver\Win8\ovsext.inf' Checksum='yes' />
          <File Id='win8_ovsext.cat' Name='ovsext.cat' DiskId='1' Source='Driver\Win8\ovsext.cat' Checksum='yes' />
     </Component>
     <Component Id='OpenvSwitchDriver_Win8.1' Guid='{7A1E2446-8196-4738-8362-5CFD55896A7C}'>
        <Condition><![CDATA[(VersionNT = "603") AND (BUILDNUMBER < 10000)]]></Condition>
          <File Id='win81_ovsext.sys' Name='ovsext.sys' DiskId='1' Source='Driver\Win8.1\ovsext.sys' Checksum='yes' KeyPath='yes'/>
          <File Id='win81_ovsext.inf' Name='ovsext.inf' DiskId='1' Source='Driver\Win8.1\ovsext.inf' Checksum='yes' />
          <File Id='win81_ovsext.cat' Name='ovsext.cat' DiskId='1' Source='Driver\Win8.1\ovsext.cat' Checksum='yes' />
      </Component>
      <Component Id='OpenvSwitchDriver_Win10' Guid='{7A1E2446-8196-4738-8362-5CFD55896A7D}'>
      <Condition><![CDATA[BUILDNUMBER > 10000]]></Condition>
          <File Id='win10_ovsext.sys' Name='ovsext.sys' DiskId='1' Source='Driver\Win10\ovsext.sys' Checksum='yes' KeyPath='yes'/>
          <File Id='win10_ovsext.inf' Name='ovsext.inf' DiskId='1' Source='Driver\Win10\ovsext.inf' Checksum='yes' />
          <File Id='win10_ovsext.cat' Name='ovsext.cat' DiskId='1' Source='Driver\Win10\ovsext.cat' Checksum='yes' />
      </Component>
    </DirectoryRef>
  </Fragment>

  <Fragment>
    <DirectoryRef Id="TARGETDIR">
      <Merge Id="VC140Redistx86" SourceFile="Redist\Microsoft_VC140_CRT_x86.msm" DiskId="1" Language="0" />
      <Merge Id="VC140Redistx64" SourceFile="Redist\Microsoft_VC140_CRT_x64.msm" DiskId="1" Language="0" />
    </DirectoryRef>
  </Fragment>

</Wix>
