/* groovylint-disable CompileStatic, DuplicateMapLiteral, DuplicateNumberLiteral, DuplicateStringLiteral, NestedBlockDepth */
pipeline {
    agent any
    parameters {
        string(
            name: 'LTS',
            description: """
                Code name of the LTS branch (e.g. jabbah, kuma, luna, maia, ...)
                or numeric version (e.g. 2022.10, 2023.05, 2023.10, 2024.05 ...).
                The new version is published to BOTH matching repositories:
                e.g. 'maia' publishes to xivo-maia and xivo-2024.05-latest.
            """
        )
        string(name: 'SNAPSHOT', description: 'The aptly snapshot of the new asterisk version', trim: true)
    }
    stages {
        stage('Load Functions') {
            steps {
                script {
                    sshPut remote: [
                        name: 'aptly',
                        host: '192.168.225.14',
                        user: 'aptly',
                        password: 'superpass',
                        allowAnyHosts: true
                    ],
                    from: 'scripts/aptly_functions.sh',
                    into: '/tmp/aptly_functions.sh'

                    sshPut remote: [
                        name: 'aptly',
                        host: '192.168.225.14',
                        user: 'aptly',
                        password: 'superpass',
                        allowAnyHosts: true
                    ],
                    from: 'bin/lib/xivo-version.sh',
                    into: '/tmp/xivo-version.sh'
                }
            }
        }
        stage('Resolve targets') {
            steps {
                script {
                    String ltsInput = params.LTS

                    String command = """
                        source /tmp/xivo-version.sh
                        source /tmp/aptly_functions.sh
                        get_lts_targets "${ltsInput}"
                    """
                    String output = sshCommand remote: [
                        name: 'aptly',
                        host: '192.168.225.14',
                        user: 'aptly',
                        password: 'superpass',
                        allowAnyHosts: true
                    ],
                    command: command

                    env.TARGETS = output.trim()
                    echo "Publishing '${ltsInput}' to repositories: ${env.TARGETS}"
                }
            }
        }
        stage('display-probable-outcome') {
            steps {
                script {
                    String sourceSnapshot = params.SNAPSHOT
                    String[] targets = env.TARGETS.split(' ')
                    for (int i = 0; i < targets.length; i++) {
                        String ltsCodename = targets[i]

                        String command = """
                            source /tmp/aptly_functions.sh
                            pseudo_dry_run "${ltsCodename}" "${sourceSnapshot}"
                        """
                        sshCommand remote: [
                            name: 'aptly',
                            host: '192.168.225.14',
                            user: 'aptly',
                            password: 'superpass',
                            allowAnyHosts: true
                        ],
                        command: command
                    }
                }
            }
        }
        stage('move-asterisk-to-oldstable') {
            input {
                message "Are you ok with what is expected to be done (see log of the step before) ?"
                ok "Ready"
            }
            steps {
                script {
                    String[] targets = env.TARGETS.split(' ')
                    for (int i = 0; i < targets.length; i++) {
                        String ltsCodename = targets[i]

                        String command = """
                            source /tmp/aptly_functions.sh
                            move_asterisk_to_oldstable "${ltsCodename}"
                        """
                        sshCommand remote: [
                            name: 'aptly',
                            host: '192.168.225.14',
                            user: 'aptly',
                            password: 'superpass',
                            allowAnyHosts: true
                        ],
                        command: command
                    }
                }
            }
        }
        stage('update-lts-from-candidate') {
            steps {
                script {
                    String[] targets = env.TARGETS.split(' ')
                    for (int i = 0; i < targets.length; i++) {
                        String ltsCodename = targets[i]

                        String command = """
                            source /tmp/aptly_functions.sh
                            update_lts_from_candidate "${ltsCodename}"
                        """
                        sshCommand remote: [
                            name: 'aptly',
                            host: '192.168.225.14',
                            user: 'aptly',
                            password: 'superpass',
                            allowAnyHosts: true
                        ],
                        command: command
                    }
                }
            }
        }
        stage('update-candidate-from-snapshot') {
            steps {
                script {
                    String sourceSnapshot = params.SNAPSHOT
                    String[] targets = env.TARGETS.split(' ')
                    for (int i = 0; i < targets.length; i++) {
                        String ltsCodename = targets[i]

                        String command = """
                            source /tmp/aptly_functions.sh
                            update_candidate_from_snapshot "${ltsCodename}" "${sourceSnapshot}"
                        """
                        sshCommand remote: [
                            name: 'aptly',
                            host: '192.168.225.14',
                            user: 'aptly',
                            password: 'superpass',
                            allowAnyHosts: true
                        ],
                        command: command
                    }
                }
            }
        }
        stage('Cleanup script file') {
            steps {
                script {
                    sshCommand remote: [
                        name: 'aptly',
                        host: '192.168.225.14',
                        user: 'aptly',
                        password: 'superpass',
                        allowAnyHosts: true
                    ],
                    command: 'rm /tmp/aptly_functions.sh /tmp/xivo-version.sh'
                }
            }
        }
    }
}
