name: Release

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:

permissions:
  contents: write
  packages: write

jobs:

  build-binaries:
    strategy:
      matrix:
        pg_version: [15, 16, 17, 18]
    runs-on: ubuntu-latest
    env:
      PG_CONFIG: /usr/lib/postgresql/${{ matrix.pg_version }}/bin/pg_config


    steps:
    - uses: actions/checkout@v3
      with:
        submodules: true
        lfs: false

    - name: Install dependencies
      run: |
        sudo apt-get update
        sudo apt-get install -y build-essential libprotobuf-c-dev protobuf-c-compiler

    - name: Setup PostgreSQL ${{ matrix.pg_version }}
      run: |
        sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
        wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
        sudo apt-get update
        sudo apt-get install -y postgresql-${{ matrix.pg_version }} postgresql-server-dev-${{ matrix.pg_version }}

    - name: Build
      run: |
        make clean
        make

    - name: Package Binary
      run: |
        mkdir -p dist
        cp pgproto.so dist/
        cp sql/pgproto--1.0.sql dist/
        cp pgproto.control dist/
        zip -r pgproto-pg${{ matrix.pg_version }}-linux-x86_64.zip dist/

    - name: Upload Release Asset
      if: startsWith(github.ref, 'refs/tags/')
      uses: softprops/action-gh-release@v1
      with:
        files: pgproto-pg${{ matrix.pg_version }}-linux-x86_64.zip
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  source-distribution:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: true

    - name: Set Version
      run: |
        VERSION=$(grep '"version":' META.json | head -n 1 | cut -d '"' -f 4)
        echo "VERSION=$VERSION" >> $GITHUB_ENV

    - name: Package Source Bundle for PGXN
      run: |
        mkdir -p dist/pgproto-${{ env.VERSION }}
        # Copy source files
        cp -R src/ dist/pgproto-${{ env.VERSION }}/
        cp -R sql/ dist/pgproto-${{ env.VERSION }}/
        cp Makefile dist/pgproto-${{ env.VERSION }}/
        cp pgproto.control dist/pgproto-${{ env.VERSION }}/
        cp META.json dist/pgproto-${{ env.VERSION }}/
        cp LICENSE dist/pgproto-${{ env.VERSION }}/
        cp README.md dist/pgproto-${{ env.VERSION }}/
        cp -R third_party/ dist/pgproto-${{ env.VERSION }}/
        # Zip it
        cd dist && zip -r ../pgproto-${{ env.VERSION }}.zip pgproto-${{ env.VERSION }}/

    - name: Upload Source Release Asset
      if: startsWith(github.ref, 'refs/tags/')
      uses: softprops/action-gh-release@v1
      with:
        files: pgproto-${{ env.VERSION }}.zip
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    - name: Upload Source Bundle Artifact
      uses: actions/upload-artifact@v4
      with:
        name: pgxn-bundle
        path: pgproto-${{ env.VERSION }}.zip

  test-pgxn-install:
    runs-on: ubuntu-latest
    needs: source-distribution
    steps:
    - uses: actions/checkout@v3
    - name: Set Version
      run: |
        VERSION=$(grep '"version":' META.json | head -n 1 | cut -d '"' -f 4)
        echo "VERSION=$VERSION" >> $GITHUB_ENV
    - name: Download Source Bundle
      uses: actions/download-artifact@v4
      with:
        name: pgxn-bundle
        path: .
    - name: Install Dependencies
      run: |
        sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
        wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
        sudo apt-get update
        sudo apt-get install -y postgresql-server-dev-18 libprotobuf-c-dev protobuf-c-compiler python3-pip
        sudo pip3 install pgxnclient
    - name: List files
      run: ls -la
    - name: Test PGXN Install
      run: |
        sudo pgxn install ./pgproto-${{ env.VERSION }}.zip

  publish-docker:
    runs-on: ubuntu-latest
    needs: build-binaries
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: true
    - name: Log in to GHCR
      uses: docker/login-action@v2
      with:
        registry: ghcr.io
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}
    - name: Lowercase repo name
      run: |
        echo "REPO_LOWER=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
    - name: Build and push Docker image
      if: startsWith(github.ref, 'refs/tags/')
      uses: docker/build-push-action@v4
      with:
        context: .
        file: example/Dockerfile
        push: true
        tags: |
          ghcr.io/${{ env.REPO_LOWER }}:latest
          ghcr.io/${{ env.REPO_LOWER }}:${{ github.ref_name }}

  build-windows:
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: true
    - name: Setup MSYS2
      uses: msys2/setup-msys2@v2
      with:
        msystem: MINGW64
        install: |
          make
          zip
          mingw-w64-x86_64-gcc
          mingw-w64-x86_64-protobuf-c
          mingw-w64-x86_64-postgresql
    - name: Build
      run: |
        make
      shell: msys2 {0}
    - name: Package Binary
      run: |
        mkdir -p dist
        cp pgproto.dll dist/ || cp pgproto.so dist/
        cp sql/pgproto--1.0.sql dist/
        cp pgproto.control dist/
        PG_VERSION=$(pg_config --version | cut -d ' ' -f 2 | cut -d '.' -f 1)
        zip -r pgproto-pg$PG_VERSION-windows-x86_64.zip dist/
      shell: msys2 {0}
    - name: Upload Release Asset
      if: startsWith(github.ref, 'refs/tags/')
      uses: softprops/action-gh-release@v1
      with:
        files: pgproto-pg*-windows-x86_64.zip
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
