﻿<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Target Name="DownloadXZ">
        <SetConsoleTitle
            Title="$(CONSOLE_TITLE) - DownloadXZ" />
        <WebDownload
            Condition="!Exists('$(MSBuildProjectDirectory)\_archives\$(XZ_SRC_TAR_GZ)')"
            FileUri="$(XZ_URL)/$(XZ_SRC_TAR_GZ)"
            FileName="$(MSBuildProjectDirectory)\_archives\$(XZ_SRC_TAR_GZ)"
            UseDefaultCredentials="True" />
    </Target>
    <Target Name="ExtractXZ">
        <SetConsoleTitle
            Title="$(CONSOLE_TITLE) - ExtractXZ" />
        <ExtractWith7za
            Condition="!Exists('$(MSBuildProjectDirectory)\$(XZ_DIR)')"
            FileToExtract="$(MSBuildProjectDirectory)\_archives\$(XZ_SRC_TAR_GZ)" />
    </Target>
    <Target Name="PatchXZ">
        <SetConsoleTitle
            Title="$(CONSOLE_TITLE) - PatchXZ" />
        <Exec
            Condition="!Exists('$(MSBuildProjectDirectory)\$(XZ_DIR)\XZ_patched')"
            Command="&quot;$(PATCH_EXE)&quot; -p1 -i &quot;$(MSBuildProjectDirectory)\patches\$(XZ_PATCH_FILE)&quot;"
            WorkingDirectory="$(MSBuildProjectDirectory)\$(XZ_DIR)" />
        <Touch
            AlwaysCreate='true'
            Files="$(MSBuildProjectDirectory)\$(XZ_DIR)\XZ_patched" />
    </Target>
    <Target Name="BuildXZ">
        <SetConsoleTitle
            Title="$(CONSOLE_TITLE) - BuildXZ" />
        <MSBuild
            Projects="$(MSBuildProjectDirectory)\$(XZ_DIR)\windows\vs2017\xz_win.sln"
            Properties="Configuration=Release;Platform=Win32;VcpkgEnabled=false"/>
        <MSBuild
            Projects="$(MSBuildProjectDirectory)\$(XZ_DIR)\windows\vs2017\xz_win.sln"
            Properties="Configuration=Debug;Platform=Win32;VcpkgEnabled=false"/>
        <MSBuild
            Projects="$(MSBuildProjectDirectory)\$(XZ_DIR)\windows\vs2017\xz_win.sln"
            Properties="Configuration=Release;Platform=x64;VcpkgEnabled=false"/>
        <MSBuild
            Projects="$(MSBuildProjectDirectory)\$(XZ_DIR)\windows\vs2017\xz_win.sln"
            Properties="Configuration=Debug;Platform=x64;VcpkgEnabled=false"/>
    </Target>
    <Target Name="CopyXZ">
        <ItemGroup>
            <XZ_BIN_X64_DEBUG_FILES
                Include="$(MSBuildProjectDirectory)\build\out\x64\Debug\*.dll" />
            <XZ_BIN_X64_RELEASE_FILES
                Include="$(MSBuildProjectDirectory)\build\out\x64\Release\*.dll" />
            <XZ_BIN_X86_DEBUG_FILES
                Include="$(MSBuildProjectDirectory)\build\out\x86\Debug\*.dll" />
            <XZ_BIN_X86_RELEASE_FILES
                Include="$(MSBuildProjectDirectory)\build\out\x86\Release\*.dll" />
            <XZ_DOCS_FILES
                Include="$(MSBuildProjectDirectory)\$(XZ_DIR)\AUTHORS" />
            <XZ_DOCS_FILES
                Include="$(MSBuildProjectDirectory)\$(XZ_DIR)\COPYING" />
            <XZ_DOCS_FILES
                Include="$(MSBuildProjectDirectory)\$(XZ_DIR)\NEWS" />
            <XZ_DOCS_FILES
                Include="$(MSBuildProjectDirectory)\$(XZ_DIR)\THANKS" />
            <XZ_DOCS_FILES
                Include="$(MSBuildProjectDirectory)\$(XZ_DIR)\TODO" />
            <XZ_DOCS_FILES
                Include="$(MSBuildProjectDirectory)\$(XZ_DIR)\doc\*.txt" />
            <XZ_DOCS_FILES
                Include="$(MSBuildProjectDirectory)\$(XZ_DIR)\doc\man\pdf-letter\*.pdf" />
            <XZ_DOCS_FILES
                Include="$(MSBuildProjectDirectory)\$(XZ_DIR)\doc\man\txt\*.txt" />
            <XZ_INCLUDE_FILES
                Include="$(MSBuildProjectDirectory)\$(XZ_DIR)\src\liblzma\api\**\*.h" />
            <XZ_LIB_X64_DEBUG_FILES
                Include="$(MSBuildProjectDirectory)\build\out\x64\Debug\*.lib" />
            <XZ_LIB_X64_DEBUG_FILES
                Include="$(MSBuildProjectDirectory)\build\out\x64\Debug\*.pdb" />
            <XZ_LIB_X64_DEBUG_FILES
                Include="$(MSBuildProjectDirectory)\build\out\x64\Debug\*.lib" />
            <XZ_LIB_X64_DEBUG_FILES
                Include="$(MSBuildProjectDirectory)\build\out\x64\Debug\*.pdb" />
            <XZ_LIB_X64_RELEASE_FILES
                Include="$(MSBuildProjectDirectory)\build\out\x64\Release\*.lib" />
            <XZ_LIB_X64_RELEASE_FILES
                Include="$(MSBuildProjectDirectory)\build\out\x64\Release\*.pdb" />
            <XZ_LIB_X64_RELEASE_FILES
                Include="$(MSBuildProjectDirectory)\build\out\x64\Release\*.lib" />
            <XZ_LIB_X64_RELEASE_FILES
                Include="$(MSBuildProjectDirectory)\build\out\x64\Release\*.pdb" />
            <XZ_LIB_X86_DEBUG_FILES
                Include="$(MSBuildProjectDirectory)\build\out\x86\Debug\*.lib" />
            <XZ_LIB_X86_DEBUG_FILES
                Include="$(MSBuildProjectDirectory)\build\out\x86\Debug\*.pdb" />
            <XZ_LIB_X86_DEBUG_FILES
                Include="$(MSBuildProjectDirectory)\build\out\x86\Debug\*.lib" />
            <XZ_LIB_X86_DEBUG_FILES
                Include="$(MSBuildProjectDirectory)\build\out\x86\Debug\*.pdb" />
            <XZ_LIB_X86_RELEASE_FILES
                Include="$(MSBuildProjectDirectory)\build\out\x86\Release\*.lib" />
            <XZ_LIB_X86_RELEASE_FILES
                Include="$(MSBuildProjectDirectory)\build\out\x86\Release\*.pdb" />
            <XZ_LIB_X86_RELEASE_FILES
                Include="$(MSBuildProjectDirectory)\build\out\x86\Release\*.lib" />
            <XZ_LIB_X86_RELEASE_FILES
                Include="$(MSBuildProjectDirectory)\build\out\x86\Release\*.pdb" />
        </ItemGroup>
        <SetConsoleTitle
            Title="$(CONSOLE_TITLE) - CopyXZ" />
        <MakeDir
            Condition="!Exists('$(COMMON_SDKS)\docs\XZ')"
            Directories="$(COMMON_SDKS)\docs\XZ" />
        <Copy
            SourceFiles="@(XZ_BIN_X64_DEBUG_FILES)"
            DestinationFolder="$(COMMON_SDKS)\bin\x64\Debug"
            SkipUnchangedFiles="true" />
        <Copy
            SourceFiles="@(XZ_BIN_X64_RELEASE_FILES)"
            DestinationFolder="$(COMMON_SDKS)\bin\x64\Release"
            SkipUnchangedFiles="true" />
        <Copy
            SourceFiles="@(XZ_BIN_X86_DEBUG_FILES)"
            DestinationFolder="$(COMMON_SDKS)\bin\x86\Debug"
            SkipUnchangedFiles="true" />
        <Copy
            SourceFiles="@(XZ_BIN_X86_RELEASE_FILES)"
            DestinationFolder="$(COMMON_SDKS)\bin\x86\Release"
            SkipUnchangedFiles="true" />
        <Copy
            SourceFiles="@(XZ_DOCS_FILES)"
            DestinationFolder="$(COMMON_SDKS)\docs\XZ"
            SkipUnchangedFiles="true" />
        <Copy
            SourceFiles="@(XZ_INCLUDE_FILES)"
            DestinationFolder="$(COMMON_SDKS)\include\%(RecursiveDir)"
            SkipUnchangedFiles="true" />
        <Copy
            SourceFiles="@(XZ_LIB_X64_DEBUG_FILES)"
            DestinationFolder="$(COMMON_SDKS)\lib\x64\Debug"
            SkipUnchangedFiles="true" />
        <Copy
            SourceFiles="@(XZ_LIB_X64_RELEASE_FILES)"
            DestinationFolder="$(COMMON_SDKS)\lib\x64\Release"
            SkipUnchangedFiles="true" />
        <Copy
            SourceFiles="@(XZ_LIB_X86_DEBUG_FILES)"
            DestinationFolder="$(COMMON_SDKS)\lib\x86\Debug"
            SkipUnchangedFiles="true" />
        <Copy
            SourceFiles="@(XZ_LIB_X86_RELEASE_FILES)"
            DestinationFolder="$(COMMON_SDKS)\lib\x86\Release"
            SkipUnchangedFiles="true" />
    </Target>
    <Target Name="CleanXZ">
        <SetConsoleTitle
            Title="$(CONSOLE_TITLE) - CleanXZ" />
        <RemoveDir
            Condition="Exists('$(MSBuildProjectDirectory)\$(XZ_DIR)')"
            Directories="$(MSBuildProjectDirectory)\$(XZ_DIR)" />
    </Target>
    <Target Name="XZ">
        <SetConsoleTitle
            Title="$(CONSOLE_TITLE) - XZ" />
        <CallTarget
            Condition="!Exists('$(MSBuildProjectDirectory)\build_state\XZ_build_successful')"
            Targets="DownloadXZ;ExtractXZ;PatchXZ;BuildXZ;CopyXZ" />
        <Touch
            AlwaysCreate='true'
            Files="$(MSBuildProjectDirectory)\build_state\XZ_build_successful" />
    </Target>
</Project>
