#!/usr/bin/env bash

function upsert_dir_symlink() {
  local from="$1"
  local to="$2"
  { [ -d "$to" ] || [ -L "$to" ]; } || {
    ln -s "$from" "$to"
  }
}
export -f upsert_dir_symlink

upsert_dir_symlink "$HOME/dev/libre/borg/config" ~/.config/borg

ln -fs "$DOTFILES/bash/bashrc.sh"               ~/.bashrc
ln -fs "$DOTFILES/bash/bash_profile.sh"         ~/.bash_profile
ln -fs "$DOTFILES/git/gitattributes"            ~/.gitattributes
ln -fs "$DOTFILES/git/gitignore"                ~/.gitignore_global
ln -fs "$DOTFILES/sbclrc.lisp"                  ~/.sbclrc
ln -fs "$DOTFILES/inputrc.conf"                 ~/.inputrc
ln -fs "$DOTFILES/Xmodmap.conf"                 ~/.Xmodmap
ln -fs "$DOTFILES/nixos/npmrc.sh"             ~/.npmrc

mkdir -p ~/.gnupg
ln -fs "${HOME}/annex/bin/misc/gpg/gpg.conf"       ~/.gnupg/gpg.conf
ln -fs "${HOME}/annex/bin/misc/gpg/gpg-agent.conf" ~/.gnupg/gpg-agent.conf
ln -fs "${HOME}/annex/bin/misc/git/mrconfig.ini"   ~/.mrconfig

MEDIA_PREFIX=/run/media/andreh
upsert_dir_symlink "$MEDIA_PREFIX/UTCLOUD"      ~/UTCLOUD
upsert_dir_symlink "$MEDIA_PREFIX/SNEAKER"     ~/SNEAKER

## tmux
ln -fs "$DOTFILES/tmux/tmux.conf"               ~/.tmux.conf
ln -fs "$DOTFILES/tmux/tmux-macos.conf"         ~/.tmux-macos.conf
ln -fs "$DOTFILES/tmux/tmux-gnu-linux.conf"     ~/.tmux-gnu-linux.conf

## NixOS
ln -fs "${DOTFILES}/nixos/evince.desktop"       ~/.local/share/applications/evince.desktop
ln -fs "${DOTFILES}/nixos/firefox.desktop"      ~/.local/share/applications/firefox.desktop
ln -fs "${DOTFILES}/nixos/nautilus.desktop"     ~/.local/share/applications/nautilus.desktop

## Emacs
mkdir -p "$HOME/.emacs.d/private/layers/"
ln -fs "$DOTFILES/spacemacs.el"                 ~/.spacemacs
ln -fs "$DOTFILES/emacs/gnus.el"                ~/.gnus.el

## xmonad
mkdir -p ~/.xmonad
ln -fs "$DOTFILES/xmonad/xsession.sh"           ~/.xsession
ln -fs "$DOTFILES/xmonad/xmonad.hs"             ~/.xmonad/xmonad.hs
ln -fs "$DOTFILES/xmonad/xmobar.hs"             ~/.xmobarrc

## Mercurial
ln -fs "${DOTFILES}/mercurial/hgrc.ini"         ~/.hgrc

## ranger
mkdir -p ~/.config/ranger/
ln -fs "${DOTFILES}/ranger.conf"                ~/.config/ranger/rc.conf

## Vim
ln -fs "${DOTFILES}/vimrc.conf"                 ~/.vimrc
