#!/bin/sh
# vim: set ft=sh sw=2 et :

install_nix() {
  # This one courtesy of:
  # https://github.com/shadowrylander/shadowrylander/blob/35bb51822c46578d0a5da5810263fa85d464043c/.config/yadm/bootstrap#L56
  install_nix_bin() {

    # Single User install
    # Alternate nix-daemon path: . /nix/var/nix/profiles/per-user/${USER}/profile/etc/profile.d/nix-daemon.sh
    # curl -L https://nixos.org/nix/install | sh
    #. $HOME/.nix-profile/etc/profile.d/nix.sh

    # For Multi-User install
    curl -L https://nixos.org/nix/install | sh -s -- --daemon --yes
    . '/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh'

    if [ -f "$HOME/.config/nix/installed_packages" ]; then
      # This list created with `nix-env -qaPs|grep '^I'|awk '{print $2}' > ~/.config/nix/installed_packages`
      cat "$HOME/.config/nix/installed_packages" | xargs nix-env -iA
    fi
  }
  command -v nix >/dev/null 2>&1 || install_nix_bin
}

configure_vim() {
  # Install vim-plug for Vim
  #curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  #vim +'PlugInstall --sync' +qa

  # Install vim-plug for Neovim
  # This is temporary disable, because that it hundreds modules and many error on compilation stage of this
  curl -fLo ~/.local/share/nvim/site/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  nvim +'PlugInstall --sync' +qa
}

install_starship() {
  # https://starship.rs/guide/#%F0%9F%9A%80-installation
  curl -sS https://starship.rs/install.sh | sh -s -- -f
}

install_spacevim() {
  # https://habr.com/ru/companies/cloud4y/articles/574450/
  curl -sLf https://spacevim.org/install.sh | bash
}

download_font() {
  url="https://raw.githubusercontent.com/ryanoasis/nerd-fonts/master/patched-fonts/SourceCodePro/Regular/complete/${1// /%20}"
  path="/usr/share/fonts/$1"
  if [[ -f "$path" && ! -s "$path" ]]; then
    rm "$path"
  fi
  if [[ -f "$path" ]]; then
    success "Downloaded $1"
  else
    info "Downloading $1"
    curl -s -o "$path" "$url"
    success "Downloaded $1"
  fi
}

install_fonts() {
  download_font "Sauce Code Pro Nerd Font Complete.ttf"
  info "Updating font cache, please wait ..."

  fc-cache -fv >/dev/null
  success "font cache done!"
}

install_pathogen() {
  mkdir -p ~/.vim/autoload ~/.vim/bundle

  curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
}

install_vim_plugins() {
  cd ~/.vim/bundle

  git clone https://github.com/tpope/vim-sensible.git
  git clone https://github.com/altercation/vim-colors-solarized.git
  git clone https://github.com/cespare/vim-toml.git
}

# That is not work, because starship.toml not exists on the bootstrap stage
#change_starship_character() {
#  # shellcheck disable=SC2039
#  if [[ "$USER" != root ]]; then
#    sed -i 's/#/\\$/g' "$HOME"/.config/starship.toml
#  fi
#}

#install_nix

#configure_vim

#install_starship
#install_spacevim

install_pathogen
install_vim_plugins
#change_starship_character