-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.sh
executable file
·142 lines (115 loc) · 2.93 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env zsh
# fast copy dotfiles config, includes shell, brew.
set -e
declare -r APP_PATH="${HOME}/.dotfiles"
declare -r APP_NAME="dotfiles"
declare -r REPO_URL="https://github.com/clay-f/dotfiles.git"
debug_mode=0
msg() {
printf '%b\n' "$1" >&2
}
success() {
if [[ "$?" -eq 0 ]]; then
msg "\33[32m[✔]\33[0m ${1}${2}"
fi
}
error() {
msg "\33[31m[✘]\33[0m ${1}${2}"
exit 1
}
debug() {
if [[ $debug_mode -eq 1 && $ret -gt 1 ]]; then
msg "An error occurred in function ${FUNCNAME[1]}, ${BASH_LINENO[1]}, sorry for that."
fi
}
program_exists() {
local ret='0'
command -v "$1" >/dev/null 2>&1 || { local ret='1'; }
if [[ "$ret" -ne 0 ]]; then
return 1
fi
return 0
}
program_must_exist() {
program_exist $1
if [[ "$?" -ne 0 ]]; then
error "You muse have '$1' installed to continue."
fi
}
sync_repo() {
local repo_path="$1"
local repo_url="$2"
local repo_branch="$3"
msg "Trying to update $APP_NAME"
if [[ ! -e "$repo_path" ]]; then
mkdir -p "$repo_path"
git clone "$repo_url" "$repo_path"
success "Successfully updated $APP_NAME"
else
cd "$repo_path" && git pull origin "$repo_branch"
success "Successfully updated $APP_NAME"
fi
}
do_backup() {
if [[ -e "$1" ]] || [[ -e "$2" ]] || [[ -e "$3" ]]; then
msg "Attempting to back up your original dotfiles configration."
today=$(date +%Y%m%d_%s)
for i in "$1" "$2" "$3"; do
[[ -e "$i" ]] && [[ -L "$i" ]] && mv -v "$i" "$i.$today"
done
success "Your original dotfiles configuration has been backed up."
fi
}
lnif() {
if [[ -e "$1" ]]; then
ln -sf "$1" "$2"
fi
ret="$?"
debug
}
create_symlinks() {
local source_path="$1"
local target_path="$2"
lnif "$source_path/git/gitignore/macOS.gitignore" "$target_path/.gitignore_global"
cp "$source_path/git/gitconfig" "$target_path/.gitconfig"
cp "$source_path/tools/help_do.sh" "$target_path/.help_do.sh"
ret="$?"
success "Setting up link files"
debug
}
brew_config_tools() {
if [[ $count -gt 0 ]]; then
config_brew_and_relate_tools
(zsh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)")
fi
cat <<- EOF >> ~/.zshrc
[[ -f "$HOME/.help_do.sh" ]] && source ~/.help_do.sh
EOF
ret="$?"
debug
}
# install brew stuff, init emacs
config_brew_and_relate_tools() {
program_exists "brew"
if [[ "$?" -ne 0 ]]; then
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
(program_must_exist brew) && zsh "$APP_PATH/etc/brew.sh"
fi
if [[ ! -e ~/.emacs.d ]]; then
mkdir ~/.emacs.d
fi
cp -f ${APP_PATH}/etc/emacs_init_config.el ~/.emacs.d/init.el
ret="$?"
debug
}
main() {
do_backup "$HOME/.dotfiles"
sync_repo "$HOME/.dotfiles" \
"$REPO_URL" \
"master"
create_symlinks "${APP_PATH}" \
"${HOME}"
uanme -s | grep -i 'darwin' >/dev/null && source brew_config_tools
msg "config dotfiles succeed"
}
main