#!/bin/sh
set -eu

script_dir=$(CDPATH= cd -- "$(dirname "$0")" && pwd)
root_dir=$(CDPATH= cd -- "$script_dir/.." && pwd)
output="${1:-$root_dir/pins/base-channels.sexp}"
channels_source_host="${NBDE_GUIX_CHANNELS_SOURCE_HOST:-pguix}"

mkdir -p "$(dirname "$output")"

if command -v guix >/dev/null 2>&1; then
  guix describe -f channels >"$output"
else
  ssh -o BatchMode=yes -o ConnectTimeout=10 \
    "$channels_source_host" 'guix describe -f channels' >"$output"
fi

printf '%s\n' "$output"
