-
Notifications
You must be signed in to change notification settings - Fork 2
/
bootstrap.sh
executable file
·143 lines (110 loc) · 3.01 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
143
#!/bin/bash
#-----------------------------------------------------------------------------
# Functions
#-----------------------------------------------------------------------------
# Notice title
notice() { echo -e "\033[0;34m=> $1\033[0m"; }
# Error title
error() { echo -e "\033[0;31m=> Error: $1\033[0m"; }
# List item
c_list() { echo -e " \033[0;34m✔\033[0m $1"; }
# Error list item
e_list() { echo -e " \033[0;31m✖\033[0m $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
}
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 vim)
excluded=(. .. .git .gitignore bootstrap.sh Gemfile Gemfile.lock Rakefile README.md .DS_Store)
#-----------------------------------------------------------------------------
# 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
# Backup
notice "Backup up old files ($backupdir)"
backup
# Install
notice "Installing"
install
else
# Clone Repo
notice "Downloading"
git clone --recursive git://github.com/rishabhmhjn/dotfiles.git $HOME/.dotfiles
pushd $HOME/.dotfiles
# Backup
notice "Backup up old files ($backupdir)"
backup
# Install
notice "Installing"
install
fi
#-----------------------------------------------------------------------------
# Finished
#-----------------------------------------------------------------------------
popd
source $HOME/.bashrc
notice "Done"
# exec $SHELL -l
notice "Please add the following to your .bashrc or .bash_profile"
cat <<END
# Common junk
[[ -s "$HOME/.commonrc" ]] && source "$HOME/.commonrc"
END