#!/usr/bin/env bash
# Fake ssh-keygen for tests. Env: FAKE_KEYGEN_LOG, FAKE_KEYGEN_FAIL (substring of path), FAKE_KEYGEN_HANG
file="" comment="" pass="<unset>"
while (($#)); do
    case "$1" in
        -f) file="$2"; shift 2 ;; -C) comment="$2"; shift 2 ;; -N) pass="$2"; shift 2 ;;
        -t) shift 2 ;; -q) shift ;; *) shift ;;
    esac
done
[[ -n "${FAKE_KEYGEN_LOG:-}" ]] && printf 'file=%s comment=%s pass=%s\n' "$file" "$comment" "$pass" >> "$FAKE_KEYGEN_LOG"
if [[ -n "${FAKE_KEYGEN_FAIL:-}" && "$file" == *"$FAKE_KEYGEN_FAIL"* ]]; then
    echo "ssh-keygen: simulated failure" >&2
    : > "$file"   # leave debris, like a real mid-flight failure could
    exit 1
fi
umask 077
printf 'PRIVATE %s\n' "$comment" > "$file"
if [[ -n "${FAKE_KEYGEN_HANG:-}" ]]; then sleep "$FAKE_KEYGEN_HANG"; fi
printf 'ssh-ed25519 AAAAFAKE %s\n' "$comment" > "$file.pub"
chmod 644 "$file.pub"
