#!/usr/bin/env bash

DEST=${1-/etc/skel}
NEOVIDE_HOME=${DEST}/.config/neovide
NEOVIDE_CONFIG=${NEOVIDE_HOME}/config.toml
NVIM_HOME="${DEST}/.config/nvim"
NVIM_PLUGINS_HOME=${NVIM_HOME}/lua/plugins

mkdir -p "${NEOVIDE_HOME}"
sudo apt update --qq
sudo apt install -y curl \
  gnupg ca-certificates git \
  gcc-multilib g++-multilib cmake libssl-dev pkg-config \
  libfreetype6-dev libasound2-dev libexpat1-dev libxcb-composite0-dev \
  libbz2-dev libsndio-dev freeglut3-dev libxmu-dev libxi-dev libfontconfig1-dev \
  libxcursor-dev

printf "Using cargo to fetch, build and install, it's a rust thing."
cargo install --git https://github.com/neovide/neovide

conf_print_neovide_conf() {
  cat <<'EOF'
backtraces-path = "${HOME}/.local/share/neovide/neovide_backtraces.log" # see below for the default platform specific location
chdir = "/path/to/dir"
fork = false
frame = "full"
# grid = "420x240" # mutually exclusive with `size` and `maximized`
# size = "1200x800" # mutually exclusive with `grid` and `maximized`
idle = true
icon = "${HOME}/.local/share/nvim/lazy/neovide/assets/neovide.ico"
maximized = false
mouse-cursor-icon = "arrow"  # "i-beam" 
neovim-bin = "/usr/bin/nvim" # in reality found dynamically on $PATH if unset
no-multigrid = false
opengl = false # macOS/Windows only
# server = "/tmp/nvim.sock" # or "127.0.0.1:7777"
srgb = false # platform-specific: false (Linux/macOS) or true (Windows)
tabs = true
title-hidden = false
vsync = true
# wayland-app-id = "neovide"
wsl = false
# x11-wm-class = "neovide"
# x11-wm-class-instance = "neovide"

[font]
normal = [] # Will use the bundled Fira Code Nerd Font by default
size = 14.0

[box-drawing]
# "font-glyph", "native" or "selected-native"
mode = "font-glyph"

[box-drawing.sizes]
default = [2, 4]  # Thin and thick values respectively, for all sizes

EOF
}
conf_print_neovide_conf | tee "${NEOVIDE_CONFIG}/neovide_config.toml"

## Font scaling t o match normal font size in neovide
# vim.g.neovide_scale_factor controls the font scaling dynamically.
# vim.g.neovide_increment_scale_factor sets the step size for scaling (e.g., 0.1 = 10% change per keypress).
# vim.g.neovide_min_scale_factor and vim.g.neovide_max_scale_factor prevent extreme scaling.
# Keymaps allow real-time adjustment using Ctrl+=, Ctrl+-, and Ctrl+0.

conf_print_neovide_lua() {
  cat <<EOF
-- lua/plugins/neovide_scaling.lua
return {
  "neovide/neovide", -- Required: any valid plugin name (even if not used)
  config = function()
    vim.g.neovide_scale_factor = 0.5
    vim.g.neovide_min_scale_factor = 0.7
    vim.g.neovide_max_scale_factor = 2.0
    vim.g.neovide_increment_scale_factor = 0.1

    local function change_scale_factor(increment)
      local new_scale = vim.g.neovide_scale_factor + increment
      vim.g.neovide_scale_factor = math.max(
        vim.g.neovide_min_scale_factor,
        math.min(vim.g.neovide_max_scale_factor, new_scale)
      )
    end

    vim.keymap.set("n", "<C-=>", function() change_scale_factor(0.1) end, { desc = "Increase scale" })
    vim.keymap.set("n", "<C-->", function() change_scale_factor(-0.1) end, { desc = "Decrease scale" })
    vim.keymap.set("n", "<C-0>", function() vim.g.neovide_scale_factor = 1.0 end, { desc = "Reset scale" })

    -- Ctrl+Scrollwheel font size up/down like Nvy or VSCode.
    vim.keymap.set({ "n", "v" }, "<C-ScrollWheelUp>", ":lua vim.g.neovide_scale_factor = vim.g.neovide_scale_factor + 0.1<CR>")
    vim.keymap.set({ "n", "v" }, "<C-ScrollWheelDown>", ":lua vim.g.neovide_scale_factor = vim.g.neovide_scale_factor - 0.1<CR>")
  end,
}
EOF
}
conf_print_neovide_lua | tee "${NVIM_PLUGINS_HOME}/neovide_scaling.lua"

conf_print_zsh_shell_wrapper() {
  cat <<EOF
  function n() {
  command neovide --mouse-cursor-icon i-beam
  }
EOF
}
conf_print_zsh_shell_wrapper | tee "${DEST}/.zsh_aliases.d/010_neovide.zsh"

# Install desktop file
# install -m644 "${DEST}/.local/share/nvim/lazy/neovide/assets/neovide.desktop" ~/.local/share/applications/neovide.desktop

conf_print_neovide_desktop() {
  cat <<EOF
[Desktop Entry]
Type=Application
Exec=sh -c 'exec ${HOME}/.cargo/bin/neovide %F'
Icon=${HOME}/.local/share/icons/hicolor/scalable/apps/neovide.png
Name=Neovide
Keywords=Text;Editor;
Categories=Utility;TextEditor;
Comment=No Nonsense Neovim Client in Rust
MimeType=text/english;text/plain;text/x-makefile;text/x-c++hdr;text/x-c++src;text/x-chdr;text/x-csrc;text/x-java;text/x-moc;text/x-pascal;text/x-tcl;text/x-tex;application/x-shellscript;text/x-c;text/x-c++;
StartupNotify=true
StartupWMClass=neovide
EOF
}
conf_print_neovide_desktop | tee "${DEST}/.local/share/applications/neovide.desktop"

update-desktop-database ~/.local/share/applications

xfce4-panel --add=launcher "${DEST}/.local/share/applications/neovide.desktop"

# Add an icon
mkdir -p ~/.local/share/icons/hicolor/scalable/apps/
cp "${DEST}/.local/share/nvim/lazy/neovide/assets/neovide-256x256.png" \
  "${DEST}/.local/share/icons/hicolor/scalable/apps/neovide.png"

gtk-update-icon-cache "${DEST}/.local/share/icons/hicolor/"
