<commit_before>var path = require('path');
var step = require('step');
var FTP = require('ftp');
var util = require('./util');
var ProcessTasks = require('./ProcessTasks');
var LantronixTelnetTasks = require('./LantronixTelnetTasks');
var rebootLantronix = require('./rebootLantronix');

module.exports = manageLantronixFiles;

var ROOT_DIR = '/mnt/flash';
var DEFAULT_FTP_USER = 'root';
var DEFAULT_FTP_PASS = '<PASSWORD>';

/**
 * @param {object} options
 * @param {string} options.host
 * @param {string=} options.user
 * @param {string=} options.pass
 * @param {number=} options.reboot
 * @param {Array.<string>=} options.kill
 * @param {Array.<string>=} options.delete
 * @param {object.<string, string>=} options.upload
 * @param {Object.<string, string>=} options.chmod
 * @param {boolean=} options.pipe
 * @param {function(?Error)=} done
 */
function manageLantronixFiles(options, done)
{
  setUpOptions(options);

  var telnet = new LantronixTelnetTasks({
    host: options.host,
    user: options.user,
    pass: options.pass
  });

  if (options.pipe)
  {
    telnet.pipeStdio();
  }

  telnet.on('timeout', function(task)
  {
    telnet.kill();

    done && done(new Error("Telnet task timed out: " + task.name));
  });

  options.kill.forEach(function(processName)
  {
    telnet.add({
      name: "Kill process " + processName,
      expect: "/ #",
      action: "killall " + processName + "\n"
    })
  });

  options.delete.forEach(function(remoteFile)
  {
    telnet.add({
      name: "Delete file " + remoteFile,
      expect: "/ #",
      action: util.format('rm -r "%s/%s"\n', ROOT_DIR, remoteFile)
    });
  });

  getDirectories(options.upload).forEach(function(dirname)
  {
    telnet.add({
      name: "Create directory " + dirname,
      expect: "/ #",
      action: util.format('mkdir -p "%s"\n', dirname)
    });
  });

  telnet.addExitTask(function()
  {
    connectToFtp(options, done);
  });
}

function setUpOptions(options)
{
  if (typeof options.reboot !== 'number')
  {
    options.reboot = -1;
  }

  if (!Array.isArray(options.kill))
  {
    options.delete = [];
  }

  if (!Array.isArray(options.delete))
  {
    options.delete = [];
  }

  options.delete = options.delete
    .map(normalizeRemoteFile)
    .filter(function(file) { return file.length > 0; });

  if (typeof options.upload !== 'object' || options.upload === null)
  {
    options.upload = {};
  }

  for (var localFile in options.upload)
  {
    if (options.upload.hasOwnProperty(localFile))
    {
      var remoteFile = normalizeRemoteFile(options.upload[localFile]);

      if (remoteFile.length === 0)
      {
        remoteFile = path.basename(remoteFile);
      }

      options.upload[localFile] = remoteFile;
    }<commit_msg>Fixed `kill` option not having a default value.<commit_after>var path = require('path');
var step = require('step');
var FTP = require('ftp');
var util = require('./util');
var ProcessTasks = require('./ProcessTasks');
var LantronixTelnetTasks = require('./LantronixTelnetTasks');
var rebootLantronix = require('./rebootLantronix');

module.exports = manageLantronixFiles;

var ROOT_DIR = '/mnt/flash';
var DEFAULT_FTP_USER = 'root';
var DEFAULT_FTP_PASS = '<PASSWORD>';

/**
 * @param {object} options
 * @param {string} options.host
 * @param {string=} options.user
 * @param {string=} options.pass
 * @param {number=} options.reboot
 * @param {Array.<string>=} options.kill
 * @param {Array.<string>=} options.delete
 * @param {object.<string, string>=} options.upload
 * @param {Object.<string, string>=} options.chmod
 * @param {boolean=} options.pipe
 * @param {function(?Error)=} done
 */
function manageLantronixFiles(options, done)
{
  setUpOptions(options);

  var telnet = new LantronixTelnetTasks({
    host: options.host,
    user: options.user,
    pass: options.pass
  });

  if (options.pipe)
  {
    telnet.pipeStdio();
  }

  telnet.on('timeout', function(task)
  {
    telnet.kill();

    done && done(new Error("Telnet task timed out: " + task.name));
  });

  options.kill.forEach(function(processName)
  {
    telnet.add({
      name: "Kill process " + processName,
      expect: "/ #",
      action: "killall " + processName + "\n"
    })
  });

  options.delete.forEach(function(remoteFile)
  {
    telnet.add({
      name: "Delete file " + remoteFile,
      expect: "/ #",
      action: util.format('rm -r "%s/%s"\n', ROOT_DIR, remoteFile)
    });
  });

  getDirectories(options.upload).forEach(function(dirname)
  {
    telnet.add({
      name: "Create directory " + dirname,
      expect: "/ #",
      action: util.format('mkdir -p "%s"\n', dirname)
    });
  });

  telnet.addExitTask(function()
  {
    connectToFtp(options, done);
  });
}

function setUpOptions(options)
{
  if (typeof options.reboot !== 'number')
  {
    options.reboot = -1;
  }

  if (!Array.isArray(options.kill))
  {
    options.kill = [];
  }

  if (!Array.isArray(options.delete))
  {
    options.delete = [];
  }

  options.delete = options.delete
    .map(normalizeRemoteFile)
    .filter(function(file) { return file.length > 0; });

  if (typeof options.upload !== 'object' || options.upload === null)
  {
    options.upload = {};
  }

  for (var localFile in options.upload)
  {
    if (options.upload.hasOwnProperty(localFile))
    {
      var remoteFile = normalizeRemoteFile(options.upload[localFile]);

      if (remoteFile.length === 0)
      {
        remoteFile = path.basename(remoteFile);
      }

      options.upload[localFile] = remoteFile;
    }
{'fraction_non_alphanumeric': 0.16769622093023256, 'fraction_numerical': 0.0010901162790697674, 'mean_word_length': 3.3176470588235296, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, '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': '745622', 'n_tokens_mistral': 1876, 'n_tokens_neox': 1814, 'n_words': 363}