<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Jasmine Spec Runner</title>

  <% css.forEach(function(style){ %>
  <link rel="stylesheet" type="text/css" href="<%= style %>">
  <% }) %>

  <% with (scripts) { %>
  <% [].concat(vendor).forEach(function(script){ %>
  <script src="<%= script %>"></script>
  <% }) %>
  <% }; %>

  <script src="<%= temp ? temp + "/" : temp %>require.js"></script>

  <% with (scripts) { %>
  <% /* LOGLEVEL-FORK: Include new script types used by grunt-contrib-jasmine v4 */ %>
  <% [].concat(polyfills, jasmine, boot, boot2, helpers).forEach(function(script){ %>
  <script src="<%= script %>"></script>
  <% }) %>
  <% /* END LOGLEVEL-FORK */ %>
  <% }; %>

  <script>
    require.onError = function(error) {
      var message = error.requireType + ': ';
      if (error.requireType === 'scripterror' || error.requireType === 'notloaded' && error.requireModules) {
        message += 'Illegal path or script error: ' + '[\'' + error.requireModules.join("', '") + '\']';
      } else {
        message += error.message;
      }

      throw Error(message);
    };
  </script>

  <script>
    <% if (options.mainRequireConfig) { %>
      require.config(<%= serializeRequireConfig(options.mainRequireConfig) %>);
    <% } %>

    <% var hasCallback = false;
       if (options.requireConfig) {
        if ('callback' in options.requireConfig) {
            /* Inserting launchTest() as the last statement in callback function
               to make sure spec is called after all initialization stuffs */

            // capture the arguments and body fo callback function
            options.requireConfig['callback'].toString().replace(/^function\s+\w*\(([^\)]*)\)\s*\{((?:.|[\n\r])*)}/m, function(match, args, body) {
                var being_recreated_function = [];
                if (args.length > 0) {
                    args = args.split(/,\s*/);
                    being_recreated_function = being_recreated_function.concat(args);
                }
                // append launchTest() to the end of function body
                body += '\nlaunchTest();';

                being_recreated_function = being_recreated_function.concat(body);

                // recreate an anonymouse function with modified body
                // re-assign callback property by just created new function
                // (return value dosen't matter)
                options.requireConfig['callback'] = Function.apply(this, being_recreated_function);
                hasCallback = true;
            });
        }
    %>
      require.config(<%= serializeRequireConfig(options.requireConfig) %>);
    <% } %>
  </script>

  <script>
    // Delay onLoad function until we are ready
    var startTests = window.onload;
    window.onload = null;

    function launchTest() {
    require([
      <% scripts.src.forEach(function(script, i){ %>
        <% script = script.replace(/\\/g, '/') // replace all backward slashes to forward slash %>
        '<%= script %>' <%= i < scripts.src.length-1 ? ',' : '' %>
      <% }) %>
    ],
    function(){
      require(['<%= [].concat(scripts.specs,scripts.reporters).join("','") %>'], function(){
        startTests();
      });
    }
    )
  }
  </script>

  <% if (!hasCallback) { %>
  <script>
  launchTest();
  </script>
  <% } %>
</head>
<body>
</body>
</html>
