<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <!-- Must be included in .vcxproj files to use the Dektec DTAPI library -->

  <!-- Define DtapiIncludePath if not already defined and DTAPI.h found in some predefined locations -->
  <Choose>
    <When Condition="'$(TS_NO_DEKTEC)$(TS_NO_DTAPI)' != ''">
      <!-- TS_NO_DEKTEC or TS_NO_DTAPI environment variables defined, do not use DTAPI -->
    </When>
    <When Condition="'$(DtapiIncludePath)' != ''">
      <!-- Already externally defined, nothing to do -->
    </When>
    <When Condition="Exists('C:\Program Files (x86)\DekTec\SDKs\WinSDK\DTAPI\Include\DTAPI.h')">
      <PropertyGroup Label="UserMacros">
        <DtapiIncludePath>C:\Program Files (x86)\DekTec\SDKs\WinSDK\DTAPI\Include</DtapiIncludePath>
      </PropertyGroup>
    </When>
    <When Condition="Exists('C:\Program Files\DekTec\SDKs\WinSDK\DTAPI\Include\DTAPI.h')">
      <PropertyGroup Label="UserMacros">
        <DtapiIncludePath>C:\Program Files\DekTec\SDKs\WinSDK\DTAPI\Include</DtapiIncludePath>
      </PropertyGroup>
    </When>
  </Choose>

  <!-- Define DtapiLibraryPath if not already defined and DTAPI found in some predefined locations -->
  <Choose>
    <When Condition="'$(TS_NO_DEKTEC)$(TS_NO_DTAPI)' != ''">
      <!-- TS_NO_DEKTEC or TS_NO_DTAPI environment variables defined, do not use DTAPI -->
    </When>
    <When Condition="'$(DtapiLibraryPath)' != '' AND '$(DtapiVSDir)' != ''">
      <!-- Already externally defined, nothing to do -->
    </When>
    <!-- Visual Studio 2015 -->
    <When Condition="'$(MSBuildToolsVersion)' == '14.0' AND Exists('C:\Program Files (x86)\DekTec\SDKs\WinSDK\DTAPI\Lib\VC14')">
      <PropertyGroup Label="UserMacros">
        <DtapiLibraryPath>C:\Program Files (x86)\DekTec\SDKs\WinSDK\DTAPI\Lib</DtapiLibraryPath>
        <DtapiVSDir>VC14</DtapiVSDir>
      </PropertyGroup>
    </When>
    <When Condition="'$(MSBuildToolsVersion)' == '14.0' AND Exists('C:\Program Files\DekTec\SDKs\WinSDK\DTAPI\Lib\VC14')">
      <PropertyGroup Label="UserMacros">
        <DtapiLibraryPath>C:\Program Files\DekTec\SDKs\WinSDK\DTAPI\Lib</DtapiLibraryPath>
        <DtapiVSDir>VC14</DtapiVSDir>
      </PropertyGroup>
    </When>
    <!-- Visual Studio 2017 -->
    <When Condition="'$(MSBuildToolsVersion)' == '15.0' AND Exists('C:\Program Files (x86)\DekTec\SDKs\WinSDK\DTAPI\Lib\VC15')">
      <PropertyGroup Label="UserMacros">
        <DtapiLibraryPath>C:\Program Files (x86)\DekTec\SDKs\WinSDK\DTAPI\Lib</DtapiLibraryPath>
        <DtapiVSDir>VC15</DtapiVSDir>
      </PropertyGroup>
    </When>
    <When Condition="'$(MSBuildToolsVersion)' == '15.0' AND Exists('C:\Program Files\DekTec\SDKs\WinSDK\DTAPI\Lib\VC15')">
      <PropertyGroup Label="UserMacros">
        <DtapiLibraryPath>C:\Program Files\DekTec\SDKs\WinSDK\DTAPI\Lib</DtapiLibraryPath>
        <DtapiVSDir>VC15</DtapiVSDir>
      </PropertyGroup>
    </When>
    <!-- Visual Studio 2019 -->
    <When Condition="'$(MSBuildAssemblyVersion)' == '16.0' AND Exists('C:\Program Files (x86)\DekTec\SDKs\WinSDK\DTAPI\Lib\VC16')">
      <PropertyGroup Label="UserMacros">
        <DtapiLibraryPath>C:\Program Files (x86)\DekTec\SDKs\WinSDK\DTAPI\Lib</DtapiLibraryPath>
        <DtapiVSDir>VC16</DtapiVSDir>
      </PropertyGroup>
    </When>
    <When Condition="'$(MSBuildAssemblyVersion)' == '16.0' AND Exists('C:\Program Files\DekTec\SDKs\WinSDK\DTAPI\Lib\VC16')">
      <PropertyGroup Label="UserMacros">
        <DtapiLibraryPath>C:\Program Files\DekTec\SDKs\WinSDK\DTAPI\Lib</DtapiLibraryPath>
        <DtapiVSDir>VC16</DtapiVSDir>
      </PropertyGroup>
    </When>
    <!-- Visual Studio 2022 -->
    <When Condition="'$(MSBuildAssemblyVersion)' == '17.0' AND Exists('C:\Program Files (x86)\DekTec\SDKs\WinSDK\DTAPI\Lib\VC17')">
      <PropertyGroup Label="UserMacros">
        <DtapiLibraryPath>C:\Program Files (x86)\DekTec\SDKs\WinSDK\DTAPI\Lib</DtapiLibraryPath>
        <DtapiVSDir>VC17</DtapiVSDir>
      </PropertyGroup>
    </When>
    <When Condition="'$(MSBuildAssemblyVersion)' == '17.0' AND Exists('C:\Program Files\DekTec\SDKs\WinSDK\DTAPI\Lib\VC17')">
      <PropertyGroup Label="UserMacros">
        <DtapiLibraryPath>C:\Program Files\DekTec\SDKs\WinSDK\DTAPI\Lib</DtapiLibraryPath>
        <DtapiVSDir>VC17</DtapiVSDir>
      </PropertyGroup>
    </When>
  </Choose>

  <!-- Make sure that DtapiIncludePath and DtapiLibraryPath are defined as environment variables -->
  <ItemGroup Condition="'$(DtapiIncludePath)'!=''">
    <BuildMacro Include="DtapiIncludePath">
      <Value>$(DtapiIncludePath)</Value>
    </BuildMacro>
  </ItemGroup>
  <ItemGroup Condition="'$(DtapiLibraryPath)'!=''">
    <BuildMacro Include="DtapiLibraryPath">
      <Value>$(DtapiLibraryPath)</Value>
    </BuildMacro>
  </ItemGroup>
  <ItemGroup Condition="'$(DtapiVSDir)'!=''">
    <BuildMacro Include="DtapiVSDir">
      <Value>$(DtapiVSDir)</Value>
    </BuildMacro>
  </ItemGroup>

  <!-- Compile with or without DTAPI support -->
  <ItemDefinitionGroup Condition="'$(DtapiIncludePath)'!=''">
    <ClCompile>
      <AdditionalIncludeDirectories>$(DtapiIncludePath);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
    </ClCompile>
  </ItemDefinitionGroup>
  <ItemDefinitionGroup Condition="'$(DtapiIncludePath)'==''">
    <ClCompile>
      <PreprocessorDefinitions>TS_NO_DTAPI;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    </ClCompile>
  </ItemDefinitionGroup>

  <!-- Link with or without DTAPI support. -->
  <ItemDefinitionGroup Condition="'$(DtapiLibraryPath)'!=''">
    <ClCompile>
      <PreprocessorDefinitions>_DTAPI_DISABLE_AUTO_LINK;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    </ClCompile>
    <Link>
      <AdditionalLibraryDirectories>$(DtapiLibraryPath)\$(DtapiVSDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
    </Link>
    <Link Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
      <AdditionalDependencies>DTAPIMD.lib;%(AdditionalDependencies)</AdditionalDependencies>
      <!-- Workaround for a bug with Feb 2017 release of Dektec DTAPI -->
      <ImageHasSafeExceptionHandlers>false</ImageHasSafeExceptionHandlers>
    </Link>
    <Link Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
      <AdditionalDependencies>DTAPI64MD.lib;%(AdditionalDependencies)</AdditionalDependencies>
    </Link>
    <Link Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
      <AdditionalDependencies>DTAPIMDd.lib;%(AdditionalDependencies)</AdditionalDependencies>
    </Link>
    <Link Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
      <AdditionalDependencies>DTAPI64MDd.lib;%(AdditionalDependencies)</AdditionalDependencies>
    </Link>
  </ItemDefinitionGroup>

</Project>
