dotfiles/.local/bin/dotfiles-install.sh

47 lines
1.4 KiB
Bash
Raw Normal View History

#!/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
# credit for this idea and basic script outline goes to https://www.atlassian.com/git/tutorials/dotfiles
## shellcheck
# Allow variables in single quotes
# shellcheck disable=SC2016
# read -d is not posix-complient (but it exists in sh on my machine, so it's probably fine)
# shellcheck disable=SC3045
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"
2021-08-12 02:24:49 -07:00
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'
2021-08-12 02:24:49 -07:00
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
2021-08-12 02:24:49 -07:00
echo "Backing up pre-existing dotfiles."
mkdir -p "$DOTFILES_BACKUP_DIR"
2021-08-12 02:24:49 -07:00
# get the list of files that need to be backed up
dot checkout 2>&1 | grep -Po "(?<=\t)(.*)$" |
while read -rd '' LINE; do
LINE_DIR="${LINE%%/*}"
mkdir -p "$DOTFILES_BACKUP_DIR/${LINE_DIR#"$HOME"}/${LINE##*/}"
mv "$FILE" "$DOTFILES_BACKUP_DIR"
done
2021-08-12 02:24:49 -07:00
fi
dot checkout
dot config status.showUntrackedFiles no
echo "Done"