pipeline {
    agent any

    tools {
        maven 'Maven-3.5.0'
        jdk 'OpenJdk-11.0.2'
    }

    environment {
        JAVA_HOME = '/opt/jdks/jdk-11.0.2'
    }

    stages {
        stage('Cleaning workspace') {
            steps {
                script {
                    sh "rm -R ./*"
                    git url: 'ssh://gogs@gogs.takiguchi.ovh:54910/florian/codiki.git'
                }
            }
        }

        stage('Configuration') {
            steps {
                dir('codiki-properties') {
                    git url: 'ssh://gogs@gogs.takiguchi.ovh:54910/florian/codiki-properties.git'
                }
                script {

                    sh """
                        cp ./codiki-properties/application-prod.yml ./src/main/resources/application.yml
                    """
                }
            }
        }

        stage('Documentation generation') {
            steps {
                script {
                    sh """
                        echo 'Génération de la documentation d'utilisation...'
                        
                        docPath='../src/main/ts/src/assets/doc/'
                        cd \$WORKSPACE/doc/ && unoconv codiki_user_manual.odt
                            && mkdir -p \$docPath
                            && mv codiki_user_manual.pdf \$docPath
                            && cd -
                        if [ \$? -eq 0 ]
                        then                        
                            echo 'Documentation générée avec succès.'
                        else
                            echo 'La génération de la documentation s'est terminée en erreur
                        fi
                    """
                }
            }
        }

        stage('Angular build') {
            steps {
                script {
                    sh """
                        echo "Construction de l'application Angular..."

                        cd \$WORKSPACE/src/main/ts && npm install && ng build --configuration=production && cd -
                        
                        echo "Application générée avec succès."
                    """
                }
            }
        }

        stage('SpringBoot build') {
            steps {
                sh 'mvn clean package'
            }
        }

        stage('Publish and restart') {
            steps {
                script {
                    sh 'mv target/codiki*.jar target/codiki.jar'
                }
                sshPublisher(
                        publishers: [
                                sshPublisherDesc(
                                        configName: 'DebianServer [codiki]',
                                        transfers: [
                                                sshTransfer(
                                                        cleanRemote: false,
                                                        excludes: '',
                                                        execCommand: '',
                                                        execTimeout: 120000,
                                                        flatten: false,
                                                        makeEmptyDirs: false,
                                                        noDefaultExcludes: false,
                                                        patternSeparator: '[, ]+',
                                                        remoteDirectory: '',
                                                        remoteDirectorySDF: false,
                                                        removePrefix: 'target/',
                                                        sourceFiles: 'target/codiki.jar'
                                                ),
                                                sshTransfer(
                                                        cleanRemote: false,
                                                        excludes: '',
                                                        execCommand: """
                                                            mv /opt/codiki-2/codiki.jar /opt/codiki-2/bin/codiki.jar
                                                            sudo /opt/codiki-2/bin/codiki-jenkins.sh
                                                            sudo service codiki-2 restart
                                                        """,
                                                        execTimeout: 120000,
                                                        flatten: false,
                                                        makeEmptyDirs: false,
                                                        noDefaultExcludes: false,
                                                        patternSeparator: '[, ]+',
                                                        remoteDirectory: '',
                                                        remoteDirectorySDF: false,
                                                        removePrefix: '',
                                                        sourceFiles: ''
                                                )
                                        ],
                                        usePromotionTimestamp: false,
                                        useWorkspaceInPromotion: false,
                                        verbose: false
                                )
                        ]
                )
            }
        }
    }
}