on:
  push:
    tags: v[0-9].[0-9]+.[0-9]+

env:
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  CARGO_TERM_COLOR: always

name: Releases

jobs:
  get_version:
    name: Get version from Git tag
    runs-on: ubuntu-latest
    outputs:
      project_version: ${{ env.VERSION }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Getting version
        run: echo "VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV

  make_release:
    name: Create Release
    runs-on: ubuntu-latest
    needs: get_version
    outputs: 
      upload_url: ${{ steps.create_release.outputs.upload_url }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          VERSION: ${{ needs.get_version.outputs.project_version }}
        with:
          tag_name: ${{ env.VERSION }}
          release_name: ${{ env.VERSION }}
          body: |
            ## New
            * Added new features.
            ## Bug Fixes & Improvements
            * Various fixes and stability improvements.
            ## Documentation & others
            * Updated documentation.
          draft: true
          prerelease: false

  build-and-upload:
    name: Create and upload builds
    needs: [ make_release, get_version ]
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ windows-latest, ubuntu-latest, macOS-latest ]
  
    defaults:
      run:
        shell: bash
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: install dependencies
        if: contains(matrix.os, 'ubuntu')
        run: |
          sudo apt update && sudo apt install libwebkit2gtk-4.0-dev upx
          cargo install cross
      - name: Build release binaries
        run: |
          cargo build --release
          cargo build --release --no-default-features --target-dir nogui
      - name: Build Windows release binaries with Edge web-engine
        if: contains(matrix.os, 'windows')
        run: cargo build --release --features "edge" --target-dir edge
      - name: Build and package deb packages
        if: contains(matrix.os, 'ubuntu')
        run: |
          PKGARCH=amd64 contrib/deb/generate.sh
          PKGARCH=i686 contrib/deb/generate.sh
          PKGARCH=mips contrib/deb/generate.sh
          PKGARCH=mipsel contrib/deb/generate.sh
          PKGARCH=armhf contrib/deb/generate.sh
          PKGARCH=armlf contrib/deb/generate.sh
          PKGARCH=arm64 contrib/deb/generate.sh

      - name: Upload bins & debs
        if: contains(matrix.os, 'ubuntu')
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          tag_name="${{ needs.get_version.outputs.project_version }}"
          hub release edit $(find ./bin -type f -name "alfis-*" -printf "-a %p ") -m "" "$tag_name"
          hub release edit $(find . -type f -name "*.deb" -printf "-a %p ") -m "" "$tag_name"

      - name: win_arch
        run: |
          echo "BIN_ARCH=windows-amd64" >> $GITHUB_ENV
          echo "BIN_ARCH_NOGUI=windows-amd64-nogui" >> $GITHUB_ENV
          echo "BIN_ARCH_EDGE=windows-amd64-edge" >> $GITHUB_ENV
        if: contains(matrix.os, 'windows')
      - name: lin_arch
        run: |
          echo "BIN_ARCH=linux-amd64" >> $GITHUB_ENV
          echo "BIN_ARCH_NOGUI=linux-amd64-nogui" >> $GITHUB_ENV
        if: contains(matrix.os, 'ubuntu')
      - name: mac_arch
        run: |
          echo "BIN_ARCH=darwin-amd64" >> $GITHUB_ENV
          echo "BIN_ARCH_NOGUI=darwin-amd64-nogui" >> $GITHUB_ENV
        if: contains(matrix.os, 'mac')
      - name: Fill variables
        run: |
          echo "BIN_PATH=./target/release/alfis" >> $GITHUB_ENV
          echo "ZIP_NAME=alfis-${{env.BIN_ARCH}}-${{ needs.get_version.outputs.project_version }}.zip" >> $GITHUB_ENV
          echo "BIN_PATH_NOGUI=./nogui/release/alfis" >> $GITHUB_ENV
          echo "ZIP_NAME_NOGUI=alfis-${{env.BIN_ARCH}}-${{ needs.get_version.outputs.project_version }}-nogui.zip" >> $GITHUB_ENV
      - name: Windows variables
        if: contains(matrix.os, 'windows')
        run: |
          echo "BIN_PATH=target/release/alfis.exe" >> $GITHUB_ENV
          echo "ZIP_NAME=alfis-${{env.BIN_ARCH}}-${{ needs.get_version.outputs.project_version }}.zip" >> $GITHUB_ENV
          echo "BIN_PATH_NOGUI=nogui/release/alfis.exe" >> $GITHUB_ENV
          echo "ZIP_NAME_NOGUI=alfis-${{env.BIN_ARCH}}-${{ needs.get_version.outputs.project_version }}-nogui.zip" >> $GITHUB_ENV
          echo "BIN_PATH_EDGE=edge/release/alfis.exe" >> $GITHUB_ENV
          echo "ZIP_NAME_EDGE=alfis-${{env.BIN_ARCH}}-${{ needs.get_version.outputs.project_version }}-edge.zip" >> $GITHUB_ENV

      - name: Packaging
        uses: papeloto/action-zip@v1
        with:
          files: ${{ env.BIN_PATH }} alfis.toml README.md LICENSE adblock.txt
          dest: ${{ env.ZIP_NAME }}
      - name: Packaging no GUI
        uses: papeloto/action-zip@v1
        with:
          files: ${{ env.BIN_PATH_NOGUI }} alfis.toml README.md LICENSE adblock.txt
          dest: ${{ env.ZIP_NAME_NOGUI }}
      - name: Packaging Edge binary
        if: contains(matrix.os, 'windows')
        uses: papeloto/action-zip@v1
        with:
          files: ${{ env.BIN_PATH_EDGE }} alfis.toml README.md LICENSE adblock.txt
          dest: ${{ env.ZIP_NAME_EDGE }}

      - name: Upload zip
        id: upload-zip
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.make_release.outputs.upload_url }}
          asset_path: ${{ env.ZIP_NAME }}
          asset_name: ${{ env.ZIP_NAME }}
          asset_content_type: application/zip

      - name: Upload NoGUI zip
        id: upload-nogui-zip
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.make_release.outputs.upload_url }}
          asset_path: ${{ env.ZIP_NAME_NOGUI }}
          asset_name: ${{ env.ZIP_NAME_NOGUI }}
          asset_content_type: application/zip

      - name: Upload Edge binary
        if: contains(matrix.os, 'windows')
        id: upload-edge-binary
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.make_release.outputs.upload_url }}
          asset_path: ${{ env.ZIP_NAME_EDGE }}
          asset_name: ${{ env.ZIP_NAME_EDGE }}
          asset_content_type: application/zip
