summaryrefslogtreecommitdiffstats
path: root/setup
blob: 586f4747abaa600d719d2801b6f411736382b556 (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
#!/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"

declare -A additional_links=(
    [.vimrc]=.config/nvim/init.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
        printf '%s does not exist!\n' "$1" >&2
        return 127
    fi
}

repo="$(dirname "$0")"
if [[ "${repo}" == '.' ]]; then
    repo="$(pwd)"
else
    repo="$(abspath "${repo}")"
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_link() {
    local dstpath pth elem elems prompt rc
    local src="$1"      # path to the file in this repository to link
    local dst="$2"      # destination file path relative to the home directory
    local dir="$(dirname "${src}")"
    local dstpath="${HOME}/${dst}"

    if [[ "${dir}" != '.' ]]; then
        IFS=/ read -a elems <<<"${dir}"
        pth="${HOME}"
        for elem in "${elems[@]}"; do
            pth="${pth}/${elem}"
            if [[ "${pth#${HOME}/}" =~ ${re_leaves} ]]; then
                return
            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) return 2; ;;
                    2) return;   ;;
                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) return 2;    ;;
            2) return; ;;
        esac
    elif [[ -e "${dstpath}" && -L "${dstpath}" && "$(readlink "${dstpath}")" == "${repo}/${src}" ]]; then
        return
    fi

    ln -vsf "${repo}/${src}" "${dstpath}"
}

function make_links() {
    local cname dir
    while IFS=$'\n' read -u 3 -r l; do
        cname="$(basename "${l}")"
        dir="$(dirname "${l}")"

        [[ "${dir}" =~ ${re_leaves} ]] && continue

        make_link "${l}" "${l}" && rc=$? || rc=$?
        [[ "${rc}" == 2 ]] && return
    done 3< <(git ls-files | egrep -v "${re_ignore}"; printf '%s\n' "${directory_leaves[@]}")

    for dst in "${!additional_links[@]}"; do
        make_link "${additional_links[$dst]}" "${dst}"
    done
}

# see https://github.com/junegunn/vim-plug#neovim
curl -fLo ~/.local/share/nvim/site/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
mkdir -p ~/.vim/autoload ||:
ln -sf ~/.local/share/nvim/site/autoload/plug.vim ~/.vim/autoload/


if ! command -v fzf-share >/dev/null && ! which fzf >/dev/null 2>&1 ; then
    echo "fzf not found!"
    args=""
    if [[ "${no_confirm}" == 1 ]]; then
        answer=y
        args="--all"
    else
        read -p "Fetch it from github to ~/.fzf? [y/n]" answer 
    fi
    case "${answer}" in
        y)
            git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
            ~/.fzf/install ${args:-}
            ;;
        n)  ;;
    esac
fi

function setup_neovim() {
    if ! which nvim >/dev/null 2>&1; then
        echo "neovim is not installed!"
        return 1
    fi

    nvim '+PlugInstall' '+qall'
    nvim '+Tmuxline airline' '+TmuxlineSnapshot ~/.rcs/.tmux/tmuxline.conf' '+qall'
}

make_links
if ! setup_neovim; then
    echo "setting up vim"
    vim '+PlugInstall' '+qall'
    vim '+Tmuxline airline' '+TmuxlineSnapshot ~/.rcs/.tmux/tmuxline.conf' '+qall'
fi

# vim: set sw=4 ts=4 et tw=98: