-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathINSTALL
executable file
·170 lines (147 loc) · 2.96 KB
/
INSTALL
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
#!/usr/bin/env bash
#
# Simple script to link dots
#
declare -a SERVER_FILES=(".bash_logout" ".bashrc" ".inputrc" ".tmux.conf")
declare -a FILES=(".Xresources" ".bash_logout" ".bash_profile" ".bashrc" ".inputrc" ".xinitrc" ".tmux.conf")
function error_msg() {
printf "\e[1;31m%s\e[0m\n" "$1" 1>&2
return 0
}
function msg() {
local color
case "$1" in
green)
color="\e[1;32m"
shift
;;
brown)
color="\e[1;33m"
shift
;;
*)
color="\e[1;37m"
;;
esac
printf "$color%s\e[0m\n" "$@"
return 0
}
function link_file() {
# If file does not exist, don't try to link it
[[ ! -f $1 ]] && return 1
local file dpath lpath
file="${1#./}"
dpath="$PWD/$file"
if [[ $2 == "abs" ]]; then
lpath="/$file"
sudo ln -sf "$dpath" "$lpath"
else
lpath="$HOME/$file"
ln -sf "$dpath" "$lpath"
fi
return 0
}
function link_dir() {
local hdir ddir
ddir="$PWD/$1"
hdir="$HOME/$1"
if [[ -L $hdir ]]; then
# If a link, remove and remake it
rm -f "$hdir"
ln -sf "$ddir" "$hdir"
printf "\e[1;37mLinked %s \u2192 %s\e[0m\n" "$ddir" "$hdir"
elif [[ -d $hdir ]]; then
# Otherwise if it's a dir, prompt before overwriting
read -rp "$hdir exists. Remove and link directory? "
if [[ $REPLY =~ y|Y ]]; then
rm -rf "$hdir"
ln -sf "$ddir" "$hdir"
else
error_msg "Cannot link directory because it already exists."
fi
fi
return 0
}
function mkdir_if_nonexistant() {
if [[ ! -d $1 ]]; then
mkdir -p "$1"
msg "Created $1"
fi
return 0
}
function link_dir_files() {
find "./$1" -type d | while read -r dir; do
# Establish an absolute or relative path
local dest
if [[ $2 == "abs" ]]; then
dest="/${dir#./}"
else
dest="$HOME/${dir#./}"
fi
mkdir_if_nonexistant "$dest"
done
# Link the files
find "./$1" -type f | while read -r file; do
link_file "$file" "$2"
done
return 0
}
function full_install() {
msg "Performing full install"
# Link entire directories
msg "Linking directories"
link_dir ".bashrc.d"
link_dir ".bin"
link_dir ".icons"
link_dir ".themes"
# Link .config files
msg "Linking config files"
link_dir_files ".config"
# Ask to link system files
msg "brown" "Link etc system files? (y|n): "
read -r
if [[ $REPLY =~ y|Y ]]; then
link_dir_files "etc" "abs"
fi
# Link misc files
for file in "${FILES[@]}"; do
link_file "$file"
done
msg "green" "Done!"
}
function server_install() {
msg "Performing server install"
# Link entire directories
msg "Linking directories"
link_dir ".bashrc.d"
link_dir ".bin"
# Link .config files
msg "Linking nvim config"
link_dir_files ".config/nvim"
# Link misc files
for file in "${SERVER_FILES[@]}"; do
link_file "$file"
done
msg "green" "Done!"
}
msg "Perform (f)ull install or (s)erver install?"
printf "(f/s): "
read -r install_type
case "$install_type" in
f | F | full)
if ! full_install; then
error_msg "Install failed"
exit 1
fi
;;
s | S | server)
if ! server_install; then
error_msg "Install failed"
exit 1
fi
;;
*)
error_msg "Aborting"
exit 1
;;
esac