#!/usr/bin/env bash
set -eu -o pipefail # See: https://sipb.mit.edu/doc/safe-shell/
declare -r script_name=$(basename "${BASH_SOURCE[0]}")
declare -r backup_dir="$HOME"/backup_of_dotfiles_$(date "+%Y-%m-%d_%H%M%S")
declare -a dotfiles=(".bashrc" ".bash_profile" ".vimrc" ".editrc" ".gitconfig"
".gitignore" ".inputrc" ".tmux.conf" ".ssh/config" ".ripgreprc"
".gdbinit" ".config/gitui/key_bindings.ron" ".visidatarc"
".config/i3/config" ".config/i3status/config"
".config/dunst/dunstrc" ".config/alacritty/alacritty.toml")
declare -i dry_run=0
## exit the shell (with status 2) after printing the message
usage() {
echo "\
$script_name -hn
-h Print this help text
-n Perform a dry run, to see what'll change
"
exit 2;
}
## Process the options
while getopts "hn" OPTION
do
case $OPTION in
h) usage;;
n) dry_run=1;;
\?) usage;;
esac
done
if [ ! -d "${backup_dir}"/.ssh ]; then
((dry_run==0)) && mkdir -p "${backup_dir}"/.ssh
fi
# Move original dot files to backup
for i in "${dotfiles[@]}"
do
if [ -e "$HOME"/"$i" ]; then
if ! cmp --silent "$HOME"/"$i" "$i" ; then
echo "$i" will be changed as follows:
diff "$HOME"/"$i" "$i" || true
if [ $dry_run -eq 0 ]; then
mv "$HOME"/"$i" "${backup_dir}"/"$i"
# Consider using symbolic links instead
# so pulling updates automatically apply
cp "$i" "$HOME"/"$i"
echo "# vimdiff \"$HOME/$i\" \"${backup_dir}/$i\""
else
# Provide a diff that can still be used