<commit_before>var cluster = require('cluster');
var path = require("path");
var fs = require("fs");
var wrench = require("wrench");
var async = require("async");
var crypto = require("crypto");
var request = require("request");
var zlib = require("zlib");
var chokidar = require("chokidar");

var secretSettingsPath = __dirname + "/../../settings/secretSettings.json";

if (!fs.existsSync(secretSettingsPath)) {
	console.error("Config file [" + secretSettingsPath + "] missing!");
	console.error("Did you forget to run `make decrypt_conf`?");
	process.exit(1);
}

(function() {
	var conf_time = fs.statSync(secretSettingsPath).mtime.getTime();
	var cast5_time = fs.statSync(secretSettingsPath + ".cast5").mtime.getTime();
 
	if (conf_time < cast5_time) {
		console.error("Your config file is out of date!");
		console.error("You need to run `make decrypt_conf` to update it.");
		process.exit(1);
	}
})();

var secretSettings = JSON.parse(fs.readFileSync(secretSettingsPath, "utf8"));

//Setup master index
var masterIndex = JSON.parse(fs.readFileSync(__dirname + "/masterIndex.json", "utf8"));

var atcTree = JSON.parse(fs.readFileSync(__dirname + "/../../npl/atcTree.json", "utf8"));
//var searchIndexer = require("../../search/createSearchIndex.js");
//var Fuse = require("./lib/fuse.js");

var settings = JSON.parse(fs.readFileSync(__dirname + "/../../settings/settings.json", "utf8"));

var networkPort = settings.internalServerPorts.site;
var searchPort = settings.internalServerPorts.search;

var app = require('./app').init(networkPort);

var finishedSearches = {};
var searchIndex = null;
var searchIndices = [];
var chapterFileNames = null;
var indexCache = {};
var siteMap = null;

initFileWatchers();

//TODO: Switch to polling
function initFileWatchers() {
	
	//var chaptersPath = path.normalize(__dirname + "/chapters/");
	var atcTreePath = path.normalize(__dirname + "/../../npl/atcTree.json");
	
	//var chaptersWatcher = chokidar.watch(chaptersPath, {ignored: /^\./, persistent: true, ignoreInitial: true, interval: 20000, binaryInterval: 20000});
	var atcTreeWatcher = chokidar.watch(atcTreePath, {persistent: true, ignoreInitial: true, interval: 20000, binaryInterval: 20000});

	//chaptersWatcher.on('error', function(error) {console.error('Error happened on chapters file watch', error);})
	//console.log("Watching " + chaptersPath + " for changes...");

	//chaptersWatcher.on('all', function(path, stats) {

	//	console.log("Clearing cached file reads.");
	//	clearCachedFileReads();

	//});

	atcTreeWatcher.on('error', function(error) {console.error('Error happened on atc file watch', error);})
	console.log("Watching " + atcTreePath + " for changes...");
	atcTreeWatcher.on('all', function(path, stats) {

		//TODO: Mail?

		console.log("Reloading ATC tree.");
		atcTree = JSON.parse(fs.readFileSync(__dirname + "/../../npl/atcTree.json", "utf8"));

	});

}

function clearCachedFileReads() {
	chapterFileNames = null;
	indexCache = {};
}

var locals = {
	title: 'Läkemedelsboken',
	description: '',
	author: '',
	version: settings.version
};

app.get('/sitemap.xml', function(req,res){

	getSiteMap(function(err, sitemapXml) {

		if (err) {
			console.log(err);
		}
		
		res.set('Content-Type', 'text/xml');
		res.send(200, sitemapXml);
	});

	
});

function htmlEscape (text) {

	return String(text)
		.replace(/&/g, '&amp;')
		.replace(/"/g, '&quot;')
		.replace(/'/g, '&#39;')
		.replace(/</g, '&lt;')
		.replace(/>/g, '&gt;');
}

//No javascript search results
app.get('/search', function(req,res){

	var terms = req.query["search"];

	if (typeof terms === "string") {
		terms = terms.trim()
	}
	
	if (terms === undefined) {
		terms = "";
	}

	locals.terms = terms;
	
	if (terms === "") {
		locals.err = false;
		locals.results = {medicinesearch: [], contentsearch: []};
		res.render('search.ejs', locals);
		
	} else {

		async.parallel({<commit_msg>Results are now undefined per default in noscript search view<commit_after>var cluster = require('cluster');
var path = require("path");
var fs = require("fs");
var wrench = require("wrench");
var async = require("async");
var crypto = require("crypto");
var request = require("request");
var zlib = require("zlib");
var chokidar = require("chokidar");

var secretSettingsPath = __dirname + "/../../settings/secretSettings.json";

if (!fs.existsSync(secretSettingsPath)) {
	console.error("Config file [" + secretSettingsPath + "] missing!");
	console.error("Did you forget to run `make decrypt_conf`?");
	process.exit(1);
}

(function() {
	var conf_time = fs.statSync(secretSettingsPath).mtime.getTime();
	var cast5_time = fs.statSync(secretSettingsPath + ".cast5").mtime.getTime();
 
	if (conf_time < cast5_time) {
		console.error("Your config file is out of date!");
		console.error("You need to run `make decrypt_conf` to update it.");
		process.exit(1);
	}
})();

var secretSettings = JSON.parse(fs.readFileSync(secretSettingsPath, "utf8"));

//Setup master index
var masterIndex = JSON.parse(fs.readFileSync(__dirname + "/masterIndex.json", "utf8"));

var atcTree = JSON.parse(fs.readFileSync(__dirname + "/../../npl/atcTree.json", "utf8"));
//var searchIndexer = require("../../search/createSearchIndex.js");
//var Fuse = require("./lib/fuse.js");

var settings = JSON.parse(fs.readFileSync(__dirname + "/../../settings/settings.json", "utf8"));

var networkPort = settings.internalServerPorts.site;
var searchPort = settings.internalServerPorts.search;

var app = require('./app').init(networkPort);

var finishedSearches = {};
var searchIndex = null;
var searchIndices = [];
var chapterFileNames = null;
var indexCache = {};
var siteMap = null;

initFileWatchers();

//TODO: Switch to polling
function initFileWatchers() {
	
	//var chaptersPath = path.normalize(__dirname + "/chapters/");
	var atcTreePath = path.normalize(__dirname + "/../../npl/atcTree.json");
	
	//var chaptersWatcher = chokidar.watch(chaptersPath, {ignored: /^\./, persistent: true, ignoreInitial: true, interval: 20000, binaryInterval: 20000});
	var atcTreeWatcher = chokidar.watch(atcTreePath, {persistent: true, ignoreInitial: true, interval: 20000, binaryInterval: 20000});

	//chaptersWatcher.on('error', function(error) {console.error('Error happened on chapters file watch', error);})
	//console.log("Watching " + chaptersPath + " for changes...");

	//chaptersWatcher.on('all', function(path, stats) {

	//	console.log("Clearing cached file reads.");
	//	clearCachedFileReads();

	//});

	atcTreeWatcher.on('error', function(error) {console.error('Error happened on atc file watch', error);})
	console.log("Watching " + atcTreePath + " for changes...");
	atcTreeWatcher.on('all', function(path, stats) {

		//TODO: Mail?

		console.log("Reloading ATC tree.");
		atcTree = JSON.parse(fs.readFileSync(__dirname + "/../../npl/atcTree.json", "utf8"));

	});

}

function clearCachedFileReads() {
	chapterFileNames = null;
	indexCache = {};
}

var locals = {
	title: 'Läkemedelsboken',
	description: '',
	author: '',
	version: settings.version
};

app.get('/sitemap.xml', function(req,res){

	getSiteMap(function(err, sitemapXml) {

		if (err) {
			console.log(err);
		}
		
		res.set('Content-Type', 'text/xml');
		res.send(200, sitemapXml);
	});

	
});

function htmlEscape (text) {

	return String(text)
		.replace(/&/g, '&amp;')
		.replace(/"/g, '&quot;')
		.replace(/'/g, '&#39;')
		.replace(/</g, '&lt;')
		.replace(/>/g, '&gt;');
}

//No javascript search results
app.get('/search', function(req,res){

	var terms = req.query["search"];

	if (typeof terms === "string") {
		terms = terms.trim()
	}
	
	if (terms === undefined) {
		terms = "";
	}

	locals.terms = terms;
	locals.results = undefined;
	
	if (terms === "") {
		locals.err = false;
		locals.results = {medicinesearch: [], contentsearch: []};
		res.render('search.ejs', locals);
		
	} else {

		async.parallel({
{'fraction_non_alphanumeric': 0.18862390001275348, 'fraction_numerical': 0.008927432725417676, 'mean_word_length': 4.518648838845883, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '4974283', 'n_tokens_mistral': 2778, 'n_tokens_neox': 2609, 'n_words': 570}