-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·146 lines (117 loc) · 3.06 KB
/
install.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
144
145
146
#!/usr/bin/env bash
{ # this ensures the entire script is downloaded #
git_repo_url='https://github.com/marcioapm/dotfiles.git'
__os() {
printf %s "$(uname -s)"
}
__is_darwin() {
[ "$(ebs_os)" == "Darwin" ]
}
__is_linux() {
[ "$(ebs_os)" == "Linux" ]
}
__has() {
type "$1" > /dev/null 2>&1
}
__script_path() {
printf %s "$(cd "$(dirname "$0")"; pwd -P)"
}
__install_dir() {
printf %s "${DOTFILES_DIR:-"$HOME/git/dotfiles"}"
}
__detect_profile() {
if [ "${PROFILE-}" = '/dev/null' ]; then
return
fi
if [ -n "${PROFILE}" ] && [ -f "${PROFILE}" ]; then
echo "${PROFILE}"
return
fi
local profile
profile=''
if [ -n "${BASH_VERSION-}" ]; then
if [ -f "$HOME/.bashrc" ]; then
profile="$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
profile="$HOME/.bash_profile"
fi
elif [ -n "${ZSH_VERSION-}" ]; then
profile="$HOME/.zshrc"
fi
if [ -z "$profile" ]; then
for eachprof in ".profile" ".bashrc" ".bash_profile" ".zshrc"
do
if [ -f "$HOME/$eachprof" ]; then
profile="$eachprof"
break
fi
done
fi
if [ ! -z "$profile" ]; then
echo "$profile"
fi
}
__ensure_profile() {
local profile=$(__detect_profile)
if [ ! -z "$profile" ]; then
echo "=> shell profile detected at ${profile}"
else
echo "=> shell profile not detected"
echo "=> creating a shell profile at ~/.bash_profile"
touch ~/.bash_profile
fi
}
__ensure_line() {
if ! grep -Fqc "$1" "$2"; then
printf "\n$1\n" >> "$2"
fi
}
__ensure_line_regex() {
if ! grep -qc "$3" "$2"; then
printf "\n$1\n" >> "$2"
fi
}
__change_profile() {
local profile="$(__detect_profile)"
local dir="$(__install_dir)"
__ensure_line "export DOTFILES_DIR=$dir" "$profile" "export DOTFILES_DIR="
__ensure_line "source \$HOME/.config/bash/profile" "$profile" "source \$HOME/.config/bash/profile"
}
__clone_dotfiles() {
local dir="$(__install_dir)"
if [ -d "$dir/.git" ]; then
echo "=> git repository already exists - updating..."
git --git-dir="$dir"/.git --work-tree="$dir" pull --rebase --recurse-submodules --autostash 1> /dev/null || {
echo >&2 "=> failed to update git repository! check your permissions!"
exit 1
}
else
mkdir -p "$dir"
git clone --recurse-submodules "$git_repo_url" "$dir" || {
echo >&2 "=> failed to clone git repository! check your permissions!"
exit 1
}
fi
}
__install_home() {
local dir="$(__install_dir)"
echo "=> installing home directory..."
cp -Rvau $dir/home/. $HOME/
}
__install() {
if ! __has git; then
echo >&2 "=> you need git to install"
exit 1
fi
echo "=> os: $(__os)"
echo "=> install at $(__install_dir)"
__clone_dotfiles
__install_home
__ensure_profile
__change_profile
echo "=> all done! re-open terminal and enjoy!"
}
__install
unset __install __clone_dotfiles __install_home __change_profile __ensure_line_regex __ensure_line \
__ensure_profile __detect_profile __install_dir __script_path __has __is_linux __is_darwin __os
} # this ensures the entire script is downloaded #