/**
 * Npm module for Unicode CLDR JSON data
 *
 * Copyright 2013 Rafael Xavier de Souza
 * Released under the MIT license
 * https://github.com/rxaviers/cldr-data-npm/blob/master/LICENSE-MIT
 */

"use strict";

var coverage, parentPackage, peerPackages, srcUrl;

var cldrDownloader = require("cldr-data-downloader");
var glob = require("glob").sync;
var path = require("path");
var child_process = require('child_process');

var options = {};

var isNpm3;
try {
  var npmv = child_process.execSync('npm -v').toString('utf8');
  isNpm3 = (npmv.split('.')[0] == '3');
} catch(error) {
  // child_process.execSync is not available on Node v0.10
  // fortunately, we can use ENV variables set by npm do detect its version
  //   npm_config_user_agent: 'npm/2.15.1 node/v0.10.46 darwin x64'
  //   npm_config_user_agent: 'npm/3.10.3 node/v6.3.0 darwin x64'
  // Note that users can override the value of this config option,
  // therefore it's safer to use this method only as a fall-back option.
  if (/^npm\/2\./.test(process.env.npm_config_user_agent)) {
    isNpm3 = false;
  } else {
    // Better safe than sorry.
    isNpm3 = true;
  }
}

try {
  parentPackage = require("../../package.json");
} catch(error) {}

try {
  peerPackages = glob("../*/package.json").map(function(file) {
    try {
      return require(path.resolve(file));
    } catch(error) {
      return {};
    }
  });
} catch(error) {
  console.error(
    "Warning: Something weird happened checking whether this is a " +
    "peer dependency.", error.message
  );
  peerPackages = [];
}

if (!isNpm3 && parentPackage &&
      !(parentPackage.dependencies && parentPackage.dependencies["cldr-data"]) &&
      !(parentPackage.devDependencies && parentPackage.devDependencies["cldr-data"]) &&
      peerPackages.some(function(peerPackage) {
        return peerPackage.peerDependencies &&
          peerPackage.peerDependencies["cldr-data"];
      })) {
  console.error(
    "Warning: Skipping to download CLDR data, because `cldr-data` is a " +
    "peer dependency. If you want it to be downloaded, make sure it's " +
    "listed under `dependencies` or `devDependencies` of the `package.json`" +
    "of your application."
  );
  return process.exit(0);
}

if (process.env.CLDR_URL) {
  console.warn("CLDR_URL is deprecated, use CLDR_DATA_URLS_JSON instead.")
  srcUrl = srcUrl.replace(
    "http://www.unicode.org/Public/cldr",
    process.env.CLDR_URL.replace(/\/$/, "")
  );

} else {

  if (process.env.CLDR_DATA_URLS_JSON) {
    srcUrl = process.env.CLDR_DATA_URLS_JSON;
  } else if (parentPackage && parentPackage["cldr-data-urls-json"]) {
    srcUrl = parentPackage["cldr-data-urls-json"];
  } else {
    srcUrl = path.join(__dirname, "./urls.json");
  }

  if (process.env.CLDR_COVERAGE) {
    coverage = process.env.CLDR_COVERAGE;
  } else if (parentPackage && parentPackage["cldr-data-coverage"] && (
        (parentPackage.dependencies && parentPackage.dependencies["cldr-data"]) ||
        (parentPackage.devDependencies && parentPackage.devDependencies["cldr-data"])
        )) {
    coverage = parentPackage["cldr-data-coverage"];
  }

  if (parentPackage && parentPackage["cldr-data-urls-filter"]) {
    options.filterRe = parentPackage["cldr-data-urls-filter"];
  }

  if (coverage) {
    options.srcUrlKey = coverage;
  }
}

cldrDownloader(
  srcUrl,
  __dirname,
  options,
  function(error) {
    if (error) {
      if (/E_ALREADY_INSTALLED/.test(error.code)) {
        error.message = error.message.replace(/Use `options.*/, "Use -f to " +
          "override.");
        return console.log(error.message);
      } else {
        console.error("Whops", error.message);
        process.exit(1);
      }
    }
  }
);

@@ -2451,17 +2451,16 @@
  else %7B%0A
-%0A
   if (pr
@@ -2720,27 +2720,27 @@
 json%22);%0A  %7D%0A
-%0A  
+%7D%0A%0A
 if (process.
@@ -2756,26 +2756,24 @@
 COVERAGE) %7B%0A
-  
   coverage =
@@ -2800,18 +2800,16 @@
 VERAGE;%0A
-  
 %7D else i
@@ -2871,26 +2871,24 @@
  && (%0A      
-  
 (parentPacka
@@ -2950,18 +2950,16 @@
 a%22%5D) %7C%7C%0A
-  
       (p
@@ -3044,17 +3044,13 @@
     
-  
 )) %7B%0A
-  
   co
@@ -3091,31 +3091,27 @@
 coverage%22%5D;%0A
-  
 %7D%0A%0A
-  
 if (parentPa
@@ -3157,26 +3157,24 @@
 filter%22%5D) %7B%0A
-  
   options.fi
@@ -3226,15 +3226,11 @@
 %22%5D;%0A
-  
 %7D%0A%0A
-  
 if (
@@ -3241,18 +3241,16 @@
 rage) %7B%0A
-  
   option
@@ -3273,20 +3273,16 @@
 verage;%0A
-  %7D%0A
 %7D%0A%0AcldrD

Make CLDR_COVERAGE and CLDR-DATA-URLS-FILTER independent from CLDR_URL