#!/usr/bin/env bash

set -euo pipefail

sudo apt-get update -qqq
sudo apt install -y kitty kitty-terminfo kitty-shell-integration fonts-firacode fonts-noto-color-emoji -q
fc-cache -fv
# https://sw.kovidgoyal.net/kitty/overview/

# Paths
DEST="${1:-/etc/skel}"
KITTY_HOME="${DEST}/.config/kitty"
ALIASES_D="${DEST}/.zsh_aliases.d"
ZSHRC_D="${DEST}/.zshrc.d"
SESSIONS_D="${KITTY_HOME}/sessions"

mkdir -p "${KITTY_HOME}" "${ALIASES_D}" "${ZSHRC_D}" "${SESSIONS_D}"

ALIAS_FILE="${ALIASES_D}/003_kitty.zsh"

# Helper function to run functions with sudo
ssudo() {
  [[ "$(type -t "$1")" == "function" ]] || {
    echo "Not a function" >&2
    return 1
  }
  local func="$1"
  shift
  sudo bash -c "$(declare -f "$func"); $func $(printf '%q ' "$@")"
}
#
# OpenGL detection → decide if we force software rendering
# Necessary is opengl is <=3.2
should_use_software() {
  local es_version=""

  # local gl_version=""
  #  if command -v glxinfo >/dev/null 2>&1; then
  #    gl_version=$(glxinfo | grep -i "OpenGL version string" | head -n 1 | awk -F: '{print $2}')
  #    if [[ "$gl_version" =~ ^([4-9]\.|3\.[3-9]) ]]; then
  #      return 1 # hardware ok
  #    fi
  #  fi
  local max_core
  max_core=$(glxinfo -B 2>/dev/null | grep "Max core profile" | awk '{print $5}')
  printf "max_core:  %s\n" "${max_core}"
  if [[ "$max_core" =~ ^([4-9]\.|3\.[3-9]) ]]; then
    return 1 # Hardware supports OpenGL 3.2+
  fi

  if command -v eglinfo >/dev/null 2>&1; then
    es_version=$(eglinfo -B 2>/dev/null | grep -i "OpenGL ES profile version" | head -n 1 | grep -oE '[0-9]+\.[0-9]+' || true)
    printf "es_version  %s\n" "${es_version}"
    if [[ "$es_version" =~ ^[3-9]\. ]]; then
      return 1 # GLES 3+ usually good enough
    fi
  fi

  return 0 # force software
}

conf_print_kitty_desktop() {
  cat <<EOF
[Desktop Entry]
Version=1.0
Type=Application
Name=kitty
GenericName=Terminal emulator
Comment=Fast, feature-rich, GPU based terminal
TryExec=env LIBGL_ALWAYS_SOFTWARE=1 kitty
StartupNotify=true
Exec=kitty
Icon=kitty
Categories=System;TerminalEmulator;
X-TerminalArgExec=--
X-TerminalArgTitle=--title
X-TerminalArgAppId=--class
X-TerminalArgDir=--working-directory
X-TerminalArgHold=--hold

[Desktop Action new-window]
Name=New Window
Exec=env LIBGL_ALWAYS_SOFTWARE=1 kitty

[Desktop Action preferences]
Name=Preferences
Exec=env LIBGL_ALWAYS_SOFTWARE=1 kitty --single-instance @ edit_config_file
EOF
}

conf_print_kitty_alias_hw() {
  cat <<'EOF' | tee "${ALIAS_FILE}" >/dev/null
# kitty → hardware rendering (OpenGL ≥3.3 detected)
alias kitty="kitty"
EOF
}

conf_print_kitty_alias_sw() {
  cat <<'EOF' | tee "${ALIAS_FILE}" >/dev/null
# kitty → software rendering (old/weak OpenGL)
alias kitty="LIBGL_ALWAYS_SOFTWARE=1 kitty"
EOF
}

conf_print_kitty_wrapper() {
  mkdir -p /usr/local/bin/
  cat >/usr/local/bin/kitty-wrapper <<'EOF'
#!/bin/sh
export LIBGL_ALWAYS_SOFTWARE=1
exec /usr/bin/kitty "$@"
EOF
}
# ssudo conf_print_kitty_wrapper
# sudo chmod +x "/usr/local/bin/kitty-wrapper"

if should_use_software; then
  conf_print_kitty_alias_sw
  echo "# Forced software rendering due to insufficient OpenGL support" | tee -a "${ALIAS_FILE}" >/dev/null

  # For xfce4 that ignores user aliases
  conf_print_kitty_desktop | tee "${DEST}/.local/share/applications/kitty.desktop" >/dev/null
  update-desktop-database ~/.local/share/applications

  # Add to alternatives and select default
  # sudo update-alternatives --install /usr/bin/x-terminal-emulator x-terminal-emulator /usr/local/bin/kitty-wrapper 60
  sudo update-alternatives --install /usr/bin/x-terminal-emulator x-terminal-emulator /usr/bin/kitty 100
  sudo update-alternatives --config x-terminal-emulator

else
  conf_print_kitty_alias_hw

  echo "# Using hardware rendering" | tee -a "${ALIAS_FILE}" >/dev/null
  sudo update-alternatives --config x-terminal-emulator
fi

# the terminfo used by kitty is not generally available on
# remote machines so we use this alias copy over the terminfo
# and   ~/.local/share/kitty-ssh-kitten/
# 5.0K	./shell-integration/zsh/completions
# 23K	  ./shell-integration/zsh
# 9.0K	./shell-integration/bash
# 9.0K	./shell-integration/fish/vendor_conf.d
# 14K	  ./shell-integration/fish/vendor_completions.d
# 24K	  ./shell-integration/fish
# 56K	  ./shell-integration
# 9.5K	./kitty/bin
# 8.3M	./kitty/install-tool
# 8.3M	./kitty
# 8.3M	.

# Start the kitty alias (multistage)
cat <<-'EOF' | tee -a "${ALIAS_FILE}" >/dev/null

	if test "${XTERM}" = "xterm-kitty"; then
EOF

cat <<-EOF | tee -a "${ALIAS_FILE}" >/dev/null
	  alias ssh="kitty +kitten ssh"
EOF

# more kitty aliases
cat <<-EOF | tee -a "${ALIAS_FILE}" >/dev/null
	  alias kt="kitty +kitten transfer"
EOF

# End the kitty aliases
cat <<-EOF | tee -a "${ALIAS_FILE}" >/dev/null
	fi
EOF

# Configuration for 'kitty +kitten ssh' aliased above to ssh
# for kitty terminal sessions
# Environment variables to set and files to copy on connection.
# https://sw.kovidgoyal.net/kitty/kittens/ssh/
mkdir -p "${KITTY_HOME}"
cat <<-EOF | tee -a "${KITTY_HOME}/ssh.conf" >/dev/null
	# Copy the files and directories needed to setup some common tools
	# copy .zshrc .vimrc .vim
	  
	# Setup some environment variables
	# env SOME_VAR=x
	  
	# COPIED_VAR will have the same value on the remote host as it does locally
	# env COPIED_VAR=_kitty_copy_env_var_

	# Create some per hostname settings
	# hostname someserver-*
	# copy env-files
	# env SOMETHING=else

	# hostname someuser@somehost
	# copy --dest=foo/bar some-file
	# copy --glob some/files.*
EOF

# set up tmuxp like sessions
mkdir -p "${KITTY_HOME}/sessions"
cat <<-EOF | tee "${DEST}/.config/kitty/sessions/startup.conf" >/dev/null
	# startup.conf
	new_tab devuan-sdk
	cd /space/code_repositories/decode-os/os-build-system
	title vim
	launch zsh -c 'nvim ../README.md'
	launch zsh -c 'nvim console.sh'
	launch zsh
	enabled_layouts tall:bias=50;full_size=1
	layout tall

	new_tab other
	cd ~
	launch zsh
	enabled_layouts tall:bias=50;full_size=1
	layout tall
EOF

# Manual shell integration to work well with sub-shells, terminal multiplexers, containers
# requires kitty.conf 'shell-integration disabled' be set below.
# https://sw.kovidgoyal.net/kitty/shell-integration/#manual-shell-integration

KITTY_INSTALLATION_DIR=${KITTY_INSTALLATION_DIR:-/usr/lib/kitty}
conf_print_kitty_shell() {
  cat <<-EOF
  # Manual shell integration to work with sub-shells, tmux and containers.  
	if test -n "$KITTY_INSTALLATION_DIR"; then
	    export KITTY_SHELL_INTEGRATION="enabled"
	    autoload -Uz -- $KITTY_INSTALLATION_DIR/shell-integration/zsh/kitty-integration
	    kitty-integration
	    unfunction kitty-integration
	fi
EOF
}
conf_print_kitty_shell | tee "${DEST}/.zshrc.d/002_kitty-shell.zsh" >/dev/null

# kitty specific zsh function eq load a session file on startup.
conf_print_kitty_functions() {
  cat <<-EOF
	#  # An emple function that loads a session file on startup.
	#  function kt_devuan-sdk() {
	#    export PROJECT_DIR=
	#    nohup kitty --session ~/.config/kitty/sessions/startup.conf & disown
	#    exit
	#  }
EOF
}
conf_print_kitty_functions | tee "${DEST}/.zshrc.d/002_kitty-functions.zsh" >/dev/null

## KITTYCONF START
mkdir -p "${KITTY_HOME}"
conf_print_kitty_base() {
  cat <<-EOF
	# vim:ft=kitty

	# Performance - Keyboard to screen latency https://sw.kovidgoyal.net/kitty/performance/
	input_delay                     0
	repaint_delay                   2
	sync_to_monitor                 no
	wayland_enable_ime              no

	# https://github.com/tonsky/FiraCode#fira-code-free-monospaced-font-with-programming-ligatures
	# Font settings (kitty +list-fonts --psnames | grep FiraCode | grep Retina)
	font_family                     FiraCodeNerdFontComplete-Retina
	font_features                   FiraCodeNerdFontComplete-Retina +cv04 +cv10 +ss04 +ss03 +cv25 +cv32 +cv28 +ss06 +ss07
	bold_font                       FiraCodeNerdFontComplete-Bold
	italic_font                     auto
	bold_italic_font                auto
	font_size                       11.5
	disable_ligatures               never
	# adjust_line_height              1

	# Mouse
	copy_on_select                  yes
	strip_trailing_spaces           always

	# Cursor
	cursor_shape                    block
	cursor_blink_interval           0

	# Window layout
	window_border_width             1px
	hide_window_decorations         no

	# Bell
	enable_audio_bell               no
	window_alert_on_bell            yes
	bell_on_tab                     no

	# Tab Bar
	tab_bar_edge                    top
	tab_bar_style                   powerline
	# tab_powerline_style             angled
	# tab_bar_min_tabs                1
	# tab_bar_align                   left

	# Advanced
EOF
}
conf_print_kitty_base | tee "${KITTY_HOME}/kitty.conf" >/dev/null

# Choose this if not using zsh or you don't want manual shell integration
# # For manual shell integration above comment out the following
conf_print_manual_shell_int() {
  cat <<-EOF
	shell_integration               enabled
EOF
}
# conf_print_manual_shell_int | tee -a ${KITTY_HOME}/kitty.conf

# For manual shell integration above uncomment the following
conf_print_no-manual_shell_int() {
  cat <<-EOF
	shell_integration               disabled
EOF
}
conf_print_no-manual_shell_int | tee -a "${KITTY_HOME}/kitty.conf" >/dev/null

# A bunch of people want auto-update in every 'app'
# and the others are unix users. Disable the update check completely.
conf_print_update_interval() {
  cat <<-EOF
	update_check_interval           0
EOF
}
conf_print_update_interval | tee -a "${KITTY_HOME}/kitty.conf" >/dev/null

# default browser and other settings
conf_print_defaults() {
  cat <<-EOF

	# Default browser
	open_url_with                   /usr/bin/brave-browser

	# linux_display_server            x11

	# Background transparency
	# https://lyz-code.github.io/blue-book/kitty/#make-the-background-transparent
	background_opacity 0.85
EOF
}
conf_print_defaults | tee -a "${KITTY_HOME}/kitty.conf" >/dev/null

conf_print_shortcuts() {
  cat <<-EOF
	####### Keyboard Shortcuts #######
	## windows
	map kitty_mod+enter new_window_with_cwd
	map kitty_mod+n new_os_window_with_cwd
	map kitty_mod+t new_tab_with_cwd
EOF
}
conf_print_shortcuts | tee -a "${KITTY_HOME}/kitty.conf" >/dev/null

# https://lyz-code.github.io/blue-book/kitty/#Movement
config_print_movement() {
  cat <<-EOF
	## Movement

	map kitty_mod+k scroll_line_up
	map kitty_mod+j scroll_line_down
	map kitty_mod+u scroll_page_up
	map kitty_mod+d scroll_page_down
EOF
}
config_print_movement | tee -a "${KITTY_HOME}/kitty.conf" >/dev/null

# scrollback buffer ctrl+shift+h
# https://lyz-code.github.io/blue-book/kitty/#the-scrollback-buffer
# https://github.com/kovidgoyal/kitty/issues/719
conf_print_scrollback() {
  cat <<-'EOF'
	# Scrollback buffer
	# https://sw.kovidgoyal.net/kitty/overview/#the-scrollback-buffer
	# $(bash -c '...') Run everything in a shell taking the scrollback content on stdin
	# $(-u NORC) Load plugins but not initialization files
	# $(-c "map q :qa!<CR>") Close with $(q) key
	# $(-c "autocmd TermOpen * normal G") On opening of the embedded terminal go to last line
	# $(-c "terminal cat /proc/$$/fd/0 -") Open the embedded terminal and read stdin of the shell
	# $(-c "set clipboard+=unnamedplus") Always use clipboard to yank/put instead of having to specify +
	scrollback_pager bash -c 'nvim </dev/null -u NORC -c "map q :qa!<CR>" -c "autocmd TermOpen * normal G" -c "terminal cat /proc/$$/fd/0 -" -c "set clipboard+=unnamedplus" -c "call cursor(CURSOR_LINE, CURSOR_COLUMN)"'

	# Infinite scrollback
	scrollback_lines -1
	scrollback_pager_history_size 0
EOF
}
# conf_print_scrollback  | tee -a ${KITTY_HOME}/kitty.conf >/dev/null

# Scrollback (2026 style with kitty-scrollback.nvim plugin)
# See /var/tmp/automate/020_neovim-lazyvim_plugins.sh
conf_print_scrollback() {
  cat <<-'EOF'

# Required for kitty-scrollback.nvim remote control
# allow_remote_control      yes
allow_remote_control      socket-only
listen_on                 unix:/tmp/kitty-remote

EOF
}
conf_print_scrollback | tee -a "${KITTY_HOME}/kitty.conf" >/dev/null

# Scrollback (2026 style with kitty-scrollback.nvim)
conf_print_scrollback_lines() {
  cat <<-'EOF'

scrollback_lines          20000
scrollback_pager_history_size 4

EOF
}
conf_print_scrollback_lines | tee -a "${KITTY_HOME}/kitty.conf" >/dev/null

conf_print_scrollback_maps() {
  cat <<'EOF'
 
## nvim --headless +'KittyScrollbackGenerateKittens'
# kitty-scrollback.nvim Kitten alias
action_alias kitty_scrollback_nvim kitten /home/default/.local/share/nvim/lazy/kitty-scrollback.nvim/python/kitty_scrollback_nvim.py

# Browse scrollback buffer in nvim
map kitty_mod+h kitty_scrollback_nvim

# Browse output of the last shell command in nvim
map kitty_mod+g kitty_scrollback_nvim --config ksb_builtin_last_cmd_output

## / 

map kitty_mod+question   kitty_scrollback_nvim --config search

# Mouse: right-click under cursor → last command output
mouse_map kitty_mod+right press ungrabbed combine : mouse_select_command_output : kitty_scrollback_nvim --config ksb_builtin_last_visited_cmd_output
EOF
}
conf_print_scrollback_maps | tee -a "${KITTY_HOME}/kitty.conf" >/dev/null

# Some other mapping examples
# conf_print_examples() {
# cat <<-EOF
#
# ## Misc
# # map alt+ctrl+shift+g launch --cwd=current gitui
#
# ## Tab management
# map alt+c new_tab
# map kitty_mod+enter new_tab
#
# map alt+1 goto_tab 1
# map alt+2 goto_tab 2
# map alt+3 goto_tab 3
# map alt+4 goto_tab 4
# map alt+5 goto_tab 5
# map alt+6 goto_tab 6
# map alt+7 goto_tab 7
# map alt+8 goto_tab 8
# map alt+9 goto_tab 9
#
# ## Window management
# map alt+enter new_window_with_cwd
#
# map alt+v toggle_layout tall
# map alt+s toggle_layout grid
#
# map alt+h neighboring_window left
# map alt+j neighboring_window bottom
# map alt+k neighboring_window top
# map alt+l neighboring_window right
#
# map alt+shift+h move_window left
# map alt+shift+j move_window bottom
# map alt+shift+k move_window top
# map alt+shift+l move_window right
#
# # map alt+shift+1 move_window left
# # map alt+shift+2 move_window bottom
# # map alt+shift+3 move_window top
# # map alt+shift+4 move_window right
# # map alt+shift+5 move_window right
# # map alt+shift+6 move_window right
# # map alt+shift+7 move_window right
# # map alt+shift+8 move_window right
# # map alt+shift+9 move_window right
# map kitty_mod+f4 detach_window ask
# EOF
# }
# conf_print_examples | tee -a ${KITTY_HOME}/kitty.conf

# Use kitty +kitten theme location
conf_print_theme() {
  cat <<-EOF
  
  # BEGIN_KITTY_THEME
	# Terminology Default
	include current-theme.conf
	# END_KITTY_THEME
EOF
}
conf_print_theme | tee -a "${KITTY_HOME}/kitty.conf" >/dev/null
## KITTYCONF END

## Themes
# The theme template for submissions.
# if the theme doesn't exist, it will be downloaded
# and put in the kitty themes directory
if [ ! -f "${KITTY_HOME}/theme-template.conf" ]; then
  echo "Downloading theme template"
  wget -c \
    -O "${KITTY_HOME}/theme-template.conf" \
    https://raw.githubusercontent.com/kovidgoyal/kitty-themes/master/template.conf
fi

conf_print_nord_theme() {
  cat <<-EOF
	# vim:ft=kitty
	  
	## name: Nord
	## author: Eric N. Vander Weele
	## upstream: https://www.nordtheme.com/
	## blurb: Nord theme utilizing the full Frost and Aurora palette.

	# Basic colors
	foreground           #d8dee9
	background           #2e3440
	selection_foreground #d8dee9
	selection_background #434c5e

	# Cursor colors
	cursor            #d8dee9
	cursor_text_color #3b4252

	# URL underline color when hovering with mouse
	url_color #0087bd

	# Window border colors and terminal bell colors
	active_border_color   #81a1c1
	inactive_border_color #4c566a
	bell_border_color     #88c0d0
	visual_bell_color     none

	# Tab bar colors
	active_tab_foreground   #3b4252
	active_tab_background   #88c0d0
	inactive_tab_foreground #e5e9f0
	inactive_tab_background #4c566a
	tab_bar_background      #3b4252
	tab_bar_margin_color    none

	# Mark colors (marked text in the terminal)
	mark1_foreground #3b4252
	mark1_background #88c0d0
	mark2_foreground #3b4252
	mark2_background #bf616a
	mark3_foreground #3b4252
	mark3_background #ebcb8b

	# The basic 16 colors
	# black
	color0 #3b4252
	color8 #4c566a

	# red
	color1 #bf616a
	color9 #bf616a

	# green
	color2  #a3be8c
	color10 #a3be8c

	# yellow
	color3  #ebcb8b
	color11 #d08770

	# blue
	color4  #81a1c1
	color12 #5e81ac

	# magenta
	color5  #b48ead
	color13 #b48ead

	# cyan
	color6  #88c0d0
	color14 #8fbcbb

	# white
	color7  #e5e9f0
	color15 #eceff4
EOF
}
conf_print_nord_theme | tee "${KITTY_HOME}/Nord.conf" >/dev/null

conf_print_nord_theme_custom() {
  cat <<-EOF
	# vim:ft=kitty

	  # Nord Colorscheme for Kitty
	  # Based on:
	  # - https://gist.github.com/marcusramberg/64010234c95a93d953e8c79fdaf94192
	  # - https://github.com/arcticicestudio/nord-hyper

	  foreground              #e0def4
	  background              #12121e
	  selection_foreground    #12121e
	  selection_background    #FFFACD
	  url_color               #9ccfd8
	  cursor                  #cccccc

	  # black
	  color0   #3B4252
	  color8   #4C566A

	  # red
	  color1   #FF789E
	  color9   #FF789E

	  # green
	  color2   #A3BE8C
	  color10  #A3BE8C

	  # yellow
	  color3   #f6c177
	  color11  #f6c177

	  # blue
	  color4  #81A1C1
	  color12 #81A1C1

	  # magenta
	  color5   #c4a7e7
	  color13  #c4a7e7

	  # cyan
	  color6   #9ccfd8
	  color14  #8FBCBB

	  # white
	  color7   #E5E9F0
	  color15  #ECEFF4

	  # Windows
	  active_border_color     #f6c177
	  inactive_border_color   #12121e

	  # Tabs
	  active_tab_foreground   #12121e
	  active_tab_background   #9ccfd8
	  active_tab_font_style   bold
	  inactive_tab_foreground #e0def4
	  inactive_tab_background #12121e
	  inactive_tab_font_style normal

	  # ####### Keyboard Shortcuts #######
	  #
	  # ## Misc
	  # # map alt+ctrl+shift+g launch --cwd=current gitui
	  #
	  # ## Tab management
	  # map alt+c new_tab
	  # map kitty_mod+enter new_tab
	  #
	  # map alt+1 goto_tab 1
	  # map alt+2 goto_tab 2
	  # map alt+3 goto_tab 3
	  # map alt+4 goto_tab 4
	  # map alt+5 goto_tab 5
	  # map alt+6 goto_tab 6
	  # map alt+7 goto_tab 7
	  # map alt+8 goto_tab 8
	  # map alt+9 goto_tab 9
	  #
	  # ## Window management
	  # map alt+enter new_window_with_cwd
	  #
	  # map alt+v toggle_layout tall
	  # map alt+s toggle_layout grid
	  #
	  # map alt+h neighboring_window left
	  # map alt+j neighboring_window bottom
	  # map alt+k neighboring_window top
	  # map alt+l neighboring_window right
	  #
	  # map alt+shift+h move_window left
	  # map alt+shift+j move_window bottom
	  # map alt+shift+k move_window top
	  # map alt+shift+l move_window right
	  #
	  # # map alt+shift+1 move_window left
	  # # map alt+shift+2 move_window bottom
	  # # map alt+shift+3 move_window top
	  # # map alt+shift+4 move_window right
	  # # map alt+shift+5 move_window right
	  # # map alt+shift+6 move_window right
	  # # map alt+shift+7 move_window right
	  # # map alt+shift+8 move_window right
	  # # map alt+shift+9 move_window right
	  # map kitty_mod+f4 detach_window ask
EOF
}
conf_print_nord_theme_custom | tee "${KITTY_HOME}/Nord-custom.conf" >/dev/null

conf_print_monokai_theme() {
  cat <<-EOF
	# vim:ft=kitty

	background #1e1e1e
	foreground #b8bcb9
	cursor #eee
	selection_background #efcc00
	color0 #3a3c43
	color8 #888987
	color1 #be3e48
	color9 #fb001e
	color2 #869a3a
	color10 #0e712e
	color3 #c4a535
	color11 #c37033
	color4 #4e76a1
	color12 #176ce3
	color5 #855b8d
	color13 #fb0067
	color6 #568ea3
	color14 #2d6f6c
	color7 #b8bcb9
	color15 #fcffb8
	selection_foreground #1e1e1e
EOF
}
conf_print_monokai_theme | tee "${KITTY_HOME}/Dimmed Monokai.conf" >/dev/null

conf_print_adwaita_theme() {
  cat <<-EOF
	# vim:ft=kitty

	## name: Adwaita dark
	## license: MIT
	## author: Emil Löfquist (https://github.com/ewal)
	## upstream: https://github.com/ewal/kitty-adwaita/blob/main/adwaita_dark.conf
	## blurb: Adwaita dark - based on https://github.com/Mofiqul/adwaita.nvim

	background                #1d1d1d
	foreground                #deddda

	selection_background      #303030
	selection_foreground      #c0bfbc

	url_color                 #1a5fb4

	wayland_titlebar_color    system
	macos_titlebar_color      system

	cursor                    #deddda
	cursor_text_color         #1d1d1d

	active_border_color       #4f4f4f
	inactive_border_color     #282828
	bell_border_color         #ed333b
	visual_bell_color         none

	active_tab_background     #242424
	active_tab_foreground     #fcfcfc
	inactive_tab_background   #303030
	inactive_tab_foreground   #b0afac
	tab_bar_background        none
	tab_bar_margin_color      none

	color0                    #1d1d1d
	color1                    #ed333b
	color2                    #57e389
	color3                    #ff7800
	color4                    #62a0ea
	color5                    #9141ac
	color6                    #5bc8af
	color7                    #deddda

	color8                    #9a9996
	color9                    #f66151
	color10                   #8ff0a4
	color11                   #ffa348
	color12                   #99c1f1
	color13                   #dc8add
	color14                   #93ddc2
	color15                   #f6f5f4
EOF
}
conf_print_adwaita_theme | tee "${KITTY_HOME}/Adwaita dark.conf" >/dev/null

conf_print_macchiato_theme() {
  cat <<-EOF
	# vim:ft=kitty

	## name:     Catppuccin-Macchiato
	## author:   Pocco81 (https://github.com/Pocco81)
	## license:  MIT
	## upstream: https://github.com/catppuccin/kitty/blob/main/macchiato.conf
	## blurb:    Soothing pastel theme for the high-spirited!



	# The basic colors
	foreground              #CAD3F5
	background              #24273A
	selection_foreground    #24273A
	selection_background    #F4DBD6

	# Cursor colors
	cursor                  #F4DBD6
	cursor_text_color       #24273A

	# URL underline color when hovering with mouse
	url_color               #F4DBD6

	# Kitty window border colors
	active_border_color     #B7BDF8
	inactive_border_color   #6E738D
	bell_border_color       #EED49F

	# OS Window titlebar colors
	wayland_titlebar_color system
	macos_titlebar_color system

	# Tab bar colors
	active_tab_foreground   #181926
	active_tab_background   #C6A0F6
	inactive_tab_foreground #CAD3F5
	inactive_tab_background #1E2030
	tab_bar_background      #181926

	# Colors for marks (marked text in the terminal)
	mark1_foreground #24273A
	mark1_background #B7BDF8
	mark2_foreground #24273A
	mark2_background #C6A0F6
	mark3_foreground #24273A
	mark3_background #7DC4E4

	# The 16 terminal colors

	# black
	color0 #494D64
	color8 #5B6078

	# red
	color1 #ED8796
	color9 #ED8796

	# green
	color2  #A6DA95
	color10 #A6DA95

	# yellow
	color3  #EED49F
	color11 #EED49F

	# blue
	color4  #8AADF4
	color12 #8AADF4

	# magenta
	color5  #F5BDE6
	color13 #F5BDE6

	# cyan
	color6  #8BD5CA
	color14 #8BD5CA

	# white
	color7  #B8C0E0
	color15 #A5ADCB
EOF
}
conf_print_macchiato_theme | tee "${KITTY_HOME}/Catppuccin-Macchiato.config" >/dev/null

conf_print_tokyo_night_theme() {
  cat <<-EOF
	# vim:ft=kitty

	## name: Tokyo Night Moon
	## license: MIT
	## author: Folke Lemaitre
	## upstream: https://github.com/folke/tokyonight.nvim/raw/main/extras/kitty/tokyonight_moon.conf


	background #222436
	foreground #c8d3f5
	selection_background #2d3f76
	selection_foreground #c8d3f5
	url_color #4fd6be
	cursor #c8d3f5
	cursor_text_color #222436

	# Tabs
	active_tab_background #82aaff
	active_tab_foreground #1e2030
	inactive_tab_background #2f334d
	inactive_tab_foreground #545c7e
	#tab_bar_background #1b1d2b

	# Windows
	active_border_color #82aaff
	inactive_border_color #2f334d

	# normal
	color0 #1b1d2b
	color1 #ff757f
	color2 #c3e88d
	color3 #ffc777
	color4 #82aaff
	color5 #c099ff
	color6 #86e1fc
	color7 #828bb8

	# bright
	color8 #444a73
	color9 #ff757f
	color10 #c3e88d
	color11 #ffc777
	color12 #82aaff
	color13 #c099ff
	color14 #86e1fc
	color15 #c8d3f5

	# extended colors
	color16 #ff966c
	color17 #c53b53
EOF
}
conf_print_tokyo_night_theme | tee "${KITTY_HOME}/Tokyo Night Moon.conf" >/dev/null

cat <<-EOF

	Get more themes or switch themes with:

	$ kitty +kitten themes

	which adds:

	    # BEGIN_KITTY_THEME
	    # Nord
	    include current-theme.conf
	    # END_KITTY_THEME%

	    to the end of the ${KITTY_HOME}/kitty.conf file 
	      and copies the chosen theme to current-theme.config
	      so don't use 'ln -sf' to change the theme.
EOF
