#!/usr/bin/env node
'use strict';

var fileStart = Date.now();

var hostname = require('os').hostname;
var process = require('process');
var path = require('path');
var getRepoInfo = require('git-repo-info');
var parseArgs = require('minimist');

var Application = require('./app.js');

module.exports = main;

/*istanbul ignore else: dead branch*/
if (require.main === module) {
    main({
        argv: parseArgs(process.argv.slice(2))
    });
}

function main(opts) {
    var mainStart = Date.now();

    /*eslint no-process-exit: 0*/
    var app = Application();

    // attach before throwing exception
    process.on('uncaughtException', app.clients.uncaught);

    var gitRepo = path.join(__dirname, '.git');
    var gitSha = getRepoInfo(gitRepo).sha;

    app.bootstrap(onAppReady);

    var bootstrapEnd = Date.now();

    function onAppReady(err) {
        /*istanbul ignore if: should never happen in prod*/
        if (err) {
            app.clients.logger.fatal('Could not start', {
                error: err
            });
            return process.exit(1);
        }

        // TODO set process.title

        var now = Date.now();
        app.clients.logger.info('server started', {
            serverAddress: app.clients.rootChannel.address(),
            gitSha: gitSha,
            startupTiming: {
                beforeMain: mainStart - fileStart,
                appTime: bootstrapEnd - mainStart,
                startTime: now - bootstrapEnd,
                totalTime: now - fileStart
            }
        });
    });
}

@@ -1091,33 +1091,234 @@
     
-// TODO set process.title
+var processTitle = opts.argv && opts.argv.processTitle;%0A        if (typeof processTitle === 'string') %7B%0A            process.title = processTitle;%0A        %7D else %7B%0A            process.title = 'my-title-' + hostname();%0A        %7D
 %0A%0A  
@@ -1734,18 +1734,92 @@
     %7D);%0A
+%0A
     
-%7D);
+    app.clients.statsd.timing('server.startup-time', now - mainStart);%0A    %7D
 %0A%7D%0A

add startup time stat