#!/usr/bin/env sh
#
# dotfiles update + git commit/push
#
# notes:
# rsync -avhHP ~/.[^.]* - all hidden dotfiles from home to..
# cp .config/vim/*(^/)" - cp only files - exclude dirs.

is-mac || exit 1

DOTFILES="${HOME}/.dotfiles"
DOTREPO="${HOME}/projects/dotfiles"

dot_packages() {
  brew ls --formula -1 --full-name > "${DOTFILES}/brew_ls_-1"
  brew ls --cask -1 --full-name > "${DOTFILES}/brew_cask_ls_-1"
  cargo install --list > "${DOTFILES}/cargo_install_--list"

  uv tool list > "${DOTFILES}/uv_tool_list"

  if command -v npm >/dev/null 2>&1; then
    npm list -g --depth=0 > "${DOTFILES}/npm_list_-g_--depth_0"
  fi

  if command -v go >/dev/null 2>&1; then
    /bin/ls -1 ~/go/bin/ \
      | while read -r bin; do go version -m "${HOME}/.local/share/go/bin/${bin}" \
      | grep '^[[:space:]]path' \
      | awk '{ print $2 }' \
      | grep '^github.com' \
      | sort \
      | uniq;\
    done > "${DOTREPO}/linux/go_list_github-com"
  fi
}

dot_rsync() {
  command rsync -avE --progress --exclude=".DS_Store" \
    "${HOME}/.profile" "${DOTFILES}/os/.profile"

  command rsync -avE --progress --exclude=".git/" --exclude=".DS_Store" \
    --exclude ".gitignore" --exclude="theme/" \
    --exclude="nvim/pack" --exclude="Session.vim" --exclude="tags" \
    --exclude=".zcompdump" --exclude=".zsh_history" --exclude=".zfunc" \
    "${DOTFILES}/" "${DOTREPO}/"

  # if [ ! -d "${DOTREPO}/zsh/.config/zsh" ]; then
  #   mkdir -p "${DOTREPO}/zsh/.config/zsh"
  # fi

  # zsh/zdotdir
  # command rsync -avE --progress --exclude=".DS_Store" \
  #   --exclude=".zcompdump" --exclude=".zsh_history" --exclude=".zfunc" \
  #   "${HOME}/.config/zsh/" "${DOTREPO}/zsh/.config/zsh/"

  # command rsync -avE --progress --exclude=".DS_Store" \
  #   "${HOME}/.zshenv" "${DOTREPO}/zsh/.zshenv"

  command rsync -avz --progress --exclude=".DS_Store" \
    "${HOME}/.zshrc" "${DOTREPO}/zsh/.zshrc"

  # command rsync -avHu --progress \
  #   --exclude={'.DS_Store','pipx','plugged/','after/*'} \
  #   "${DOTFILES}/" "${DOTREPO}/"
}

#dot_rsync() {
#  command rsync -avHE --progress --exclude=".DS_Store" --exclude="pipx" \
#    --exclude="vim/plugged" --exclude="vim/after" --exclude="vim/doc/tags" \
#    --exclude="nvim/plugged" --exclude="nvim/after" --exclude="nvim/pack" \
#    --exclude="aichat/functions" --exclude="aichat/agents" \
#    --exclude="aichat/messages.md" \
#    --exclude="io.datasette.llm/templates" --exclude="io.datasette.llm/logs.db" \
#    --exclude="io.datasette.llm/default_model.txt" \
#    --exclude="smartcat/conversation.toml" --exclude="theme/" \
#    --exclude="fish/fish_variables" \
#    "${DOTFILES}/" "${DOTREPO}/"
#
#  # zsh/zdotdir
#  command rsync -avHE --progress --exclude=".DS_Store" \
#    --exclude=".zkbd" --exclude=".zfunc*" --exclude="*zsh_history" \
#    --exclude="*.zwc" --exclude="*.zwc.old" \
#    --exclude=".zcompdump*" --exclude="zcompdump*" --exclude="compdump*" \
#    --exclude="compcache*" --exclude="zcompcache*" \
#    "${HOME}/.config/zsh/" "${DOTREPO}/zsh/.config/zsh/"
#
#  command rsync -avHE --progress --exclude=".DS_Store" \
#    "${HOME}/.zshenv" "${DOTREPO}/zsh/.zshenv"
#
#  # command rsync -avHu --progress \
#  #   --exclude={'.DS_Store','pipx','plugged/','after/*'} \
#  #   "${DOTFILES}/" "${DOTREPO}/"
#}

dot_commit() {
  cd "${DOTREPO}" || return
  git -C "${DOTREPO}" add --all
  git -C "${DOTREPO}" commit -a -m "*" -S
}

dot_push() {
  cd "${DOTREPO}" || return
  git -C "${DOTREPO}" add --all
  git -C "${DOTREPO}" commit -a -m "*" -S
  git -C "${DOTREPO}" push
}

if [ $# -eq 0 ]; then
  printf "%s\n" "usage: $(basename "$0") [f|ull] [s|ync] [p|push] [h|elp]"
  exit 0
fi

  case "$1" in
    f|full)
      command -v mullvad >/dev/null 2>&1 &&
        mullvad lan set allow
      dot_packages
      dot_rsync
      dot_push
      ;;
    s|sync)
      dot_rsync
      dot_commit
      ;;
    p|push)
      command -v mullvad >/dev/null 2>&1 &&
        mullvad lan set allow
      dot_rsync
      dot_push
      ;;
    *)
      printf "%s\n" "usage: $(basename "$0") [f|ull] [s|ync] [p|push] [h|elp]"
      exit 0
      ;;
  esac
