<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" InitialTargets="GitVersion" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <GitSkipCache>true</GitSkipCache>
  </PropertyGroup>
  <ImportGroup Label="PropertySheets">
    <Import Project="basedir.props" Condition=" '$(BaseDirImported)' == ''"/>
    <Import Project="Setup\GitInfo\GitInfo.targets" />
  </ImportGroup>

  <PropertyGroup>
    <switch_versionPropsImported>true</switch_versionPropsImported>
  </PropertyGroup>

  <UsingTask TaskName="SwitchVersionTask" 
             TaskFactory="CodeTaskFactory"  
             AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
             <ParameterGroup>
                <commits Required="true" />
                <revision Required="true" />
             </ParameterGroup>
             <Task>  
                <Reference Include="Microsoft.Build" />
                <Reference Include="Microsoft.Build.Framework" />
                <Code Type="Class" Language="cs">
<![CDATA[  
using System;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Microsoft.Build.Framework;

    public class SwitchVersionTask : Microsoft.Build.Utilities.Task
    {
        [Required]
        public string revision { get; set; }
        public string commits { get; set; }

        private string basedir;
        private string TemplatePath;
        private string DestinationPath;

        public override bool Execute()
        {
            basedir = Path.GetFullPath(@"$(BaseDir)");

            int commit_count = 0;
            Int32.TryParse(commits, out commit_count);

            Log.LogMessage(MessageImportance.High,
                  "Parsing FreeSWITCH version.");                    

            string ConfigureAC = File.ReadAllText(@"$(BaseDir)configure.ac");                
            string pattern = @"AC_SUBST\((SWITCH_VERSION_[A-Z_]+),.*\[(.*)\]\)";

            Dictionary<string, string> v = new Dictionary<string, string>();

            string year = DateTime.UtcNow.Year.ToString();
            v.Add("SWITCH_VERSION_YEAR", year);
            
            foreach (Match m in Regex.Matches(ConfigureAC, pattern)) {
               string value = m.Groups[2].Value.Trim();
               if (value.Contains("-")) {
                   string[] tokens = value.Split('-');
                   value = tokens[0];
               }        
               var name = m.Groups[1].Value.Trim();
               value = value.Trim();

               if (name.StartsWith("SWITCH_VERSION_REVISION")) {
                   if (string.IsNullOrWhiteSpace(value)) {
                       value = revision;
                   }

                   if (name != "SWITCH_VERSION_REVISION_HUMAN") {
                       value = "~" + value;
                   
                       if (commit_count > 0) {
                           value = "-dev-"+ commits + value;
                       } else {
                           value = "-release" + value;
                       }
                   }
               }
               v.Add(name, value);
               Log.LogMessage(MessageImportance.High, name + " = '" + value + "'");
            }
            
            //---------------------------------------------------------            
            Log.LogMessage(MessageImportance.High,
                "Generating switch_version.h");            
                
            TemplatePath = basedir + @"src\include\switch_version.h.template";
            DestinationPath = basedir + @"src\include\switch_version.h";
            
            string template = File.ReadAllText(TemplatePath);
            foreach (var version_bit in v) {
                template = template.Replace("@" + version_bit.Key + "@", version_bit.Value);
            }            
                       
            File.WriteAllText(DestinationPath, template);            
            
            //---------------------------------------------------------
            Log.LogMessage(MessageImportance.High,
                "Generating switch_version.inc");                    
            
            TemplatePath = @"$(ProjectDir)\switch_version.inc.template";
            DestinationPath = @"$(ProjectDir)\switch_version.inc";            

            template = File.ReadAllText(TemplatePath);
            foreach (var version_bit in v) {
                template = template.Replace("@" + version_bit.Key + "@", version_bit.Value);
            }
                                                           
            File.WriteAllText(DestinationPath, template);
            
            //---------------------------------------------------------

            return true;
        }
    }
]]>  
                </Code>  
             </Task>  
  </UsingTask>  

  <Target Name="SwitchVersionTarget" BeforeTargets="CustomBuild;Build">  
      <SwitchVersionTask commits="$(GitCommits)" revision="$(GitCommit)">
      </SwitchVersionTask>         
  </Target> 

</Project>