#!/usr/bin/env bash
set -euo pipefail

script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
root="$(cd "$script_dir/.." && pwd)"

if [ ! -f "$root/manifest.scm" ]; then
  root="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
fi

channels="$root/guix/channels.scm"
manifest="$root/manifest.scm"

if [ ! -f "$channels" ] || [ ! -f "$manifest" ]; then
  echo "guix-dev: expected guix/channels.scm and manifest.scm in the project root" >&2
  exit 1
fi

if [ -f "$root/.env" ]; then
  set -a
  . "$root/.env"
  set +a
fi

exec guix time-machine -C "$channels" -- \
  shell -m "$manifest" "$@"
