variables:
  # Web app name
  webAppName: 'MuE-Website'
  # Agent VM image name
  vmImageName: 'ubuntu-latest'
  # Root folder under which your composer.json file is available.
  rootFolder: $(System.DefaultWorkingDirectory)

stages:
- stage: Build
  displayName: Build stage
  variables:
    phpVersion: '8.1'
  jobs:
  - job: BuildJob
    pool:
      vmImage: $(vmImageName)
    steps:
    - script: |
        sudo update-alternatives --set php /usr/bin/php$(phpVersion)
        sudo update-alternatives --set phar /usr/bin/phar$(phpVersion)
        sudo update-alternatives --set phpdbg /usr/bin/phpdbg$(phpVersion)
        sudo update-alternatives --set php-cgi /usr/bin/php-cgi$(phpVersion)
        sudo update-alternatives --set phar.phar /usr/bin/phar.phar$(phpVersion)
        php -version
      workingDirectory: $(rootFolder)
      displayName: 'Use PHP version $(phpVersion)'

    - script: |
        git config --global user.email "pipeline@mobene.de"
        git config --global user.name "Pipeline"
        git commit -m"(Pipeline) File- and Templatemerge"
        git push
      enabled: false
      displayName: 'Push into GIT'

    - task: DownloadPipelineArtifact@2
      displayName: 'Download MobeneGroupBase'
      inputs:
        buildType: 'specific'
        project: 'f15f4d9e-0130-4adc-95a8-3a03b296e8f1'
        definition: '27'
        buildVersionToDownload: 'latest'
        artifactName: 'Base'
        targetPath: '$(Pipeline.Workspace)/MobeneGroupBase-Bundle'
        itemPattern: '**'
        allowPartiallySucceededBuilds: true   
   
    - task: DownloadPipelineArtifact@2
      displayName: 'Download Zerberus'
      inputs:
        buildType: 'specific'
        project: 'f15f4d9e-0130-4adc-95a8-3a03b296e8f1'
        definition: '22'
        buildVersionToDownload: 'latest'
        artifactName: 'Zerberus'
        targetPath: '$(Pipeline.Workspace)/Sub-Zerberus'
        itemPattern: '**'
        allowPartiallySucceededBuilds: true
    - task: DownloadPipelineArtifact@2
      condition: ne(variables.usePriceCalculator, false)
      displayName: 'Download Preisrechner'
      inputs:
        buildType: 'specific'
        project: 'f15f4d9e-0130-4adc-95a8-3a03b296e8f1'
        definition: '24'
        artifactName: 'Pricecalculator'
        buildVersionToDownload: 'latest'
        targetPath: '$(Pipeline.Workspace)/Sub-Preisrechner'
        itemPattern: '**'
        allowPartiallySucceededBuilds: true
    - task: DownloadPipelineArtifact@2
      condition: ne(variables.usePriceGraph, false)
      displayName: 'Download Preisgraph'
      inputs:
        buildType: 'specific'
        project: 'f15f4d9e-0130-4adc-95a8-3a03b296e8f1'
        definition: '25'
        buildVersionToDownload: 'latest'
        artifactName: 'Pricegraph'
        targetPath: '$(Pipeline.Workspace)/Sub-Preisgraph'
        itemPattern: '**'
        allowPartiallySucceededBuilds: true
    - task: DownloadPipelineArtifact@2
      condition: ne(variables.usePriceAlert, false)
      displayName: 'Download Preisalarm/Preismail'
      inputs:
        buildType: 'specific'
        project: 'f15f4d9e-0130-4adc-95a8-3a03b296e8f1'
        definition: '26'
        buildVersionToDownload: 'latest'
        artifactName: 'Pricealert'
        targetPath: '$(Pipeline.Workspace)/Sub-Preisalarm'
        itemPattern: '**'
        allowPartiallySucceededBuilds: true
    - task: DownloadPipelineArtifact@2
      condition: ne(variables.useContentMigration, false)
      displayName: 'Download Content-Migration'
      inputs:
        buildType: 'specific'
        project: 'f15f4d9e-0130-4adc-95a8-3a03b296e8f1'
        definition: '52'
        buildVersionToDownload: 'latest'
        artifactName: 'ContentMigration'
        targetPath: '$(Pipeline.Workspace)/Sub-ContentMigration'
        itemPattern: '**'
        allowPartiallySucceededBuilds: true
    - task: ExtractFiles@1
      displayName: 'Entpacke MobeneGroupBase'
      inputs:
        archiveFilePatterns: '$(Pipeline.Workspace)/MobeneGroupBase-Bundle/*.zip'
        cleanDestinationFolder: false
        overwriteExistingFiles: true
        destinationFolder: './packages/mobenegroup/MobeneGroupBase-Bundle'
    - task: ExtractFiles@1
      displayName: 'Entpacke Zerberus'
      inputs:
        archiveFilePatterns: '$(Pipeline.Workspace)/Sub-Zerberus/*.zip'
        cleanDestinationFolder: false
        overwriteExistingFiles: true
        destinationFolder: './packages/mobenegroup/Sub-Zerberus.php'
    - task: ExtractFiles@1
      displayName: 'Entpacke Preisrechner'
      condition: ne(variables.usePriceCalculator, false)
      inputs:
        archiveFilePatterns: '$(Pipeline.Workspace)/Sub-Preisrechner/*.zip'
        cleanDestinationFolder: false
        overwriteExistingFiles: true
        destinationFolder: './packages/mobenegroup/Sub-Preisrechner'
    - task: ExtractFiles@1
      displayName: 'Entpacke Preisgraph'
      condition: ne(variables.usePriceGraph, false)
      inputs:
        archiveFilePatterns: '$(Pipeline.Workspace)/Sub-Preisgraph/*.zip'
        cleanDestinationFolder: false
        overwriteExistingFiles: true
        destinationFolder: './packages/mobenegroup/Sub-Preisgraph'
    - task: ExtractFiles@1
      displayName: 'Entpacke Preisalarm'
      condition: ne(variables.usePriceAlert, false)
      inputs:
        archiveFilePatterns: '$(Pipeline.Workspace)/Sub-Preisalarm/*.zip'
        cleanDestinationFolder: false
        overwriteExistingFiles: true
        destinationFolder: './packages/mobenegroup/Sub-Preisalarm'  
    - task: ExtractFiles@1
      displayName: 'Entpacke Content-Migration'
      condition: ne(variables.useContentMigration, false)
      inputs:
        archiveFilePatterns: '$(Pipeline.Workspace)/Sub-ContentMigration/*.zip'
        cleanDestinationFolder: false
        overwriteExistingFiles: true
        destinationFolder: './packages/mobenegroup/Sub-ContentMigration'  
        
    - script: |
        pwd
        echo './packages/mobenegroup/'
        ls -ltra ./packages/mobenegroup/
      displayName: 'Information'
      workingDirectory: $(rootFolder)

    - script: |
        pwd
        echo 'rm -rf composer.lock'
        rm -rf composer.lock
        echo 'rm -rf vendor'
        rm -rf vendor
        echo 'composer config repositories.mobene {"type": "path", "url": "./packages/mobenegroup/*"}'
        composer config repositories.mobene '{"type": "path", "url": "./packages/mobenegroup/*"}'
      displayName: 'Composer config'
      workingDirectory: $(rootFolder)

    - script: |
        echo 'composer install'
        composer install
      displayName: 'Composer install'
      workingDirectory: $(rootFolder)

    - script: |
        echo 'composer require mobenegroup/pricecalculatorbundle:$(usePriceCalculator)'
        composer require mobenegroup/pricecalculatorbundle:$(usePriceCalculator)
      displayName: 'Composer require mobenegroup/pricecalculatorbundle'
      condition: ne(variables.usePriceCalculator, false)
      workingDirectory: $(rootFolder)

    - script: |
        echo 'composer require mobenegroup/pricegraphbundle:$(usePriceGraph)'
        composer require mobenegroup/pricegraphbundle:$(usePriceGraph)
      displayName: 'Composer require mobenegroup/pricegraphbundle'
      condition: ne(variables.usePriceGraph, false)
      workingDirectory: $(rootFolder)

    - script: |
        echo 'composer require mobenegroup/pricealertbundle:$(usePriceAlert)'
        composer require mobenegroup/pricealertbundle:$(usePriceAlert)
      displayName: 'Composer require mobenegroup/pricealertbundle'
      condition: ne(variables.usePriceAlert, false)
      workingDirectory: $(rootFolder)
    - script: |
        echo 'composer require mobenegroup/content_migration:$(useContentMigration)'
        composer require mobenegroup/content_migration:$(useContentMigration)
      displayName: 'Composer require mobenegroup/content_migration'
      condition: ne(variables.useContentMigration, false)
      workingDirectory: $(rootFolder)

    - script: |
        cp ./packages/version.php ./public
        cp ./packages/contao-manager.phar.php ./public
      displayName: 'Copy version.php and contao-manager.phar.php into web folder'
      workingDirectory: $(rootFolder)
    
    - script: |
        echo 'ls -ltra'
        ls -ltra
        echo 'ls -ltra'
        ls -ltra ./public

        versionPreisrechner=$(sed -n '/version/s/^[^0-9]\+\([^,]\+\).$/\1/p' ./packages/mobenegroup/Sub-Preisrechner/composer.json)
        echo "versionPreisrechner: ${versionPreisrechner%?}"

        versionPreisgraph=$(sed -n '/version/s/^[^0-9]\+\([^,]\+\).$/\1/p' ./packages/mobenegroup/Sub-Preisgraph/composer.json)
        echo "versionPreisgraph: ${versionPreisgraph%?}"

        versionPreisalarm=$(sed -n '/version/s/^[^0-9]\+\([^,]\+\).$/\1/p' ./packages/mobenegroup/Sub-Preisalarm/composer.json)
        echo "versionPreisalarm: ${versionPreisalarm%?}"

        versionContentMigration=$(sed -n '/version/s/^[^0-9]\+\([^,]\+\).$/\1/p' ./packages/mobenegroup/Sub-ContentMigration/composer.json)
        echo "versionContentMigration: ${versionContentMigration%?}"

        echo "<br><table style=\"width: 50%; font-size: 12px;\"><tr><td>Webseite</td><td>$(targetVersion)</td></tr><tr><td>mobenegroup/pricecalculatorbundle</td><td>${versionPreisrechner%?}</td></tr><tr><td>mobenegroup/pricegraphbundle</td><td> ${versionPreisgraph%?}</td></tr><tr><td>mobenegroup/pricealertbundle</td><td>${versionPreisalarm%?}</td></tr><tr><td>mobenegroup/content_migration</td><td>${versionContentMigration%?}</td></tr></table>" >> version
        mv version ./public
      displayName: 'Write version into version file'
      workingDirectory: $(rootFolder)

    - script: |
        rm -rf .git
        rm -rf var/cache/*
      displayName: 'Clear folders'
      workingDirectory: $(rootFolder)
    - task: ArchiveFiles@2
      displayName: 'Archive files'
      inputs:
        rootFolderOrFile: '$(rootFolder)'
        includeRootFolder: false
        archiveType: 'tar'
        archiveFile: $(Build.ArtifactStagingDirectory)/$(targetVersion).tar.gz
        replaceExistingArchive: true

    - publish: $(Build.ArtifactStagingDirectory)/$(targetVersion).tar.gz
      displayName: 'Save website for Release-Pipeline'
      artifact: MuE-Website