summaryrefslogtreecommitdiffstats
path: root/setup
diff options
context:
space:
mode:
authorMichal Minář <mic.liamg@gmail.com>2017-07-18 16:07:16 +0200
committerMichal Minář <mic.liamg@gmail.com>2017-07-18 16:07:16 +0200
commit1eb13e2ff6c216f142bf3e0f3e549c6ac86bf6dd (patch)
treedb4016cde46425592e689861c1776142fc1fa9d3 /setup
parentaaea20700fbe4d2a2cbffc34c7d1a0bb8e690934 (diff)
downloadrcs-1eb13e2ff6c216f142bf3e0f3e549c6ac86bf6dd.zip
rcs-1eb13e2ff6c216f142bf3e0f3e549c6ac86bf6dd.tar.gz
rcs-1eb13e2ff6c216f142bf3e0f3e549c6ac86bf6dd.tar.xz
make-links: renamed to setup
Install neovim plugins. Signed-off-by: Michal Minář <mic.liamg@gmail.com>
Diffstat (limited to 'setup')
-rwxr-xr-xsetup140
1 files changed, 140 insertions, 0 deletions
diff --git a/setup b/setup
new file mode 100755
index 0000000..2a0103b
--- /dev/null
+++ b/setup
@@ -0,0 +1,140 @@
+#!/usr/bin/env bash
+
+set -euo pipefail
+IFS=$'\n\t'
+
+readonly ignore=( 'make-links' 'setup' '.gitignore' '.gitmodules' )
+# directories that shall be treated as leaves; iow. destination should be
+# turned into a link pointing to this directory
+readonly directory_leaves=( '.config/systemd/user' '.tmuxinator' )
+readonly nvim_plug_init_path="${HOME}/.local/share/nvim/site/autoload/plug.vim"
+
+function abspath {
+ # convert relative path to absolute one
+ # http://www.linuxquestions.org/questions/programming-9/bash-script-return-full-path-and-filename-680368/page3.html#post4309783
+ if [[ -d "$1" ]]
+ then
+ pushd "$1" >/dev/null
+ pwd
+ popd >/dev/null
+ elif [[ -e $1 ]]
+ then
+ pushd $(dirname $1) >/dev/null
+ echo $(pwd)/$(basename $1)
+ popd >/dev/null
+ else
+ echo $1 does not exist! >&2
+ return 127
+ fi
+}
+
+src=`dirname $0`
+if [[ "$src" == '.' ]]; then
+ src=`pwd`
+else
+ src=`abspath "$src"`
+fi
+
+no_confirm=0
+if [[ "${1:-}" == "-n" ]]; then
+ no_confirm=1
+fi
+
+# confirm_remove returns exit code:
+# 0 - on successful remove or ignore
+# 1 - to stop the script
+# 2 - to continue with next file
+function confirm_remove() {
+ local prompt="$1"
+ local path="$2"
+ local answer
+
+ if [[ "$no_confirm" == 1 ]]; then
+ answer=y
+ else
+ read -p "$prompt" answer
+ fi
+ if [[ "$answer" =~ ^[[:space:]]*[yY][[:space:]]*$ ]]; then
+ rm -rvf "$path"
+ elif [[ "$answer" =~ ^[[:space:]]*[qQ][[:space:]]*$ ]]; then
+ return 1
+ elif [[ "$answer" =~ ^[[:space:]]*[iI][[:space:]]*$ ]]; then
+ : # ignore
+ else
+ if ! [[ "$answer" =~ ^[[:space:]]*[nN][[:space:]]*$ ]]; then
+ echo "Assuming no."
+ fi
+ return 2
+ fi
+}
+
+function make_regexp() {
+ IFS='|'
+ echo "^($*)$"
+}
+readonly re_ignore=`make_regexp "${ignore[@]}"`
+readonly re_leaves=`make_regexp "${directory_leaves[@]}"`
+
+function make_links() {
+ while IFS=$'\n' read -u 3 -r l; do
+ cname=`basename "$l"`
+ dir=`dirname "$l"`
+ dstpath="$HOME/$l"
+
+ [[ "$dir" =~ $re_leaves ]] && continue
+
+ if [[ "$dir" != '.' ]]; then
+ IFS=/ read -a elems <<<"$dir"
+ pth="$HOME"
+ for elem in "${elems[@]}"; do
+ pth="$pth/$elem"
+ if [[ "${pth#$HOME/}" =~ $re_leaves ]]; then
+ continue
+ fi
+ if [[ -L "$pth" || ( -e "$pth" && ! -d "$pth" ) ]]; then
+ prompt="\"$pth\" is not a directory. Do you want to make"
+ prompt+=" it a directory? [(y)es/(N)o/(i)gnore/(q)uit] "
+ confirm_remove "$prompt" "$pth" && rc=$? || rc=$?
+ case $rc in
+ 1) break 2; ;;
+ 2) continue 2; ;;
+ esac
+ fi
+ if [[ ! -e "$pth" ]]; then
+ mkdir -vp "$pth"
+ fi
+ done
+ fi
+
+ if [[ -e "$dstpath" && ! -L "$dstpath" ]]; then
+ prompt="File \"$dstpath\" exists do you want to remove it?"
+ prompt+=" [(y)es/(N)o/(q)uit] "
+ confirm_remove "$prompt" "$dstpath" && rc=$? || rc=$?
+ case $rc in
+ 1) break; ;;
+ 2) continue; ;;
+ esac
+ elif [[ -e "$dstpath" && -L "$dstpath" && `readlink "$dstpath"` == "$src/$l" ]]; then
+ continue
+ fi
+
+ ln -vsf "$src/$l" "$dstpath"
+ done 3< <(git ls-files | egrep -v "$re_ignore"; printf '%s\n' "${directory_leaves[@]}")
+}
+
+function setup_neovim() {
+ if ! which nvim >/dev/null 2>&1; then
+ echo "neovim is not installed!"
+ return 1
+ fi
+
+ if ! [[ -e "${nvim_plug_init_path}" ]]; then
+ curl -fLo "${nvim_plug_init_path}" --create-dirs \
+ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
+ fi
+
+ nvim '+PlugInstall' '+qall'
+}
+
+make_links
+setup_neovim