# Maintainer: Joey Yakimowich-Payne <jyapayne@pm.me>
#
# BlueRune's Neovim (PLAN.md, Phase 16): a ready-to-use Neovim, the "Neovim" app in a Neovide
# window and `bluerune-nvim` in a terminal, with its own config, plugins and data
# (NVIM_APPNAME=bluerune-nvim), so ~/.config/nvim is never read or written. The plugins are
# pinned (dist/nvim/lazy-lock.json; release/release_tests.sh checks the sources below say the
# same) and shipped here, and the common languages' tree-sitter parsers are compiled here, so
# it works offline on first start. The config's files are links to dist/nvim, which
# release/publish.sh resolves; pkgver counts changes to them or to the pins.

pkgname=bluerune-nvim
pkgver=1
pkgrel=1
pkgdesc="BlueRune's Neovim: a ready-to-use Neovim and Neovide in BlueRune's colors, with its own config"
arch=('x86_64' 'aarch64')
url='https://bluerune.dev'
license=('Apache-2.0 OR MIT')
depends=(
  'neovim>=0.12'
  # The "Neovim" app's window.
  'neovide'
  # The picker's file and text search, and the system clipboard.
  'ripgrep'
  'fd'
  'wl-clipboard'
  # The icons in the file tree, the picker and the statusline.
  'ttf-nerd-fonts-symbols'
  # Mason, which installs language servers when asked: it downloads and unpacks them.
  'git'
  'curl'
  'unzip'
  'tar'
  'gzip'
)
makedepends=('gcc')
optdepends=(
  'npm: language servers Mason installs with npm (TypeScript, JSON, YAML, HTML, CSS, Bash, Python)'
  'tree-sitter-cli: :TSInstall, for highlighting more languages than the package compiles'
)

# The plugins, at the commits dist/nvim/lazy-lock.json pins; lazy.nvim loads them by these
# names from /usr/share/bluerune/nvim/lazy.
_plugins=(
  'blink.cmp::git+https://github.com/saghen/blink.cmp.git#commit=539053d87740e357a96fb304c9a4da2ef27b3576'
  'conform.nvim::git+https://github.com/stevearc/conform.nvim.git#commit=3543d000dafbc41cc7761d860cfdb24e82154f75'
  'friendly-snippets::git+https://github.com/rafamadriz/friendly-snippets.git#commit=7a1c9f7897a4eb078b157a04c7af81e5869215b1'
  'gitsigns.nvim::git+https://github.com/lewis6991/gitsigns.nvim.git#commit=a462f416e2ce4744531c6256252dee99a7d34a83'
  'lazy.nvim::git+https://github.com/folke/lazy.nvim.git#commit=85c7ff3711b730b4030d03144f6db6375044ae82'
  'lualine.nvim::git+https://github.com/nvim-lualine/lualine.nvim.git#commit=221ce6b2d999187044529f49da6554a92f740a96'
  'mason.nvim::git+https://github.com/mason-org/mason.nvim.git#commit=2a6940af80375532e5e9e7c1f2fc6319a1b7a69d'
  'mini.nvim::git+https://github.com/nvim-mini/mini.nvim.git#commit=1345d191bb3da9c7b0e977f4387c5761f9bff68d'
  'neo-tree.nvim::git+https://github.com/nvim-neo-tree/neo-tree.nvim.git#commit=f3f3bf73414e400cf9fc13fda50f00404a8f8ab1'
  'nui.nvim::git+https://github.com/MunifTanjim/nui.nvim.git#commit=f535005e6ad1016383f24e39559833759453564e'
  'nvim-lspconfig::git+https://github.com/neovim/nvim-lspconfig.git#commit=4648fd8ba5c678d7e2db75ae44ffac269bb589d8'
  'nvim-treesitter::git+https://github.com/nvim-treesitter/nvim-treesitter.git#commit=52773f0d13a0c11d0c3ef33c14ab6ecc1c17148b'
  'plenary.nvim::git+https://github.com/nvim-lua/plenary.nvim.git#commit=74b06c6c75e4eeb3108ec01852001636d85a932b'
  'snacks.nvim::git+https://github.com/folke/snacks.nvim.git#commit=e6fd58c82f2f3fcddd3fe81703d47d6d48fc7b9f'
  'which-key.nvim::git+https://github.com/folke/which-key.nvim.git#commit=fcbf4eea17cb299c02557d576f0d568878e354a4'
)
# The grammars of the parsers build-parsers.lua compiles, at the revisions the pinned
# nvim-treesitter names (build-parsers.lua stops the build when one differs).
_grammars=(
  'tree-sitter-bash::git+https://github.com/tree-sitter/tree-sitter-bash.git#commit=a06c2e4415e9bc0346c6b86d401879ffb44058f7'
  'tree-sitter-c::git+https://github.com/tree-sitter-grammars/tree-sitter-c.git#commit=f3202ac4f1041e0b0a2b908147d5dd0e951da7b1'
  'tree-sitter-css::git+https://github.com/tree-sitter/tree-sitter-css.git#commit=dda5cfc5722c429eaba1c910ca32c2c0c5bb1a3f'
  'tree-sitter-diff::git+https://github.com/tree-sitter-grammars/tree-sitter-diff.git#commit=ada384ac7bfc1307f32de474620120add29998fb'
  'tree-sitter-gitcommit::git+https://github.com/gbprod/tree-sitter-gitcommit.git#commit=55a265cf763ec15d6e2d96bb206e3f5e30d82bae'
  'tree-sitter-go::git+https://github.com/tree-sitter/tree-sitter-go.git#commit=2346a3ab1bb3857b48b29d779a1ef9799a248cd7'
  'tree-sitter-html::git+https://github.com/tree-sitter/tree-sitter-html.git#commit=73a3947324f6efddf9e17c0ea58d454843590cc0'
  'tree-sitter-javascript::git+https://github.com/tree-sitter/tree-sitter-javascript.git#commit=58404d8cf191d69f2674a8fd507bd5776f46cb11'
  'tree-sitter-json::git+https://github.com/tree-sitter/tree-sitter-json.git#commit=254c42a6476413b776221e03982ac8ae159eeb72'
  'tree-sitter-lua::git+https://github.com/tree-sitter-grammars/tree-sitter-lua.git#commit=10fe0054734eec83049514ea2e718b2a56acd0c9'
  'tree-sitter-luadoc::git+https://github.com/tree-sitter-grammars/tree-sitter-luadoc.git#commit=4d04632a3a398b78af52e83be074883e722f40be'
  'tree-sitter-markdown::git+https://github.com/tree-sitter-grammars/tree-sitter-markdown.git#commit=41ff24a6c775492c543b09228aeb98d1cd2b3a0f'
  'tree-sitter-python::git+https://github.com/tree-sitter/tree-sitter-python.git#tag=v0.25.0'
  'tree-sitter-query::git+https://github.com/tree-sitter-grammars/tree-sitter-query.git#commit=8e9e223812ff30854fbc912adbec696ba5f0e023'
  'tree-sitter-regex::git+https://github.com/tree-sitter/tree-sitter-regex.git#commit=b2ac15e27fce703d2f37a79ccd94a5c0cbe9720b'
  'tree-sitter-rust::git+https://github.com/tree-sitter/tree-sitter-rust.git#commit=77a3747266f4d621d0757825e6b11edcbf991ca5'
  'tree-sitter-toml::git+https://github.com/tree-sitter-grammars/tree-sitter-toml.git#commit=64b56832c2cffe41758f28e05c756a3a98d16f41'
  'tree-sitter-typescript::git+https://github.com/tree-sitter/tree-sitter-typescript.git#commit=75b3874edb2dc714fb1fd77a32013d0f8699989f'
  'tree-sitter-vim::git+https://github.com/tree-sitter-grammars/tree-sitter-vim.git#commit=039c8d0aa1deae00ddeb0374dd70bcc0ec56938d'
  'tree-sitter-vimdoc::git+https://github.com/neovim/tree-sitter-vimdoc.git#commit=cf5f03718e699c0e37e4f26a4417fb744c714ba2'
  'tree-sitter-yaml::git+https://github.com/tree-sitter-grammars/tree-sitter-yaml.git#commit=a1c4812a73ec5e089de8e441fdea3a921e8d5079'
)
# The config (links to dist/nvim), by where it goes under /usr/share/bluerune/nvim.
_top=('init.lua' 'lazy-lock.json')
_modules=('options.lua' 'keymaps.lua' 'lsp.lua' 'neovide.lua' 'palette.lua' 'palette_default.lua' 'theme.lua')
_specs=('ui.lua' 'code.lua')
_colors=('bluerune.lua')
_files=("${_top[@]}" "${_modules[@]}" "${_specs[@]}" "${_colors[@]}" 'build-parsers.lua' 'bluerune-nvim' 'bluerune-nvim.desktop' 'neovim.md')
source=("${_files[@]}" "${_plugins[@]}" "${_grammars[@]}")
# The config's files are summed; the checkouts are pinned by commit instead.
sha256sums=('7eee63fff5412c6e75f3e4ab6c0c174604d80b32e6bedbe958baabc4f1def36d'
            'a8ee1662685d44b87c694f14d1998ca7c2e0165e835b56c28de2f31579bab0f3'
            'd5f323da845164553273688d962a97734fa489b69dad018672510d963185ad93'
            '3e69275325ba4916086742c534881100950adec6bb2e58ac2c16a5326a98d675'
            '5a6c4b3e3b087b08181c01b28f0d2cf254a49835071c4f74bb162326c9fce86d'
            '11fc25547b0dcb15d79365b589a7b019d4df31067d093944d9c115ead1ca4f94'
            '6831717d4200221872b390fb781ec68b7f36dd43a95003cbfeaba42b8cfb893b'
            '9b5b1c55a16fdb00880918b7348fc9f54e2450e63976cef117ef9f503fd7f2ac'
            'fc505b8205f5b81f4dd78bba3758c1d190c68a573924c2199d3bdb929c4d06e1'
            '383da9d87e7fab05f4c6af156e8102ec52c4314fd253c99180017fa012e1cca7'
            '1c31ab36f0c4a01e8d06c8cbb99d89732c2ec360620d8e5a8aa54bbab75b6493'
            '966b635867283e0ac2c9ac192ace8601a60b52b789a9c63954b7baf81c616116'
            '7601a954f0e9fbb560712c73c3fcfa44ccb3697e7443b4fdd45cf149ea0f3d4c'
            '1bd3422cb5194d359d809d7e02e3e0b208b011720dc547cd4bd656a6ad65789d'
            '6b457a3615b7dd314f96d3511099b94036aa5cd91bf7e00a053b682fed61b333'
            'fc6590432549925c2941bd00b3632babf816f2fff6e886e4f73391d1db8ab11f'
            "${_plugins[@]/*/SKIP}"
            "${_grammars[@]/*/SKIP}")

build() {
  rm -rf site
  nvim -l build-parsers.lua "$srcdir/nvim-treesitter" "$srcdir" "$srcdir/site"
  # :help for the plugins: /usr/share is read-only when lazy.nvim would make the tags.
  local plugin
  for plugin in "${_plugins[@]}"; do
    plugin=${plugin%%::*}
    if [[ -d $plugin/doc ]]; then
      nvim --headless -u NONE -i NONE -c "helptags $plugin/doc" -c quit
    fi
  done
}

package() {
  local share=$pkgdir/usr/share/bluerune/nvim plugin
  install -Dm644 "${_top[@]}" -t "$share"
  install -Dm644 "${_modules[@]}" -t "$share/lua/bluerune"
  install -Dm644 "${_specs[@]}" -t "$share/lua/bluerune/plugins"
  install -Dm644 "${_colors[@]}" -t "$share/colors"
  cp -r --no-preserve=ownership site "$share/site"
  for plugin in "${_plugins[@]}"; do
    plugin=${plugin%%::*}
    install -dm755 "$share/lazy/$plugin"
    cp -r --no-preserve=ownership "$plugin/." "$share/lazy/$plugin"
    rm -rf "$share/lazy/$plugin/.git" "$share/lazy/$plugin/.github"
    if [[ -f $plugin/LICENSE ]]; then
      install -Dm644 "$plugin/LICENSE" "$pkgdir/usr/share/licenses/$pkgname/$plugin.LICENSE"
    fi
  done
  install -Dm755 bluerune-nvim -t "$pkgdir/usr/bin"
  install -Dm644 bluerune-nvim.desktop -t "$pkgdir/usr/share/applications"
  install -Dm644 neovim.md -t "$pkgdir/usr/share/doc/bluerune"
}
