<Project DefaultTargets="run" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  
  <PropertyGroup>
    <verbose>Minimal</verbose>
    <OutputRoot>Release</OutputRoot>
    <OutputSourceRoot>$(OutputRoot)\Source</OutputSourceRoot>
    <OutputBinRoot>$(OutputRoot)\Bins</OutputBinRoot>
    <SourceRoot>..\Source</SourceRoot>
    <LibRoot>$(SourceRoot)\HtmlRenderer</LibRoot>
    <DemoRoot>$(SourceRoot)\Demo</DemoRoot>
    <BuildWarn>WarningLevel=0</BuildWarn>
    <Build20>TargetFrameworkVersion=v2.0</Build20>
    <Build35>TargetFrameworkVersion=v3.5;TargetFrameworkProfile=client</Build35>
    <Build40>TargetFrameworkVersion=v4.0;TargetFrameworkProfile=client</Build40>
    <Build45>TargetFrameworkVersion=v4.5</Build45>
    <BuildProperties>Configuration=Release</BuildProperties>
    <BuildOutputPath>OutputPath=..\..\Build\$(OutputBinRoot)</BuildOutputPath>
    <DemoBuildOutputPath>OutputPath=..\..\Build\$(OutputRoot)</DemoBuildOutputPath>
  </PropertyGroup>

  <!--Execute all steps of the build-->
  <Target Name="Run">

    <Message Importance="high" Text="====================== Clean build output..."/>
    <RemoveDir Directories="$(OutputRoot)" ContinueOnError="true"/>
    <RemoveDir Directories="$(OutputRoot)" ContinueOnError="true"/>
    <RemoveDir Directories="$(OutputRoot)" ContinueOnError="true"/>
    <RemoveDir Directories="$(OutputRoot)"/>

    <Message Importance="high" Text="====================== Build demo..."/>
    <CallTarget Targets="BuildDemo"/>

    <Message Importance="high" Text="====================== Build libraries..."/>
    <CallTarget Targets="BuildLibrary"/>

    <Message Importance="high" Text="====================== Get current version..."/>
    <GetAssemblyIdentity AssemblyFiles="$(OutputBinRoot)\NET20\HtmlRenderer.dll">
      <Output TaskParameter="Assemblies" ItemName="AsmInfo" />
    </GetAssemblyIdentity>

    <Message Importance="high" Text="====================== Build demo..."/>
    <CallTarget Targets="BuildDemo"/>

    <Message Importance="high" Text="====================== Copy source..."/>
    <CallTarget Targets="CopySource"/>

    <Message Importance="high" Text="====================== Create archive..."/>
    <MSBuild Projects="build.xml" Targets="CreateArchive" Properties="Version=%(AsmInfo.Version)"/>

    <Message Importance="high" Text="====================== Create NuGet package..."/>
    <MSBuild Projects="build.xml" Targets="CreatePackage" Properties="Version=%(AsmInfo.Version)"/>

    <Message Importance="high" Text="====================== Validating build..."/>
    <Error Condition="!Exists('$(OutputBinRoot)\NET20\HtmlRenderer.dll')" Text="Validation failed (.NET 2.0)!" />
    <Error Condition="!Exists('$(OutputBinRoot)\NET35\HtmlRenderer.dll')" Text="Validation failed (.NET 3.5)!" />
    <Error Condition="!Exists('$(OutputBinRoot)\NET40\HtmlRenderer.dll')" Text="Validation failed (.NET 4.0)!" />
    <Error Condition="!Exists('$(OutputBinRoot)\NET45\HtmlRenderer.dll')" Text="Validation failed (.NET 4.5)!" />
    <Error Condition="!Exists('$(OutputSourceRoot)\HtmlRenderer.sln')" Text="Validation failed (Source)!" />
    <Error Condition="!Exists('$(OutputRoot)\HtmlRendererDemo.exe')" Text="Validation failed (Demo)!" />
    <Error Condition="!Exists('$(OutputRoot)\HtmlRenderer.dll')" Text="Validation failed (Demo)!" />
    <Error Condition="!Exists('$(OutputRoot)\HtmlRenderer.dll')" Text="Validation failed (Demo)!" />
    <Error Condition="!Exists('$(OutputRoot)\HtmlRenderer %(AsmInfo.Version).zip')" Text="Validation failed (zip)!" />

    <Message Importance="high" Text="====================== SUCCESS!"/>

  </Target>

  <!--Build all the multi-target dlls of the library-->
  <Target Name="BuildLibrary">
    <Message Importance="high" Text="====================== Build .NET 2.0..."/>
    <MSBuild Properties="$(BuildProperties);$(BuildOutputPath)\NET20;$(BuildWarn);$(Build20)" UseResultsCache="true" Targets="Rebuild" Projects="$(LibRoot)\HtmlRenderer.csproj" />

    <Message Importance="high" Text="====================== Build .NET 3.5..."/>
    <MSBuild Properties="$(BuildProperties);$(BuildOutputPath)\NET35;$(BuildWarn);$(Build35)" Targets="Rebuild" Projects="$(LibRoot)\HtmlRenderer.csproj" />

    <Message Importance="high" Text="====================== Build .NET 4.0..."/>
    <MSBuild Properties="$(BuildProperties);$(BuildOutputPath)\NET40;$(BuildWarn);$(Build40)" Targets="Rebuild" Projects="$(LibRoot)\HtmlRenderer.csproj" />

    <Message Importance="high" Text="====================== Build .NET 4.5..."/>
    <MSBuild Properties="$(BuildProperties);$(BuildOutputPath)\NET45;$(BuildWarn);$(Build45)" Targets="Rebuild" Projects="$(LibRoot)\HtmlRenderer.csproj" />
  </Target>

  <!--Get current version-->
  <Target Name="GetVersion">
    <GetAssemblyIdentity AssemblyFiles="$(OutputBinRoot)\NET20\HtmlRenderer.dll">
      <Output TaskParameter="Assemblies" ItemName="AsmInfo" />
    </GetAssemblyIdentity>
    <CreateProperty Value="%(AsmInfo.Version)">
      <Output TaskParameter="Value" PropertyName="Version"/>
    </CreateProperty>
  </Target>

    <!--Build the demo application and delete the pdbs-->
  <Target Name="BuildDemo">
    <MSBuild Properties="$(BuildProperties);$(DemoBuildOutputPath);$(BuildWarn)" UseResultsCache="true" Targets="Rebuild" Projects="$(DemoRoot)\HtmlRenderer.Demo.csproj" />
    <ItemGroup>
      <files Include="$(OutputRoot)\*.*" Exclude="$(OutputRoot)\*.exe;$(OutputRoot)\*.dll;"/>
    </ItemGroup>
    <Delete Files="@(files)"/>
  </Target>

  <!--Copy all the source and remove the bin and obj folders-->
  <Target Name="CopySource">
    <ItemGroup>
      <Source Include="$(SourceRoot)\**\*.*" />
    </ItemGroup>
    <Copy SourceFiles="@(Source)" DestinationFolder="$(OutputSourceRoot)\%(RecursiveDir)"/>
    <RemoveDir Directories="$(OutputSourceRoot)\Demo\bin;$(OutputSourceRoot)\Demo\obj;$(OutputSourceRoot)\HtmlRenderer\bin;$(OutputSourceRoot)\HtmlRenderer\obj"/>
  </Target>

  <!--Create zip archive for the bins, source and demo-->
  <Target Name="CreateArchive">
    <Exec Command="..\7za.exe a &quot;HtmlRenderer $(Version).zip&quot; **" WorkingDirectory="$(OutputRoot)" />
  </Target>

  <!--Create NuGet package-->
  <Target Name="CreatePackage">
    <Exec Command="nuget.exe pack HtmlRenderer.WinForms.nuspec -Version $(Version) -OutputDirectory $(OutputRoot)" />
  </Target>

</Project>