### codetube
    Copyright (C) 2011 payload payload@lavabit.com
    Copyright (C) 2011 dodo dodo.the.last@gmail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
###

require('colors')
fs = require('fs')
path = require('path')
async = require('async')
config = require('jsconfig')
gettext = require('gettext')
formatdate = require('formatdate')
appconfig = require('./appconfig')
sha1 = require('./helper').hash('sha1')

# current working directory, where config and repositories should exists
config.cwd = cwd = path.join(__dirname, "..", "..")
configjs = path.join(cwd, "config.js")
config.defaults(path.join(cwd, "config.default.js"))


formatdate.options.max.unit = 6
formatdate.options.max.amount = 0


config.check = (callback) ->

    config.set('ignore unknown', yes)
    config.set 'env',
        HOST: 'host'
        PORT: ['port', parseInt]

    # load cli
    config.cli
        host:    ['host', ['b', "server listen address", 'host'  ]]
        port:    ['port', ['p', "server listen port"   , 'number']]
        profile: ['debug.profiler', [off, "enable profiling" ]]
        debug:   ['debug.enable'  , [off, "enable debug mode"]]
        debugger:[off, "[INTERNAL] tell server node is running in debug mode"]
        nobuild: [off, "[INTERNAL] disable build"]

    config.load configjs, (args, opts) ->
        config.cli = {args, opts}

        appconfig.user_blacklist = appconfig.user_blacklist.concat(
            config.user_blacklist or [])
        for own name, salt of config.salt
            config.salt[name] = sha1.hash(salt)

        tasks = []

        # load https

        if config.https
            tasks.push (done) ->
                fs.readFile path.join(cwd, config.https.cert), (err, data) ->
                    config.https.cert = data
                    done(err)
            tasks.push (done) ->
                fs.readFile path.join(cwd, config.https.key), (err, data) ->
                    config.https.key = data
                    done(err)

        # load localization

        gettext._ = (args...) -> gettext.gettext.apply(gettext, args)
        gettext._s = (args...) -> gettext.strargs.apply(gettext, args)
        gettext._t = (funname, args...) -> gettext[funname].apply(gettext, args)

        gettext.setlocale('LC_ALL', config.localization['default'])
        tasks.push (done) ->
            gettext.loadLocaleDirectory(path.join(config.cwd, "locale"), done)

        # load authors file
        tasks.push (done) ->
            fs.readFile path.join(cwd, "AUTHORS"), 'utf8', (err, data) ->
                config.AUTHORS = data.replace /@/g, ' (a) '
                done(err)

        # start tasks

        async.parallel tasks, (err) ->
            if err
                console.log("ERROR:".red, err)
                process.exit(5)
            callback(config)

        console.log("debug mode".yellow.bold) if config.debug.enable
        delete config.check
        config

# exports

module.exports = config

