/* 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 ...)'
            """
        )
        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'
                }
            }
        }
        stage('display-probable-outcome') {
            steps {
                script {
                    String ltsCodename = params.LTS
                    String sourceSnapshot = params.SNAPSHOT

                    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 ltsCodename = params.LTS

                    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 ltsCodename = params.LTS

                    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 ltsCodename = params.LTS
                    String sourceSnapshot = params.SNAPSHOT

                    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'
                }
            }
        }
    }
}
