-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.sh
executable file
·137 lines (107 loc) · 2.92 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
#!/bin/bash
#-----------------------------------------------------------------------------
# Functions
#-----------------------------------------------------------------------------
# Notice title
notice() { echo "=> $1"; }
# Error title
error() { echo "=> Error: $1"; }
# List item
c_list() { echo " ✔ $1"; }
# Error list item
e_list() { echo " ✖ $1"; }
# Check for dependency
dep() {
type -p $1 &> /dev/null
local installed=$?
if [ $installed -eq 0 ]; then
c_list $1
else
e_list $1
fi
return $installed
}
backup() {
mkdir -p $backupdir
local files=( $(ls -a) )
for file in "${files[@]}"; do
in_array $file "${excluded[@]}" || cp -Rf "$HOME/$file" "$backupdir/$file"
done
}
install() {
local files=( $(ls -a) )
for file in "${files[@]}"; do
in_array $file "${excluded[@]}"
should_install=$?
if [ $should_install -gt 0 ]; then
[ -d "$HOME/$file" ] && rm -rf "$HOME/$file"
cp -Rf "$file" "$HOME/$file"
fi
done
cp -Rf ".vim/vimrc" "$HOME/.vimrc"
cp -Rf ".vim/vimencrypt" "$HOME/.vimencrypt"
}
in_array() {
local hay needle=$1
shift
for hay; do
[[ $hay == $needle ]] && return 0
done
return 1
}
#-----------------------------------------------------------------------------
# Initialize
#-----------------------------------------------------------------------------
backupdir="$HOME/.dotfiles-backup/$(date "+%Y%m%d%H%M.%S")"
dependencies=(git tree vim virtualenv "virtualenvwrapper.sh")
excluded=(. .. .git .gitignore .gitmodules bootstrap.sh README.md LICENSE)
#-----------------------------------------------------------------------------
# Dependencies
#-----------------------------------------------------------------------------
notice "Checking dependencies"
not_met=0
for need in "${dependencies[@]}"; do
dep $need
met=$?
not_met=$(echo "$not_met + $met" | bc)
done
if [ $not_met -gt 0 ]; then
error "$not_met dependencies not met!"
exit 1
fi
#-----------------------------------------------------------------------------
# Install
#-----------------------------------------------------------------------------
# Assumes $HOME/.dotfiles is *ours*
if [ -d $HOME/.dotfiles ]; then
pushd $HOME/.dotfiles
# Update Repo
notice "Updating"
git pull origin master
git submodule init
git submodule update
git pull --recurse-submodules
# Backup
notice "Backup up old files ($backupdir)"
backup
# Install
notice "Installing"
install
else
# Clone Repo
notice "Downloading"
git clone --recursive https://github.com/tvieira/dotfiles.git $HOME/.dotfiles
pushd $HOME/.dotfiles
# Backup
notice "Backup up old files ($backupdir)"
backup
# Install
notice "Installing"
install
fi
#-----------------------------------------------------------------------------
# Finished
#-----------------------------------------------------------------------------
popd
notice "Done"
notice "Restart your terminal to get the configuration."