name: Make Release

on:
  push:
    tags:
      - 'v**'

env:
  APPDIR: WlxOverlay-S.AppDir
  CARGO_TERM_COLOR: always
  SCCACHE_GHA_ENABLED: "true"
  RUSTC_WRAPPER: "sccache"
  
jobs:
  make_release:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v3
      - name: Setup sccache
        uses: mozilla-actions/sccache-action@v0.0.9
      - name: Prepare Environment
        run: |
          .github/workflows/scripts/appimage_prepare_env.sh
      - name: Cargo Build
        run: |
          cargo build --release
          cp target/release/wlx-overlay-s ${APPDIR}/usr/bin
          chmod +x ${APPDIR}/usr/bin/wlx-overlay-s

      - name: Package AppImage
        run: |
          export VERSION=$GITHUB_REF_NAME
          ./linuxdeploy-x86_64.AppImage -dwlx-overlay-s.desktop -iwlx-overlay-s.png --appdir=${APPDIR} --output appimage --exclude-library '*libpipewire*'
          mv WlxOverlay-S-$VERSION-x86_64.AppImage WlxOverlay-S-x86_64.AppImage

      - name: Make tarball
        run: |
          pip install portage pycargoebuild
          wget https://github.com/gentoo/gentoo/raw/refs/heads/master/metadata/license-mapping.conf
          mkdir dist
          pycargoebuild --distdir dist --license-mapping license-mapping.conf --crate-tarball --crate-tarball-path wlx-overlay-s-crates.tar.xz

      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.RELEASE_KEY }}
        with:
          tag_name: ${{ github.ref_name }}
          release_name: WlxOverlay-S ${{ github.ref_name }}
          draft: true
          prerelease: false

      - name: Upload ELF
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.RELEASE_KEY }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: target/release/wlx-overlay-s
          asset_name: wlx-overlay-s
          asset_content_type: application/octet-stream

      - name: Upload AppImage
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.RELEASE_KEY }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./WlxOverlay-S-x86_64.AppImage
          asset_name: WlxOverlay-S-${{ github.ref_name }}-x86_64.AppImage
          asset_content_type: application/octet-stream

      - name: Upload crates tarball
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.RELEASE_KEY }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./wlx-overlay-s-crates.tar.xz
          asset_name: WlxOverlay-S-${{ github.ref_name }}-crates.tar.xz
          asset_content_type: application/x-gtar
