/*
	Licensed Material - Property of IBM

	(C) Copyright 2015, 2016 IBM Corp.

	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.
*/


'use strict';

var findup = require('findup-sync');
var path = require('path');
var Q = require('q');

module.exports = function () {
    var configXMLPath,
        platformsPath,
        wwwFolderPath,
        d = Q.defer();

    //If not at root, findup to platforms and then hooks to make sure it is a cordova project, then check for config.xml
    platformsPath = findup('platforms');
    
    //To detect DAB ionic projects
    if(!platformsPath) {
      platformsPath = findup('platforms', {cwd: path.join(process.cwd(), 'ionic')});
    }
    if(platformsPath) {
      wwwFolderPath = findup('www', {cwd: platformsPath});
    }
    if(wwwFolderPath) {
        configXMLPath = findup('config.xml', {cwd: wwwFolderPath});
    }

    if (configXMLPath) {
        d.resolve(configXMLPath);
    } else {
        d.reject();
    }

    return d.promise;
};
