trigger:
- main

pool:
  vmImage: ubuntu-latest

variables:
  phpVersion: '8.2'
  vmImageName: 'ubuntu-latest'
  environmentName: 'Numo-Short-Link'

stages:
- stage: Build
  displayName: Build stage
  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
          displayName: 'Use PHP version $(phpVersion)'
        
        - script: composer install --no-interaction --prefer-dist
          displayName: 'composer install'
        
        - task: Bash@3
          displayName: 'delete env and cache/config'
          inputs:
             targetType: 'inline'
             script: |
              rm $(Agent.BuildDirectory)/s/.env
              rm $(Agent.BuildDirectory)/s/bootstrap/cache/config.php
              rm $(Agent.BuildDirectory)/s/public/sitemap.xml
              rm $(Agent.BuildDirectory)/s/public/.htaccess
              rm -r $(Agent.BuildDirectory)/s/storage/*
          
        - task: ArchiveFiles@2
          displayName: 'Archive files'
          inputs:
            rootFolderOrFile: '$(Agent.BuildDirectory)/s'
            includeRootFolder: false
            archiveType: zip
            archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
            replaceExistingArchive: true

        - upload: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
          displayName: 'Upload package'
          artifact: drop
          

- stage: Deploy
  displayName: 'Deploy to cPanel'
  dependsOn: Build
  condition: succeeded()
  jobs:
    - deployment: DeploymentJob
      pool:
        vmImage: $(vmImageName)
      environment: $(environmentName)
      strategy:
        runOnce:
          deploy:
            steps:
              - script: |
                  sudo apt-get update
                  sudo apt-get install -y lftp
                  lftp --version
                  which lftp

                  # FTP settings
                  ftpServer="$(webftb)" # Replace with your FTP server
                  ftpUser="$(ftpUser)" # Replace with your FTP username
                  ftpPass="$(ftpPass)" # Replace with your FTP password
                  
                  # File to upload (your zip package)
                  localFilePath="$(Pipeline.Workspace)/drop/$(Build.BuildId).zip"
                  remoteDirectory="/" # Replace with the appropriate path on your server
                  
                  # Use lftp to upload the file via FTP
                  /usr/bin/lftp -u $ftpUser,$ftpPass ftp://$ftpServer <<EOF
                  set ftp:ssl-force true
                  set ftp:ssl-protect-data true
                  set ftp:ssl-auth TLS
                  set ssl:verify-certificate no  # Disable certificate verification
                  set net:timeout 60
                  put $localFilePath -o $remoteDirectory/$(Build.BuildId).zip
                  bye
                  EOF

                displayName: 'Upload files via FTP'
