<commit_before>Fs       = require 'fs'
OptParse = require 'optparse'
Path     = require 'path'
MewBot   = require './mewbot.coffee'
Fse      = require 'fs.extra'


checkDirectory = (path)->
    dataFile = Path.join __dirname,"..",path
    if Fs.existsSync(dataFile) is false
        Fse.mkdirRecursiveSync dataFile

checkDirectory "/script"
checkDirectory "/testrc"
checkDirectory "/var"
checkDirectory "/var/run"
checkDirectory "/var/log"
checkDirectory "/var/data"
checkDirectory "/var/conf"
checkDirectory "/mew_modules"

Switches = [
    [ "-n", "--name name", "name of this mewbot" ],
    [ "-t", "--test testcase", "test case to run" ],
    [ "-r", "--role client", "role of this mewbot" ],
    [ "-h", "--help", "print help information" ],
    [ "-p", "--profile profile", "config profile of this mewbot" ],
    [ "-u", "--update", "update mewbot" ],
    [ "-a", "--archive pack", "archive mewbot" ],
    [ "-m", "--module module", "check or get module from remote server" ]
]

Options = 
    adapter    :     process.env.MEWBOT_ADAPTER or "shell"
    test       :     process.env.MEWBOT_TEST    or ""
    role       :     process.env.MEWBOT_ROLE    or "client"
    version    :     false
    help       :     false
    update     :     false
    module     :     ""
    archive    :     ""
    name       :     process.env.MEWBOT_NAME    or "mewbot"
    profile    :     process.env.MEWBOT_PROFILE or "default"

Parser = new OptParse.OptionParser(Switches)
Parser.banner = "Usage mewbot [options]"

Parser.on "test",(opt,value)->
    Options.test = value

Parser.on "profile",(opt,value)->
    Options.profile = value

Parser.on "role",(opt,value)->
    Options.role = value

Parser.on "update",(opt,value)->
    Options.update = true

Parser.on "help",(opt,value)->
    Options.help = true

Parser.on "module",(opt,value)->
    Options.module = value

Parser.on "archive",(opt,value)->
    if value
        Options.archive = value
    else
        Options.archive = "mewbot-#{new Date().getTime()}"

Parser.on "name",(opt,value)->
    Options.name = value

Parser.parse process.argv

unless process.platform is "win32"
  process.on 'SIGTERM', ->
    process.exit 0

if Options.help
    console.log Parser.toString()
    process.exit 0

mewbot = new MewBot Options.name,Options.adapter

mewbot.init Options.profile,(err)->
    if Options.update
        console.log "update mewbot"
    else if Options.archive.length
        archiver = mewbot.module("archiver")
        packFile = mewbot.getTmpFile Options.archive
        if Options.archive.indexOf(".zip") < 0
            packFile = "#{packFile}.zip"
        mewbot.logger.info "mewbot start archive at #{packFile}"
        archiver.zipFolder packFile,Path.join(__dirname,".."),(err,pointer)->
            mewbot.logger.info "mewbot archive complete at #{packFile}"
            process.exit 0
    else if Options.test and Options.test.length
        if Options.test is "all"
            Fs.readdir Path.join(__dirname,"..","testrc"),(err,files)->
                if files and files.length
                    testPathArray = []
                    files.sort (a,b)->
                        amatch = a.match /^\[(\d)\].+\.coffee$/
                        bmatch = b.match /^\[(\d)\].+\.coffee$/
                        if amatch and bmatch
                            return parseInt(amatch[1])-parseInt(bmatch[1])
                        else if amatch
                            return -1
                        else if bmatch
                            return 1
                        else
                            return 0
                    for file in files
                        if Path.extname(file) is ".coffee"
                            testPathArray.push Path.join(__dirname,"..","testrc",file)
                    mewbot.test.runTest testPathArray,(err,result)->
                        process.exit 0
                else
                    console.log "there is no test"
                    process.exit 0
        else<commit_msg>update start.coffee<commit_after>Fs       = require 'fs'
OptParse = require 'optparse'
Path     = require 'path'
MewBot   = require './mewbot.coffee'
Fse      = require 'fs.extra'


checkDirectory = (path)->
    dataFile = Path.join __dirname,"..",path
    if Fs.existsSync(dataFile) is false
        Fse.mkdirRecursiveSync dataFile

checkDirectory "/script"
checkDirectory "/testrc"
checkDirectory "/var"
checkDirectory "/var/run"
checkDirectory "/var/log"
checkDirectory "/var/data"
checkDirectory "/var/conf"
checkDirectory "/mew_modules"

Switches = [
    [ "-n", "--name name", "name of this mewbot" ],
    [ "-t", "--test testcase", "test case to run" ],
    [ "-r", "--role client|server", "role of this mewbot" ],
    [ "-h", "--help", "print help information" ],
    [ "-p", "--profile profile", "config profile of this mewbot" ],
    [ "-u", "--update", "update mewbot" ],
    [ "-a", "--archive pack", "archive mewbot" ],
    [ "-m", "--module module", "check or get module from remote server" ]
]

Options = 
    adapter    :     process.env.MEWBOT_ADAPTER or "shell"
    test       :     process.env.MEWBOT_TEST    or ""
    role       :     process.env.MEWBOT_ROLE    or "client"
    version    :     false
    help       :     false
    update     :     false
    module     :     ""
    archive    :     ""
    name       :     process.env.MEWBOT_NAME    or "mewbot"
    profile    :     process.env.MEWBOT_PROFILE or "default"

Parser = new OptParse.OptionParser(Switches)
Parser.banner = "Usage mewbot [options]"

Parser.on "test",(opt,value)->
    Options.test = value

Parser.on "profile",(opt,value)->
    Options.profile = value

Parser.on "role",(opt,value)->
    Options.role = value

Parser.on "update",(opt,value)->
    Options.update = true

Parser.on "help",(opt,value)->
    Options.help = true

Parser.on "module",(opt,value)->
    Options.module = value

Parser.on "archive",(opt,value)->
    if value
        Options.archive = value
    else
        Options.archive = "mewbot-#{new Date().getTime()}"

Parser.on "name",(opt,value)->
    Options.name = value

Parser.parse process.argv

unless process.platform is "win32"
  process.on 'SIGTERM', ->
    process.exit 0

if Options.help
    console.log Parser.toString()
    process.exit 0

mewbot = new MewBot Options.name,Options.adapter

mewbot.init Options.profile,(err)->
    if Options.update
        console.log "update mewbot"
    else if Options.archive.length
        archiver = mewbot.module("archiver")
        packFile = mewbot.getTmpFile Options.archive
        if Options.archive.indexOf(".zip") < 0
            packFile = "#{packFile}.zip"
        mewbot.logger.info "mewbot start archive at #{packFile}"
        archiver.zipFolder packFile,Path.join(__dirname,".."),(err,pointer)->
            mewbot.logger.info "mewbot archive complete at #{packFile}"
            process.exit 0

    else if Options.test and Options.test.length
        if Options.test is "all"
            Fs.readdir Path.join(__dirname,"..","testrc"),(err,files)->
                if files and files.length
                    testPathArray = []
                    files.sort (a,b)->
                        amatch = a.match /^\[(\d)\].+\.coffee$/
                        bmatch = b.match /^\[(\d)\].+\.coffee$/
                        if amatch and bmatch
                            return parseInt(amatch[1])-parseInt(bmatch[1])
                        else if amatch
                            return -1
                        else if bmatch
                            return 1
                        else
                            return 0
                    for file in files
                        if Path.extname(file) is ".coffee"
                            testPathArray.push Path.join(__dirname,"..","testrc",file)
                    mewbot.test.runTest testPathArray,(err,result)->
                        process.exit 0
                else
                    console.log "there is no test"
                    process.exit 0
        else
{'fraction_non_alphanumeric': 0.13124533929903057, 'fraction_numerical': 0.003231419338801889, 'mean_word_length': 1.8535460992907802, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, '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': '2714204', 'n_tokens_mistral': 2431, 'n_tokens_neox': 2342, 'n_words': 582}