-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·143 lines (127 loc) · 3.76 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
#!/bin/sh
# Script to install dotfiles
# Written in POSIX sh for compatibility
set -e
todir="${HOME}"
vim_plugins_dir=~/.vim/plugged
vim_plug_file=~/.vim/autoload/plug.vim
show_help() {
cat << EOF
${0} [OPTION]...
Install dotfiles
Options:
--help Show help
--un Uninstall by removing symlinks
--clean Remove installed vim-plug plugins before installing
EOF
}
uninstall=0
clean=0
# Flag arg parsing
for i in "$@"; do
case ${i} in
--help)
show_help
exit 0
;;
--clean)
clean=1
;;
--un)
uninstall=1
;;
esac
done
# Clean plugins if required
if [ -d "${vim_plugins_dir}" ] && [ "${clean}" -eq 1 ]; then
echo "Removing installed vim plugins..."
rm -r "${vim_plugins_dir}"
fi
# Backup an exiting file
# Argment 1: File path
backup_file() {
backup_file="${1}.bak"
if [ -e "${backup_file}" ]; then
echo "Could not backup ${1} to ${backup_file} file already exists"
exit 1
fi
echo "Backing up ${1} to ${backup_file}"
mv "${1}" "${1}.bak"
}
# Create a symlink to file under current dir
# Argument 1: Path to file / dir to be linked to (relative to current dir)
# Argument 2: Path to directory link should be placed in
make_link() {
linkname="${2}/${1}"
if [ -h "${linkname}" ]; then
echo "${linkname} was already a symlink"
else
if [ -f "${linkname}" ]; then
backup_file "${linkname}"
fi
ln -s "${PWD}/${1}" "${linkname}"
echo "Linked ${linkname} to ${PWD}/${1}"
fi
}
# Remove a symlink if it exists
# Same arguments as make_link above
remove_link() {
linkname="${2}/${1}"
if [ -h "${linkname}" ]; then
rm "${linkname}"
echo "Removed symlink ${linkname}"
else
echo "Symlink ${linkname} doesn't exist"
fi
}
# Process a symlink by creating it if installing or deleting if uninstalling
# Same arguments as make_link above
process_link() {
if [ "${uninstall}" -eq 1 ]; then
remove_link "$1" "$2"
else
make_link "$1" "$2"
fi
}
# Symlink dotfiles
find "${PWD}" -maxdepth 1 -type f -name '.*' | while read -r path; do
file=$(basename "${path}")
process_link "${file}" "${todir}"
done
# link from inside .vim
mkdir -p "${todir}/.vim"
for vpath in "${PWD}"/.vim/*; do
vfile=$(basename "${vpath}")
process_link ".vim/${vfile}" "${todir}"
done
# Link .config files
find .config -type f | while read -r cpath; do
cdir=$(dirname "${cpath}")
# TODO: handle putting vscode config in ~/Library/Application Support/Code on Macos
mkdir -p "${todir}/${cdir}"
process_link "${cpath}" "${todir}"
done
# Symlinked directories
process_link ".git_template" "${todir}"
process_link ".pandoc" "${todir}"
# Install vim-plug if required
if [ ! -f "${vim_plug_file}" ]; then
# Use either wget or curl since some os's come with only wget (like alpine) while others come with only curl (like macOS)
if [ -x "$(command -v curl)" ]; then
echo "Installing vim-plug with curl"
mkdir -p "$(dirname ${vim_plug_file})"
curl --fail -o "${vim_plug_file}" https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
elif [ -x "$(command -v wget)" ]; then
echo "Installing vim-plug with wget"
mkdir -p "$(dirname ${vim_plug_file})"
wget -O "${vim_plug_file}" https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
else
echo "Neither wget or curl are installed, skipping vim plugin installation"
fi
fi
# Run PlugInstall if the directory is missing and we have vim plug installed
if [ -f "${vim_plug_file}" ] && [ ! -d "${vim_plugins_dir}" ]; then
echo "Installing vim plugins..."
vim --not-a-term +PlugInstall +qall > /dev/null
fi
echo "Done!"