name: Linux (Nixpkgs)

on:
  push:
    branches: master
    tags: 'v*.*.*'
    paths-ignore:
      - '**.md'
      - '**.txt'
      - 'LICENSE'
      - '.gitignore'
  pull_request:
    branches: master
    paths-ignore:
      - '**.md'
      - '**.txt'
      - 'LICENSE'
      - '.gitignore'

defaults:
  run:
    shell: bash

env:
  BUILD_TARGET: linux-64bit
  NIXPKGS_CHANNEL: nixos-23.05
  BT_INSTALLBASE: ${{ github.workspace }}/target/

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Identify build type.
        id: identify-build
        run: |
          case ${GITHUB_REF} in
            refs/tags/* )
              TAG=${GITHUB_REF#refs/tags/}
              echo "Release ${TAG}"
              echo "build-tag=${TAG}" >> $GITHUB_OUTPUT
              echo "release=true" >> $GITHUB_OUTPUT
              echo "buildVersion=${TAG#v}" >> $GITHUB_OUTPUT # nixpkgs
              ;;
            refs/pull/*)
              PR=$(echo ${GITHUB_REF} | cut -d/ -f3)
              echo "Test PR #${PR}"
              echo "build-tag=pr-${PR}" >> $GITHUB_OUTPUT
              echo "release=false" >> $GITHUB_OUTPUT
              echo "buildVersion=pr-${PR}-${GITHUB_SHA}" >> $GITHUB_OUTPUT # nixpkgs
              ;;
            refs/heads/* )
              BRANCH=${GITHUB_REF#refs/heads/}
              echo "Test ${BRANCH}"
              echo "build-tag=${BRANCH}" >> $GITHUB_OUTPUT
              echo "release=false" >> $GITHUB_OUTPUT
              echo "buildVersion=${BRANCH}-${GITHUB_SHA}" >> $GITHUB_OUTPUT # nixpkgs
              ;;
            * )
              echo "Test (unknown)"
              echo "build-tag=unknown" >> $GITHUB_OUTPUT
              echo "release=false" >> $GITHUB_OUTPUT
              echo "buildVersion=unknown-${GITHUB_SHA}" >> $GITHUB_OUTPUT # nixpkgs
              ;;
          esac

      - name: Checking out repository.
        uses: actions/checkout@v4
        with:
          submodules: recursive

      ## Nixpkgs-specific steps

      - name: Installing Nix.
        uses: cachix/install-nix-action@v18
        with:
          nix_path: "nixpkgs=channel:${{ env.NIXPKGS_CHANNEL }}"
      - name: Show Nixpkgs version.
        run: nix-instantiate --eval -E '(import <nixpkgs> {}).lib.version'

      - name: Building.
        run: |
          nix-build scripts/build_nixpkgs_local.nix --argstr buildVersion "${{ steps.identify-build.outputs.buildVersion }}" -A build --no-out-link > ../outlink

      - name: Test packaging.
        if: env.DONT_PACKAGE != 'true'
        run: |
          nix-build scripts/build_nixpkgs_local.nix --argstr buildVersion "${{ steps.identify-build.outputs.buildVersion }}" -A bundle
          install -Dm755 $(realpath result) ${BT_INSTALLBASE}/bin/BambooTracker
          cp -r --no-preserve=all "$(cat ../outlink)/share" ${BT_INSTALLBASE}/share
          rm -rf ${BT_INSTALLBASE}/share/BambooTracker/lang # bundled into binary, presumably

      ## End Nixpkgs-specific steps

      - name: Finalize packaging.
        id: packaging
        if: steps.identify-build.outputs.release == 'true' && env.DONT_PACKAGE != 'true'
        run: |
          export package_name="BambooTracker-${{ steps.identify-build.outputs.build-tag }}-${BUILD_TARGET}"
          echo "package-name=${package_name}" >> $GITHUB_OUTPUT
          mv -v ${BT_INSTALLBASE} ${package_name}
          7z a -tzip ${package_name}{.zip,}

      - name: Upload release package.
        if: steps.identify-build.outputs.release == 'true' && env.DONT_PACKAGE != 'true'
        uses: svenstaro/upload-release-action@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: ${{ steps.packaging.outputs.package-name }}.zip
          asset_name: ${{ steps.packaging.outputs.package-name }}.zip
          tag: ${{ github.ref }}
