#!/usr/bin/env sh
#
# ---
# source: https://gist.github.com/Defaultldentity/de764ef9ab21a1c1c090c2fd2139ce81

PLAYGROUNDS_DIR="${TMPDIR:-/tmp}/rust-playgrounds"
TIMESTAMP=$(date "+%Y%m%d%H%M%S")
PROJECT_DIR="${PLAYGROUNDS_DIR}/playground${TIMESTAMP}"

cargo new "${PROJECT_DIR}" && cd "${PROJECT_DIR}" || exit 1

# add dependencies to Cargo.toml
for CRATE in "$@"; do
  sed "/^\[dependencies\]/a ${CRATE} = \"*\"" -i Cargo.toml
done

if [ -z "${TMUX}" ]; then
  tmux new-session -s playground -n playground
else
  tmux new-window -n playground -c "${PROJECT_DIR}"
fi

tmux send-keys "vim +2 ./src/main.rs" C-m \; \
  split-window -v "cargo watch -s 'clear && cargo run -q'" \; \
  select-pane -U \; \
