on:
  push:
    tags:
      - "v*"

jobs:
  prep:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Get Version
        id: get_version
        run: |
          version_git=$(echo "$GITHUB_REF" | sed -e 's/refs\/tags\///')
          version_cargo="v$(cargo read-manifest | jq -r '.version')"
          if [ "$version_git" != "$version_cargo" ]; then
              echo "git != cargo: $version_git != $version_cargo" >&2
              exit 1
          fi
          echo "git_version=$version_git" >> $GITHUB_OUTPUT
      - name: Create release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: Release ${{ github.ref }}
          draft: true
          prerelease: false
    outputs:
      upload_url: ${{ steps.create_release.outputs.upload_url }}
      git_version: ${{ steps.get_version.outputs.git_version }}

  build-musl:
    needs: prep
    runs-on: ubuntu-latest
    container: alpine:3.16
    steps:
      - name: Prepare
        run: |
          apk add git ca-certificates rust cargo clang-dev acl-static musl-dev linux-headers
          wget $(sed -ne '/community$/{;s/v3[.]16/v3.17/;p;q;}' /etc/apk/repositories)/x86_64/pandoc-2.19.2-r0.apk
          apk add make pandoc-2.19.2-r0.apk
      - uses: actions/checkout@v2
      - name: Build
        run: |
          RUSTC=$(pwd)/contrib/musl-static-build/rustc-wrapper cargo build --release
          make -C man
      - name: Build tarball
        run: |
          ln -f target/release/laurel .
          tar -czf laurel.tar.gz laurel *.md man/
      - name: Upload Release Asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.prep.outputs.upload_url }}
          asset_path: laurel.tar.gz
          asset_name: laurel-${{ needs.prep.outputs.git_version }}-x86_64-musl.tar.gz
          asset_content_type: application/tar

  build-glibc:
    needs: prep
    runs-on: ubuntu-latest
    container: centos:7
    steps:
      - name: Prepare
        run: |
          yum -q -y update
          yum -q -y install centos-release-scl
          yum -q -y install gcc llvm-toolset-7-clang file libacl-devel
          yum -q -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
          yum -q -y install make pandoc
      - name: Install Rust toolchain (stable)
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
      - uses: actions/checkout@v2
      - name: Build
        run: |
          scl enable llvm-toolset-7 "cargo build --release"
          make -C man
      - name: Determine minimum GNU libc version
        run: |
          objdump -t target/release/laurel | sed -ne '/@GLIBC_/{s/^.*@GLIBC_//;p;}' | sort -uV
          min_glibc_version=$(objdump -t target/release/laurel \
              | sed -ne '/@GLIBC_/{s/^.*@GLIBC_//;p;}' \
              | sort -uV \
              | tail -n1)
          echo Minimum GNU libc version: $min_glibc_version
      - name: Build tarball
        run: |
          ln -f target/release/laurel .
          tar -czf laurel.tar.gz laurel *.md man/
      - name: Upload Release Asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.prep.outputs.upload_url }}
          asset_path: laurel.tar.gz
          asset_name: laurel-${{ needs.prep.outputs.git_version }}-x86_64-glibc.tar.gz
          asset_content_type: application/tar
