#!/bin/sh -e
#
# tmux-tmp-project - [rust | js | npm | [py]thon] default: quick project
# dependencies: tmuxsession
#
# for use in tmux config: bind M-t run-shell 'tmux-tmp-project'

tmpdir=$(mktemp -d "${TMPDIR:-/tmp}"/temp-sessions) || exit
trap 'rm -rf "$tmpdir"' EXIT INT TERM HUP

rust_project() {
  DIR="$(date '+%s')"
  mkdir -p "${tmpdir}/${DIR}" && cd "${tmpdir}/${DIR}"

  printf "Rust project name: "
  read -r REPLY
  cargo new "${REPLY}"
  cd "${REPLY}"
  ,tmuxsession "temp-${REPLY}"
}

javascript_project() {
  DIR="$(date '+%s')"

  printf "Javascript project name: "
  read -r REPLY
  mkdir -p "${tmpdir}/${DIR}/${REPLY}" \
    && cd "${tmpdir}/${DIR}/${REPLY}"
  npm init -y
  ,tmuxsession "temp-${REPLY}"
}

python_project() {
  DIR="$(date '+%s')"

  printf "Python project name: "
  read -r REPLY
  mkdir -p "${tmpdir}/${DIR}" && cd "${tmpdir}/${DIR}"
  uv venv
  mkdir "${REPLY}" && cd "${REPLY}" \
    && ,tmuxsession "temp-${REPLY}" "source ../.venv/bin/activate"
}

quick_project() {
  DIR="$1"
  mkdir -p "${tmpdir}/${DIR}" && cd "${tmpdir}/${DIR}" \
    && ,tmuxsession "temp-${DIR}"
}

if [ -n "$1" ]; then
  case "$1" in
    rust) rust_project ;;
    js | npm) javascript_project ;;
    py | python) python_project ;;
    -h | --help)
      awk 'NR == 1 { next } /^$/ { exit } { print substr($0, 3) }' "$0"
      exit 0
      ;;
    *) quick_project "$@" ;;
  esac
else
  DIR="$(date '+%s')"
  mkdir -p "${tmpdir}/${DIR}" && cd "${tmpdir}/${DIR}" \
    && ,tmuxsession "temp-${DIR}"
fi
