name: Release

on:
  push:
    tags: ["*"]

env:
  PROJECT_NAME: rnr
  CARGO_TERM_COLOR: always
  ARTIFACT_NAME: artifacts

jobs:
  build-artifacts:
    name: Build a release artifacts
    runs-on: ${{ matrix.build.os }}
    strategy:
      fail-fast: false
      matrix:
        build:
          # Linux
          - os: "ubuntu-latest"
            target: "x86_64-unknown-linux-gnu"
            cross: false
          - os: "ubuntu-latest"
            target: "x86_64-unknown-linux-musl"
            cross: true
          - os: "ubuntu-latest"
            target: "aarch64-unknown-linux-gnu"
            cross: true
          - os: "ubuntu-latest"
            target: "armv7-unknown-linux-gnueabihf"
            cross: true
          # MacOS
          - os: "macos-latest"
            target: "x86_64-apple-darwin"
            cross: false
          # Windows
          - os: "windows-latest"
            target: "x86_64-pc-windows-msvc"
            cross: false
          - os: "windows-latest"
            target: "x86_64-pc-windows-gnu"
            cross: false

    env:
      BUILD_DIR: "build"
      TARGET: ${{ matrix.build.target }}
      USE_CROSS: ${{ matrix.build.cross }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Get version
        id: version
        shell: bash
        run: |
          echo ::set-output name=version::${GITHUB_REF/refs\/tags\//}

      - name: Configure toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          profile: minimal
          target: ${{ env.TARGET }}
          override: true

      - name: Build
        uses: actions-rs/cargo@v1
        with:
          use-cross: ${{ env.USE_CROSS }}
          command: build
          args: --target=${{ env.TARGET }} --release --verbose

      - name: Create package
        id: package
        shell: bash
        env:
          VERSION: ${{ steps.version.outputs.version }}
        run: |
          base_name="${PROJECT_NAME}-${VERSION}-${TARGET}"
          deploy_dir="${BUILD_DIR}/${base_name}"

          # Create deployment directory
          mkdir -p "${deploy_dir}"
          mkdir -p "${deploy_dir}/completion"
          mkdir -p "${deploy_dir}/man1"

          # Copy files
          cp README.md "${deploy_dir}"
          cp LICENSE "${deploy_dir}"
          cp "target/${TARGET}/release/${PROJECT_NAME}"                   "${deploy_dir}/"
          ## Man
          cp "target/${TARGET}/release/build/rnr"-*/out/"rnr.1"           "${deploy_dir}/man1/"
          cp "target/${TARGET}/release/build/rnr"-*/out/"rnr-help.1"      "${deploy_dir}/man1/"
          cp "target/${TARGET}/release/build/rnr"-*/out/"rnr-regex.1"     "${deploy_dir}/man1/"
          cp "target/${TARGET}/release/build/rnr"-*/out/"rnr-from-file.1" "${deploy_dir}/man1/"
          cp "target/${TARGET}/release/build/rnr"-*/out/"rnr-to-ascii.1"  "${deploy_dir}/man1/"
          ## Completion
          cp "target/${TARGET}/release/build/rnr"-*/out/"_rnr"            "${deploy_dir}/completion/"
          cp "target/${TARGET}/release/build/rnr"-*/out/"rnr.bash"        "${deploy_dir}/completion/"
          cp "target/${TARGET}/release/build/rnr"-*/out/"rnr.fish"        "${deploy_dir}/completion/"
          cp "target/${TARGET}/release/build/rnr"-*/out/"rnr.elv"         "${deploy_dir}/completion/"
          cp "target/${TARGET}/release/build/rnr"-*/out/"_rnr.ps1"        "${deploy_dir}/completion/"

          # Archive
          if [[ "${TARGET}" == *"-windows-"* ]]; then
            package_name="${base_name}.zip"
            7z a "${package_name}" "./${BUILD_DIR}/${base_name}"
          else
            package_name="${base_name}.tar.gz"
            tar czf "${package_name}" -C "${BUILD_DIR}" "${base_name}"
          fi

          echo ::set-output name=name::${package_name}

      - name: Store artifacts
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.ARTIFACT_NAME }}-${{ strategy.job-index }}
          path: ${{ steps.package.outputs.name }}

  publish-release:
    name: Publish a new release
    runs-on: ubuntu-latest
    needs: build-artifacts
    steps:
      - name: Get the version
        id: version
        shell: bash
        run: |
          echo ::set-output name=version::${GITHUB_REF/refs\/tags\//}

      - name: Fetch artifacts
        uses: actions/download-artifact@v4
        with:
          path: ${{ env.ARTIFACT_NAME }}
          pattern: ${{ env.ARTIFACT_NAME }}-*
          merge-multiple: true

      - name: Publish new release
        uses: softprops/action-gh-release@v2
        with:
          name: "Release ${{ steps.version.outputs.version }}"
          files: "${{ env.ARTIFACT_NAME }}/*"
          draft: true
