-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepareHome.sh
executable file
·152 lines (134 loc) · 5.98 KB
/
prepareHome.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
147
148
149
150
151
152
#!/usr/bin/env bash
set -e
declare force absoluteScriptDir
force=false
declare -a files_to_be_linked dirs_to_be_linked
files_to_be_linked=( "bash_profile" "bashrc" "gitconfig" "pythonrc" "tmux.conf" "vimrc" "bash_completion" "blerc" )
dirs_to_be_linked=( "tmux" )
declare -i temp1 temp2 temp3
function getScriptAbsoluteDir() {
(
unset CDPATH
absoluteScriptDir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
printf "%s\\n" "${absoluteScriptDir}"
)
}
absoluteScriptDir=$(getScriptAbsoluteDir)
function makeSymLinkAtHomeDir() {
local toBeLinked="${1}"
if [ "${force}" = true ]; then
ln -fs "${absoluteScriptDir}"/"${toBeLinked}" "${HOME}"/."${toBeLinked}"
printf "Link from %s/%s created at %s/.%s\\n" "${absoluteScriptDir}" "${toBeLinked}" "${HOME}" "${toBeLinked}"
else
if [ -f "${HOME}"/."${toBeLinked}" ]; then
printf "File %s/.%s already exists. Overwrite? (y/N)\\n" "${HOME}" "${toBeLinked}"
read -r substitute
substitute=${substitute:-N}
if [ "${substitute}" = "n" ] || [ "${substitute}" = "N" ]; then
printf "%s/.%s was not overwritten.\\n" "${HOME}" "${toBeLinked}"
elif [ "${substitute}" = "y" ] || [ "${substitute}" = "Y" ]; then
ln -fs "${absoluteScriptDir}"/"${toBeLinked}" "${HOME}"/."${toBeLinked}"
printf "%s/.%s overwritten.\\n" "${HOME}" "${toBeLinked}"
else
printf "'%s' isn't a recognized option.\\n" "${substitute}"
fi
elif [ -d "${HOME}"/."${toBeLinked}" ]; then
printf "Directory %s/.%s already exists. Rename? (y/N)\\n" "${HOME}" "${toBeLinked}"
read -r substitute
substitute=${substitute:-N}
if [ "${substitute}" = "n" ] || [ "${substitute}" = "N" ]; then
printf "%s/.%s was not renamed.\\n" "${HOME}" "${toBeLinked}"
elif [ "${substitute}" = "y" ] || [ "${substitute}" = "Y" ]; then
mv "${HOME}"/."${toBeLinked}" "${HOME}"/."${toBeLinked}".old
printf "%s/.%s renamed to %s/.%s.old\\n" "${HOME}" "${toBeLinked}" "${HOME}" "${toBeLinked}"
ln -fs "${absoluteScriptDir}"/"${toBeLinked}" "${HOME}"/."${toBeLinked}"
printf "Link from %s/%s created at %s/.%s\\n" "${absoluteScriptDir}" "${toBeLinked}" "${HOME}" "${toBeLinked}"
else
printf "'%s' isn't a recognized option.\\n" "${substitute}"
fi
else
force=true
makeSymLinkAtHomeDir "${toBeLinked}"
force=false
fi
fi
}
function cloneGitSubmodules() {
pushd "${absoluteScriptDir}"
git submodule update --recursive --init
popd
}
function control_c() {
trap - SIGINT
cloneGitSubmodules
}
if [ "$#" -eq 0 ]; then
for file in "${files_to_be_linked[@]}"; do
if [ ! -f "${absoluteScriptDir}"/"${file}" ]; then
printf "File %s/%s does not exist inside ${absoluteScriptDir}.\\n" "${absoluteScriptDir}" "${file}"
exit 13
fi
done
for dir in "${dirs_to_be_linked[@]}"; do
if [ ! -d "${absoluteScriptDir}"/"${dir}" ]; then
printf "Directory %s/%s does not exist inside ${absoluteScriptDir}.\\n" "${absoluteScriptDir}" "${dir}"
exit 14
fi
done
printf "Files to be linked:\\n"
for ((temp1=0; temp1 < "${#files_to_be_linked[@]}"; temp1++)); do
printf "[%s]: %s\\n" "${temp1}" "${files_to_be_linked[${temp1}]}"
done
printf "\\nDirectories to be linked:\\n"
for ((temp2=0; temp2 < "${#dirs_to_be_linked[@]}"; temp2++)); do
(( temp3=temp1+temp2 ))
printf "[%s]: %s\\n" "${temp3}" "${dirs_to_be_linked[${temp2}]}"
done
(( temp3=temp1+temp2 ))
printf "\\n[%s]: All options above\\n" "${temp3}"
trap control_c SIGINT
printf "Press Ctrl + C to clone/update the submodules.\\n\\n"
printf "Which option would you like to create a link in your home directory?\\n"
printf "Or would you like to create a link to all options above?\\n"
while IFS= read -rp "> " option; do
if [ "${option}" -eq "${temp3}" ]; then
force=true
for ((temp1=0; temp1 < "${#files_to_be_linked[@]}"; temp1++)); do
makeSymLinkAtHomeDir "${files_to_be_linked[${temp1}]}"
done
for ((temp2=0; temp2 < "${#dirs_to_be_linked[@]}"; temp2++)); do
makeSymLinkAtHomeDir "${dirs_to_be_linked[${temp2}]}"
done
kill -SIGINT $$
else
if [ "${option}" -gt ${temp3} ] || [ "${option}" -lt "$(( temp1 - temp1 ))" ]; then
printf "%s is not a valid choice.\\n" "${option}"
printf "It must be between %i and %s.\\n" "$(( temp1 - temp1 ))" "${temp3}"
elif [ "${option}" -ge "$(( temp1 - temp1 ))" ] && [ "${option}" -lt "${temp1}" ]; then
makeSymLinkAtHomeDir "${files_to_be_linked[${option}]}"
elif [ "${option}" -ge "${temp1}" ] && [ "${option}" -lt "${temp3}" ]; then
option=$(( option - temp1 ))
makeSymLinkAtHomeDir "${dirs_to_be_linked[${option}]}"
fi
fi
done
else
if [ "${1,,}" == "all" ] || [ "$1" -gt $(( "${#files_to_be_linked[@]}"+"${#dirs_to_be_linked[@]}" )) ]; then
temp3=$(( "${#files_to_be_linked[@]}"+"${#dirs_to_be_linked[@]}" ))
elif [ "$1" -gt 0 ] && [ "$1" -le $(( "${#files_to_be_linked[@]}"+"${#dirs_to_be_linked[@]}" )) ]; then
temp3="$1"
else
printf "%s is not a valid value for files/dirs to be linked.\\n" "$1"
exit 15
fi
if [ "${3,,}" == "force" ]; then
force=true
fi
for ((temp1=0; temp1 < temp3 - "${#dirs_to_be_linked[@]}"; temp1++)); do
makeSymLinkAtHomeDir "${files_to_be_linked[${temp1}]}"
done
for ((temp2=0; temp2 < temp3 - "${#files_to_be_linked[@]}"; temp2++)); do
makeSymLinkAtHomeDir "${dirs_to_be_linked[${temp2}]}"
done
cloneGitSubmodules
fi