#!/bin/sh -e

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

link() {
  [ -L "~/.config" ] || return 0

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

  source ~/.profile
}

install() {
  sudo pacman -S --needed \
    abook \
    alacritty \
    expect \
    ffmpeg \
    firefox \
    fzf \
    git \
    grim \
    gtk-layer-shell \
    isync \
    lynx \
    mpd \
    mpv \
    msmtp \
    ncmpcpp \
    neomutt \
    neovim \
    newsboat \
    nnn \
    notmuch \
    nodejs \
    npm \
    pass \
    pass-otp \
    playerctl \
    pulseaudio \
    python \
    python-pip \
    rsync \
    slurp \
    sway \
    transmission-cli \
    ttf-ubuntu-font-family \
    youtube-dl \
    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/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

  [ -d "/usr/share/themes/Nova" ] || sudo git clone \
    https://git.vimium.com/jordan/gtk-nova-theme.git \
    /usr/share/themes/Nova

  [ -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
  systemctl --user enable ssh-agent
}

install && link && configure
