
name: release
on:
  push:
    tags:
      - '*'
jobs:
  create-release:
    name: create-release
    runs-on: ubuntu-latest
    outputs:
      upload_url: ${{ steps.release.outputs.upload_url }}
      lnav_version_number: ${{ env.LNAV_VERSION_NUMBER }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
      - name: Make release notes
        run: make -C release release-NEWS.md
      - name: Get the release version from the tag
        shell: bash
        run: |
          # Apparently, this is the right way to get a tag name. Really?
          #
          # See: https://github.community/t5/GitHub-Actions/How-to-get-just-the-tag-name/m-p/32167/highlight/true#M1027
          echo "LNAV_VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
          echo "LNAV_VERSION_NUMBER=${GITHUB_REF#refs/tags/v}" >> $GITHUB_ENV
          echo "version is: ${{ env.LNAV_VERSION }}"
      - name: Create GitHub release
        id: release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ env.LNAV_VERSION }}
          release_name: ${{ env.LNAV_VERSION }}
          body_path: release/release-NEWS.md
          draft: true

  build-binaries:
    name: build-binaries
    needs: ['create-release']
    uses: ./.github/workflows/bins.yml
    secrets: inherit
    with:
      lnav_version_number: ${{ needs.create-release.outputs.lnav_version_number }}
      upload_url: ${{ needs.create-release.outputs.upload_url }}

  build-release:
    name: build-release
    needs: ['create-release']
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
      - name: Update apt
        run: sudo apt-get update
      - name: Install packages
        run: >-
          sudo apt-get install -y
          make
          automake
          autoconf
          g++
          libpcre2-dev
          libpcre3-dev
          libncurses-dev
          libsqlite3-dev
          libbz2-dev
          libcurl4-openssl-dev
          libreadline-dev
          zlib1g-dev
      - name: autogen
        run: ./autogen.sh
      - name: configure
        run: ./configure --disable-static
      - name: make dist
        run: >-
          make dist dist-bzip2 &&
          mv -n lnav-*.tar.gz lnav-${{ needs.create-release.outputs.lnav_version_number }}.tar.gz &&
          mv -n lnav-*.tar.bz2 lnav-${{ needs.create-release.outputs.lnav_version_number }}.tar.bz2
      - name: Upload tgz archive
        uses: actions/upload-release-asset@v1.0.2
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create-release.outputs.upload_url }}
          asset_path: lnav-${{ needs.create-release.outputs.lnav_version_number }}.tar.gz
          asset_name: lnav-${{ needs.create-release.outputs.lnav_version_number }}.tar.gz
          asset_content_type: application/octet-stream
      - name: Upload tbz2 archive
        uses: actions/upload-release-asset@v1.0.2
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create-release.outputs.upload_url }}
          asset_path: lnav-${{ needs.create-release.outputs.lnav_version_number }}.tar.bz2
          asset_name: lnav-${{ needs.create-release.outputs.lnav_version_number }}.tar.bz2
          asset_content_type: application/octet-stream
