-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·69 lines (55 loc) · 1.77 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
#!/bin/bash
# A pure bash implementation of the playbook.
# It's... a lot more convenient for installing on the local system.
# shellcheck disable=SC2155
set -euo pipefail
IFS='\n\t'
REQUIREMENTS=("git" "vim" "curl")
target_user=$(id -un)
homedir=""
PLUG_URL="https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim"
VIMRC_URL="https://raw.githubusercontent.com/rgm3/ansible-vim/master/vimrc"
function main {
# shellcheck disable=SC2230
! which "${REQUIREMENTS[@]}" &>/dev/null && die "missing one of: ${REQUIREMENTS[*]}"
if [[ -n "${1:-}" ]]; then
! id "$1" &> /dev/null && die "invalid user: $1"
target_user="$1"
fi
homedir=$(get_homedir "${target_user}")
[[ -w "${homedir}" ]] || die "can't write to directory: ${homedir}"
[[ -e ${homedir}/.vim ]] && die "${homedir}/.vim already exists"
confirm "Install for ${target_user} in ${homedir}?" || return
install_vim_settings
}
function install_vim_settings {
mkdir -p "${homedir}/.vim/autoload"
mkdir -p "${homedir}/.vim/undo"
curl -so "${homedir}/.vim/autoload/plug.vim" "${PLUG_URL}"
curl -so "${homedir}/.vimrc" "${VIMRC_URL}"
local group=$(id -gn "${target_user}")
chown -R "${target_user}":"${group}" "${homedir}/.vim"
chown -R "${target_user}":"${group}" "${homedir}/.vimrc"
if [[ ${target_user} == $(id -un) ]]; then
vim +PlugInstall +qall
else
echo "User ${target_user} should run: vim +PlugInstall +qall"
fi
}
function confirm {
read -n1 -rp "$1 [y/n]: "
echo
[[ $REPLY =~ ^[Yy]$ ]]
}
function get_homedir {
local user="$1"
case "$(uname -s)" in
Darwin) dscl . -read /users/"${user}" NFSHomeDirectory | awk -F': ' '{print $2}' ;;
*) getent passwd "${user}" | cut -d: -f6 ;;
esac
}
function die {
[[ -n $1 ]] && >&2 echo "$1"
exit "${2:-1}"
}
main "$@"