#!/bin/sh

dotfiles_update_remote() {
  cp "${HOME}/.zshrc" "${DOTFILES-}/.zshrc"

  # rsync --archive --verbose --hard-links
  rsync -avH \
    --include-from="${DOTFILES}/.include" \
    "${XDG_CONFIG_HOME-}/" "${DOTFILES}/.config/" --delete-before

  mkdir -p "${DOTFILES}/usr/local/bin/"
  rsync -avH \
    --include-from="${DOTFILES}/.include" \
    "/usr/local/" "${DOTFILES}/usr/local/"

  mkdir -p "${DOTFILES}/.local/share/applications/"
  rsync -avH \
    "${HOME}/.local/share/applications/browser.desktop" \
    "${DOTFILES}/.local/share/applications/browser.desktop"

  cargo install --list > "${DOTFILES}/cargo_install_--list"

  npm list -g --depth=0 > "${DOTFILES}/npm_list_-g_--depth_0"

  /bin/ls -1 ~/.go/bin/ \
    | while read -r bin; \
    do go version -m "${HOME}/.go/bin/${bin}" \
    | grep '^[[:space:]]path' \
    | awk '{ print $2 }' \
    | grep '^github.com' \
    | sort \
    | uniq;\
    done > "${DOTFILES}/go_list_github-com"

  gh extension list > "${DOTFILES}/gh_extension_list"

  git -C "${DOTFILES}" commit -a -S
  return 0
}

dotfiles_update_local() {
  printf "are you sure? (y/n) "
  read -r confirmation

  [ "${confirmation}" != "y" ] && return 1

  cp "${DOTFILES}/.zshrc" "${HOME}/.zshrc"
  cp "${DOTFILES}/.motd" "${HOME}/.motd"
  cp "${DOTFILES}/.vale.ini" "${HOME}/.vale.ini"
  cp "${DOTFILES}/.wallpaper" "${HOME}/.wallpaper"

  rsync -avH \
    --include-from="${DOTFILES}/.include" \
    "${DOTFILES}/.config/" "${XDG_CONFIG_HOME}/" 

  cp "${DOTFILES}/usr/local/bin/"* /usr/local/bin/

  mkdir -p "${HOME}/.local/share/applications/" 
  cp "${DOTFILES}/local/share/applications/browser.desktop" \
    "${HOME}/.local/share/applications/browser.desktop"
  return 0
}
