<commit_before>#!/usr/bin/env coffee

path        = require 'path'
program     = require 'commander'
read        = require 'read'
process     = require 'process'
log         = require('printit')
                  prefix: 'Cozy Desktop'

config      = require './backend/config'
replication = require './backend/replication'
filesystem  = require './backend/filesystem'
binary      = require './backend/binary'
pouch       = require './backend/db'
pkg         = require './package.json'


# Helpers to get cozy password from user.
module.exports.getPassword = (callback) ->
    promptMsg = 'Please enter your password to register your device to ' + \
                'your remote Cozy: '
    read prompt: promptMsg, silent: true , callback


# Register current device to remote Cozy. Then it saves related informations
# to the config file.
module.exports.addRemote = addRemote = (url, deviceName, syncPath) ->
    saveConfig = (err, credentials) ->
        if err
            log.error err
            log.error 'An error occured while registering your device.'
        else
            options =
                url: url
                deviceName: deviceName
                path: path.resolve syncPath
                deviceId: credentials.id
                devicePassword: <PASSWORD>

            config.addRemoteCozy options
            log.info 'Remote Cozy properly configured to work ' + \
                     'with current device.'

    register = (err, password) ->
        options =
            url: url
            deviceName: deviceName
            password: password

        replication.registerDevice options, saveConfig

    module.exports.getPassword register


# Unregister current device from remote Cozy. Then it removes remote from
# config file.
module.exports.removeRemote = removeRemote = (args) ->
    remoteConfig = config.getConfig()
    deviceName = args.deviceName or config.getDeviceName()

    saveConfig = (err) ->
        if err
            log.error err
            log.error 'An error occured while unregistering your device.'
        else
            config.removeRemoteCozy deviceName
            log.info 'Current device properly removed from remote cozy.'

    unregister = (err, password) ->
        options =
            url: remoteConfig.url
            deviceId: remoteConfig.deviceId
            password: password
        replication.unregisterDevice options, saveConfig

    module.exports.getPassword unregister


# Display the whole content of the database.
displayDatabase = ->
    db = require('./backend/db').db
    db.allDocs include_docs: true, (err, results) ->
        if err
            log.error err
        else
            results.rows.map (row) ->
                console.log row.doc

# Disaply all docs returned by a given query.
displayQuery = (query) ->
    db = require('./backend/db').db
    log.info "Query: #{query}"
    db.query query, (err, results) ->
        if err
            log.error err
        else
            results.rows.map (row) ->
                console.log "key: #{row.key}"
                console.log "value #{JSON.stringify row.value}"


# Start database sync process and setup file change watcher.
sync = (args) ->

    # Watch local changes
    if args.twoway
        filesystem.watchChanges true, true

    config = config.getConfig()

    if not (config.deviceName? and config.url? and config.path?)
        log.error """
No configuration found, please run add-remote-cozy command before running a synchronization.
"""
    else

        pouch.addAllFilters ->
            # Replicate databases
            replication.runReplication force: args.force, (err) ->
                log.info 'Sync ended'
                if err
                    log.error err
                    log.error 'An error occured while running synchronisation.'
                    process.exit 1
                else
                    process.exit 0


# Display current configuratioN
displayConfig = ->
    console.log JSON.stringify config.config, null, 2


program
    .command('add-remote-cozy <url> <devicename> <syncPath>')
    .description('Configure current device to sync with given cozy')
    .option('-d, --deviceName [deviceName]', 'device name to deal with')
    .action addRemote

program
    .command('remove-remote-cozy')
    .description('Unsync current device with its remote cozy')
    .action removeRemote

program
    .command('sync')
    .description('Sync databases, apply and/or watch changes')
    .option('-2, --twoway', 'apply local changes to remote as well as pulling changes')
    .option('-c, --catchup', 're-detect all the files locally (works only along --two-way)')
    .option('-f, --force', 'Run sync from the beginning of all the Cozy changes.')
    .action sync

program
    .command 'reset-database'
    .description 'Recreates the local database'
    .action ->
        log.info "Recreates the local database..."
        pouch.resetDatabase ->
            log.info "Database recreated"

program
    .command('display-database')
    .description('Display database content')
    .action displayDatabase

program
    .command('display-query <query>')
    .description('Display database query result')
    .action displayQuery

program
    .command('display-config')
    .description('Display device configuration and exit')
    .action displayConfig

program
    .command("*")
    .description("Display help message for an unknown command.")
    .action ->
        log.info 'Unknown command, run "cozy-desktop --help"' + \
                 ' to know the list of available commands.'

program
    .version(pkg.version)


if not module.parent
    program.parse process.argv

unless process.argv.slice(2).length
    program.outputHelp()
<commit_msg>make sync two way by default<commit_after>#!/usr/bin/env coffee

path        = require 'path'
program     = require 'commander'
read        = require 'read'
process     = require 'process'
log         = require('printit')
                  prefix: 'Cozy Desktop'

config      = require './backend/config'
replication = require './backend/replication'
filesystem  = require './backend/filesystem'
binary      = require './backend/binary'
pouch       = require './backend/db'
pkg         = require './package.json'


# Helpers to get cozy password from user.
module.exports.getPassword = (callback) ->
    promptMsg = 'Please enter your password to register your device to ' + \
                'your remote Cozy: '
    read prompt: promptMsg, silent: true , callback


# Register current device to remote Cozy. Then it saves related informations
# to the config file.
module.exports.addRemote = addRemote = (url, deviceName, syncPath) ->
    saveConfig = (err, credentials) ->
        if err
            log.error err
            log.error 'An error occured while registering your device.'
        else
            options =
                url: url
                deviceName: deviceName
                path: path.resolve syncPath
                deviceId: credentials.id
                devicePassword: <PASSWORD>

            config.addRemoteCozy options
            log.info 'Remote Cozy properly configured to work ' + \
                     'with current device.'

    register = (err, password) ->
        options =
            url: url
            deviceName: deviceName
            password: password

        replication.registerDevice options, saveConfig

    module.exports.getPassword register


# Unregister current device from remote Cozy. Then it removes remote from
# config file.
module.exports.removeRemote = removeRemote = (args) ->
    remoteConfig = config.getConfig()
    deviceName = args.deviceName or config.getDeviceName()

    saveConfig = (err) ->
        if err
            log.error err
            log.error 'An error occured while unregistering your device.'
        else
            config.removeRemoteCozy deviceName
            log.info 'Current device properly removed from remote cozy.'

    unregister = (err, password) ->
        options =
            url: remoteConfig.url
            deviceId: remoteConfig.deviceId
            password: password
        replication.unregisterDevice options, saveConfig

    module.exports.getPassword unregister


# Display the whole content of the database.
displayDatabase = ->
    db = require('./backend/db').db
    db.allDocs include_docs: true, (err, results) ->
        if err
            log.error err
        else
            results.rows.map (row) ->
                console.log row.doc


# Disaply all docs returned by a given query.
displayQuery = (query) ->
    db = require('./backend/db').db
    log.info "Query: #{query}"
    db.query query, (err, results) ->
        if err
            log.error err
        else
            results.rows.map (row) ->
                console.log "key: #{row.key}"
                console.log "value #{JSON.stringify row.value}"


# Start database sync process and setup file change watcher.
sync = (args) ->

    config = config.getConfig()

    if not (config.deviceName? and config.url? and config.path?)
        log.error """
No configuration found, please run add-remote-cozy command before running
a synchronization.
"""
    else
        # Watch local changes
        if not args.readonly
            filesystem.watchChanges true, true

        pouch.addAllFilters ->
            # Replicate databases
            replication.runReplication force: args.force, (err) ->
                log.info 'Sync ended'
                if err
                    log.error err
                    log.error 'An error occured while running synchronisation.'
                    process.exit 1
                else
                    process.exit 0


# Display current configuratioN
displayConfig = ->
    console.log JSON.stringify config.config, null, 2


resetDatabase = ->
    log.info "Recreates the local database..."
    pouch.resetDatabase ->
        log.info "Database recreated"


program
    .command 'add-remote-cozy <url> <devicename> <syncPath>'
    .description 'Configure current device to sync with given cozy'
    .option '-d, --deviceName [deviceName]', 'device name to deal with'
    .action addRemote

program
    .command 'remove-remote-cozy'
    .description 'Unsync current device with its remote cozy'
    .action removeRemote

program
    .command 'sync'
    .description 'Sync databases, apply and/or watch changes'
    .option('-r, --readonly',
            'only apply remote changes to local folder')
    .option('-f, --force',
            'Run sync from the beginning of all the Cozy changes.')
    .action sync

program
    .command 'reset-database'
    .description 'Recreates the local database'
    .action resetDatabase

program
    .command 'display-database'
    .description 'Display database content'
    .action displayDatabase

program
    .command 'display-query <query>'
    .description 'Display database query result'
    .action displayQuery

program
    .command 'display-config'
    .description 'Display device configuration and exit'
    .action displayConfig

program
    .command "*"
    .description "Display help message for an unknown command."
    .action ->
        log.info 'Unknown command, run "cozy-desktop --help"' + \
                 ' to know the list of available commands.'

program
    .version pkg.version


if not module.parent
    program.parse process.argv

unless process.argv.slice(2).length
    program.outputHelp()

{'fraction_non_alphanumeric': 0.09168629503620344, 'fraction_numerical': 0.0007851347814708192, 'mean_word_length': 2.145993413830955, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 53, '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': '2578593', 'n_tokens_mistral': 3014, 'n_tokens_neox': 2903, 'n_words': 989}