/*
 *  doGet.js : rajah mainfile for GAS environment.
 */

require('global');

function doGet(e) {

    var config, calledByHttp, error,
        timers = require('./timers');

    if (typeof e !== 'undefined' && typeof e.queryString !== 'undefined') {
        // execute by Web Access.
        calledByHttp = true;

        config = {
            specs:          e.parameters || null,
            match:          e.parameter.match || null,
            showColor:      (typeof e.parameter.color !== 'undefined') ||
                            (typeof e.parameter.noColor === 'undefined')
        };

        if (config.reportType) {
            this.config.reportType = config.reportType[0];
        }

    } else {
        // execute by debugger or time trigger.
        calledByHttp = false;
        config = null;
    }

    var rajahApp = require('./rajahApp.js').create();

    if (typeof rajahConfig === 'object') {
        error = rajahApp.addConfig(rajahConfig);
        _check(error);
    }

    if (config) {
        error = rajahApp.addConfig(config);
        _check(error);
    }

    if (calledByHttp) {
        rajahApp.addConfig({
            showColor:  false,
            reportType: 'onMemory'
        });
    }

    error = rajahApp.run();
    _check(error);

    timers.execute();

    if (calledByHttp) {
        var output = ContentService.createTextOutput(rajahApp.getReport())
                     .setMimeType(ContentService.MimeType.TEXT);
        return output;
    }

    return undefined;
}

function _check(error) {
    if (error !== null) {
        if (error.substr(0, 6) === 'Error:') {
            console.error(error);
            console.log('');
            process.exit(1);    // exit not exists. this code will throw.
        } else {
            console.log(error);
        }
    }
}

@@ -122,16 +122,28 @@
 dByHttp,
+ consoleOut,
  error,%0A
@@ -320,16 +320,43 @@
 = true;%0A
+        consoleOut = false;
 %0A       
@@ -639,105 +639,163 @@
     
-    if (config.reportType) %7B%0A            this.config.reportType = config.reportType%5B0%5D;%0A        %7D
+%7D else if (typeof e !== 'undefined') %7B%0A        // execute by some trigger.%0A        calledByHttp = false;%0A        consoleOut = false;%0A        config = null;
 %0A%0A  
@@ -839,24 +839,8 @@
 gger
- or time trigger
 .%0A  
@@ -859,32 +859,59 @@
 ByHttp = false;%0A
+        consoleOut = true;%0A
         config =
@@ -1197,36 +1197,37 @@
  %7D%0A%0A    if (
-calledByHttp
+ ! consoleOut
 ) %7B%0A        
@@ -1328,24 +1328,285 @@
  %7D);%0A    %7D%0A%0A
+    if ((typeof rajahConfig === 'object') &&%0A        (typeof rajahConfig.onComplete === 'function')) %7B%0A        // add reporter to set callback 'onComplete'.%0A        rajahApp.rajah.addReporter(%7B%0A            jasmineDone: rajahConfig.onComplete%0A        %7D);%0A    %7D%0A%0A
     error = 

add case called by other trigger.