name: Release CLI

on:
  push:
    tags:
      - v*

jobs:
  release:
    permissions:
      contents: write
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@master

      - name: Install latest rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable

      - name: Compile
        id: compile
        run: |
          OUTPUT_DIR="built/output"
          mkdir -p "$OUTPUT_DIR"
          echo "BUILT_ARCHIVES=$OUTPUT_DIR" >> $GITHUB_OUTPUT

          ARTIFACTS_FOLDER="${GITHUB_REPOSITORY#*/}_${GITHUB_REF_NAME}"
          mkdir $ARTIFACTS_FOLDER

          set -x

          make build_cli

          BIN=maccel
          ARCHIVE=$BIN-cli.tar.gz
          BIN_PATH=target/release/$BIN

          strip $BIN_PATH;

          cp -r $BIN_PATH LICENSE README.md $ARTIFACTS_FOLDER

          tar -caf $ARCHIVE $ARTIFACTS_FOLDER/*

          mv $ARCHIVE $OUTPUT_DIR

      - name: Name Release
        run: echo "RELEASE_NAME=${GITHUB_REPOSITORY#*/}-cli ${GITHUB_REF_NAME#v}" >> $GITHUB_ENV

      - name: Release
        uses: softprops/action-gh-release@v1
        if: startsWith(github.ref, 'refs/tags/')
        with:
          generate_release_notes: true
          name: ${{ env.RELEASE_NAME }}
          files: |
            ${{ steps.compile.outputs.BUILT_ARCHIVES }}/*
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
