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

set -e

is-mac || exit 1

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

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

  if command -v tlmgr >/dev/null 2>&1; then
    tlmgr info --only-installed >"${DOTFILES}/tlmgr.packages"
    # tlmgr info --list --only-installed --data name
  fi

  pipx list >"${DOTFILES}/pipx_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 && [ -n "${GOPATH-}" ]; 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 --archive --verbose --executability --progress --exclude=".DS_Store" \
    "${HOME}/.profile" "${DOTFILES}/profile/.profile"

  command rsync --archive --verbose --executability --progress \
    --exclude=".DS_Store" --exclude="*.BAK" --exclude="config.local" \
    --exclude="*.key" --exclude="*.pem" --exclude="*.cer" \
    --exclude=".git/" --exclude ".gitignore" --exclude="config-git" \
    --exclude="Session.vim" --exclude="tags" --exclude=".add.spl" \
    --exclude=".zcompdump*" --exclude=".zsh_history" --exclude=".zfunc" --exclude="*.zwc" \
    --exclude=".db" --exclude="uv-receipt.json" \
    --exclude="wall/" --exclude="theme/" --exclude="fonts/" \
    --exclude="uxnasm" --exclude="uxncli" --exclude="uxnemu" \
    "${DOTFILES}/" "${DOTREPO}/"

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

  cd "${DOTREPO}" || return 1
  git -C "${DOTREPO}" add --all
}

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

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

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

case "$1" in
  f | full)
    if command -v mullvad >/dev/null 2>&1; then
      if mullvad lan get | grep -q 'block'; then
        mullvad lan set allow
      fi
    fi
    dot_packages
    dot_rsync
    shift
    dot_push "$1"
    ;;
  s | sync)
    dot_rsync
    shift
    # dot_commit "$1"
    ;;
  p | push)
    if command -v mullvad >/dev/null 2>&1; then
      if mullvad lan get | grep -q 'block'; then
        mullvad lan set allow
      fi
    fi
    dot_rsync
    shift
    printf '%s' "Enter commit message:" ' '
    read -r ans
    dot_push "${ans}"
    ;;
  h | help)
    printf '%s\n' "Usage: $(basename "$0") [f|ull] [s|ync] [p|push] [h|elp]"
    exit 0
    ;;
  *)
    printf '%s\n' "Usage: $(basename "$0") [f|ull] [s|ync] [p|push] [h|elp]" >&2
    exit 1
    ;;
esac
