<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup>
    <OverwriteOnPublish Condition="'$(OverwriteOnPublish)' == ''">false</OverwriteOnPublish>
  </PropertyGroup>

  <!-- gathers the items to be published -->
  <Target Name="GatherItemsForPattern">
    <Error Condition="'$(PublishPattern)' == ''" Text="Please specify a value for PublishPattern using standard msbuild 'include' syntax." />
    <ItemGroup>
      <ForPublishing Include="$(PublishPattern)" />
    </ItemGroup>
    <!-- add relative blob path metadata -->
    <ItemGroup>
      <ForPublishing>
        <RelativeBlobPath>$([System.String]::Copy('%(RecursiveDir)%(Filename)%(Extension)').Replace('\' ,'/'))</RelativeBlobPath>
      </ForPublishing>
    </ItemGroup>
    <Error Condition="'@(ForPublishing)' == ''" Text="No items were found matching pattern '$(PublishPattern)'." />
  </Target>

  <!-- publishes artifacts to blob storage in Azure -->
  <Target Name="UploadArtifactsToAzure" DependsOnTargets="GatherItemsForPattern">
    <Error Condition="'$(ArtifactContainerName)' == ''" Text="Missing property ArtifactContainerName." />
    <Error Condition="'$(ArtifactCloudDropAccountName)' == ''" Text="Missing property ArtifactCloudDropAccountName." />
    <Error Condition="'$(ArtifactCloudDropAccessToken)' == ''" Text="Missing property ArtifactCloudDropAccessToken." />
    <!-- create the container if it doesn't exist -->
    <CreateAzureContainer
      AccountKey="$(ArtifactCloudDropAccessToken)"
      AccountName="$(ArtifactCloudDropAccountName)"
      ContainerName="$(ArtifactContainerName)" />
    <!-- now upload the items -->
    <UploadToAzure
      AccountKey="$(ArtifactCloudDropAccessToken)"
      AccountName="$(ArtifactCloudDropAccountName)"
      ContainerName="$(ArtifactContainerName)"
      Items="@(ForPublishing)"
      Overwrite="$(OverwriteOnPublish)" />
  </Target>

  <!-- publishes checksums for the artifacts to blob storage in Azure -->
  <Target Name="UploadChecksumsToAzure" DependsOnTargets="GenerateChecksums">
    <Error Condition="'$(ChecksumContainerName)' == ''" Text="Missing property ChecksumContainerName." />
    <Error Condition="'$(ChecksumCloudDropAccountName)' == ''" Text="Missing property ChecksumCloudDropAccountName." />
    <Error Condition="'$(ChecksumCloudDropAccessToken)' == ''" Text="Missing property ChecksumCloudDropAccessToken." />
    <!-- create the container if it doesn't exist -->
    <CreateAzureContainer
      AccountKey="$(ChecksumCloudDropAccessToken)"
      AccountName="$(ChecksumCloudDropAccountName)"
      ContainerName="$(ChecksumContainerName)" />
    <!-- now upload the items -->
    <UploadToAzure
      AccountKey="$(ChecksumCloudDropAccessToken)"
      AccountName="$(ChecksumCloudDropAccountName)"
      ContainerName="$(ChecksumContainerName)"
      Items="@(ArtifactsForGeneratingChecksums->'%(DestinationPath)')"
      Overwrite="$(OverwriteOnPublish)" />
  </Target>

</Project>