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('Configuration') {
            steps {
                dir('codiki-properties') {
                    git url:'https://gogs.takiguchi.ovh/florian/codiki-properties.git', branch: 'master', credentialsId: 'a6494064-8130-42fd-9d3d-e7734518c79e'
                }
                script {
                    sh 'cp ./codiki-properties/application-integ.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 -
                            
                        echo "Documentation générée avec succès."
                    '''
                }
            }
        }

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

                        cd $WORKSPACE/src/main/ts && npm install && ng build --configuration=integ && 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-integ]',
                                        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-integ/codiki.jar /opt/codiki-integ/bin/codiki.jar
                                                            sudo /opt/codiki-integ/bin/codiki-jenkins.sh
                                                            sudo service codiki-integ restart
                                                        ''',
                                                        execTimeout: 120000,
                                                        flatten: false,
                                                        makeEmptyDirs: false,
                                                        noDefaultExcludes: false,
                                                        patternSeparator: '[, ]+',
                                                        remoteDirectory: '',
                                                        remoteDirectorySDF: false,
                                                        removePrefix: '',
                                                        sourceFiles: ''
                                                )
                                        ],
                                        usePromotionTimestamp: false,
                                        useWorkspaceInPromotion: false,
                                        verbose: false
                                )
                        ]
                )
            }
        }
    }
}