{ lib, stdenv, fetchFromGitHub, nix-gitignore, dtc, installShellFiles, logLevel ? 5, ... }:

let
  rawSrc = fetchFromGitHub {
    owner = "mjallen18";
    repo = "argononed";
    rev = "master";  # replace with actual commit or tag
    sha256 = "sha256-PpFR+6Aa4Pz9EmxOayMSsSTKFzUR6sYIAkGZ8+SUK18=";  # fill this in with actual hash
  };

  ignores = ''
    /*
    !/version
    !/makefile
    !/configure
    !/src
    !/OS
    /OS/*
    !/OS/_common/
    !/OS/nixos/
  '';

  cleanSrc = nix-gitignore.gitignoreSourcePure ignores rawSrc;
in

stdenv.mkDerivation {
  pname = "argononed";
  version = lib.strings.fileContents "${cleanSrc}/version";

  src = cleanSrc;

  nativeBuildInputs = [ dtc installShellFiles ];

  preConfigure = ''
    patchShebangs --build ./configure
    export TARGET_DISTRO=nixos
  '';

  patches = [
    "${cleanSrc}/OS/nixos/patches/nixos.patch"
    "${cleanSrc}/OS/nixos/patches/shutdown.patch"
  ];

  buildFlags = [ "LOGLEVEL=${toString logLevel}" ];

  installFlags = [ "NIX_DRVOUT=$(out)" ];

  postInstall = ''
    installShellCompletion --bash --name argonone-cli OS/_common/argonone-cli-complete.bash
  '';

  meta = {
    description = "A replacement daemon for the Argon One Raspberry Pi case";
    homepage = "https://gitlab.com/DarkElvenAngel/argononed";
    license = lib.licenses.mit;
    platforms = [ "aarch64-linux" ];
  };
}
