-
Notifications
You must be signed in to change notification settings - Fork 2
/
install_dotfiles.sh
executable file
·61 lines (49 loc) · 1.75 KB
/
install_dotfiles.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
#########
# Setup #
#########
# We'll be changing these for the installation... :)
shopt -s nullglob &> /dev/null # Expand no matches to empty string
shopt -s globstar &> /dev/null # Enable ** globbing in bash
# Utility function to recursively install into a given directory any files matching the given
# extension. Makes a back up of any existing files that will be replaced by appending ".bak" to
# their name.
#
# Args:
# install_dir: Directory to install into
# file_ext: File extension to match
# install_cmd (optional): Command to use for installation. Defaults to "cp".
install_ext() {
if [[ $# -ne 1 && $# -ne 2 && $# -ne 3 ]]; then
return 1 # Return error if not given right arguments
fi
local install_dir="$1"
local file_ext="$2"
local install_cmd="${3:-cp}"
for file_to_install in "$DOTFILES"/**/*."${file_ext}"; do
base_file_name=${file_to_install##*/}
file_dest="$install_dir"/."${base_file_name%.$file_ext}" # Prepend $install_dir, make it a dotfile, and strip the trailing file extension
echo "Installing $file_dest"
if [[ -e $file_dest ]]; then
mv "$file_dest" "$file_dest".bak # Back up currently install configuration to .bak
echo " Original $file_dest has been backed up to $file_dest.bak"
fi
$install_cmd "$file_to_install" "$file_dest"
done
}
################
# Installation #
################
# Install .local files
echo "Installing local dotfiles..."
echo
install_ext "$HOME" "local"
# Install .symlink files
echo "Installing symlinked files..."
echo
install_ext "$HOME" "symlink" "ln -s"
############
# Clean up #
############
# And now turn off our (probably) changed settings
shopt -u nullglob globstar &> /dev/null