var is = require('annois');
var utils = require('./utils');


module.exports = parse;

function parse(env, template, config) {
    return parseEnv(env, template, config);
}

function parseEnv(env, template, config, parent, prefix) {
    var k, v, envVar;

    if(!parent) parent = {};

    for(k in template) {
        v = template[k];

        envVar = prefix? prefix + '_': '';
        envVar += utils.toEnvVar(k);

        if(is.object(v)) {
            parent[k] = {};
            parseEnv(env, v, v, parent[k], envVar);
        }
        else {
            if(envVar in env) {
                parent[k] = env[envVar];
            }
            else if(k in config) {
                parent[k] = config[k];
            }
            else {
                parent[k] = undefined;

                console.warn('Failed to find value for ' + envVar);
            }
        }
    }

    return parent;
}

@@ -1,12 +1,48 @@
+var annotate = require('annotate');%0A
 var is = req
@@ -108,21 +108,144 @@
 ports = 
-parse
+annotate('parse-env', 'Parses configuration from env')%0A    .on(is.object, is.object, is.object, parse)%0A    .satisfies(is.object)
 ;%0A%0Afunct

Annotate parser