#!/bin/sh -e

repo_dir=$(readlink -f $(dirname "$0"))

link() {
  [ -L "$HOME/.config" ] && return 0

  for file in ".profile" ".zprofile" ".config"; do
    ln -sf "$repo_dir/$file" "$HOME/$file"
  done

  source ~/.profile
}

install() {
  sudo pacman -S --needed \
    base-devel \
    bmon \
    cgdb \
    expect \
    fd \
    ffmpeg \
    firefox \
    fzf \
    git \
    gdb \
    grim \
    gtk-layer-shell \
    iotop \
    imagemagick \
    isync \
    kitty \
    lynx \
    mpd \
    mpv \
    msmtp \
    ncmpcpp \
    neomutt \
    neovim \
    newsboat \
    ninja \
    nnn \
    notmuch \
    noto-fonts \
    nodejs \
    npm \
    ntfs-3g \
    otf-font-awesome \
    pass \
    pass-otp \
    playerctl \
    powertop \
    python \
    python-pip \
    python-neovim \
    qt5-wayland \
    qt6-wayland \
    ripgrep \
    rsync \
    slurp \
    sway \
    tcpdump \
    tokei \
    transmission-cli \
    tree \
    ttf-ubuntu-font-family \
    yt-dlp \
    waybar \
    wf-recorder \
    wl-clipboard \
    zathura \
    zathura-pdf-mupdf \
    zsh \
    zsh-autosuggestions \
    zsh-history-substring-search

  [ -d "/usr/share/zsh/plugins/fast-syntax-highlighting" ] || sudo git clone \
    https://github.com/zdharma-continuum/fast-syntax-highlighting \
    /usr/share/zsh/plugins/fast-syntax-highlighting

  [ -d "/usr/share/zsh/plugins/zsh-vim-mode" ] || sudo git clone \
    https://github.com/softmoth/zsh-vim-mode \
    /usr/share/zsh/plugins/zsh-vim-mode

  [ -x "/usr/bin/pam_exec-ssh" ] || {
    sudo install -m755 \
      "$repo_dir/.config/ssh-agent/pam_exec-ssh" \
      /usr/bin/pam_exec-ssh
      mkdir ~/.ssh/unlock.d
      ln -s ~/.ssh/id_ed25519 ~/.ssh/unlock.d/id_ed25519
      sudo sed -i '$a auth      optional  pam_exec.so expose_authtok /usr/bin/pam_exec-ssh' \
        /etc/pam.d/system-local-login
    }

  [ -x "/usr/bin/sirula" ] || {
    tmp_dir="$(mktemp -d)" && pushd "$tmp_dir" && {
      curl -sSfO "https://jordanholt.xyz/sirula"
      sudo install -m755 sirula /usr/bin/
      popd
      rm -rf $tmp_dir
    }
  }
}

configure() {
  loginctl enable-linger $UID
}

install && link && configure
