#!/usr/bin/env bash

apply_dotfile() {
  directory_to_stow="${1}"
  stow --adopt -S -t "${HOME}" "${directory_to_stow}"
}

# Adopt will change the files in stow to match existing files.
# We don't want this, we just want to handle existing files gracefully.
# We simply adopt all the files and then at the end restore our git repo to undo changes
restore_from_adoption() {
  git restore .
}

apply_dotfile bash
apply_dotfile sh
apply_dotfile readline
apply_dotfile screen

apply_dotfile git
apply_dotfile xdg

apply_dotfile vim

apply_dotfile ruby
apply_dotfile python

# Only if inside a coder instance
if [ -n "${CODER}" ]; then
  apply_dotfile gnupg
fi

# Check that SSH version is above 9.2 (where EnableEscapeCommandline was added) before conditionally adding
# a config file. This is then included in the main config
SSH_VERSION=$(ssh -V 2>&1 | cut -d',' -f 1 | cut -d '_' -f 2)
if [[ "${SSH_VERSION}" =~ ^9\.[1-9]{0,1}[2-9].*$ ]]; then
cat <<CONFIG > ssh/.ssh/extra.conf
EnableEscapeCommandline yes
CONFIG
fi
apply_dotfile ssh

restore_from_adoption
