#!/bin/sh -e
#
# ,template - dump out a template from datafiles

DATAFILES_PATH=${DATAFILES_PATH:-${HOME%/}/.dotfiles/datafiles}

# shell.nix shell.nix Simple nix shell
# default.nix default.nix Simple default.nix

templates="bash.sh bash.sh Bash script starter
template.sh template.sh Shell script starter
zfunc zfunc ZSH function starter
gitignore gitignore Basic gitignore
index.html index.html Basic html starter
Dockerfile dockerfile/python-gunicorn A guicorn based dockerfile for python
Dockerfile dockerfile/static-serving Static serving for nodejs project"

# <target-filename> <source-filename> <info>
# shellcheck disable=SC2016
choice="$(echo "${templates}" | awk '{ $2=""; print $0 }' \
  | fzf --preview 'cat "${DATAFILES_PATH}"/templates/{1}')"
tgt="$(echo "${choice}" | cut -d' ' -f 1)"
info="$(echo "${choice}" | cut -d' ' -f 2-)"
src="$(echo "${templates}" | grep "${info}" | cut -d' ' -f 1)"

echo "Creating ${src} using ${tgt}.
${info}"

cat "${DATAFILES_PATH}/templates/${src}" >"${tgt}"
