#!/bin/sh
# things you should remember but dont

REM="${XDG_DATA_HOME:=$HOME/.local/share}/rem.cmd"

if ! [ -f "$REM" ]; then
  echo "missing $REM"
  exit 1
fi

cmd="$(cat $REM | fzf -e -q "$1" -1 -d "^" --with-nth="{1}" --accept-nth="{2..}")"
rest="$cmd"
final=""

prompt() {
  if [ -z "$2" ]; then
    while [ -z "$output" ]; do
      read -E -p "$1: " output
    done
  else
    read -E -p "$1 ($2): " output
  fi
  printf "%s" "${output:-$2}"
}

while case "$rest" in *\{\{*\}\}*) true;; *) false;; esac; do
    prefix="${rest%%\{\{*}"
    temp="${rest#*\{\{}"
    cmd="${temp%%\}\}*}"
    rest="${temp#*\}\}}"
    final="$(printf "%s%s%s" "$final" "$prefix" "$(eval "$cmd")")"
done
final="$(printf "%s%s" "$final" "$rest")"

printf "%s" "$final"
