<commit_before>'use strict';

module.exports = function () {
	var bodyParser = require('body-parser');
	var exec = require('child_process').exec;
	var express = require('express');
	var fs = require('fs');
	var path = require('path');
	var q = require('q');
	var Transifex = require('transifex');
	
	var cloned, cloneError, lastAttempt;
	var cloneDir = __dirname + '/clone';
	
	var app = express();
	app.use(bodyParser.json());
	app.use('/media', express.static(__dirname + '/media'));
	app.set('views', path.join(__dirname, '/views'));
	app.set('view engine', 'ejs');
	app.enable('trust proxy');
	
	// Use to clear the current cloned repo locally
	var clear = function () {
		var defer = q.defer();
		
		fs.exists(cloneDir, function (exists) {
			if (exists) {
				exec('rm -r ' + cloneDir, function (err) {
					err ? defer.reject(err) : defer.resolve();
				});
			}
			else {
				defer.resolve();
			}
		});
		
		return defer.promise;
	};
	
	/*
	 * This will checkout the repo to the "clone" directory. It uses the
	 * credentials provided in the ENV. First we need to make sure there is no
	 * left over clone, we want to start fresh whenever the server is started.
	 */
	var checkout = function () {
		cloneError = false;
		cloned = false;
		
		return clear()
			.then(function () {
				var defer = q.defer();
				
				if (process.env.GIT_REPO_URL) {
					exec('git clone ' + process.env.GIT_REPO_URL + ' ' + cloneDir, function (err) {
						err ? defer.reject(err) : defer.resolve();
					});
				}
				else {
					defer.reject(new Error('Missing required GIT_REPO_URL.'));
				}
				
				return defer.promise;
			})
			.then(function () {
				cloned = true;
			})
			.catch(function (err) {
				console.error(err);
				cloneError = err;
			})
		;
	};
	
	
	// Initial checkout of repo
	checkout().done();
	
	app.get('/', function (req, res) {
		res.render('index', {
			host: req.get('host'),
			TRANSIFEX_USERNAME: process.env.TRANSIFEX_USERNAME,
			TRANSIFEX_PASSWORD: Boolean(process.env.TRANSIFEX_PASSWORD),
			GIT_REPO_URL: process.env.GIT_REPO_URL,
			GIT_NAME: process.env.GIT_NAME,
			GIT_EMAIL: process.env.GIT_EMAIL,
			LOCALE_DIR: process.env.LOCALE_DIR,
			LOCALE_EXT: process.env.LOCALE_EXT,
			cloned: cloned,
			cloneError: cloneError,
			lastAttempt: lastAttempt
		});
	});
	
	app.post('/transifex', function (req, res, next) {
		// Do a fresh checkout every time (lazyman reset)
		checkout()
			.then(function () {
				// If body is empty, everything is wrong!
				if (Object.keys(req.body).length === 0) {
					lastAttempt = false;
					return res.status(400).send('Invalid body.');
				}
				
				// Must provide us with all the information we need
				if (!req.body.project || !req.body.resource || !req.body.language) {
					lastAttempt = false;
					return res.status(400).send('Required params missing. Must have "project", "resource" and "language".');
				}
				
				// TODO: validate transifex signature
				
				// Setup transifex connection<commit_msg>Improved logging of 400 errors<commit_after>'use strict';

module.exports = function () {
	var bodyParser = require('body-parser');
	var exec = require('child_process').exec;
	var express = require('express');
	var fs = require('fs');
	var path = require('path');
	var q = require('q');
	var Transifex = require('transifex');
	
	var cloned, cloneError, lastAttempt;
	var cloneDir = __dirname + '/clone';
	
	var app = express();
	app.use(bodyParser.json());
	app.use('/media', express.static(__dirname + '/media'));
	app.set('views', path.join(__dirname, '/views'));
	app.set('view engine', 'ejs');
	app.enable('trust proxy');
	
	// Use to clear the current cloned repo locally
	var clear = function () {
		var defer = q.defer();
		
		fs.exists(cloneDir, function (exists) {
			if (exists) {
				exec('rm -r ' + cloneDir, function (err) {
					err ? defer.reject(err) : defer.resolve();
				});
			}
			else {
				defer.resolve();
			}
		});
		
		return defer.promise;
	};
	
	/*
	 * This will checkout the repo to the "clone" directory. It uses the
	 * credentials provided in the ENV. First we need to make sure there is no
	 * left over clone, we want to start fresh whenever the server is started.
	 */
	var checkout = function () {
		cloneError = false;
		cloned = false;
		
		return clear()
			.then(function () {
				var defer = q.defer();
				
				if (process.env.GIT_REPO_URL) {
					exec('git clone ' + process.env.GIT_REPO_URL + ' ' + cloneDir, function (err) {
						err ? defer.reject(err) : defer.resolve();
					});
				}
				else {
					defer.reject(new Error('Missing required GIT_REPO_URL.'));
				}
				
				return defer.promise;
			})
			.then(function () {
				cloned = true;
			})
			.catch(function (err) {
				console.error(err);
				cloneError = err;
			})
		;
	};
	
	
	// Initial checkout of repo
	checkout().done();
	
	app.get('/', function (req, res) {
		res.render('index', {
			host: req.get('host'),
			TRANSIFEX_USERNAME: process.env.TRANSIFEX_USERNAME,
			TRANSIFEX_PASSWORD: Boolean(process.env.TRANSIFEX_PASSWORD),
			GIT_REPO_URL: process.env.GIT_REPO_URL,
			GIT_NAME: process.env.GIT_NAME,
			GIT_EMAIL: process.env.GIT_EMAIL,
			LOCALE_DIR: process.env.LOCALE_DIR,
			LOCALE_EXT: process.env.LOCALE_EXT,
			cloned: cloned,
			cloneError: cloneError,
			lastAttempt: lastAttempt
		});
	});
	
	app.post('/transifex', function (req, res, next) {
		// Do a fresh checkout every time (lazyman reset)
		checkout()
			.then(function () {
				// If body is empty, everything is wrong!
				if (Object.keys(req.body).length === 0) {
					lastAttempt = false;
					var msg = 'Invalid body: ' + req.body;
					console.error(msg);
					return res.status(400).send(msg);
				}
				
				// Must provide us with all the information we need
				if (!req.body.project || !req.body.resource || !req.body.language) {
					lastAttempt = false;
					var msg = 'Required params missing. Must have "project", "resource" and "language".';
					console.error(msg);
					return res.status(400).send(msg);
				}
				
				// TODO: validate transifex signature
				
				// Setup transifex connection
{'fraction_non_alphanumeric': 0.14836697849991795, 'fraction_numerical': 0.0027900869850648285, 'mean_word_length': 3.0357615894039736, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '554497', 'n_tokens_mistral': 2464, 'n_tokens_neox': 1992, 'n_words': 514}