name: abi-checker
on:
  workflow_dispatch:
    branches: [ master, stable* ]
    inputs:
      API_BASE_REF:
        description: 'Base revision for ABI compatibility check'
        required: true
        default: '3.6.0'
  pull_request:
    branches: [ master, stable* ]
  schedule:
    - cron: '30 4 * * SUN'

jobs:
  build:
    runs-on: ubuntu-latest
    name: "Run ABI checker on ubuntu-latest"
    steps:
      - name: "Check out pull request"
        if: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event_name == 'pull_request'  }}
        uses: suzuki-shunsuke/get-pr-action@v0.1.0
        id: pr

      - name: "Check out source"
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          ref: ${{steps.pr.outputs.merge_commit_sha}}

      - name: Restore abigail tools
        id: cache-abigail-tools-restore
        if: always() && steps.cache-abigail-tools-restore.outputs.cache-hit != 'true'
        uses: actions/cache/restore@v4
        with:
          key: cache-abigail-tools
          path: |
            deb

      - name: "Prepare abigail-tools"
        if: always() && steps.cache-abigail-tools-restore.outputs.cache-hit != 'true'
        run: |
          mkdir deb-build
          cd deb-build
          echo "deb-src http://archive.ubuntu.com/ubuntu/ plucky main restricted universe multiverse" | sudo tee /etc/apt/sources.list.d/plucky.list
          sudo apt-get update -q -y
          sudo apt-get build-dep -q -y libabigail
          apt source libabigail
          cd libabigail-2.6
          dpkg-buildpackage

      - name: "Prepare abigail-tools debs"
        if: always() && steps.cache-abigail-tools-restore.outputs.cache-hit != 'true'
        run: |
          mkdir deb
          find . -name "*.deb" -print -exec mv {} deb \;

      - name: Save abigail tools
        id: cache-abigail-tools-save
        if: always() && steps.cache-abigail-tools-restore.outputs.cache-hit != 'true'
        uses: actions/cache/save@v4
        with:
          key: cache-abigail-tools
          path: |
            deb

      - name:  "Install abigail-tools"
        continue-on-error: true
        run: |
          cd deb
          sudo dpkg -i *.deb

      - name: "Prepare environment"
        run: |
          sudo apt-get update -q -y
          sudo apt-get --fix-broken install -q -y
          sudo apt-get install -q -y devscripts equivs \
            clang \
            pylint \
            curl
          ./packaging/scripts/prepare_deb_freerdp-nightly.sh
          sudo mk-build-deps -i

      - name: "Prepare configuration"
        run: |
          mkdir -p abi-checker
          cp scripts/abi-diff.sh abi-checker/
          echo "GITHUB_BASE_REF=$GITHUB_BASE_REF"
          echo "GITHUB_HEAD_REF=$GITHUB_HEAD_REF"
          echo "API_BASE_REF=${{ inputs.API_BASE_REF || '3.6.0' }}"
          echo "HEAD=$(git rev-parse HEAD)"
          echo "remotes=$(git remote -v)"

      - name: "Run ABI check..."
        env:
          BASE_REF: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event_name == 'workflow_dispatch' && inputs.API_BASE_REF || '3.6.0' }}
        run: |
          echo "BASE_REF=$BASE_REF"
          ./abi-checker/abi-diff.sh $BASE_REF
