summaryrefslogtreecommitdiff
path: root/setup.sh
blob: 829c520e7b75e5ff4a36754fc788efafc0d1ad6e (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
#!/bin/bash

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
cd "$SCRIPT_DIR" || exit

# Vim LSP setup (idempotent; each step = action + check, first failure stops).
# Run only this part: ./setup.sh vimlsp
vimlsp() {
  set -e
  ok()   { echo "ok   $*"; }
  die()  { echo "FAIL $*" >&2; exit 1; }
  VIMRC_SRC="$SCRIPT_DIR/core/vimrc"
  export PATH="$HOME/.cargo/bin:$HOME/.local/bin:$PATH"

  # vimrc: system-wide only (same path as core/vim.sh); a ~/.vimrc would load on top of it
  VIMRC_DST=/etc/vimrc; [ -d /etc/vim ] && VIMRC_DST=/etc/vim/vimrc
  cmp -s "$VIMRC_SRC" "$VIMRC_DST" || sudo cp "$VIMRC_SRC" "$VIMRC_DST"
  cmp -s "$VIMRC_SRC" "$VIMRC_DST" || die "$VIMRC_DST"; ok "$VIMRC_DST matches core/vimrc"
  [ ! -e ~/.vimrc ] || die "~/.vimrc exists and overrides $VIMRC_DST; remove it"; ok "no ~/.vimrc"

  # vim-plug
  [ -f ~/.vim/autoload/plug.vim ] || curl -fsSLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  grep -q 'plug#begin' ~/.vim/autoload/plug.vim || die "plug.vim"; ok "vim-plug ~/.vim/autoload/plug.vim"

  # plugins: every Plug line of the vimrc, plain depth-1 git clone into ~/.vim/plugged
  for repo in $(sed -n "s/^Plug '\([^']*\)'.*/\1/p" "$VIMRC_SRC"); do
    d=~/.vim/plugged/${repo#*/}
    [ -d "$d/.git" ] || git clone -q --depth 1 "https://github.com/$repo" "$d"
    git -C "$d" rev-parse --verify -q HEAD > /dev/null || die "plugin $repo"; ok "plugin $repo"
  done
  [ -x ~/.vim/plugged/fzf/bin/fzf ] || ~/.vim/plugged/fzf/install --bin > /dev/null 2>&1
  ~/.vim/plugged/fzf/bin/fzf --version > /dev/null || die "fzf binary"; ok "fzf binary"

  # rust-analyzer (Rust itself comes from core/packages/install_rust.sh)
  if rustup which rustc > /dev/null 2>&1; then
    rustup component list --installed | grep -q '^rust-analyzer' || rustup component add rust-analyzer
    rustup component list --installed | grep -q '^rust-src' || rustup component add rust-src
    rust-analyzer --version > /dev/null || die "rust-analyzer"; ok "rust-analyzer $(rust-analyzer --version)"
  else
    echo "skip rust-analyzer: no rustup toolchain (run core/packages/install_rust.sh first)"
  fi

  # uv, ruff, ty
  command -v uv > /dev/null || curl -LsSf https://astral.sh/uv/install.sh | UV_NO_MODIFY_PATH=1 sh
  uv --version > /dev/null || die "uv"; ok "$(uv --version)"
  for t in ruff ty; do
    command -v $t > /dev/null || uv tool install $t
    $t --version > /dev/null || die "$t"; ok "$($t --version)"
  done
  echo "vimlsp done"
}
[ "$1" = vimlsp ] && { vimlsp; exit $?; }

# Shell appearance (only append if not already present)
grep -q 'di=38;5;135' /etc/bashrc || sudo tee -a /etc/bashrc > /dev/null << 'EOF'
LS_COLORS=$LS_COLORS:'di=38;5;135:ex=00;32:' ; export LS_COLORS
PS1='[\[\033[01;32m\]\u\[\033[00m\]@\h \[\033[38;5;135m\]\W\[\033[00m\]]\$ '
export TERM=xterm-256color
EOF
source /etc/bashrc

# Timezone
sudo timedatectl set-timezone Asia/Singapore

# Core setup
sudo bash ./core/ssh.sh              || { echo "Failed to configure SSH"; exit 1; }
sudo bash ./core/install-packages.sh || { echo "Failed to install packages"; exit 1; }
sudo bash ./core/vim.sh              || { echo "Failed to configure Vim"; exit 1; }
sudo bash ./core/tmux.sh             || { echo "Failed to configure Tmux"; exit 1; }
[ ! -e ~/.tmux.conf ] || { echo "~/.tmux.conf exists and overrides /etc/tmux.conf; remove it"; exit 1; }

# Vim LSP setup for the invoking user
( vimlsp ) || { echo "Failed to set up vim LSP"; exit 1; }