dotfiles/install.sh

208 lines
7 KiB
Bash
Executable file

#!/bin/bash
# The directory where this script lives - used as the base for finding all
# of the configurations to be linked.
src_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# Colors for output
COLOR_SUCCESS='\033[0;32m'
COLOR_ERROR='\033[0;31m'
COLOR_NOTIFY='\033[1;33m'
NC='\033[0m' # No Color
# Default settings for this script
auto_backup=false
config_target="all"
# Installation sources
bash_source="$src_dir/shell/bashrc"
i3_source="$src_dir/i3/config"
xresources_source="$src_dir/xresources/Xresources"
neovim_source="$src_dir/neovim/init.vim"
git_source="$src_dir/git/gitconfig"
# Installation targets (symlinks to be created)
bash_target="$HOME/.bashrc"
i3_target="$HOME/.config/i3/config"
xresources_target="$HOME/.Xresources"
neovim_target="$HOME/.config/nvim/init.vim"
git_target="$HOME/.gitconfig"
# Backup names
backup_dir="$HOME/.dotfiles/backups"
bash_backup="$backup_dir/bashrc.backup"
i3_backup="$backup_dir/i3.config.backup"
xresources_backup="$backup_dir/Xresources.backup"
neovim_backup="$backup_dir/neovim.init.vim.backup"
git_backup="$backup_dir/global.git.backup"
function display_configs {
echo 'Supported config targets: all, bash, i3, xresources, neovim, git'
}
function display_usage {
echo 'Usage: install.sh [-c|--config <target>] [-a|--auto-backup]'
echo ' [-h|--help]'
echo ''
echo 'Install configurations.'
echo ''
echo ' -c|--config Describes what to install: Default=all'
echo " $(display_configs)"
echo ' -a|--auto-backup Automatically backup existing configs.'
echo ' -h|--help Display this usage text.'
echo ''
echo 'Examples:'
echo ''
echo "./install.sh --target=all --auto-backup"
}
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-c|--config)
config_target="$2"
shift
shift
;;
-a|--auto-backup)
auto_backup=true
shift
;;
-h|--help)
display_usage
exit 0
;;
*)
echo "Unrecognized option: $1"
display_usage
exit 1
;;
esac
done
case $config_target in
all|bash|i3|xresources|neovim|git)
;;
*)
echo "Unrecognized configuration target selected."
display_configs
exit 1
;;
esac
# Ensure the backup directory exists
if $auto_backup; then
if [ ! -d $backup_dir ]; then
if `mkdir $backup_dir`; then
echo -e "${COLOR_NOTIFY}[Note]${NC} created '$backup_dir' to store dotfile backups."
else
echo -e "${COLOR_ERROR}[Error]${NC} Failed to create backup directory '$backup_dir'."
fi
fi
fi
# Install bashrc
if [ "$config_target" = "all" ] || [ "$config_target" = "bash" ]; then
echo -e "\t- Installing .bashrc to '$bash_target'"
if [ -f $bash_target ]; then
# The file exists - we either create a backup and remove it, or fail.
if $auto_backup; then
echo -e "\t ${COLOR_NOTIFY}[Note]${NC} creating a backup of '$bash_target'"
cp $bash_target $bash_backup
rm $bash_target
ln -s $bash_source $bash_target
else
echo -e "\t ${COLOR_ERROR}[Error]${NC} File '$bash_target' already exists. Please manually remove it or set --auto-backup"
exit 1
fi
else
# The file doesn't exist, we can safely create a symlink.
ln -s $bash_source $bash_target
fi
fi
# Install i3 configuration
if [ "$config_target" = "all" ] || [ "$config_target" = "i3" ]; then
echo -e "\t- Installing i3 config to '$i3_target'"
if [ -f $i3_target ]; then
# The file exists - we either create a backup and remove it, or fail.
if $auto_backup; then
echo -e "\t ${COLOR_NOTIFY}[Note]${NC} creating a backup of '$i3_target'"
cp $i3_target $i3_backup
rm $i3_target
ln -s $i3_source $i3_target
else
echo -e "\t ${COLOR_ERROR}[Error]${NC} File '$i3_target' already exists. Please manually remove it or set --auto-backup"
echo "File '$i3_target' already exists. Please manually remove it or set --auto-backup"
exit 1
fi
else
# The file doesn't exist, we can safely create a symlink.
ln -s $i3_source $i3_target
fi
fi
# Install .Xresources
if [ "$config_target" = "all" ] || [ "$config_target" = "xresources" ]; then
echo -e "\t- Installing xresources config to '$xresources_target'"
if [ -f $xresources_target ]; then
# The file exists - we either create a backup and remove it, or fail.
if $auto_backup; then
echo -e "\t ${COLOR_NOTIFY}[Note]${NC} creating a backup of '$xresources_target'"
cp $xresources_target $xresources_backup
rm $xresources_target
ln -s $xresources_source $xresources_target
else
echo -e "\t ${COLOR_ERROR}[Error]${NC} File '$xresources_target' already exists. Please manually remove it or set --auto-backup"
echo "File '$xresources_target' already exists. Please manually remove it or set --auto-backup"
exit 1
fi
else
# The file doesn't exist, we can safely create a symlink.
ln -s $xresources_source $xresources_target
fi
fi
# Install neovim config
if [ "$config_target" = "all" ] || [ "$config_target" = "neovim" ]; then
echo -e "\t- Installing neovim config to '$neovim_target'"
if [ -f $neovim_target ]; then
# The file exists - we either create a backup and remove it, or fail.
if $auto_backup; then
echo -e "\t ${COLOR_NOTIFY}[Note]${NC} creating a backup of '$neovim_target'"
cp $neovim_target $neovim_backup
rm $neovim_target
ln -s $neovim_source $neovim_target
else
echo -e "\t ${COLOR_ERROR}[Error]${NC} File '$neovim_target' already exists. Please manually remove it or set --auto-backup"
echo "File '$neovim_target' already exists. Please manually remove it or set --auto-backup"
exit 1
fi
else
# The file doesn't exist, we can safely create a symlink.
ln -s $neovim_source $neovim_target
fi
fi
# Install git config
if [ "$config_target" = "all" ] || [ "$config_target" = "git" ]; then
echo -e "\t- Installing git config to '$git_target'"
if [ -f $git_target ]; then
# The file exists - we either create a backup and remove it, or fail.
if $auto_backup; then
echo -e "\t ${COLOR_NOTIFY}[Note]${NC} creating a backup of '$git_target'"
cp $git_target $git_backup
rm $git_target
ln -s $git_source $git_target
else
echo -e "\t ${COLOR_ERROR}[Error]${NC} File '$git_target' already exists. Please manually remove it or set --auto-backup"
echo "File '$git_target' already exists. Please manually remove it or set --auto-backup"
exit 1
fi
else
# The file doesn't exist, we can safely create a symlink.
ln -s $git_source $git_target
fi
fi