<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Default"
         xmlns='http://schemas.microsoft.com/developer/msbuild/2003' ToolsVersion="4.0">
  <Import Project="$(MSBuildProjectDirectory)/build.tasks" />

  <PropertyGroup>
    <MajorVersion Condition="$(MajorVersion) == ''">6</MajorVersion>
    <MinorVersion Condition="$(MinorVersion) == ''">0</MinorVersion>
    <PatchVersion Condition="$(PatchVersion) == ''">$(BUILD_NUMBER)</PatchVersion>
  </PropertyGroup>

  <PropertyGroup>
    <BuildSolutionDir>$(MSBuildProjectDirectory)/..</BuildSolutionDir>
    <SrcDir>$(BuildSolutionDir)/src</SrcDir>
    <TestsDir>$(BuildSolutionDir)/tests</TestsDir>
    <Configuration Condition="$(Configuration) == ''">Release</Configuration>
    <PackageVersion>$(MajorVersion).$(MinorVersion).$(PatchVersion)</PackageVersion>
  </PropertyGroup>

  <PropertyGroup>
    <DoBuildSolutionsDependsOn>
      BeforeBuildSolutions;
      BuildSolutions
    </DoBuildSolutionsDependsOn>
  </PropertyGroup>

  <Target Name="BeforeBuildSolutions">
    <Message Text="*****Before building solution*****" Importance="high"/>
    <CallTarget Targets="UpdateVersion" />
  </Target>

  <Target Name="BuildSolutions">
    <ItemGroup>
      <NugetPackageFilesToDelete Include="$(NuGetPackageDir)/*.nupkg"/>
    </ItemGroup>
    <Delete Files="@(NugetPackageFilesToDelete)" />
    <CallTarget Targets="ClearNuGetRestoreCaches" />
  </Target>

  <!-- need to remove old caches from nuget restore from .Core csproj's -->
  <Target Name="ClearNuGetRestoreCaches">
    <RemoveDir Directories="$(SrcDir)/ServiceStack.OrmLite/obj" />
    <RemoveDir Directories="$(SrcDir)/ServiceStack.OrmLite.Firebird/obj" />
    <RemoveDir Directories="$(SrcDir)/ServiceStack.OrmLite.MySql/obj" />
    <RemoveDir Directories="$(SrcDir)/ServiceStack.OrmLite.MySqlConnector/obj" />
    <RemoveDir Directories="$(SrcDir)/ServiceStack.OrmLite.PostgreSQL/obj" />
    <RemoveDir Directories="$(SrcDir)/ServiceStack.OrmLite.Sqlite/obj" />
    <RemoveDir Directories="$(SrcDir)/ServiceStack.OrmLite.Sqlite.Data/obj" />
    <RemoveDir Directories="$(SrcDir)/ServiceStack.OrmLite.Sqlite.Cil/obj" />
    <RemoveDir Directories="$(SrcDir)/ServiceStack.OrmLite.SqlServer/obj" />
    <RemoveDir Directories="$(SrcDir)/ServiceStack.OrmLite.SqlServer.Data/obj" />
    <RemoveDir Directories="$(SrcDir)/ServiceStack.OrmLite.SqlServer.Converters/obj" />
    <RemoveDir Directories="$(SrcDir)/ServiceStack.OrmLite.Oracle/obj" />
    <RemoveDir Directories="$(TestsDir)/ServiceStack.OrmLite.Tests/obj" />
    <RemoveDir Directories="$(TestsDir)/ServiceStack.OrmLite.SqliteTests/obj" />
    <RemoveDir Directories="$(TestsDir)/ServiceStack.OrmLite.Tests.Setup/obj" />
  </Target>

  <Target Name="UpdateVersion">
    <!-- Update Version -->
    <ItemGroup>
      <RegexTransform Include="$(BuildSolutionDir)/src/Directory.Build.props;$(BuildSolutionDir)/tests/Directory.Build.props">
        <Find>&lt;Version&gt;[^&lt;]*</Find>
        <ReplaceWith>&lt;Version&gt;$(PackageVersion)</ReplaceWith>
      </RegexTransform>
    </ItemGroup>

    <RegexTransform Items="@(RegexTransform)" />
  </Target>

  <Target Name="Default" DependsOnTargets="$(DoBuildSolutionsDependsOn)">
    
    <!-- ServiceStack.OrmLite.Core -->
    <MSBuild Projects="$(BuildSolutionDir)/src/ServiceStack.OrmLite/ServiceStack.OrmLite.Core.csproj" Targets="Restore" />
    <MSBuild Projects="$(BuildSolutionDir)/src/ServiceStack.OrmLite/ServiceStack.OrmLite.Core.csproj"
             Targets="Build;Pack"
             Properties="Configuration=$(Configuration)" />

    <!-- ServiceStack.OrmLite.Firebird.Core -->
    <MSBuild Projects="$(BuildSolutionDir)/src/ServiceStack.OrmLite.Firebird/ServiceStack.OrmLite.Firebird.Core.csproj" Targets="Restore" />
    <MSBuild Projects="$(BuildSolutionDir)/src/ServiceStack.OrmLite.Firebird/ServiceStack.OrmLite.Firebird.Core.csproj"
             Targets="Build;Pack"
             Properties="Configuration=$(Configuration)" />

    <!-- ServiceStack.OrmLite.MySql.Core -->
    <MSBuild Projects="$(BuildSolutionDir)/src/ServiceStack.OrmLite.MySql/ServiceStack.OrmLite.MySql.Core.csproj" Targets="Restore" />
    <MSBuild Projects="$(BuildSolutionDir)/src/ServiceStack.OrmLite.MySql/ServiceStack.OrmLite.MySql.Core.csproj"
             Targets="Build;Pack"
             Properties="Configuration=$(Configuration)" />

    <!-- ServiceStack.OrmLite.MySqlConnector.Core -->
    <MSBuild Projects="$(BuildSolutionDir)/src/ServiceStack.OrmLite.MySqlConnector/ServiceStack.OrmLite.MySqlConnector.Core.csproj" Targets="Restore" />
    <MSBuild Projects="$(BuildSolutionDir)/src/ServiceStack.OrmLite.MySqlConnector/ServiceStack.OrmLite.MySqlConnector.Core.csproj"
             Targets="Build;Pack"
             Properties="Configuration=$(Configuration)" />

    <!-- ServiceStack.OrmLite.PostgreSQL.Core -->
    <MSBuild Projects="$(BuildSolutionDir)/src/ServiceStack.OrmLite.PostgreSQL/ServiceStack.OrmLite.PostgreSQL.Core.csproj" Targets="Restore" />
    <MSBuild Projects="$(BuildSolutionDir)/src/ServiceStack.OrmLite.PostgreSQL/ServiceStack.OrmLite.PostgreSQL.Core.csproj"
             Targets="Build;Pack"
             Properties="Configuration=$(Configuration)" />

    <!-- ServiceStack.OrmLite.Sqlite.Core -->
    <MSBuild Projects="$(BuildSolutionDir)/src/ServiceStack.OrmLite.Sqlite/ServiceStack.OrmLite.Sqlite.Core.csproj" Targets="Restore" />
    <MSBuild Projects="$(BuildSolutionDir)/src/ServiceStack.OrmLite.Sqlite/ServiceStack.OrmLite.Sqlite.Core.csproj"
             Targets="Build;Pack"
             Properties="Configuration=$(Configuration)" />

    <!-- ServiceStack.OrmLite.SqlServer.Core -->
    <MSBuild Projects="$(BuildSolutionDir)/src/ServiceStack.OrmLite.SqlServer/ServiceStack.OrmLite.SqlServer.Core.csproj" Targets="Restore" />
    <MSBuild Projects="$(BuildSolutionDir)/src/ServiceStack.OrmLite.SqlServer/ServiceStack.OrmLite.SqlServer.Core.csproj"
             Targets="Build;Pack"
             Properties="Configuration=$(Configuration)" />

    <!-- leave .sln in optimal state then NuGet restore .sln to build -->
    <CallTarget Targets="ClearNuGetRestoreCaches" />

  </Target>
</Project>