-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.sh
executable file
·105 lines (91 loc) · 3.03 KB
/
bootstrap.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/bash
set -euo pipefail # https://gist.github.com/robin-a-meade/58d60124b88b60816e8349d1e3938615
IFS=$'\n\t'
# Bootstrap a new Ubuntu or MacOS computer. Assumes a minimalist OS
# installation as a starting point.
# Variables
REPO_URL="https://github.com/benglazer/dotfiles.git"
DOTFILES_DIR="${HOME}/.dotfiles"
DOTFILES_BACKUP="${HOME}/.dotfiles-backup"
STOW_TARGETS=(bash git vim)
# Ensure the script is run as a normal user, not root
if [ "$(id -u)" = "0" ]; then
echo "This script should not be run as root. Please run it as a normal user, without sudo."
exit 1
fi
install_dependencies() {
# Install and update OS package managers
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
echo "Updating apt sources."
sudo apt-get update && sudo apt-get upgrade -y
echo "Installing minimal dependencies."
sudo apt-get install -y git stow
elif [[ "$OSTYPE" == "darwin"* ]]; then
echo "Installing homebrew."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
echo "Installing minimal dependencies."
brew install git stow
else
echo "Unsupported OS. Cannot continue."
exit 1
fi
}
clone_dotfiles_repo() {
if [ ! -d "${DOTFILES_DIR}" ]; then
echo "Cloning dotfiles git repo from ${REPO_URL}"
git clone "${REPO_URL}" "${DOTFILES_DIR}"
else
echo "Using existing dotfiles directory."
fi
}
backup_existing_dotfiles() {
echo "Backing up existing dotfiles."
cd "${DOTFILES_DIR}" || return
for dotfile_group in "${STOW_TARGETS[@]}" ; do
cd "${dotfile_group}" || return
find . -type f -exec sh -c '
if [ -f "${HOME}/$1" ] ; then
mkdir -pv "$(dirname "$2/${dotfile_group}/$1")"
mv -v "${HOME}/$1" \
"$2/${dotfile_group}/$1"
fi' shell {} "${DOTFILES_BACKUP}" \;
cd .. || return
done
}
install_dotfiles() {
echo "Installing dotfiles via stow."
cd "${DOTFILES_DIR}" || return
stow "${STOW_TARGETS[@]}"
}
install_optional_packages() {
echo "Installing new packages."
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
sudo xargs -a "${DOTFILES_DIR}/installers/apt-install.txt" sudo apt-get install -y
elif [[ "$OSTYPE" == "darwin"* ]]; then
brew bundle --file="${DOTFILES_DIR}/installers/Brewfile"
fi
}
post_install_config() {
git config --global core.excludesfile "${HOME}/.gitignore"
if [[ "$OSTYPE" == "darwin"* ]]; then
git config --global credential.helper "osxkeychain"
fi
}
run_installers() {
echo "Running installers."
source "${DOTFILES_DIR}/installers/ssh.sh"
source "${DOTFILES_DIR}/installers/pyenv.sh"
}
main() {
pushd . > /dev/null
install_dependencies
clone_dotfiles_repo
backup_existing_dotfiles
install_dotfiles
install_optional_packages
post_install_config
run_installers
popd > /dev/null || return
}
main
echo "Bootstrap complete. Reboot to verify everything is working as expected."