2021-08-12 02:24:49 -07:00
|
|
|
#!/bin/bash
|
|
|
|
# this script will install the dotfiles from the target git repository, and back up any conflicting files
|
|
|
|
# credit for this idea and basic script outline goes to https://www.atlassian.com/git/tutorials/dotfiles
|
|
|
|
|
2022-04-08 13:49:06 -07:00
|
|
|
# TODO: make it so line 31 (DOTFILES_BACKUP_LIST) can handle spaces (do it in a for loop)
|
2021-10-24 12:17:34 -07:00
|
|
|
|
2021-08-12 02:24:49 -07:00
|
|
|
DOTFILES_REPO_DIR="$HOME/git/dotfiles"
|
|
|
|
DOTFILES_REPO_URL="https://git.blakenorth.net/dotfiles"
|
|
|
|
DOTFILES_BACKUP_DIR="$HOME/.dotfiles-backup" # cannot contain the character '#'
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
# check for the git command and exit if it doesn't exist
|
|
|
|
if ! command -v git > /dev/null;then
|
|
|
|
echo 'git is not installed or could not be found in $PATH, please install git to proceed'
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
git clone --bare "$DOTFILES_REPO_URL" "$DOTFILES_REPO_DIR"
|
|
|
|
|
|
|
|
dot() {
|
|
|
|
git --git-dir="$DOTFILES_REPO_DIR" --work-tree="$HOME" "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
# remove the '&> /dev/null' part if things don't seem to work right
|
|
|
|
if dot checkout > /dev/null 2>&1; then
|
|
|
|
echo "Checked out dotfiles."
|
|
|
|
else
|
|
|
|
echo "Backing up pre-existing dotfiles."
|
|
|
|
# get the list of files that need to be backed up
|
2021-08-17 15:26:46 -07:00
|
|
|
DOTFILES_BACKUP_LIST=($(dot checkout 2>&1 | grep -Po "(?<=\t)(.*)$"))
|
2021-08-12 02:24:49 -07:00
|
|
|
# create directories for everything that needs one
|
2021-08-16 11:36:27 -07:00
|
|
|
mkdir -p "$DOTFILES_BACKUP_DIR" $(printf '%s\n' "${DOTFILES_BACKUP_LIST[@]}" | grep -Po '.*/' | sed "s#.*#$DOTFILES_BACKUP_DIR/&#g")
|
2022-04-08 13:49:06 -07:00
|
|
|
# backup existing files to avoid them being overwritten
|
2021-08-12 02:24:49 -07:00
|
|
|
printf '%s\n' "${DOTFILES_BACKUP_LIST[@]}" | xargs -I{} mv "$HOME"/{} "$DOTFILES_BACKUP_DIR"/{}
|
|
|
|
fi
|
|
|
|
|
|
|
|
dot checkout
|
|
|
|
dot config status.showUntrackedFiles no
|
|
|
|
|