#!/bin/sh -e
if [ "$1" = '-f' ]; then
	force=true
fi
if ! command -v cargo > /dev/null; then
	echo "Error: cargo not found"
	exit 1
fi
if ! command -v c++ > /dev/null; then
	echo "Error: c++ not found"
	exit 1
fi
if [ -d ~/.config/helix ]; then
	if [ "$force" = true ]; then
		rm -rf ~/.config/helix
	else
		echo "Error: helix config files already exist; use -f to force erase and reinstall"
		exit 1
	fi
fi
rm -rf helix-git
git clone --single-branch https://github.com/helix-editor/helix helix-git
cd helix-git
git checkout 7f5940be80eaa3aec7903903072b7108f41dd97b
cargo install -f --locked --path helix-term
mkdir -p ~/.config/helix
rm -rf runtime/grammars/sources
mv runtime ~/.config/helix
cd ..
rm -rf helix-git
cat > ~/.config/helix/config.toml << 'EOF'
theme = "monokai_pro_ristretto"

[keys.normal]
space.z = ":reflow 100"
space.i = ":toggle lsp.display-inlay-hints"
space.u = ":update"

[editor]
auto-pairs = false
bufferline = "multiple"
rulers = [101]

[editor.lsp]
display-messages = true
goto-reference-include-declaration = false

[editor.soft-wrap]
enable = true

[editor.statusline]
left = ["mode", "spinner", "file-name"]
center = ["position-percentage"]
right = [
	"version-control", "diagnostics", "selections", "position", "file-encoding", "file-line-ending"
]
EOF
cat > ~/.config/helix/languages.toml << 'EOF'
[editor]
auto-pairs = false

[[language]]
name = 'rust'
indent = { tab-width = 4, unit = "\t" }
config.checkOnSave = { command = "clippy" }
shebangs = ["rust-script"]

[[language]]
name = "c"
indent = { tab-width = 4, unit = "\t" }

[[language]]
name = "cpp"
indent = { tab-width = 4, unit = "\t" }

[[language]]
name = 'java'
indent = { tab-width = 4, unit = "\t" }

[[language]]
name = "bash"
indent = { tab-width = 4, unit = "\t" }

[[language]]
name = "toml"
indent = { tab-width = 4, unit = "\t" }

[[language]]
name = "markdown"
indent = { tab-width = 4, unit = "\t" }
language-server = { command = "ltex-ls" }
file-types = ["md"]
scope = "source.markdown"
roots = []
EOF
