/*
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.
*/


var args = WScript.Arguments;
var wscript_shell = WScript.CreateObject("WScript.Shell");

var REQUIRE_GIT = false;

function Usage() {
    Log("Usage: [ check_reqs | cscript check_reqs.js ]");
    Log("examples:");
    Log("    cscript C:\\Users\\anonymous\\cordova-wp8\\bin\\check_reqs.js");
    Log("    CordovaWindowsPhone\\bin\\check_reqs");

}

// log to stdout or stderr
function Log(msg, error) {
    if (error) {
        WScript.StdErr.WriteLine(msg);
    }
    else {
        WScript.StdOut.WriteLine(msg);
    }
}

// gets the output from a command, failing with the given error message
function check_command(cmd, fail_msg) {
    Log("CMD:"+cmd);
    var out = wscript_shell.Exec(cmd);
    while (out.Status == 0) {
        WScript.Sleep(100);
    }

    //Check that command executed
    if (!out.StdErr.AtEndOfStream) {
        var line = out.StdErr.ReadLine();
        Log(fail_msg, true);
        Log('Output : ' + line, true);
        WScript.Quit(1);
    }

    if (!out.StdOut.AtEndOfStream) {
        var line = out.StdOut.ReadAll();
        return line;
    }
    else {
         Log('Unable to get output from command "' + cmd + '"', true);
         WScript.Quit(1);
    }
}

/* The tooling for cordova windows requires these commands
 *  in the environment PATH variable.
 * - msbuild (C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319)
 * - git? (for dynamic cli loading of projects?)
 * TODO: Check for SDK and whatnot.
 */
function CheckSystem() {
    var cmd = 'msbuild -version'
    var fail_msg = 'The command `msbuild` failed. Make sure you have the latest Windows Phone SDKs installed, and the `msbuild.exe` command (inside C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319) is added to your path.'
    var output = check_command(cmd, fail_msg);
    var msversion = output.match(/Microsoft\s\(R\)\s+Build\sEngine\s[a-z]+\s4\.0/i);
    if (!msversion) {
        Log('Please install the .NET Framwork v4.0.30319 (in the latest windows phone SDK\'s).', true);
        Log('Make sure the "msbuild" command in your path is pointing to  v4.0.30319 of msbuild as well (inside C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319).', true);
        WScript.Quit(1);
    }

    if(REQUIRE_GIT) {
        cmd = 'git --version';
        fail_msg = 'The command `git` failed. Make sure you have git installed as well ad in your PATH environment so the tool can use it';
        output = check_command(cmd, fail_msg);
        var gitVersion = output.match(/git\sversion\s1\./);
        if (!gitVersion) {
            Log('Please ensure you have at least git v1 installed and added to you PATH so this tool can use it to get the latest codova.');
        }
    }
}


if (args.Count() > 0) {
    Usage();
    WScript.Quit(1);
}
else
{
    CheckSystem();
}


@@ -2448,21 +2448,17 @@
 Windows 
-Phone
+8
  SDKs in
@@ -2692,16 +2692,18 @@
 %5CsEngine
+,?
 %5Cs%5Ba-z%5D+
@@ -2819,13 +2819,9 @@
 ows 
-phone
+8
  SDK

Fix issue using VS 2013 version of MSBuild