aboutsummaryrefslogtreecommitdiff
path: root/setup.sh
blob: 0cb9c786dc76e6f44f5df68c873cbfeeec480b3f (plain) (blame)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/bin/env bash

NC='\033[0m'
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'

# Locations
GIT=$HOME/git
BINS="$HOME/.local/bin"
PROGRAMS=$HOME/programs
DOTFILES=$HOME/.dotfiles
DOTPRIVATE=$HOME/.dotfiles.private
XDG_DATA_HOME=${XDG_DATA_HOME:-$HOME/.local/share}

# Software version
LTEX_VERSION="15.2.0"
METALS_VERSION="0.10.8"
NNN_VERSION="4.3"
NVIM_VERSION="0.5.1"
RIPGREP_VERSION="13.0.0"
TMUX_VERSION="3.2a"
RUST_ANALYZER_VERSION="2021-11-01"

# Flags
YES=false
PRIVATE=false

DID_BASH=false
DID_NEOVIM=false
DID_NNN=false
DID_RIPGREP=false
DID_TMUX=false

msg_info() {
    echo -e "${GREEN}[INFO] $1${NC}"
}

msg_warn() {
    echo -e "${YELLOW}[WARN] $1${NC}"
}

msg_error() {
    echo -e "${RED}[ERROR] $1${NC}"
}

print_help() {
    echo
    echo "A script to setup dyamon's dotfiles."
    echo
    echo "It takes care of deploying dotfiles for any requested package."
    echo "It tries hard to be as self-contained as possible installing"
    echo "dependences locally in a distribution agnostic way."
    echo
    echo "Requires 'git' and 'stow' to be installed in the system."
    echo
    echo "USAGE:"
    echo "    setup.sh [OPTION ...] [PACKAGE ...]"
    echo
    echo "OPTIONs are:"
    echo "    -y | --yes:"
    echo "        do not prompt for missing dependences. Just install them."
    echo "    -p | --private:"
    echo "        clone private dotfiles alongside the main repository."
    echo "        Repos will be cloned via SSH instead of HTTPS."
    echo "    -h | --help:"
    echo "        print this help."
    echo
    echo "PACKAGEs are:"
    echo "    bash neovim nnn ripgrep tmux"
    #         extra fonts fzf grimoire mailcap misc mpd mpv ncmpcpp
    #         neomutt readline sxhkd sxiv transmission x11
    echo
}

setup_bash() {
    if [ "$DID_BASH" = false ]; then
        DID_BASH=true
        if [[ "$SHELL" =~ "bash" ]]; then
            # Dotfiles deployment
            msg_info "Bash dotfiles deployment..."
            pushd $DOTFILES >/dev/null
            stow --verbose --restow bash
            popd >/dev/null
        else
            msg_warn "Default shell is set to '$SHELL'."
            msg_warn "Set it to $(which bash) to avoid incompatibilities."
        fi
    fi
}

setup_tmux() {
    if [ "$DID_TMUX" = false ]; then
        DID_TMUX=true
        # Dependences
        setup_bash
        # Installation
        msg_info "Installing tmux..."
        if which tmux >/dev/null 2>&1 ; then
            msg_info "'tmux' executable found in the system."
            tmux -V
        else
            [ "$YES" = true ] && \
                REPLY="Y" || \
                read -p "Install 'tmux' locally? [yN]: " -n 1 -r </dev/tty && echo
            if [[ $REPLY =~ ^[Yy]$ ]] ; then
                pushd $PROGRAMS >/dev/null
                TMUX="tmux-$TMUX_VERSION"
                wget "https://github.com/tmux/tmux/releases/download/$TMUX_VERSION/$TMUX.tar.gz"
                tar -zxvf "$TMUX.tar.gz"
                pushd "$TMUX"
                ./configure && make && \
                    ln -s "$PROGRAMS/$TMUX/tmux" "$BINS/"
                popd >/dev/null
                popd >/dev/null
                tmux -V
            else
                msg_warn "You should provide an alternative installation to avoid any problem."
            fi
        fi
        # Dotfiles deployment
        msg_info "tmux dotfiles deployment..."
        pushd $DOTFILES >/dev/null
        stow --verbose --restow tmux
        popd >/dev/null
    fi
}

setup_nnn() {
    if [ "$DID_NNN" = false ]; then
        DID_NNN=true
        # Dependences
        # trash-cli
        msg_info "Installing trash-cli (nnn dependency)..."
        if which trash >/dev/null 2>&1 ; then
            msg_info "'trash' executable found in the system."
            trash --version
        else
            pushd "$GIT" >/dev/null
            git clone https://github.com/andreafrancia/trash-cli.git 2>/dev/null
            cd trash-cli
            git pull --ff-only
            pip install .
            popd >/dev/null
        fi
        # Installation
        msg_info "Installing nnn..."
        if which nnn >/dev/null 2>&1 ; then
            msg_info "'nnn' executable found in the system."
            nnn -V
        else
            [ "$YES" = true ] && \
                REPLY="Y" || \
                read -p "Install 'nnn' locally? [yN]: " -n 1 -r </dev/tty && echo
            if [[ $REPLY =~ ^[Yy]$ ]] ; then
                pushd $PROGRAMS >/dev/null
                NNN="nnn-static-$NNN_VERSION.x86_64"
                wget "https://github.com/jarun/nnn/releases/download/v$NNN_VERSION/$NNN.tar.gz"
                tar -zxvf "$NNN.tar.gz"
                ln -s "$PROGRAMS/nnn-static" "$BINS/nnn"
                popd >/dev/null
                nnn -V
            else
                msg_warn "You should provide an alternative installation to avoid any problem."
            fi
        fi
        # Dotfiles deployment
        msg_info "nnn dotfiles deployment..."
        pushd $DOTFILES >/dev/null
        stow --verbose --restow nnn
        popd >/dev/null
    fi
}

setup_ripgrep() {
    if [ "$DID_RIPGREP" = false ]; then
        DID_RIPGREP=true
        # Installation
        msg_info "Installing ripgrep..."
        if which rg >/dev/null 2>&1 ; then
            msg_info "'ripgrep' executable found in the system."
            rg --version
        else
            [ "$YES" = true ] && \
                REPLY="Y" || \
                read -p "Install 'ripgrep' locally? [yN]: " -n 1 -r </dev/tty && echo
            if [[ $REPLY =~ ^[Yy]$ ]] ; then
                pushd $PROGRAMS >/dev/null
                RIPGREP="ripgrep-$RIPGREP_VERSION-x86_64-unknown-linux-musl"
                wget "https://github.com/BurntSushi/ripgrep/releases/download/$RIPGREP_VERSION/$RIPGREP.tar.gz"
                tar -zxvf "$RIPGREP.tar.gz"
                ln -s "$PROGRAMS/$RIPGREP/rg" "$BINS/"
                popd >/dev/null
                rg --version
            else
                msg_warn "You should provide an alternative installation to avoid any problem."
            fi
        fi
        # Dotfiles deployment
        msg_info "Ripgrep dotfiles deployment..."
        pushd $DOTFILES >/dev/null
        stow --verbose --restow ripgrep
        popd >/dev/null
    fi
}

setup_neovim() {
    if [ "$DID_NEOVIM" = false ]; then
        DID_NEOVIM=true
        # Dependences
        setup_bash
        setup_nnn
        setup_ripgrep
        # Metals
        if which metals >/dev/null 2>&1 ; then
            msg_info "'metals' executable found in the system."
            metals --version
        else
            [ "$YES" = true ] && \
                REPLY="Y" || \
                read -p "Install 'metals' locally? [yN]: " -n 1 -r </dev/tty && echo
            if [[ $REPLY =~ ^[Yy]$ ]] ; then
                pushd $BINS >/dev/null
                if which coursier >/dev/null 2>&1 ; then
                    msg_info "'coursier' executable found in the system."
                    coursier --version
                else
                    curl -L -o coursier https://git.io/coursier-cli
                    chmod u+x coursier
                    coursier --version
                fi
                coursier bootstrap \
                    --java-opt -Xss4m \
                    --java-opt -Xms100m \
                    org.scalameta:metals_2.12:$METALS_VERSION \
                    -r bintray:scalacenter/releases \
                    -r sonatype:snapshots \
                    -o ./metals -f
                popd >/dev/null
                metals --version
            else
                msg_warn "You will need to provide an alternative installation to use LSP functionalities with Scala Metals"
            fi
        fi
        # Rust Analyzer
        if which rust-analyzer >/dev/null 2>&1 ; then
            msg_info "'rust-analyzer' executable found in the system."
            rust-analyzer --version
        else
            [ "$YES" = true ] && \
                REPLY="Y" || \
                read -p "Install 'rust-analyzer' locally? [yN]: " -n 1 -r </dev/tty && echo
            if [[ $REPLY =~ ^[Yy]$ ]] ; then
                pushd $PROGRAMS >/dev/null
                RUST="rust-analyzer-x86_64-unknown-linux-gnu"
                wget "https://github.com/rust-analyzer/rust-analyzer/releases/download/$RUST_ANALYZER_VERSION/$RUST.gz"
                gzip -d "$RUST.gz"
                chmod u+x "$RUST"
                ln -s "$PROGRAMS/$RUST" "$BINS/rust-analyzer"
                rust-analyzer --version
            else
                msg_warn "You will need to provide an alternative installation to use LSP functionalities with Rust Analyzer"
            fi
        fi
        # lTeX-ls
        if which ltex-ls >/dev/null 2>&1 ; then
            msg_info "'ltex-ls' executable found in the system."
            ltex-ls --version
        else
            [ "$YES" = true ] && \
                REPLY="Y" || \
                read -p "Install 'ltex-ls' locally? [yN]: " -n 1 -r </dev/tty && echo
            if [[ $REPLY =~ ^[Yy]$ ]] ; then
                pushd $PROGRAMS >/dev/null
                LTEX="ltex-ls-$LTEX_VERSION"
                wget "https://github.com/valentjn/ltex-ls/releases/download/$LTEX_VERSION/$LTEX-linux-x64.tar.gz"
                tar -zxvf "$LTEX-linux-x64.tar.gz"
                ln -s "$PROGRAMS/$LTEX/bin/ltex-ls" "$BINS/"
                ltex-ls --version
            else
                msg_warn "You will need to provide an alternative installation to use LSP functionalities with lTeX."
            fi
        fi
        # Installation
        msg_info "Installing neovim..."
        if which nvim >/dev/null 2>&1 ; then
            msg_info "'nvim' executable found in the system."
            nvim --version
        else
            [ "$YES" = true ] && \
                REPLY="Y" || \
                read -p "Install 'nvim' locally? [yN]: " -n 1 -r </dev/tty && echo
            if [[ $REPLY =~ ^[Yy]$ ]] ; then
                pushd $PROGRAMS >/dev/null
                NVIM="nvim-linux64"
                wget "https://github.com/neovim/neovim/releases/download/v$NVIM_VERSION/$NVIM.tar.gz"
                wget "https://github.com/neovim/neovim/releases/download/v$NVIM_VERSION/$NVIM.tar.gz.sha256sum"
                if sha256sum --check --status "$NVIM.tar.gz.sha256sum" ; then
                    tar -zxvf "$NVIM.tar.gz"
                    ln -s "$PROGRAMS/$NVIM/bin/nvim" "$BINS/"
                else
                    print_err "$NVIM.tar.gz checksum check failed."
                fi
                popd >/dev/null
                nvim --version
            else
                msg_warn "You should provide an alternative installation to avoid any problem."
            fi
        fi
        # Dotfiles deployment
        msg_info "Neovim dotfiles deployment..."
        pushd $DOTFILES >/dev/null
        stow --verbose --restow neovim
        popd >/dev/null
        mkdir -p $XDG_DATA_HOME/nvim/{backup,sessions,swap,undo}
        pushd $XDG_DATA_HOME/nvim/site/pack/personal/opt/telescope-fzf-native.nvim/ >/dev/null
        make
        popd >/dev/null
    fi
}

mkdir -p "$GIT"
mkdir -p "$BINS"
mkdir -p "$PROGRAMS"

# Make new binaries available right away
export PATH=$BINS:$PATH

while [[ $# -gt 0 ]]; do
  OPTION="$1"
  case $OPTION in
    -y|--yes)
      YES=true
      shift
      ;;
    -p|--private)
      PRIVATE=true
      shift
      ;;
    -h|--help)
      print_help
      exit 0
      ;;
    *)
      break
      ;;
  esac
done

pushd $HOME >/dev/null
if [ "$PRIVATE" = true ]; then
    [[ -d "$DOTFILES" ]] || \
        git clone --recurse-submodules gitolite3@git.dyamon.me:dotfiles.git "$DOTFILES"
    [[ -d "$DOTPRIVATE" ]] || \
        git clone --recurse-submodules gitolite3@git.dyamon.me:dotfiles-private.git "$DOTPRIVATE"
else
    [[ -d "$DOTFILES" ]] || \
        git clone --recurse-submodules https://git.dyamon.me/dotfiles "$DOTFILES"
fi

while [[ $# -gt 0 ]]; do
  PACKAGE="$1"
  case $PACKAGE in
    bash)
        msg_info "[Package BASH]"
        setup_bash
        ;;
    neovim)
        msg_info "[Package NEOVIM]"
        setup_neovim
        ;;
    nnn)
        msg_info "[Package NNN]"
        setup_nnn
        ;;
    ripgrep)
        msg_info "[Package RIPGREP]"
        setup_ripgrep
        ;;
    tmux)
        msg_info "[Package TMUX]"
        setup_tmux
        ;;
    *)
        msg_warn "'$PACKAGE' is not a valid package. Ignoring."
      ;;
  esac
  shift
done

popd >/dev/null

exit 0