Skip to content

Instantly share code, notes, and snippets.

@baquaz
Last active June 18, 2024 19:38
Show Gist options
  • Save baquaz/296cb06e075ee9d715dd2058a44fb686 to your computer and use it in GitHub Desktop.
Save baquaz/296cb06e075ee9d715dd2058a44fb686 to your computer and use it in GitHub Desktop.
Automatic script to install dotfiles bare repo and back up all pre-existing files into '.config-backup' folder
#!/bin/bash
# Create the backup directory if it does not exist
mkdir -p $HOME/.config-backup
# Check if $HOME/.config exists
if [ -d "$HOME/.config" ]; then
if [ -L "$HOME/.config" ]; then
# It is a symlink!
echo "found a symlink: $HOME/.config"
rm "$HOME/.config"
echo "removed symlink: $HOME/.config !"
else
# It's a directory!
echo "found already existing directory: $HOME/.config"
# Check if the target directory already exists
if [ -d "$HOME/.config-backup/.config.old" ]; then
# Handle the existing target directory
# Option 1: Rename the existing target directory
mv $HOME/.config-backup/.config.old $HOME/.config-backup/.config.old.bak
# Option 2: Remove the existing target directory
# rm -rf $HOME/.config-backup/.config.old
fi
# Move the .config directory to the backup location
mv $HOME/.config $HOME/.config-backup/.config.old
echo "moved '.config' to the path: $HOME/.config-backup/.config.old"
fi
fi
# Check if $HOME/.github exists
if [ -d "$HOME/.github" ]; then
mv $HOME/.github $HOME/.config-backup/.github.old.bak
fi
git clone --bare https://github.com/baquaz/dotfiles.git $HOME/.config
function config {
/usr/bin/git --git-dir=$HOME/.config/ --work-tree=$HOME $@
}
config checkout
if [ $? = 0 ]; then
echo "Checked out config.";
else
echo "Backing up pre-existing files.";
mkdir -p .config-backup
config checkout 2>&1 | egrep "^\s+" | awk '{print $1}' | xargs -I{} mv {} .config-backup/{}
echo "Backup completed!. Retrying checkout..."
fi;
config checkout
config config status.showUntrackedFiles no
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment