-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathINSTALL
executable file
·283 lines (232 loc) · 7.15 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env bash
#
# Script to install all of the correct symlinks
#
# Enable strict error handling
set -u
# Define constants
readonly CONFIGDIR='./config-rc'
# Declare variables
readonly POSSIBLEPARAMETERS="v"
verbose=false;
# Let's set some values based on the parameters
while getopts "$POSSIBLEPARAMETERS" opt; do
case $opt in
v) verbose=true;;
\?) echo "Invalid option: -$OPTARG"; exit 1;;
esac
done
# Get the current directory
readonly GITBOXDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Define an array of files/directories
readonly FILESANDDIRECTORIES=(
"$HOME/bash_completion.d"
"$HOME/.ssh/config"
"$HOME/.vim"
"$HOME/local"
# XDG Base Directory Specification paths
#
# See https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
#
# We shouldn't HAVE to do this, and rather just define the environment
# variables ("$XDG_CONFIG_HOME", etc), but some apps just hard-code the XDG
# paths and abuse the whole spec... which is a massive bummer... so we'll
# just symlink them like we do the others.
"$HOME/.config"
);
# Define an array for all of the config-rc files
shopt -s dotglob # Allow * to match hidden files
readonly CONFIGFILES=("$CONFIGDIR"/*)
shopt -u dotglob # Undo the allowing of * to match hidden files
# Define an array of Windows/Cygwin ONLY files/directories
readonly CYGFILELOCATIONS=(
"$HOME/_vimrc"
"$HOME/_gvimrc"
"$HOME/vimfiles"
"$HOME/.sift.conf"
"$HOME/.ConEmu.xml"
"$HOME/.wslttyrc"
"$HOME/.windows_terminal_settings.json"
);
# Define an array containing the destination of the Windows/Cygwin ONLY files/directories for linking
# MUST ALIGN WITH 'CYGFILELOCATIONS'
readonly CYGFILEDESTINATIONS=(
"$CONFIGDIR/.vimrc"
"$CONFIGDIR/.gvimrc"
".vim"
"$CONFIGDIR/.sift.conf"
"$CONFIGDIR/.ConEmu.xml"
"$CONFIGDIR/.minttyrc"
"$CONFIGDIR/.windows_terminal_settings.json"
);
# Create a function to echo out errors
error() {
# Let's get our arguments
local -r message=$1
# Echo out our message
echo -e "\033[00;31mERROR:\033[00m $message"
}
# Create a function to remove the passed target
removeTarget() {
# The target should be the first (only) argument
local -r TARGET=$1
# Define some local variables
local recursive=false # Whether we should remove recursively or not
local force=false # Whether we should ignore "rm" errors or not
local removeCommand="rm" # Our rm's argument
local status=1 # Let's have a variable to hold the return status
local possible=false # Keep track of whether its possible to remove
local message="" # Hold our message here
# Make sure the target exists before we bother doing anything else
if [ -e "$TARGET" ] || [ -L "$TARGET" ] ; then
# If the target is a symlink
if [ -L "$TARGET" ] ; then
possible=true
message="REMOVING:\tsymlink $TARGET"
# If we DON'T have access to the symlink's parent directory
if [ ! -w "$(dirname "$TARGET")" ]; then
# Don't allow the file to be removed
possible=false
# If the target doesn't actually exist
elif [ ! -e "$TARGET" ] ; then
message="REMOVING:\tbroken link $TARGET"
fi
# Let's make SURE that we have write permissions
elif [ -w "$TARGET" ] ; then
# If the target is a file
if [ -f "$TARGET" ] ; then
# Remove the original file... we're gonna link it instead
possible=true
message="REMOVING:\tfile $TARGET"
# If the target is a directory
elif [ -d "$TARGET" ] ; then
# Its a directory, so set this to remove recursively
recursive=true
# Remove the original directory... we're gonna link it instead
possible=true
message="REMOVING:\tdirectory $TARGET"
fi
fi
# Refine our remove command based on our options
if $recursive ; then
removeCommand="$removeCommand -r"
fi
if $force ; then
removeCommand="$removeCommand -f"
fi
# If its possible to make the delete
if $possible ; then
# Remove the target
eval "$removeCommand $TARGET"
# Save the return status
status=$?
# Verbose info
if $verbose && [ $status == 0 ] ; then
echo -e "$message"
fi
# I guess its not possible
else
# Display an error
error "insufficient permissions to delete $TARGET"
fi
fi
# Give a return code as an exit status
return $status
}
# Create a function to symbolically link the passed source to the passed target
symLink() {
# Let's have a variable to hold the return status
local status=1
# Let's get our arguments
local -r SOURCE=$1
local -r TARGET=$2
# Let's make SURE that the target doesn't already exist... or we might make a recursive symlink (AH!)
if [ ! -e "$TARGET" ] ; then
# Let's symbolically link them
ln -s "$SOURCE" "$TARGET"
# Save the return status
status=$?
# Verbose info
if $verbose && [ $status == 0 ] ; then
echo -e "LINKING:\t$SOURCE -> $TARGET"
fi
# The target must already exist
else
# Display an error
error "location $TARGET already exists"
fi
# Give a return code as an exit status
return $status
}
# Create a function to get the Windows equivalent path of a given path
windowsPath() {
# Define our return value
local windowsPath
# Check if we're running CYGWIN
if [[ $OSTYPE == "cygwin" ]] ; then
windowsPath="$(cygpath -w "$1")"
else
# Convert our path to a windows path
windowsPath="${1#\/mnt\/}"
windowsPath="${windowsPath//\//\\}"
windowsPath="${windowsPath/\\/:\\}"
fi
echo "$windowsPath"
}
# Let's loop through each file/directory
for target in "${FILESANDDIRECTORIES[@]}"
do
# Get the path relative to the stored directory
gitBoxPath="${target#"$HOME"/}"
# Use the removeTarget function and pass the target as an argument
removeTarget "$target"
# Relink the file
symLink "$GITBOXDIR/$gitBoxPath" "$target"
done;
# Let's loop through each config file
for target in "${CONFIGFILES[@]}"
do
# Get the basename
baseName="$(basename "$target")"
# Use the removeTarget function
removeTarget "$HOME/$baseName"
# Relink the file
symLink "$GITBOXDIR/$target" "$HOME/$baseName"
done;
# Check if we're running CYGWIN or "Bash on Windows"
if [[ $OSTYPE == "cygwin" ]] || [ -e /proc/version ] && grep -qi Microsoft /proc/version ; then
# Create a quick counter, to keep our two cygFile arrays aligned
count=0
windowsOutputSuppressor=''
directoryFlag=''
# Not using verbose output?
if [ "$verbose" != true ] ; then
# Suppress the output
windowsOutputSuppressor='>nul'
fi
# Let's loop through each file/directory
for cygTarget in "${CYGFILELOCATIONS[@]}"
do
cygDestination="$GITBOXDIR/${CYGFILEDESTINATIONS[$count]}"
# If the target is a directory
if [ -d "$cygDestination" ] ; then
directoryFlag='/d '
else
directoryFlag=''
fi
# Use the removeTarget function and pass the target as an argument
removeTarget "$cygTarget"
# Get the windows target path
windowsTargetPath="$(windowsPath "$cygTarget")";
# Get the windows destination path
windowsDestinationPath="$(windowsPath "$cygDestination")";
# Use cmd (windows native command prompt) to setup Windows NT Symbolic Links
cmd.exe /c mklink $directoryFlag"$windowsTargetPath" "$windowsDestinationPath" $windowsOutputSuppressor
# Increment the counter
((count++))
done;
fi
# Let's change the permissions of some files
chmod 600 ./.ssh/config
# Exit gracefully (positive/good exit code)
exit 0