name: Build

on:
  workflow_call:
    inputs:
      ref:
        default: ''
        required: false
        type: string
  pull_request:
    branches:
      - '*'


jobs:
  build:
    runs-on: ubuntu-latest
    container:
      image: archlinux:latest
    steps:
      - name: Install packages
        run: pacman -Syy -q --noconfirm && pacman -S -q --noconfirm git sudo base-devel p7zip nodejs jq asar electron29 libpulse dpkg unzip xdg-utils python rpm-tools glibc
      - name: Checkout
        uses: actions/checkout@v4
        with:
          ref: ${{ (inputs.ref || '') }}

      - name: Create dist directory
        run: mkdir -p dist

      - name: Fix permissions for build
        run: mkdir /home/build && chgrp nobody /home/build && chmod g+ws /home/build && setfacl -m u::rwx,g::rwx /home/build && setfacl -d --set u::rwx,g::rwx,o::- /home/build && chown nobody .

      - name: Make Arch package
        run: sudo -u nobody makepkg --log && mv *.pkg.tar.zst dist/

      - name: Build tarball packages
        run: bash ./build_tarball.sh -a all && cp tar/* dist/

      - name: Build Deb packages
        run: bash ./build_deb.sh -a all &&  mv deb/*.deb dist

      - name: Build RPM packages
        run: bash ./build_rpm.sh && mv rpm/*.rpm dist

      - name: Retrieve Asar and release notes
        run:  mv ./src/app/yandex-music.asar dist/yandex-music.asar && mv ./src/app/release_notes.json dist/release_notes.json

      - name: Upload Arch artifacts
        uses: actions/upload-artifact@v4
        with:
          path: ./dist/*.pkg.tar.zst
          name: arch-packages

      - name: Upload tarball artifacts
        uses: actions/upload-artifact@v4
        with:
          path: ./dist/*.tar.gz
          name: tar-packages

      - name: Upload Deb artifact
        uses: actions/upload-artifact@v4
        with:
          path: ./dist/*.deb
          name: deb-packages

      - name: Upload RPM artifact
        uses: actions/upload-artifact@v4
        with:
          path: ./dist/*.rpm
          name: rpm-packages

      - name: Upload Asar artifacts
        uses: actions/upload-artifact@v4
        with:
          path: ./dist/*.asar
          name: asar-packages

      - name: Upload Release Notes
        uses: actions/upload-artifact@v4
        with:
          path: ./dist/release_notes.json

  nix-build:
    runs-on: ubuntu-latest
    env:
      NIXPKGS_ALLOW_UNFREE: 1
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          ref: ${{ (inputs.ref || '') }}

      - name: Install nix
        uses: cachix/install-nix-action@v22

      - uses: DeterminateSystems/magic-nix-cache-action@v2

      - name: Build yandex-music
        run: nix build --impure .#yandex-music
      - name: Build yandex-music without flakes binaries
        run: nix build --impure .#yandex-music-noflakes