#!/usr/bin/env bash
set -euo pipefail

mode="link"   # link|copy
do_deps=0
verbose=0

usage() {
  cat <<'USAGE'
Usage: ./install.sh [--link|--copy] [--deps] [-v|--verbose]

Installs this repo's VisiData config and plugins into standard per-user locations:
- config.py in the user config dir (VisiData 3.3+ default)
- ~/.visidatarc (legacy fallback)
- plugins into $VD_DIR/plugins (VisiData 3.3 default on macOS is ~/Library/Preferences/visidata/plugins)

Options:
  --link    Symlink files into place (default)
  --copy    Copy files into place
  --deps    Install requirements.txt into $VD_DIR/plugins-deps using pip --target
  -v,--verbose Print actions taken
  -h,--help Show this help
USAGE
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    --link) mode="link"; shift ;;
    --copy) mode="copy"; shift ;;
    --deps) do_deps=1; shift ;;
    -v|--verbose) verbose=1; shift ;;
    -h|--help) usage; exit 0 ;;
    *) echo "Unknown arg: $1" >&2; usage; exit 2 ;;
  esac
done

script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
src_rc="${script_dir}/visidatarc"
src_plugins_dir="${script_dir}/plugins"
src_reqs="${script_dir}/requirements.txt"

if [[ ! -f "${src_rc}" ]]; then
  echo "Missing ${src_rc}" >&2
  exit 1
fi

timestamp() { date +"%Y%m%d-%H%M%S"; }

backup_if_needed() {
  local dst="$1"
  local src="$2"
  if [[ -L "$dst" ]]; then
    # If it's already a symlink to our source (absolute), no-op.
    local cur
    cur="$(readlink "$dst" || true)"
    if [[ "$cur" == "$src" ]]; then
      return 1
    fi
  fi
  if [[ -e "$dst" || -L "$dst" ]]; then
    mv "$dst" "${dst}.bak.$(timestamp)"
  fi
  return 0
}

install_one() {
  local src="$1"
  local dst="$2"
  mkdir -p "$(dirname "$dst")"
  if ! backup_if_needed "$dst" "$src"; then
    [[ "$verbose" -eq 1 ]] && echo "skip (already installed): $dst"
    return 0
  fi
  [[ "$verbose" -eq 1 ]] && echo "install: $src -> $dst ($mode)"
  if [[ "$mode" == "copy" ]]; then
    cp -f "$src" "$dst"
  else
    ln -s "$src" "$dst"
  fi
}

dst_visidatarc="${HOME}/.visidatarc"

# Prefer the python used by the `vd` entrypoint (if available).
pip_py=""
if command -v vd >/dev/null 2>&1; then
  vd_bin="$(command -v vd)"
  pip_py="$(sed -n '1{s/^#!//p;}' "$vd_bin" || true)"
fi
if [[ -z "$pip_py" ]]; then
  pip_py="$(command -v python3 || true)"
fi
if [[ -z "$pip_py" ]]; then
  echo "No python found to run pip." >&2
  exit 1
fi

# Use VisiData's own idea of the user config/VD dir (VisiData 3.3 on macOS
# defaults this to ~/Library/Preferences/visidata). Allow override via $VD_DIR.
vd_dir="${VD_DIR:-$("$pip_py" -c 'from visidata import vd; print(vd.options.visidata_dir)' 2>/dev/null || true)}"
if [[ -z "$vd_dir" ]]; then
  vd_dir="${HOME}/.visidata"
fi

dst_config_py="${vd_dir}/config.py"
dst_plugins_dir="${vd_dir}/plugins"
dst_deps_dir="${vd_dir}/plugins-deps"

# Config file(s)
install_one "$src_rc" "$dst_config_py"
install_one "$src_rc" "$dst_visidatarc"

# Plugins
mkdir -p "$dst_plugins_dir"
if [[ -d "$src_plugins_dir" ]]; then
  for src_plugin in "${src_plugins_dir}"/*.py; do
    [[ -e "$src_plugin" ]] || continue
    install_one "$src_plugin" "${dst_plugins_dir}/$(basename "$src_plugin")"
  done
fi

if [[ "$do_deps" -eq 1 ]]; then
  if [[ ! -f "$src_reqs" ]]; then
    echo "Missing ${src_reqs}" >&2
    exit 1
  fi
  mkdir -p "$dst_deps_dir"

  "$pip_py" -m pip install --upgrade --target "$dst_deps_dir" -r "$src_reqs"
fi

echo "Installed:"
echo "  ${dst_config_py}"
echo "  ${dst_visidatarc}"
echo "  ${dst_plugins_dir}/"
if [[ "$do_deps" -eq 1 ]]; then
  echo "  ${dst_deps_dir}/"
fi
