def call(Map config = [:]) {
    def packageName = config.get('name', 'web')
    def phpVersion = config.get('phpVersion', '7.4')
    def dockerImage = "jenkins/build-web:${phpVersion}"
    def packageType = "laravel"

    pipeline {
        agent any

        stages {
            stage('Prepare docker') {
                steps {
                    script {
                        writeFile file: 'Dockerfile', text: libraryResource("dockerfiles/web/${phpVersion}/Dockerfile")
                        docker.build(dockerImage, "--pull=false .")
                    }
                }
            }

            stage('Checkout') {
                steps {
                    cleanWs()
                    checkout scm
                }
            }

            stage('Dependencies') {
                parallel {
                    stage('Composer') {
                        steps {
                            script {
                                docker.image(dockerImage).inside("-u jenkins --mount type=bind,source=${JENKINS_SSH_PATH},target=/home/jenkins/.ssh -w /app") {
                                    dir('src') {
                                        sh 'composer install --no-interaction --prefer-dist --no-dev -v'
                                    }
                                }
                            }
                        }
                    }

                    stage('Node') {
                        steps {
                            script {
                                docker.image(dockerImage).inside('-u jenkins -w /app') {
                                    dir('src') {
                                        sh 'npm install'
                                    }
                                }
                            }
                        }
                    }
                }
            }

            stage('Build') {
                steps {
                    script {
                        docker.image(dockerImage).inside('-u jenkins -w /app') {
                            dir('src') {
                                sh 'npm run prod'
                            }
                        }
                    }
                }
            }

            stage('Packaging') {
                steps {
                    script {
                        if (env.TAG_NAME) {
                            env['packageVersion'] = sh(returnStdout: true, script: "echo ${TAG_NAME}-${BUILD_NUMBER}").trim()
                        } else {
                            env['packageVersion'] = sh(returnStdout: true, script: "echo ${BUILD_NUMBER}").trim()
                        }

                        dir('src') {
                            sh """
                                mkdir -p ../fpm/var/www/${packageName}
                                rsync -a . ../fpm/var/www/${packageName} \
                                  --exclude=".env" \
                                  --exclude=".env.testing" \
                                  --exclude=".env.exemple" \
                                  --exclude="conf.php" \
                                  --exclude="*test*" \
                                  --exclude="vendor/phpunit" \
                                  --exclude="phpunit.xml" \
                                  --exclude="package-lock.json" \
                                  --exclude="composer.lock"
                            """
                        }

                        dir('fpm') {
                            sh """
                                fpm --deb-no-default-config-files -s dir -t deb -a all -m technique@abpcode.fr \
                                  -n ${packageType}-${packageName} \
                                  -v ${packageVersion} \
                                  --url ${GIT_URL} \
                                  -p ../${packageType}-${packageName}-${packageVersion}.deb ./
                            """
                        }
                    }
                }
            }

            stage('Publish') {
                steps {
                    script {
                        archiveArtifacts artifacts: "${packageType}-${packageName}-${packageVersion}.deb", fingerprint: true

                        if (env.TAG_NAME) {
                            def debFile = sh(
                                script: "ls -1 ${packageType}-${packageName}-${packageVersion}.deb",
                                returnStdout: true
                            ).trim()

                            sh """
                                scp -P 2224 -i /var/jenkins_home/.ssh/id_rsa -o StrictHostKeyChecking=no \
                                    ${debFile} jenkins@repo.abpcode.fr:/repo/pool/.tmp.deb
                                ssh -p 2224 -i /var/jenkins_home/.ssh/id_rsa -o StrictHostKeyChecking=no \
                                    jenkins@repo.abpcode.fr "mv /repo/pool/.tmp.deb /repo/pool/${debFile}"
                            """
                        }
                    }
                }
            }
        }
    }
}
