2022-06-15 19:51:37 -07:00
|
|
|
#!/bin/sh
|
2021-08-12 02:24:49 -07:00
|
|
|
# this script will install the dotfiles from the target git repository, and back up any conflicting files
|
2022-06-17 00:47:58 -07:00
|
|
|
# credit for the basic outline of this script goes to https://www.atlassian.com/git/tutorials/dotfiles
|
2021-10-24 12:17:34 -07:00
|
|
|
|
2022-09-16 19:03:17 -07:00
|
|
|
set -eu
|
|
|
|
|
2021-08-12 02:24:49 -07:00
|
|
|
DOTFILES_REPO_DIR="$HOME/git/dotfiles"
|
|
|
|
DOTFILES_REPO_URL="https://git.blakenorth.net/dotfiles"
|
2022-06-17 00:47:58 -07:00
|
|
|
DOTFILES_BACKUP_DIR="$HOME/.dotfiles-backup_$(date +'%Y-%m-%d_%H:%m:%S')"
|
2021-08-12 02:24:49 -07:00
|
|
|
|
2022-09-16 19:03:17 -07:00
|
|
|
printf "\n Installing dotfiles from %s\n\n" "$DOTFILES_REPO_URL"
|
2022-06-17 00:47:58 -07:00
|
|
|
|
2021-08-12 02:24:49 -07:00
|
|
|
# check for the git command and exit if it doesn't exist
|
|
|
|
if ! command -v git > /dev/null;then
|
2022-09-16 19:03:17 -07:00
|
|
|
echo 'Error: git is not installed or could not be found in PATH. Please install git to proceed.' >&2
|
|
|
|
return 1
|
2021-08-12 02:24:49 -07:00
|
|
|
fi
|
|
|
|
|
2022-09-16 19:03:17 -07:00
|
|
|
mkdir -p "$DOTFILES_REPO_DIR"
|
2022-10-07 20:57:31 -07:00
|
|
|
git clone --depth 1 --bare "$DOTFILES_REPO_URL" "$DOTFILES_REPO_DIR"
|
2021-08-12 02:24:49 -07:00
|
|
|
|
|
|
|
dot() {
|
|
|
|
git --git-dir="$DOTFILES_REPO_DIR" --work-tree="$HOME" "$@"
|
|
|
|
}
|
|
|
|
|
2022-06-15 19:51:37 -07:00
|
|
|
# remove the '> /dev/null' part if things don't seem to work right
|
|
|
|
if ! dot checkout > /dev/null 2>&1; then
|
2022-09-16 19:03:17 -07:00
|
|
|
echo " Backing up pre-existing dotfiles."
|
2022-06-15 19:51:37 -07:00
|
|
|
mkdir -p "$DOTFILES_BACKUP_DIR"
|
|
|
|
|
2021-08-12 02:24:49 -07:00
|
|
|
# get the list of files that need to be backed up
|
2022-09-17 21:13:23 -07:00
|
|
|
dot checkout 2>&1 \
|
|
|
|
| grep -Po '\t\K.*$' \
|
|
|
|
| while read -r LINE; do
|
2022-09-17 20:23:12 -07:00
|
|
|
LINE_DIR="$(dirname "$LINE")" # No file
|
|
|
|
mkdir -p "$DOTFILES_BACKUP_DIR/$LINE_DIR"
|
|
|
|
mv "$HOME/$LINE" "$DOTFILES_BACKUP_DIR/$LINE_DIR"
|
|
|
|
done
|
2022-09-16 19:03:17 -07:00
|
|
|
|
|
|
|
# now that all the files are out of the way, we can do one final checkout
|
|
|
|
dot checkout
|
2021-08-12 02:24:49 -07:00
|
|
|
fi
|
|
|
|
|
|
|
|
dot config status.showUntrackedFiles no
|
|
|
|
|
2022-09-16 19:03:17 -07:00
|
|
|
printf "\n Installation complete!\n"
|