{ nixpkgs ? import ./nixpkgs.nix {} }:

let
  inherit (nixpkgs {}) pkgs;
  # haskellPackages = import ./stackage.nix { inherit nixpkgs; };
  haskellPackages = pkgs.haskellPackages;

  drv = haskellPackages.callPackage ./uniworx.nix {};

  postgresSchema = pkgs.writeText "schema.sql" ''
    CREATE USER uniworx WITH SUPERUSER;
    CREATE DATABASE uniworx_test;
    GRANT ALL ON DATABASE uniworx_test TO uniworx;
    CREATE DATABASE uniworx;
    GRANT ALL ON DATABASE uniworx TO uniworx;
  '';

  postgresHba = pkgs.writeText "hba_file" ''
    local all all trust
  '';

  override = oldAttrs: {
    nativeBuildInputs = oldAttrs.nativeBuildInputs ++ (with pkgs; [ nodejs-14_x postgresql_12 openldap google-chrome exiftool memcached minio minio-client ]) ++ (with pkgs.haskellPackages; [ stack yesod-bin hlint cabal-install weeder profiteur ]);
    shellHook = ''
      export PROMPT_INFO="${oldAttrs.name}"

      export EDITOR=emacsclient

      cleanup() {
        set +e -x
        type cleanup_postgres &>/dev/null && cleanup_postgres
        type cleanup_widget_memcached &>/dev/null && cleanup_widget_memcached
        type cleanup_session_memcached &>/dev/null && cleanup_session_memcached
        type cleanup_cache_memcached &>/dev/null && cleanup_cache_memcached
        type cleanup_minio &>/dev/null && cleanup_minio
        set +x
      }

      trap cleanup EXIT

      if [[ -z "$PGHOST" ]]; then
        set -xe

        pgDir=$(mktemp -d)
        pgSockDir=$(mktemp -d)
        pgLogFile=$(mktemp)
        initdb --no-locale -D ''${pgDir}
        pg_ctl start -D ''${pgDir} -l ''${pgLogFile} -w -o "-k ''${pgSockDir} -c listen_addresses=''' -c hba_file='${postgresHba}' -c unix_socket_permissions=0700 -c max_connections=9990"
        export PGHOST=''${pgSockDir} PGLOG=''${pgLogFile}
        psql -f ${postgresSchema} postgres
        printf "Postgres logfile is %s\nPostgres socket directory is %s\n" ''${pgLogFile} ''${pgSockDir}

        cleanup_postgres() {
            set +e -x
            pg_ctl stop -D ''${pgDir}
            rm -rvf ''${pgDir} ''${pgSockDir} ''${pgLogFile}
            set +x
        }

        set +xe
      fi

      if [[ -z "$WIDGET_MEMCACHED_HOST" ]]; then
        set -xe

        memcached -l localhost -p 11211 &>/dev/null &
        widget_memcached_pid=$?

        cleanup_widget_memcached() {
          [[ -n "$widget_memcached_pid" ]] && kill $widget_memcached_pid
        }

        export WIDGET_MEMCACHED_HOST=localhost WIDGET_MEMCACHED_PORT=11211

        set +xe
      fi

      if [[ -z "$SESSION_MEMCACHED_HOST" ]]; then
        set -xe

        memcached -l localhost -p 11212 &>/dev/null &
        session_memcached_pid=$?

        cleanup_session_memcached() {
          [[ -n "$session_memcached_pid" ]] && kill $session_memcached_pid
        }

        export SESSION_MEMCACHED_HOST=localhost SESSION_MEMCACHED_PORT=11212

        set +xe
      fi

      if [[ -z "$MEMCACHED_HOST" ]]; then
        set -xe

        memcached -l localhost -p 11213 &>/dev/null &
        memcached_pid=$?

        cleanup_session_memcached() {
          [[ -n "$memcached_pid" ]] && kill $memcached_pid
        }

        export MEMCACHED_HOST=localhost MEMCACHED_PORT=11212

        set +xe
      fi

      if [[ -z "$UPLOAD_S3_HOST" ]]; then
         set -xe

         cleanup_minio() {
           [[ -n "$minio_pid" ]] && kill $minio_pid
           [[ -n "$minio_dir" ]] && rm -rvf ''${minio_dir}
           [[ -n "MINIO_LOGFILE" ]] && rm -rvf ''${MINIO_LOGFILE}
         }

         export MINIO_DIR=$(mktemp -d)
         export MINIO_LOGFILE=$(mktemp --tmpdir minio.XXXXXX.log)
         export MINIO_ACCESS_KEY=$(${pkgs.pwgen}/bin/pwgen -s 16 1)
         export MINIO_SECRET_KEY=$(${pkgs.pwgen}/bin/pwgen -s 32 1)

         minio server --address localhost:9000 ''${MINIO_DIR} &>''${MINIO_LOGFILE} &
         minio_pid=$?

         sleep 1

         export UPLOAD_S3_HOST=localhost UPLOAD_S3_PORT=9000 UPLOAD_S3_SSL=false UPLOAD_S3_KEY_ID=''${MINIO_ACCESS_KEY} UPLOAD_S3_KEY=''${MINIO_SECRET_KEY}

         set +xe
      fi

      if [ -n "$ZSH_VERSION" ]; then
        autoload -U +X compinit && compinit
        autoload -U +X bashcompinit && bashcompinit
      fi
      eval "$(stack --bash-completion-script stack)"

      ${oldAttrs.shellHook}
    '';
  };

  dummy = pkgs.stdenv.mkDerivation {
    name = "interactive-uniworx-environment";
    shellHook = "";
  };
in pkgs.stdenv.lib.overrideDerivation dummy override
  #pkgs.stdenv.lib.overrideDerivation drv.env override
