param($installPath, $toolsPath, $package, $project)

$source = @"
    public class CBAInstaller
    {
        public static void Install(string htmlPath)
        {
            var strReader = new System.IO.StreamReader(htmlPath);
            var html = strReader.ReadToEnd();
            strReader.Close();

            var lastLinkPos = html.LastIndexOf("<link", html.LastIndexOf("<link"));
            var cssPos = html.IndexOf("\r\n", lastLinkPos);
            var jsPos = html.LastIndexOf("\r\n", html.LastIndexOf("Scripts/Generated/generatedAssets.js"));

            if (cssPos != -1 && jsPos != -1)
            {
                var headPart = html.Substring(0, cssPos);
                var bodyPart = html.Substring(cssPos, jsPos - cssPos);
                var endPart = html.Substring(jsPos);

                var isChanged = false;
                if (headPart.IndexOf("Content/wijmo.min.css") == -1)
                {
                    headPart = headPart + "\r\n\r\n    <!-- Wijmo5 CSS -->";
                    headPart = headPart + "\r\n    <link rel=\"stylesheet\" type=\"text/css\" href=\"Content/wijmo.min.css\" />";
                    headPart = headPart + "\r\n    <link rel=\"stylesheet\" type=\"text/css\" href=\"Content/c1.cba.css\" />";
                    headPart = headPart.Replace("</script>", "    window.__mslsTestMode = true;\r\n    </script>");
                    isChanged = true;
                }

                if (bodyPart.IndexOf("Scripts/wijmo.cba.js") == -1)
                {
                    bodyPart = bodyPart + "\r\n\r\n    <!-- Wijmo5 JS -->";
                    bodyPart = bodyPart + "\r\n    <script type=\"text/javascript\" src=\"Scripts/wijmo.min.js\"></script>";
                    bodyPart = bodyPart + "\r\n    <script type=\"text/javascript\" src=\"Scripts/wijmo.chart.min.js\"></script>";
                    bodyPart = bodyPart + "\r\n    <script type=\"text/javascript\" src=\"Scripts/wijmo.chart.interaction.min.js\"></script>";
                    bodyPart = bodyPart + "\r\n    <script type=\"text/javascript\" src=\"Scripts/wijmo.chart.analytics.min.js\"></script>";
                    bodyPart = bodyPart + "\r\n    <script type=\"text/javascript\" src=\"Scripts/wijmo.gauge.min.js\"></script>";
                    bodyPart = bodyPart + "\r\n    <script type=\"text/javascript\" src=\"Scripts/wijmo.input.min.js\"></script>";
                    bodyPart = bodyPart + "\r\n    <script type=\"text/javascript\" src=\"Scripts/wijmo.grid.min.js\"></script>";
                    bodyPart = bodyPart + "\r\n    <script type=\"text/javascript\" src=\"Scripts/wijmo.grid.filter.min.js\"></script>";
                    bodyPart = bodyPart + "\r\n    <script type=\"text/javascript\" src=\"Scripts/wijmo.cba.js\"></script>\r\n";
                    isChanged = true;
                }

                if (isChanged)
                {
                    System.IO.File.SetAttributes(htmlPath, System.IO.FileAttributes.Normal);
                    var writer = new System.IO.StreamWriter(htmlPath);
                    writer.Write(headPart);
                    writer.Write(bodyPart);
                    writer.Write(endPart);
                    writer.Close();
                }
            }
        }
    }
"@

$default = $project.ProjectItems | Where-Object { $_.Name -eq "default.htm" }
if ($default -ne $null)
{

  if (-not $default.Saved)
  {
    $default.Save($default.Properties.Item("FullPath").Value)
  }

  Add-Type -TypeDefinition $source
  [CBAInstaller]::Install($default.Properties.Item("FullPath").Value)
}
