-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstall.sh
executable file
·172 lines (147 loc) · 4.32 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
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
#!/bin/bash
#
# Minifies and symlinks vimfiles to $HOME.
# Prints usage
usage() {
cat << EOF
usage: $0 [-s|--submodules] [-h|--help]
OPTIONS:
-s | --submodules Initialize and update included git submodules
EOF
}
# Exits with given error message
die() {
echo "$@"
exit 1
}
# Minifies vim script files in place
minify_vim_script_file_in_place() {
# 1. Convert tabs to spaces
# 2. Remove blank lines
# 3. Remove beginning spaces
# 4. Remove whole line comments
# 5. Remove trailing spaces
# Set LANG as C to treat all ASCII characters as themselves and all
# non-ASCII characters as literals
env LANG=C sed 's/ / /g' "$1" |
env LANG=C sed '/^$/d' |
env LANG=C sed 's/^[ ]*//' |
env LANG=C sed '/^[ ]*"/d' |
env LANG=C sed 's/[ ]*$//' > tmp
# Copy tmp to the given file and remove tmp
cp tmp "$1"
rm tmp
}
# Returns the source file given a symlink target file
readlink_posix() {
target_file="$1"
if [ -h "$target_file" ]; then
ls_res="$(LC_TIME=C ls -ld "$target_file")"
source_file=${ls_res#*"${target_file} -> "}
echo "$source_file"
fi
}
here=$(dirname "$0") && here=$(cd "$here" && pwd -P)
while :
do
case "$1" in
-h | --help )
usage
exit 0
;;
-s | --submodules )
cd "$here"
# Sync git submodules if already initialized
if [ -f "${here}/core/pathogen/autoload/pathogen.vim" ]; then
echo "Updating git submodules"
(git submodule sync && git submodule update --init) \
|| die "Could not sync git submodules"
else
echo "Initializing git submodules"
(git submodule init && git submodule update) \
|| die "Could not update git submodules"
fi
shift
;;
-- )
shift
break
;;
-* )
echo "Unknown option: $1"
usage
exit 1
;;
* )
break
;;
esac
done
# Back up any existing configurations
echo
echo "Backing up any existing configurations"
# Actually copy the contents of the symlinks (or regular files), not just the
# link if exists
for file in "${HOME}/.vimrc" "${HOME}/.gvimrc"; do
if [ -e "$file" ]; then
cp -v "$file" "${file}.old" || die "Could not copy ${file} to ${file}.old"
fi
done
if [ -h "${HOME}/.vim" ] &&
[ -e $(readlink_posix "${HOME}/.vim") ]; then
# Remove any existing ~/.vim.old because of `cp` symlink issues
rm -rf "${HOME}/.vim.old"
echo "Copying contents of symlink ${HOME}/.vim to ${HOME}/.vim.old"
cp -R "$(readlink_posix "${HOME}/.vim")" "${HOME}/.vim.old" \
|| die "Could not copy ${HOME}/.vim to ${HOME}/.vim.old"
else
if [ -d "${HOME}/.vim" ]; then
echo "Renaming ${HOME}/.vim to ${HOME}/.vim.old"
mv "${HOME}/.vim" "${HOME}/.vim.old" \
|| die "Could not move ${HOME}/.vim to ${HOME}/.vim.old"
fi
fi
echo
echo "Minifying *.vim files"
# Remove any existing build/vim.min because of `cp` symlink issues
rm -rf "${here}/build/vim.min"
mkdir -p "${here}/build/vim.min/bundle"
# Consolidate all plugins into bundle and save pathogen from having to load
# multiple paths
for file in "$here"/*; do
dir_name="$(basename "$file")"
if [ -d "$file" ] &&
[ "$dir_name" != "build" ] &&
[ "$dir_name" != "bin" ]; then
for plugin in "$file"/*; do
plugin_name="$(basename "$plugin")"
cp -R "$plugin" "${here}/build/vim.min/bundle/${plugin_name}"
done
fi
done
# Copy vim files, locally, to be minified
cp "${here}/gvimrc" "${here}/build/gvimrc.min"
# Concatenate vimrc and any vimrc-labs/vimrc into build/vimrc.min
touch "${here}/build/vimrc.min"
cat /dev/null > "${here}/build/vimrc.min"
cat "${here}/vimrc" >> "${here}/build/vimrc.min"
# Minify
minify_vim_script_file_in_place "${here}/build/vimrc.min"
minify_vim_script_file_in_place "${here}/build/gvimrc.min"
find "${here}/build/vim.min" -name "*.vim" |
while read file; do minify_vim_script_file_in_place "$file"; done
# Link to minified configurations
ln -sfv "${here}/build/vimrc.min" "${HOME}/.vimrc"
ln -sfv "${here}/build/gvimrc.min" "${HOME}/.gvimrc"
ln -sfvn "${here}/build/vim.min" "${HOME}/.vim"
echo
echo "Linking files in bin/"
# Create $HOME/bin if it doesn't already exist
if [ ! -d "${HOME}/bin" ]; then
mkdir -v "${HOME}/bin"
fi
# Link files in bin to $HOME/bin
for file in "${here}/bin"/*; do
filename="$(basename "$file")"
ln -sfv "${here}/bin/${filename}" "${HOME}/bin/${filename}"
done