<commit_before>var path = require('path');
var fs = require('fs');
var env = require('./env');

var getLogfile = function(workPath, prefix) {
    var timeString = (new Date().toISOString()).replace(/[-T:]/g, '').split('.').shift();
    var logFileName = timeString + '-server.log';
    if(prefix) {
        logFileName = logFileName.replace('-server', '-'+prefix+'-server');
    }
    return path.join(workPath, logFileName);
};

var handleLogging = function(server, data, callback) {
    callback(null, data);
    // server loops
    if (data.match(/^Opening entry list:/)) {
        // use new log file
        server.logFile = getLogfile(server.workPath);
        console.log('new.logfile', server.logFile);
    }
    fs.appendFileSync(server.logFile, data+'\n');
    console.log('[LOG]', data);
};

var handleError = function(server, data, callback) {
    callback(null, data);
    if(data.length === 0)
        return;
    fs.appendFileSync(server.logFile.replace('.log', '.err'), data+'\n');
    console.log('[ERR]', data);
};

var handleExit = function(server) {
    fs.unlink(server.pidFile, function(err) {
        if(err) return console.error(err);
        console.log('Server process exit, removing pid file: ', server.pidFile);
    });
};

var copyFile = function(fromPath, toPath, fileName) {
    return require('fs-sync').copy(
        path.join(fromPath, fileName),
        path.join(toPath, fileName),
        { force: true }
    );
};

var getServerWorkPath = function(preset) {
    return path.join(env.getServersPath(), preset.presetName);
};

var getPidFile = function(preset) {
    return path.join(getServerWorkPath(preset), 'server.pid')
};

var prepareServerPath = function(preset) {
    var serverPath = getServerWorkPath(preset);
    var rc = fs.existsSync(serverPath) ? true : fs.mkdirSync(serverPath);
    if(rc !== false) {
        ['server_cfg.ini', 'entry_list.ini'].forEach(
            function (fileName) {
                rc = copyFile(preset.presetPath, serverPath, fileName);
            }
        );
    }
    return rc;
};

module.exports = function (presetName) {
    var preset = require('./preset')(presetName);
    return {
        preset: preset,
        name: preset.serverName,
        isReady: prepareServerPath(preset),
        pidFile: getPidFile(preset),
        logFile: getLogfile(getServerWorkPath(preset), 'init'),<commit_msg>fixed error when servers directory did not exist<commit_after>var path = require('path');
var fs = require('fs');
    fs.extra = require('fs-extra');
var env = require('./env');

var getLogfile = function(workPath, prefix) {
    var timeString = (new Date().toISOString()).replace(/[-T:]/g, '').split('.').shift();
    var logFileName = timeString + '-server.log';
    if(prefix) {
        logFileName = logFileName.replace('-server', '-'+prefix+'-server');
    }
    return path.join(workPath, logFileName);
};

var handleLogging = function(server, data, callback) {
    callback(null, data);
    // server loops
    if (data.match(/^Opening entry list:/)) {
        // use new log file
        server.logFile = getLogfile(server.workPath);
        console.log('new.logfile', server.logFile);
    }
    fs.appendFileSync(server.logFile, data+'\n');
    console.log('[LOG]', data);
};

var handleError = function(server, data, callback) {
    callback(null, data);
    if(data.length === 0)
        return;
    fs.appendFileSync(server.logFile.replace('.log', '.err'), data+'\n');
    console.log('[ERR]', data);
};

var handleExit = function(server) {
    fs.unlink(server.pidFile, function(err) {
        if(err) return console.error(err);
        console.log('Server process exit, removing pid file: ', server.pidFile);
    });
};

var copyFile = function(fromPath, toPath, fileName) {
    return require('fs-sync').copy(
        path.join(fromPath, fileName),
        path.join(toPath, fileName),
        { force: true }
    );
};

var getServerWorkPath = function(preset) {
    return path.join(env.getServersPath(), preset.presetName);
};

var getPidFile = function(preset) {
    return path.join(getServerWorkPath(preset), 'server.pid')
};

var prepareServerPath = function(preset) {
    var serverPath = getServerWorkPath(preset);
    var rc = fs.existsSync(serverPath) ? true : fs.extra.ensureDirSync(serverPath);
    if(rc !== false) {
        ['server_cfg.ini', 'entry_list.ini'].forEach(
            function (fileName) {
                rc = copyFile(preset.presetPath, serverPath, fileName);
            }
        );
    }
    return rc;
};

module.exports = function (presetName) {
    var preset = require('./preset')(presetName);
    return {
        preset: preset,
        name: preset.serverName,
        isReady: prepareServerPath(preset),
        pidFile: getPidFile(preset),
        logFile: getLogfile(getServerWorkPath(preset), 'init'),
{'fraction_non_alphanumeric': 0.16050402809336914, 'fraction_numerical': 0.0004131377814501136, 'mean_word_length': 3.174137931034483, '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': '176596', 'n_tokens_mistral': 1524, 'n_tokens_neox': 1526, 'n_words': 308}