<Project InitialTargets="Build" DefaultTargets="AfterBuild" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <!-- 
  ****************************************************
  INCLUDES 
  *****************************************************
  -->

  <PropertyGroup>
    <MSBuildCommunityTasksPath>..\MSBuildCommunityTasks</MSBuildCommunityTasksPath>
    <UmbracoMSBuildTasksPath>..\UmbracoMSBuildTasks</UmbracoMSBuildTasksPath>
  </PropertyGroup>
  
  <Import Project="..\lib\Umbraco\UmbracoMSBuildTasks\Umbraco.MSBuild.Tasks.Targets" />
  <Import Project="..\lib\MSBuild\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />

  <!-- 
  ****************************************************
  VARIABLES 
  *****************************************************
  -->

  <!-- NB: BUILD_NUMBER is passed in by the build server -->
  <PropertyGroup Condition="'$(BUILD_NUMBER)'!=''">
    <DECIMAL_BUILD_NUMBER>.$(BUILD_NUMBER)</DECIMAL_BUILD_NUMBER>
  </PropertyGroup>
  
  <PropertyGroup>
    <BuildConfiguration>Release</BuildConfiguration>
    <BuildFolder>_BuildOutput\</BuildFolder>
    <BuildZipFileName>UmbracoCms$(DECIMAL_BUILD_NUMBER).zip</BuildZipFileName>
    <BuildZipFileNameBin>UmbracoCms.AllBinaries$(DECIMAL_BUILD_NUMBER).zip</BuildZipFileNameBin>
    <BuildZipFileNameWebPi>UmbracoCms.WebPI$(DECIMAL_BUILD_NUMBER).zip</BuildZipFileNameWebPi>
    <IncludeSymbols>False</IncludeSymbols>
    <BuildFolderRelativeToProjects>..\..\build\$(BuildFolder)</BuildFolderRelativeToProjects>
    <BuildFolderAbsolutePath>$(MSBuildProjectDirectory)\$(BuildFolder)</BuildFolderAbsolutePath>
    <SolutionBinFolder>$(BuildFolder)bin\</SolutionBinFolder>
    <WebAppFolder>$(BuildFolder)WebApp\</WebAppFolder>
    <WebPiFolder>$(BuildFolder)WebPi\</WebPiFolder>
    <SolutionBinFolderRelativeToProjects>$(BuildFolderRelativeToProjects)bin\</SolutionBinFolderRelativeToProjects>
    <SolutionBinFolderAbsolutePath>$(BuildFolderAbsolutePath)bin\</SolutionBinFolderAbsolutePath>
    <SolutionBinBuildFolderWildcard>$(SolutionBinFolder)**\*.*</SolutionBinBuildFolderWildcard>
    <SolutionBinBuildFolderExclusionWildcard>$(SolutionBinFolder)**\Web.Template.*;$(SolutionBinFolder)**\*.pdb</SolutionBinBuildFolderExclusionWildcard>
    <WebAppFolderRelativeToProjects>$(BuildFolderRelativeToProjects)WebApp\</WebAppFolderRelativeToProjects>
    <WebAppFolderAbsolutePath>$(BuildFolderAbsolutePath)WebApp\</WebAppFolderAbsolutePath>
    <WebPiFolderRelativeToProjects>$(BuildFolderRelativeToProjects)WebPi\</WebPiFolderRelativeToProjects>
    <WebPiFolderAbsolutePath>$(BuildFolderAbsolutePath)WebPi\</WebPiFolderAbsolutePath>
    <WebBuildFolderWildcard>$(WebAppFolder)**\*.*</WebBuildFolderWildcard>
    <WebBuildFolderExclusionWildcard>$(WebAppFolder)**\Web.Template.*;$(WebAppFolder)**\*.pdb</WebBuildFolderExclusionWildcard>
  </PropertyGroup>

  <!-- 
  ****************************************************
  TARGETS 
  *****************************************************
  -->

  <Target Name="Build" DependsOnTargets="ZipWebPiApp">
    <Message Text="Build finished" />
  </Target>
  
  <Target Name="CleanUp" DependsOnTargets="ZipWebPiApp">
    <Message Text="Deleting $(BuildFolder)" Importance="high" />
    <RemoveDir Directories="$(BuildFolder)" />
    <Message Text="Finished deleting $(BuildFolder)" Importance="high" />
  </Target>

  <Target Name="ZipWebPiApp" DependsOnTargets="ZipWebApp" >

    <!-- Clean folders -->
    <RemoveDir Directories="$(WebPiFolder)" />
    <MakeDir Directories="$(WebPiFolder)" />
    <MakeDir Directories="$(WebPiFolder)umbraco" />

    <!-- Copy fresh built umbraco files -->
    <ItemGroup>
      <WebAppFiles Include="$(WebAppFolder)\**\*.*" />
    </ItemGroup>

    <Copy SourceFiles="@(WebAppFiles)"
        DestinationFiles="@(WebAppFiles->'$(WebPiFolder)umbraco\%(RecursiveDir)%(Filename)%(Extension)')" />

    <!-- Copy Web Pi template files -->
    <ItemGroup>
      <WebPiFiles Include="..\src\WebPi\**\*.*" />
    </ItemGroup>

    <Copy SourceFiles="@(WebPiFiles)"
        DestinationFiles="@(WebPiFiles->'$(WebPiFolder)%(RecursiveDir)%(Filename)%(Extension)')" />

    <!-- Zip the files -->
    <ItemGroup>
      <WebPiFilesToZip Include="$(WebPiFolder)**\*.*" />
    </ItemGroup>
    
    <MSBuild.Community.Tasks.Zip Files="@(WebPiFilesToZip)" ZipFileName="$(BuildZipFileNameWebPi)" WorkingDirectory="$(WebPiFolder)"  />

  </Target>
  
  <Target Name="ZipWebApp" DependsOnTargets="CopyLibraries" >
    
    <CreateItem Include="$(WebBuildFolderWildcard)" Exclude="$(WebBuildFolderExclusionWildcard)">
      <Output TaskParameter="Include" ItemName="WebFilesToZip" />
    </CreateItem>

    <CreateItem Include="$(SolutionBinBuildFolderWildcard)" Exclude="$(SolutionBinBuildFolderExclusionWildcard)">
      <Output TaskParameter="Include" ItemName="BinFilesToZip" />
    </CreateItem>
    <Message Text="Starting to zip to $(buildDate)-$(BuildZipFileName)" Importance="high" />
    <MSBuild.Community.Tasks.Zip Files="@(BinFilesToZip)" ZipFileName="$(BuildZipFileNameBin)" WorkingDirectory="$(SolutionBinFolder)" />
    <MSBuild.Community.Tasks.Zip Files="@(WebFilesToZip)" ZipFileName="$(BuildZipFileName)" WorkingDirectory="$(WebAppFolder)" />
    <Message Text="Finished zipping to build\$(BuildFolder)\$(buildDate)-$(BuildZipFileName)" Importance="high" />
  </Target>

  <Target Name="CopyLibraries" DependsOnTargets="OffsetTimestamps" >

    <!-- Copy SQL CE -->
    <ItemGroup>
      <SQLCE4Files Include="..\lib\SQLCE4\**\*.*" />
    </ItemGroup>

    <Copy SourceFiles="@(SQLCE4Files)"
        DestinationFiles="@(SQLCE4Files->'$(SolutionBinFolder)%(RecursiveDir)%(Filename)%(Extension)')"
        OverwriteReadOnlyFiles="true"
        SkipUnchangedFiles="false" />
    <Copy SourceFiles="@(SQLCE4Files)"
        DestinationFiles="@(SQLCE4Files->'$(WebAppFolder)bin\%(RecursiveDir)%(Filename)%(Extension)')"
        OverwriteReadOnlyFiles="true"
        SkipUnchangedFiles="false" />

    <!-- Copy WebPages -->
    <ItemGroup>
      <WebPageFiles Include="..\lib\WebPages\*.dll" />
    </ItemGroup>

    <Copy SourceFiles="@(WebPageFiles)"
        DestinationFiles="@(WebPageFiles->'$(SolutionBinFolder)%(RecursiveDir)%(Filename)%(Extension)')"
        OverwriteReadOnlyFiles="true"
        SkipUnchangedFiles="false" />
    <Copy SourceFiles="@(WebPageFiles)"
        DestinationFiles="@(WebPageFiles->'$(WebAppFolder)bin\%(RecursiveDir)%(Filename)%(Extension)')"
        OverwriteReadOnlyFiles="true"
        SkipUnchangedFiles="false" />
    
  </Target>

  <!-- Offset the modified timestamps on all umbraco dlls, as WebResources break if date is in the future, which, due to timezone offsets can happen. -->
  <Target Name="OffsetTimestamps" DependsOnTargets="CompileProjects">
    <CreateItem Include="$(BuildFolder)**\umbraco.*.dll">
      <Output TaskParameter="Include" ItemName="FilesToOffsetTimestamp" />
    </CreateItem>
    <Message Text="Starting to offset timestamps" Importance="high" />
    <Umbraco.MSBuild.Tasks.TimestampOffset Files="@(FilesToOffsetTimestamp)" Offset="-11" />
    <Message Text="Finished offsetting timestamps" Importance="high" />
  </Target>
  
  <Target Name="CompileProjects">

    <Message Text="Compiling web project to build\$(BuildFolder)" Importance="high" />
    <!-- For UseWPP_CopyWebApplication=True see http://stackoverflow.com/questions/1983575/copywebapplication-with-web-config-transformations -->
    <MSBuild Projects="..\src\umbraco.presentation\umbraco.presentation.csproj" Properties="WarningLevel=0;Configuration=$(BuildConfiguration);UseWPP_CopyWebApplication=True;PipelineDependsOnBuild=False;OutDir=$(SolutionBinFolderAbsolutePath);WebProjectOutputDir=$(WebAppFolderAbsolutePath)" Targets="Clean;Build;" BuildInParallel="False" ToolsVersion="4.0" UnloadProjectsOnCompletion="False">
    </MSBuild>

    <!--     
    Now, because of all the circular dependencies we need to build those projects and have them dump their DLLs/assets out to the same folder. Really wish this wasn't the case! 
    These are:    umbraco.editorControls    umbraco.webservices    umbraco.MacroEngines
    
    Each of these projects has a post build to put their stuff in the correct location based on the solution and unfortunately for us each 
    of these projects references the web application! So if we specify an OutDir parameter it will also trigger a web deploy and chuck another
    website output in bin/_PublishedWebSites folder. we cannot disable this: http://social.msdn.microsoft.com/forums/en-US/tfsbuild/thread/3ec642ad-2e6d-424c-891a-62f6409da62a/
    so we need to just build the project 'in-place' then copy its output to the SolutionBinFolder    
    -->

    <!-- MACRO ENGINES -->
    <Message Text="Compiling MacroEngines project" Importance="high" />
    <MSBuild Projects="..\src\umbraco.MacroEngines\umbraco.MacroEngines.csproj" Properties="WarningLevel=0;Configuration=$(BuildConfiguration);" Targets="Clean;Build;" BuildInParallel="False" ToolsVersion="4.0" UnloadProjectsOnCompletion="False">
    </MSBuild>
    <ItemGroup>
      <MacroEngineDll Include="..\src\umbraco.MacroEngines\bin\$(BuildConfiguration)\umbraco.MacroEngines.dll"/>
    </ItemGroup>
    <Copy SourceFiles="@(MacroEngineDll)"
          DestinationFolder="$(SolutionBinFolder)" OverwriteReadOnlyFiles="True"/>
    <Copy SourceFiles="@(MacroEngineDll)"
            DestinationFolder="$(WebAppFolder)\bin" OverwriteReadOnlyFiles="True"/>

    <!-- EDITOR CONTROLS -->
    <Message Text="Compiling editorControls project" Importance="high" />
    <MSBuild Projects="..\src\umbraco.editorControls\umbraco.editorControls.csproj" Properties="WarningLevel=0;Configuration=$(BuildConfiguration);" Targets="Clean;Build;" BuildInParallel="False" ToolsVersion="4.0" UnloadProjectsOnCompletion="False">
    </MSBuild>
    <ItemGroup>
      <EditorControlsDll Include="..\src\umbraco.editorControls\bin\$(BuildConfiguration)\umbraco.editorControls.dll"/>
    </ItemGroup>
    <Copy SourceFiles="@(EditorControlsDll)"
          DestinationFolder="$(SolutionBinFolder)" OverwriteReadOnlyFiles="True"/>
    <Copy SourceFiles="@(EditorControlsDll)"
            DestinationFolder="$(WebAppFolder)\bin" OverwriteReadOnlyFiles="True"/>

    <!-- WEBSERVICES -->
    <Message Text="Compiling webservices project" Importance="high" />
    <MSBuild Projects="..\src\umbraco.webservices\umbraco.webservices.csproj" Properties="WarningLevel=0;Configuration=$(BuildConfiguration);" Targets="Clean;Build;" BuildInParallel="False" ToolsVersion="4.0" UnloadProjectsOnCompletion="False">
    </MSBuild>
    <ItemGroup>
      <WebServicesDll Include="..\src\umbraco.webservices\bin\$(BuildConfiguration)\umbraco.webservices.dll"/>
      <WebServicesASMX Include="..\src\umbraco.webservices\*.asmx"/>
    </ItemGroup>
    <Copy SourceFiles="@(WebServicesDll)"
          DestinationFolder="$(SolutionBinFolder)" OverwriteReadOnlyFiles="True"/>
    <Copy SourceFiles="@(WebServicesDll)"
          DestinationFolder="$(WebAppFolder)\bin" OverwriteReadOnlyFiles="True"/>
    <Copy SourceFiles="@(WebServicesASMX)"
            DestinationFolder="$(WebAppFolder)\umbraco\webservices\api" OverwriteReadOnlyFiles="True"/>

    <!-- SQLCE4UMBRACO -->
    <Message Text="Compiling SQLCE4Umbraco project" Importance="high" />
    <MSBuild Projects="..\src\SQLCE4Umbraco\SQLCE4Umbraco.csproj" Properties="WarningLevel=0;Configuration=$(BuildConfiguration);" Targets="Clean;Build;" BuildInParallel="False" ToolsVersion="4.0" UnloadProjectsOnCompletion="False">
    </MSBuild>
    <ItemGroup>
      <SQLCE4UmbracoDll Include="..\src\SQLCE4Umbraco\bin\$(BuildConfiguration)\SQLCE4Umbraco.dll" />
    </ItemGroup>
    <Copy SourceFiles="@(SQLCE4UmbracoDll)"
          DestinationFolder="$(SolutionBinFolder)" OverwriteReadOnlyFiles="True"/>
    <Copy SourceFiles="@(SQLCE4UmbracoDll)"
          DestinationFolder="$(WebAppFolder)\bin" OverwriteReadOnlyFiles="True"/>

    <!-- DONE -->
    <Message Text="Finished compiling projects" Importance="high" />
  </Target>
  
  <Target Name="AfterBuild">
    <Message Text="Hi" />
  </Target>
  
</Project>