<commit_before>//
//  RsyncParametersProcess.swift
//
//  Created by <NAME> on 08/02/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//
// swiftlint:disable type_body_length

import Foundation

final class RsyncParametersProcess {

    private var stats: Bool?
    private var arguments: [String]?
    var localCatalog: String?
    var offsiteCatalog: String?
    var offsiteUsername: String?
    var offsiteServer: String?
    var remoteargs: String?
    var linkdestparam: String?

    private func setParameters1To6(_ config: Configuration, dryRun: Bool, forDisplay: Bool, verify: Bool) {
        var parameter1: String?
        if verify {
            parameter1 = "--checksum"
        } else {
            parameter1 = config.parameter1
        }
        let parameter2: String = config.parameter2
        let parameter3: String = config.parameter3
        let parameter4: String = config.parameter4
        let offsiteServer: String = config.offsiteServer
        self.arguments!.append(parameter1 ?? "")
        if verify {
            if forDisplay {self.arguments!.append(" ")}
            self.arguments!.append("--recursive")
        }
        if forDisplay {self.arguments!.append(" ")}
        self.arguments!.append(parameter2)
        if forDisplay {self.arguments!.append(" ")}
        if offsiteServer.isEmpty  == false {
            if parameter3.isEmpty == false {
                self.arguments!.append(parameter3)
                if forDisplay {self.arguments!.append(" ")}
            }
        }
        self.arguments!.append(parameter4)
        if forDisplay {self.arguments!.append(" ")}
        if offsiteServer.isEmpty {
            // nothing
        } else {
            self.sshportparameter(config, forDisplay: forDisplay)
        }
    }

    private func sshportparameter(_ config: Configuration, forDisplay: Bool) {
        let parameter5: String = config.parameter5
        let parameter6: String = config.parameter6
        // -e
        self.arguments!.append(parameter5)
        if forDisplay {self.arguments!.append(" ")}
        if let sshport = config.sshport {
            // "ssh -p xxx"
            if forDisplay {self.arguments!.append(" \"")}
            self.arguments!.append("ssh -p " + String(sshport))
            if forDisplay {self.arguments!.append("\" ")}
        } else {
            // ssh
            self.arguments!.append(parameter6)
        }
        if forDisplay {self.arguments!.append(" ")}
    }

    // Compute user selected parameters parameter8 ... parameter14
    // Brute force, check every parameter, not special elegant, but it works

    private func setParameters8To14(_ config: Configuration, dryRun: Bool, forDisplay: Bool) {
        self.stats = false
        if config.parameter8 != nil {
            self.appendParameter(parameter: config.parameter8!, forDisplay: forDisplay)
        }
        if config.parameter9 != nil {
            self.appendParameter(parameter: config.parameter9!, forDisplay: forDisplay)
        }
        if config.parameter10 != nil {
            self.appendParameter(parameter: config.parameter10!, forDisplay: forDisplay)
        }
        if config.parameter11 != nil {
            self.appendParameter(parameter: config.parameter11!, forDisplay: forDisplay)
        }
        if config.parameter12 != nil {
            self.appendParameter(parameter: config.parameter12!, forDisplay: forDisplay)
        }
        if config.parameter13 != nil {
            self.appendParameter(parameter: config.parameter13!, forDisplay: forDisplay)
        }
        if config.parameter14 != nil {
            self.appendParameter(parameter: config.parameter14!, forDisplay: forDisplay)
        }
        // Append --stats parameter to collect info about run
        if dryRun {
            self.dryrunparameter(config, forDisplay: forDisplay)
        } else {
            if self.stats == false {
                self.appendParameter(parameter: "--stats", forDisplay: forDisplay)
            }
        }
    }

    private func setParameters8To14restore(_ config: Configuration, dryRun: Bool, forDisplay: Bool) {
        self.stats = false
        // Append --stats parameter to collect info about run
        if dryRun {
            self.dryrunparameter(config, forDisplay: forDisplay)
        } else {
            if self.stats == false {
                self.appendParameter(parameter: "--stats", forDisplay: forDisplay)
            }
        }
    }

    private func dryrunparameter(_ config: Configuration, forDisplay: Bool) {
        let dryrun: String = config.dryrun
        self.arguments!.append(dryrun)
        if forDisplay {self.arguments!.append(" ")}
        if self.stats! == false {
            self.arguments!.append("--stats")
            if forDisplay {self.arguments!.append(" ")}
        }
    }

    // Check userselected parameter and append it to arguments array passed to rsync or displayed
    // on screen.

    private func appendParameter (parameter: String, forDisplay: Bool) {
        if parameter.count > 1 {
            if parameter == "--stats" {
                self.stats = true
            }
            self.arguments!.append(parameter)
            if forDisplay {
                self.arguments!.append(" ")
            }
        }
    }

    /// Function for initialize arguments array. RsyncOSX computes four argumentstrings
    /// two arguments for dryrun, one for rsync and one for display
    /// two arguments for realrun, one for rsync and one for display
    /// which argument to compute is set in parameter to function
    /// - parameter config: structure (configuration) holding configuration for one task
    /// - parameter dryRun: true if compute dryrun arguments, false if compute arguments for real run
    /// - paramater forDisplay: true if for display, false if not
    /// - returns: Array of Strings
    func argumentsRsync(_ config: Configuration, dryRun: Bool, forDisplay: Bool) -> [String] {
        guard config.task != "restore" else { return [] }
        self.localCatalog = config.localCatalog
        self.remoteargs(config)
        self.setParameters1To6(config, dryRun: dryRun, forDisplay: forDisplay, verify: false)
        self.setParameters8To14(config, dryRun: dryRun, forDisplay: forDisplay)
        switch config.task {
        case ViewControllerReference.shared.backup, ViewControllerReference.shared.combined:
            self.argumentsforsynchronize(dryRun: dryRun, forDisplay: forDisplay)
        case ViewControllerReference.shared.snapshot:
            self.linkdestparameter(config, verify: false)
            self.argumentsforsynchronizesnapshot(dryRun: dryRun, forDisplay: forDisplay)
        default:
            break
        }
        return self.arguments!
    }

    func argumentsRestore(_ config: Configuration, dryRun: Bool, forDisplay: Bool, tmprestore: Bool) -> [String] {
        guard config.task != "restore" else { return [] }
        self.localCatalog = config.localCatalog
        if config.snapshotnum != nil {
            self.remoteargssnapshot(config)
        } else {
            self.remoteargs(config)
        }
        self.setParameters1To6(config, dryRun: dryRun, forDisplay: forDisplay, verify: false)
        self.setParameters8To14(config, dryRun: dryRun, forDisplay: forDisplay)
        if tmprestore {
            self.argumentsforrestore(dryRun: dryRun, forDisplay: forDisplay, tmprestore: tmprestore)
        } else {
            self.argumentsforrestore(dryRun: dryRun, forDisplay: forDisplay, tmprestore: tmprestore)
        }
        return self.arguments!
    }

    func argumentsVerify(_ config: Configuration, forDisplay: Bool) -> [String] {
        guard config.task != "restore" else { return [] }
        self.localCatalog = config.localCatalog
        self.remoteargs(config)
        self.setParameters1To6(config, dryRun: true, forDisplay: forDisplay, verify: true)
        self.setParameters8To14(config, dryRun: true, forDisplay: forDisplay)
        switch config.task {
        case ViewControllerReference.shared.backup, ViewControllerReference.shared.combined:
            self.argumentsforsynchronize(dryRun: true, forDisplay: forDisplay)
        case ViewControllerReference.shared.snapshot:
            self.linkdestparameter(config, verify: true)
            self.argumentsforsynchronizesnapshot(dryRun: true, forDisplay: forDisplay)
        default:<commit_msg>cleanup<commit_after>//
//  RsyncParametersProcess.swift
//
//  Created by <NAME> on 08/02/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//
// swiftlint:disable type_body_length

import Foundation

final class RsyncParametersProcess {

    private var stats: Bool?
    private var arguments: [String]?
    var localCatalog: String?
    var offsiteCatalog: String?
    var offsiteUsername: String?
    var offsiteServer: String?
    var remoteargs: String?
    var linkdestparam: String?

    private func setParameters1To6(_ config: Configuration, dryRun: Bool, forDisplay: Bool, verify: Bool) {
        var parameter1: String?
        if verify {
            parameter1 = "--checksum"
        } else {
            parameter1 = config.parameter1
        }
        let parameter2: String = config.parameter2
        let parameter3: String = config.parameter3
        let parameter4: String = config.parameter4
        let offsiteServer: String = config.offsiteServer
        self.arguments!.append(parameter1 ?? "")
        if verify {
            if forDisplay {self.arguments!.append(" ")}
            self.arguments!.append("--recursive")
        }
        if forDisplay {self.arguments!.append(" ")}
        self.arguments!.append(parameter2)
        if forDisplay {self.arguments!.append(" ")}
        if offsiteServer.isEmpty  == false {
            if parameter3.isEmpty == false {
                self.arguments!.append(parameter3)
                if forDisplay {self.arguments!.append(" ")}
            }
        }
        self.arguments!.append(parameter4)
        if forDisplay {self.arguments!.append(" ")}
        if offsiteServer.isEmpty {
            // nothing
        } else {
            self.sshportparameter(config, forDisplay: forDisplay)
        }
    }

    private func sshportparameter(_ config: Configuration, forDisplay: Bool) {
        let parameter5: String = config.parameter5
        let parameter6: String = config.parameter6
        // -e
        self.arguments!.append(parameter5)
        if forDisplay {self.arguments!.append(" ")}
        if let sshport = config.sshport {
            // "ssh -p xxx"
            if forDisplay {self.arguments!.append(" \"")}
            self.arguments!.append("ssh -p " + String(sshport))
            if forDisplay {self.arguments!.append("\" ")}
        } else {
            // ssh
            self.arguments!.append(parameter6)
        }
        if forDisplay {self.arguments!.append(" ")}
    }

    // Compute user selected parameters parameter8 ... parameter14
    // Brute force, check every parameter, not special elegant, but it works

    private func setParameters8To14(_ config: Configuration, dryRun: Bool, forDisplay: Bool) {
        self.stats = false
        if config.parameter8 != nil {
            self.appendParameter(parameter: config.parameter8!, forDisplay: forDisplay)
        }
        if config.parameter9 != nil {
            self.appendParameter(parameter: config.parameter9!, forDisplay: forDisplay)
        }
        if config.parameter10 != nil {
            self.appendParameter(parameter: config.parameter10!, forDisplay: forDisplay)
        }
        if config.parameter11 != nil {
            self.appendParameter(parameter: config.parameter11!, forDisplay: forDisplay)
        }
        if config.parameter12 != nil {
            self.appendParameter(parameter: config.parameter12!, forDisplay: forDisplay)
        }
        if config.parameter13 != nil {
            self.appendParameter(parameter: config.parameter13!, forDisplay: forDisplay)
        }
        if config.parameter14 != nil {
            self.appendParameter(parameter: config.parameter14!, forDisplay: forDisplay)
        }
        // Append --stats parameter to collect info about run
        if dryRun {
            self.dryrunparameter(config, forDisplay: forDisplay)
        } else {
            if self.stats == false {
                self.appendParameter(parameter: "--stats", forDisplay: forDisplay)
            }
        }
    }

    private func setParameters8To14restore(_ config: Configuration, dryRun: Bool, forDisplay: Bool) {
        self.stats = false
        // Append --stats parameter to collect info about run
        if dryRun {
            self.dryrunparameter(config, forDisplay: forDisplay)
        } else {
            if self.stats == false {
                self.appendParameter(parameter: "--stats", forDisplay: forDisplay)
            }
        }
    }

    private func dryrunparameter(_ config: Configuration, forDisplay: Bool) {
        let dryrun: String = config.dryrun
        self.arguments!.append(dryrun)
        if forDisplay {self.arguments!.append(" ")}
        if self.stats! == false {
            self.arguments!.append("--stats")
            if forDisplay {self.arguments!.append(" ")}
        }
    }

    // Check userselected parameter and append it to arguments array passed to rsync or displayed
    // on screen.

    private func appendParameter (parameter: String, forDisplay: Bool) {
        if parameter.count > 1 {
            if parameter == "--stats" {
                self.stats = true
            }
            self.arguments!.append(parameter)
            if forDisplay {
                self.arguments!.append(" ")
            }
        }
    }

    /// Function for initialize arguments array. RsyncOSX computes four argumentstrings
    /// two arguments for dryrun, one for rsync and one for display
    /// two arguments for realrun, one for rsync and one for display
    /// which argument to compute is set in parameter to function
    /// - parameter config: structure (configuration) holding configuration for one task
    /// - parameter dryRun: true if compute dryrun arguments, false if compute arguments for real run
    /// - paramater forDisplay: true if for display, false if not
    /// - returns: Array of Strings
    func argumentsRsync(_ config: Configuration, dryRun: Bool, forDisplay: Bool) -> [String] {
        self.localCatalog = config.localCatalog
        self.remoteargs(config)
        self.setParameters1To6(config, dryRun: dryRun, forDisplay: forDisplay, verify: false)
        self.setParameters8To14(config, dryRun: dryRun, forDisplay: forDisplay)
        switch config.task {
        case ViewControllerReference.shared.backup, ViewControllerReference.shared.combined:
            self.argumentsforsynchronize(dryRun: dryRun, forDisplay: forDisplay)
        case ViewControllerReference.shared.snapshot:
            self.linkdestparameter(config, verify: false)
            self.argumentsforsynchronizesnapshot(dryRun: dryRun, forDisplay: forDisplay)
        default:
            break
        }
        return self.arguments!
    }

    func argumentsRestore(_ config: Configuration, dryRun: Bool, forDisplay: Bool, tmprestore: Bool) -> [String] {
        self.localCatalog = config.localCatalog
        if config.snapshotnum != nil {
            self.remoteargssnapshot(config)
        } else {
            self.remoteargs(config)
        }
        self.setParameters1To6(config, dryRun: dryRun, forDisplay: forDisplay, verify: false)
        self.setParameters8To14(config, dryRun: dryRun, forDisplay: forDisplay)
        if tmprestore {
            self.argumentsforrestore(dryRun: dryRun, forDisplay: forDisplay, tmprestore: tmprestore)
        } else {
            self.argumentsforrestore(dryRun: dryRun, forDisplay: forDisplay, tmprestore: tmprestore)
        }
        return self.arguments!
    }

    func argumentsVerify(_ config: Configuration, forDisplay: Bool) -> [String] {
        guard config.task != "restore" else { return [] }
        self.localCatalog = config.localCatalog
        self.remoteargs(config)
        self.setParameters1To6(config, dryRun: true, forDisplay: forDisplay, verify: true)
        self.setParameters8To14(config, dryRun: true, forDisplay: forDisplay)
        switch config.task {
        case ViewControllerReference.shared.backup, ViewControllerReference.shared.combined:
            self.argumentsforsynchronize(dryRun: true, forDisplay: forDisplay)
        case ViewControllerReference.shared.snapshot:
            self.linkdestparameter(config, verify: true)
            self.argumentsforsynchronizesnapshot(dryRun: true, forDisplay: forDisplay)
        default:
{'fraction_non_alphanumeric': 0.09547196933397221, 'fraction_numerical': 0.009822712026832775, 'mean_word_length': 2.3414048429057432, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1350202', 'n_tokens_mistral': 4437, 'n_tokens_neox': 4230, 'n_words': 1139}