# SPDX-FileCopyrightText: 2022 Gregor Kleen <gregor.kleen@ifi.lmu.de>,Sarah Vaupel <sarah.vaupel@ifi.lmu.de>,Steffen Jost <jost@tcs.ifi.lmu.de>
#
# SPDX-License-Identifier: AGPL-3.0-or-later

{ pkgs ? (import ./nixpkgs.nix {}).pkgs }:

let
  inherit (pkgs.lib) optionalString;

  haskellPackages = pkgs.haskellPackages;

  develop = pkgs.writeScriptBin "develop" (import ./nix/develop.nix { inherit pkgs; } ''
    if [ -x .develop.cmd ]; then
      ./.develop.cmd
    else
      if [ -n "$ZSH_VERSION" ]; then
        autoload -U +X compinit && compinit
        autoload -U +X bashcompinit && bashcompinit
      fi
      eval "$(stack --bash-completion-script stack)"

      $(getent passwd $USER | cut -d: -f 7)
    fi
  '');

  inDevelop = pkgs.writeScriptBin "in-develop" ''
    #!${pkgs.zsh}/bin/zsh -e

    if [[ -z "''${PORT_OFFSET}" ]]; then
      echo "Not in develop"
    else
      echo "In develop"
    fi
  '';

  killallUni2work = pkgs.writeScriptBin "killall-uni2work" ''
    #!${pkgs.zsh}/bin/zsh

    set -o pipefail

    lockFile=

    if [[ ''${#@} -gt 0 ]]; then
      lockFile=''${1}
      shift

      if [[ -d ''${lockFile} && -f ''${lockFile}/.stack-work.lock ]]; then
        lockFile=''${lockFile}/.stack-work.lock
      fi
    else
      if [[ -f .stack-work.lock ]]; then
        lockFile=.stack-work.lock
      elif [[ -f ~/projects/uni2work/.stack-work.lock ]]; then
        lockFile=~/projects/uni2work/.stack-work.lock
      fi
    fi

    if [[ -z "''${lockFile}" && !(-f ''${lockFile}) ]]; then
      echo "Could not find lockfile" >&2
      exit 1
    fi

    printf "Killing users of %s...\n" ''${lockFile}

    while ${pkgs.psmisc}/bin/fuser ''${lockFile} 2>/dev/null | ${pkgs.coreutils}/bin/cut -d ':' -f 2- | ${pkgs.findutils}/bin/xargs -tr -- ${pkgs.util-linux}/bin/kill; do
      sleep 1
    done
  '';

  diffRunning = pkgs.writeScriptBin "diff-running" ''
    #!${pkgs.zsh}/bin/zsh

    git diff $(cut -d '-' -f 1 <(curl -sH 'Accept: text/plain' https://uni2work.ifi.lmu.de/version))
  '';
in pkgs.mkShell {
  name = "uni2work";
  nativeBuildInputs = [develop inDevelop killallUni2work diffRunning]
  ++ (with pkgs;
      [ nodejs-14_x postgresql_12 openldap google-chrome exiftool memcached minio minio-client                        
        gup skopeo reuse pre-commit
        # busybox # for print services, but interferes with build commands in develop-shell
        htop
        pdftk #pdftk just for testing pdf-passwords
        #texlive.combined.scheme-full # works
        #texlive.combined.scheme-medium
        # texlive.combined.scheme-small
        (texlive.combine {
          inherit (texlive) scheme-basic
            babel-german babel-english booktabs textpos
            enumitem eurosym koma-script parskip xcolor
            # required fro LuaTeX
            luatexbase lualatex-math unicode-math selnolig
            ;
        })
      ]
     )
  ++ (with pkgs.haskellPackages; [ stack yesod-bin hlint cabal-install weeder profiteur ]);
}
