-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·173 lines (134 loc) · 4.97 KB
/
install.sh
File metadata and controls
executable file
·173 lines (134 loc) · 4.97 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/bash
platform=""
backup_dir="${HOME}/dotfilesbackup"
update_dir="${HOME}/.dotfiles"
system_deps=""
install_system_package() {
[[ -z "${*// }" ]] && return 0
if [[ "${platform}" == "wsl" ]] || [[ "${platform}" == "unix" ]]; then
if [ -n "$(command -v apt-get)" ]; then
sudo apt-get update -qq && sudo apt-get install ${@} -y
elif [ -n "$(command -v yum)" ]; then
sudo yum update && sudo yum install ${@}
else
>&2 echo "Installing system packages currently not compatibile with your package manager"
return 1
fi
else
>&2 echo "Unknown platform"
return 1
fi
}
confirm() {
local prompt default reply pdefault
prompt="${1}"
if [ "${2:-}" = "Y" ]; then
pdefault="Y/n"
default=Y
elif [ "${2:-}" = "N" ]; then
pdefault="y/N"
default=N
else
pdefault="y/n"
default=
fi
while true; do
# Ask the question (not using "read -p" as it uses stderr not stdout)
echo -n "$prompt [$pdefault] "
# Read the answer (use /dev/tty in case stdin is redirected from somewhere else)
read -r reply </dev/tty
# Default?
[ -z "${reply// }" ] && reply=$default
# Check if the reply is valid
case "$reply" in
Y*|y*) return 0 ;;
N*|n*) return 1 ;;
esac
done
}
confirm_string() {
local prompt default reply pdefault
prompt="${1}"
default="${2}"
# Default?
[[ -n "${default// }" ]] && pdefault=" [${default}]" || pdefault=""
# Ask the question
read -r -p "$prompt$pdefault " reply
# Empty reply?
echo "${reply:-$default}"
}
generic_install() {
cd "${update_dir}/${platform}" > /dev/null || return
# Create directory structure in $HOME
find . ! -path . -and ! -path "./.git/*" -and ! -path "./.git" -type d | xargs -i mkdir -p "${HOME}/{}"
# Copy files to $HOME
find . -type f | xargs -i cp "{}" "${HOME}/{}"
# install vim plug
curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
}
backup_existing_files() {
mkdir -p "${backup_dir}" || return
cd "${update_dir}/${platform}" > /dev/null || return
# Create directory structure in $backup_dir
find . ! -path . -and ! -path "./.git/*" -and ! -path "./.git" -type d | xargs -i mkdir -p "${backup_dir}/{}"
# Copy files to $backup_dir
find . -type f | xargs -i cp "${HOME}/{}" "${backup_dir}/{}" 2> /dev/null
}
view_diff() {
cd "${update_dir}/${platform}" > /dev/null || return
find . -type f | xargs -i colordiff "${HOME}/{}" "{}" 2> /dev/null
}
preinstall() {
mkdir -p "${update_dir}" || return
cd "${update_dir}" > /dev/null || return
git init -q
git remote add -f origin https://github.com/pingwindyktator/dotfiles > /dev/null
git config core.sparseCheckout true
echo "${platform}" >> .git/info/sparse-checkout
git pull -q origin master
cd "${platform}" > /dev/null || return
response=$(confirm_string "Enter git user.name" "$(git config --get user.name)")
find . -type f | xargs -i sed -i "s/##git_name##/${response}/g" "{}"
response=$(confirm_string "Enter git user.email" "$(git config --get user.email)")
find . -type f | xargs -i sed -i "s/##git_email##/${response}/g" "{}"
response=$(confirm_string "Enter git user.signingKey" "$(git config --get user.signingKey)")
find . -type f | xargs -i sed -i "s/##git_signingKey##/${response}/g" "{}"
}
postinstall() {
rm -rf "${update_dir}"
sudo -k # No need for `sudo` anymore
}
detect_platform() {
if [[ "$(expr substr "$(uname -s)" 1 5)" == "Linux" && ("$(uname -a)" == *"Microsoft"* || "$(uname -a)" == *"microsoft"*) ]]; then
platform="wsl"
system_deps="git vim colordiff mawk gawk silversearcher-ag exuberant-ctags fonts-powerline curl dos2unix"
elif [[ "$(expr substr "$(uname -s)" 1 5)" == "Linux" ]]; then
platform="unix"
system_deps="git vim colordiff xdotool wmctrl mawk gawk silversearcher-ag exuberant-ctags fonts-powerline curl libnotify-bin"
else
>&2 echo "Unknown platform"
return 1
fi
}
assert_compatibility() {
if [[ ! $(git --version | awk '{print $3}') > 1.7.0 ]]; then
>&2 echo "Requires git version 1.7.0 or higher, you've got $(git --version | awk '{print $3}')"
return 1
fi
}
main() {
detect_platform || exit 1
confirm "Install dotfiles of $platform platform?" Y || exit 0
install_system_package "${system_deps}" || exit 1
assert_compatibility || exit 1
preinstall
confirm "View diff of replaced files?" N && view_diff
confirm "Backup existing files to $backup_dir?" N && backup_existing_files
generic_install
postinstall && echo "Done!"
}
if (( ${BASH_VERSION%%.*} < 4 )) ; then
>&2 echo "Requires bash version 4.0.0 or higher, you've got ${BASH_VERSION}"
return 1
fi
main