#!/usr/bin/env bash
export HOST_SHELL="$(command -v "$SHELL")"
export IS_REMOTE_ENV=1

flake_shell=.#remoteenv
if [[ ! -f /run/current-system/nixos-version ]]; then
  flake_shell+=-fhs
fi

# nix() {
#   if [[ "$(nix --version | cut -d. -f2)" -lt 18 ]]; then
#     command nix run nixpkgs#nix -- "$@"
#   else
#     command nix "$@"
#   fi
# }

# `test A -nt B` deferences symlinks
t1=$(stat --format=%Z flake.nix  2>/dev/null || printf 0)
t2=$(stat --format=%Z flake.lock  2>/dev/null || printf 0)
t3=$(stat --format=%Z .direnv/flake-remote-devshell-profile  2>/dev/null || printf 0)
if [[ "$t1" -gt "$t3" || "$t2" -gt "$t3" ]]; then
  mkdir -p .direnv/
  eval "$(nix --extra-experimental-features "nix-command flakes" print-dev-env --profile ".direnv/flake-remote-devshell-profile" "$flake_shell")"
else
  eval "$(nix --extra-experimental-features "nix-command flakes" print-dev-env ".direnv/flake-remote-devshell-profile")"
fi
unset t1 t2 t3 flake_shell

echo Entered "$(hostname --fqdn)"
df -h /nix/store
