#!/usr/bin/env bash

DEST=${1:-/etc/skel}
BAT_HOME=${DEST}/.config/bat
BAT_THEMES=${BAT_HOME}/themes
BAT_SYNTAX=${BAT_HOME}/syntaxes

sudo apt install bat

# <https://github.com/catppuccin/bat?
mkdir -p "${BAT_THEMES}"
mkdir -p "${BAT_SYNTAX}"

wget -P "${BAT_THEMES}" "https://github.com/catppuccin/bat/raw/main/themes/Catppuccin%20Mocha.tmTheme"
for theme in Latte Frappe Macchiato Mocha; do
  wget \
    -P "${BAT_THEMES}" \
    "https://github.com/catppuccin/bat/raw/main/themes/Catppuccin%20${theme}.tmTheme"
done

conf_print_batcat_env() {
  cat <<EOF
BAT_THEME="Catppuccin Mocha"
EOF
}
conf_print_batcat_env | tee ${DEST}/.zshrc.d/002_batcat.zsh

# <https://github.com/Sublime-Instincts/BetterJinja/blob/master/Syntaxes/YAML/YAML%20(Jinja).sublime-syntax>
# wget -P "${BAT_SYNTAX}" "https://raw.githubusercontent.com/Sublime-Instincts/BetterJinja/refs/heads/master/Syntaxes/YAML/YAML%20(Jinja).sublime-syntax"
# wget -P "${BAT_SYNTAX}" "https://raw.githubusercontent.com/Sublime-Instincts/BetterJinja/refs/heads/master/Syntaxes/Text/Text%20(Jinja).sublime-syntax"

conf_print_jinja_syntax() {
  cat <<'EOF'
%YAML 1.2
---
# http://www.sublimetext.com/docs/syntax.html
name: YAML (Jinja)
scope: source.yaml.jinja
version: 2

#extends: Packages/YAML/YAML.sublime-syntax
extends: YAML

file_extensions:
  - yaml.jinja
  - yaml.jinja2
  - yaml.j2
  - jinja.yaml
  - jinja2.yaml
  - j2.yaml
  - yml.jinja
  - yml.jinja2
  - yml.j2
  - jinja.yml
  - jinja2.yml
  - j2.yml
  # Django
  - djyml
  - dj.yml
  - yml.djt
  - djyaml
  - dj.yaml
  - yaml.djt
  - yaml.sls

variables:
  ns_plain_first_plain_in: |- # c=plain-in
    (?x:
        {{yaml_ns_plain_first_plain_in}}
      | (?:[?:-] )?{{  # begins with interpolation
    )

  # original value from YAML
  yaml_ns_plain_first_plain_in: |- # c=plain-in
    (?x:
        [^\s{{c_indicator}}]
      | [?:-] [^\s{{c_flow_indicator}}]
    )

  ns_plain_first_plain_out: |- # c=plain-out
    (?x:
        {{yaml_ns_plain_first_plain_out}}
      | (?:[?:-] )?{{  # begins with interpolation
    )

  # original value from YAML
  yaml_ns_plain_first_plain_out: |- # c=plain-out
    (?x:
        [^\s{{c_indicator}}]
      | [?:-] \S
    )

  _flow_key_in_lookahead: |-
    (?x:
      (?=
        (
            (?:
              {{yaml_ns_plain_first_plain_in}}
              | {{jinja_interpolation}}  # begins with interpolation
            )
            ( [^\s:{{c_flow_indicator}}]
            | : [^\s{{c_flow_indicator}}]
            | \s+ (?![#\s])
            | {{jinja_interpolation}}    # ignore interpolation
            )*
          | \".*\" # simplified
          | \'.*\'
        )
        \s*
        :
        (?:\s|$)
      )
    )

  _flow_key_out_lookahead: |-
    (?x:
      (?=

        (
            (?:
              {{yaml_ns_plain_first_plain_out}}
              | {{jinja_interpolation}}  # begins with interpolation
            )
            ( [^\s:]
            | : \S
            | \s+ (?![#\s])
            | {{jinja_interpolation}}    # ignore interpolation
            )*
          | \".*\" # simplified
          | \'.*\'
        )
        \s*
        :
        (?:\s|$)
      )
    )

  jinja_interpolation: '{{.*?}}'

contexts:

  main:
    - meta_prepend: true
    - meta_scope: meta.template.jinja

  block-scalar-body:
    - meta_prepend: true
    - include: Text (Jinja).sublime-syntax#interpolations

  flow-scalar-plain-in-body:
    - meta_prepend: true
    - include: Text (Jinja).sublime-syntax#interpolations

  flow-scalar-plain-out-body:
    - meta_prepend: true
    - include: Text (Jinja).sublime-syntax#interpolations

  flow-scalar-double-quoted-body:
    - meta_prepend: true
    - include: Text (Jinja).sublime-syntax#interpolations

  flow-scalar-single-quoted-body:
    - meta_prepend: true
    - include: Text (Jinja).sublime-syntax#interpolations

  flow-collection:
    - meta_prepend: true
    - include: Text (Jinja).sublime-syntax#jinja-comment-tags
    - include: Text (Jinja).sublime-syntax#jinja-statement-tags

  flow-mapping:
    - match: \{(?![{%#])
      scope: punctuation.definition.mapping.begin.yaml
      push: flow-mapping-body

EOF
}
#conf_print_jinja_syntax | tee ${BAT_SYNTAX}/YAML_jinja.sublime-syntax

conf_print_jinja_syntax() {
  cat <<'EOF'
%YAML 1.2
---
name: YAML with Jinja2
scope: source.yaml.jinja
file_extensions:
  - yaml
  - yml
extends: YAML

contexts:
  main:
    - include: jinja-variables
    - include: jinja-blocks
    - include: jinja-comments

  jinja-variables:
    - match: '\{\{'
      scope: punctuation.section.embedded.begin.jinja
      push:
        - meta_scope: source.jinja.embedded
        - match: '\}\}'
          scope: punctuation.section.embedded.end.jinja
          pop: true

  jinja-blocks:
    - match: '\{%'
      scope: punctuation.section.embedded.begin.jinja
      push:
        - meta_scope: source.jinja.embedded
        - match: '%\}'
          scope: punctuation.section.embedded.end.jinja
          pop: true

  jinja-comments:
   - match: '\{#'
     scope: punctuation.definition.comment.begin.jinja
     push:
       - meta_scope: comment.block.jinja
       - match: '#\}'
         scope: punctuation.definition.comment.end.jinja
         pop: true
EOF
}
#conf_print_jinja_syntax | tee ${BAT_SYNTAX}/YAML_jinja.sublime-syntax

# Built in themes.
# echo $(bat --list-themes)
# 1337 Coldark-Cold Coldark-Dark DarkNeon Dracula GitHub Monokai Extended (default dark) Monokai Extended Bright Monokai Extended Light (default light) Monokai Extended Origin Nord OneHalfDark OneHalfLight Solarized (dark) Solarized (light) Sublime Snazzy TwoDark Visual Studio Dark+ ansi base16 base16-256 gruvbox-darkgruvbox-light zenburn

conf_print_bat_config() {
  cat <<'EOF'
# This is $(bat)s configuration file. Each line either contains a comment or
# a command-line option that you want to pass to $(bat) by default. You can
# run $(bat --help) to get a list of all possible configuration options.

# Specify desired highlighting theme (e.g. "TwoDark"). Run $(bat --list-themes)
# for a list of all available themes
#--theme="Solarized (dark)"
--theme="Catppuccin Mocha"

# Enable this to use italic text on the terminal. This is not supported on all
# terminal emulators (like tmux, by default):
#--italic-text=always

# Uncomment the following line to disable automatic paging:
#--paging=never

# Uncomment the following line if you are using less version >= 551 and want to
# enable mouse scrolling support in $(bat) when running inside tmux. This might
# disable text selection, unless you press shift.
--pager="less --RAW-CONTROL-CHARS --quit-if-one-screen --mouse"

# Syntax mappings: map a certain filename pattern to a language.
#   Example 1: use the C++ syntax for Arduino .ino files
#   Example 2: Use ".gitignore"-style highlighting for ".ignore" files
#--map-syntax "*.ino:C++"
#--map-syntax ".ignore:Git Ignore"
EOF
}
conf_print_bat_config | tee "${BAT_HOME}/config"

conf_print_bat_aliases() {
  cat <<'EOF'
if [ -x "$(command -v bat)" ]; then
  alias bat='bat'
  export MANPAGER="sh -c 'col -bx | bat -l man -p'"
elif [ -x "$(command -v batcat)" ]; then
  alias bat='batcat'
  export MANPAGER="sh -c 'col -bx | batcat -l man -p'"
fi
export MANROFFOPT="-c"   
EOF
}
conf_print_bat_aliases | tee "${DEST}/.zsh_aliases.d/004_batcat.zsh"

source "${DEST}/.zsh_aliases.d/004_batcat.sh"

batcat cache --build
batcat --list-themes | grep "Catppuccin Mocha"
batcat --list-languages | grep -i jinja
