#!/bin/bash

# Enable strict error checking
set -euo pipefail
IFS=$'\n\t'

# Set defaults
DEST=${1:-/etc/skel}
GITUSER=wez
PROJECT=wezterm
LATEST_RELEASE_URL="https://api.github.com/repos/${GITUSER}/${PROJECT}/releases/latest"
ARCH=$(dpkg --print-architecture)


conf_print_apt_sources() {
  cat <<-'EOF'
  Types: deb
  URIs: https://apt.fury.io/wez/
  Suites: *
  Components: *
  Signed-By: /usr/share/keyrings/wezterm-fury.gpg
  EOF
}
conf_print_apt_sources | sudo tee /etc/apt/sources.list-available/wezterm.sources
sudo ln -sf /etc/apt/sources.list-available/wezterm.sources /etc/apt/sources.list.d/wezterm.sources

curl -fsSL https://apt.fury.io/wez/gpg.key | sudo gpg --yes --dearmor -o /usr/share/keyrings/wezterm-fury.gpg
sudo chmod 644 /usr/share/keyrings/wezterm-fury.gpg

sudo apt update
sudo apt install wezterm

## Function to handle errors
#handle_error() {
#    local msg="$1"
#    echo "Error: $msg" >&2
#    exit 1
#}
#
## Fetch the latest release tag
#TAG=$(torsocks curl -L -H "Accept: application/vnd.github+json" \
#     -H "X-GitHub-Api-Version: 2022-11-28" "$LATEST_RELEASE_URL" | jq -r '.tag_name') || \
#    handle_error "Failed to fetch release information"
#
## Determine architecture-specific extension
#case $ARCH in
#    amd64)
#        EXTENSION="Debian12.deb"
#        ;;
#    arm64)
#        EXTENSION="Debian12.arm64.deb"
#        ;;
#    armhf)
#        EXTENSION="Debian12.armhf.deb"
#        ;;
#    *)
#        handle_error "Unable to determine system architecture: $ARCH"
#        ;;
#esac
#
## Download files with progress
#DEB_FILE="/tmp/${PROJECT}-${TAG}.${EXTENSION}"
#
## Download the .deb file first
#wget -c -q --show-progress \
#     "https://github.com/${GITUSER}/${PROJECT}/releases/download/${TAG}/${PROJECT}-${TAG}.${EXTENSION}" \
#     -O "$DEB_FILE" || handle_error "Failed to download .deb package"
#
## Only download and verify checksum for amd64
##if [ "$ARCH" = "amd64" ]; then
#    SHA256_FILE="${DEB_FILE}.sha256"
#    
#    # Download checksum file
#    wget -c -q --show-progress \
#         "https://github.com/${GITUSER}/${PROJECT}/releases/download/${TAG}/${PROJECT}-${TAG}.${EXTENSION}.sha256" \
#         -O "$SHA256_FILE" || handle_error "Failed to download checksum file"
##    
#    # Verify checksum
#    if ! sha256sum --check "$SHA256_FILE"; then
#        handle_error "Checksum verification failed"
#    fi
#fi
#
## Install package
##sudo dpkg -i "$DEB_FILE" || handle_error "Failed to install package"
#sudo apt-get -f install || handle_error "Failed to fix dependencies"

# Create configuration directory
mkdir -p "${DEST}/.config/wezterm/colors"

# Set wallpaper path
WALLPAPER='${DEST}/Pictures/wallpapers/1920x1200-Wallpaper-041.jpg'

# Rest of the configuration remains the same...
conf_print_wezterm_theme_minimal() {
	cat <<-EOF
		local wezterm = require("wezterm")
		return {
			-- color_scheme = 'termnial.sexy',
			-- color_scheme = "Catppuccin Mocha",
			color_scheme = "mellow",
			enable_tab_bar = false,
			font_size = 11.0,
			-- macos_window_background_blur = 40,
			macos_window_background_blur = 30,

			-- window_background_image = ${WALLPAPER}',
			-- window_background_image_hsb = {
			--  brightness = 0.01,
			--  hue = 1.0,
			--  saturation = 0.5,
			-- },
			-- window_background_opacity = 0.92,
			window_background_opacity = 1.0,
			-- window_background_opacity = 0.78,
			-- window_background_opacity = 0.20,
			window_decorations = "RESIZE",
			keys = {
				{
					key = "f",
					mods = "CTRL",
					action = wezterm.action.ToggleFullScreen,
				},
			},
			mouse_bindings = {
				-- Ctrl-click will open the link under the mouse cursor
				{
					event = { Up = { streak = 1, button = "Left" } },
					mods = "CTRL",
					action = wezterm.action.OpenLinkAtMouseCursor,
				},
			},
		}
	EOF
}
conf_print_wezterm_theme_minimal | tee ${DEST}/.config/wezterm/wezterm.lua

# wget -c -O ${DEST}/.config/wezterm/colors/mellow.toml https://github.com/mellow-theme/mellow.nvim/blob/main/extras/wezterm/colors/mellow.toml
conf_print_wezterm_theme_mellow() {
  cat <<-EOF
    # Mellow colorscheme for wezterm
    [colors]
    foreground = "#c9c7cd" 
    background = "#161617"
    cursor_bg = "#e3e2e5" 
    cursor_border = "#e3e2e5" 
    cursor_fg = "#161617" 
    selection_bg = "#3c3b3e" 
    selection_fg = "#e3e2e5"  
    scrollbar_thumb = "#57575f"
    split = "#57575f"
    ansi = ["#27272a","#f5a191","#90b99f","#e6b99d","#aca1cf","#e29eca","#ea83a5","#c1c0d4"]
    brights = ["#353539","#ffae9f","#9dc6ac","#f0c5a9","#b9aeda","#ecaad6","#f591b2","#cac9dd"]
  EOF
}
conf_print_wezterm_theme_mellow | tee ${DEST}/.config/wezterm/colors/mellow.toml

# wget -c -O ${DEST}/.config/wezterm/colors/hardhacker.toml https://github.com/hardhackerlabs/theme-wezterm/blob/master/hardhacker.toml
conf_print_wezterm_theme_hardhacker() {
  cat <<-EOF
[colors]
ansi = [ "#282433", "#e965a5", "#b1f2a7", "#ebde76", "#b1baf4", "#e192ef", "#b3f4f3", "#eee9fc" ]
brights = [ "#3f3951", "#e965a5", "#b1f2a7", "#ebde76", "#b1baf4", "#e192ef", "#b3f4f3", "#eee9fc" ]

background = "#282433"
foreground = "#eee9fc"

compose_cursor = '#ebde76'
cursor_bg = "#eee9fc"
cursor_border = "#eee9fc"
cursor_fg = "#eee9fc"

selection_bg = "#e965a5"
selection_fg = "#eee9fc"

scrollbar_thumb = '#3f3951'
split = '#938aad'

[colors.indexed]

[colors.tab_bar]
background = "#282433"

[colors.tab_bar.active_tab]
bg_color = "#282433"
fg_color = "#e965a5"
intensity = "Normal"
underline = "None"
italic = false
strikethrough = false

[colors.tab_bar.inactive_tab]
bg_color = "#282433"
fg_color = "#938aad"
intensity = "Normal"
italic = false
strikethrough = false
underline = "None"

[colors.tab_bar.inactive_tab_hover]
bg_color = "#e192ef"
fg_color = "#eee9fc"
intensity = "Normal"
italic = false
strikethrough = false
underline = "None"

[colors.tab_bar.new_tab]
bg_color = "#282433"
fg_color = "#938aad"
intensity = "Normal"
italic = false
strikethrough = false
underline = "None"

[colors.tab_bar.new_tab_hover]
bg_color = "#e192ef"
fg_color = "#eee9fc"
intensity = "Normal"
italic = true
strikethrough = false
underline = "None"

[metadata]
name = 'hardhacker'
origin_url = 'https://github.com/hardhackerlabs/theme-wezterm
EOF}
conf_print_wezterm_theme_hardhacker | tee ${DEST}/.config/wezterm/colors/hardhacker.toml
# An extensive wezterm config, maybe too extensive, using tmux as default.
# and a complete set of aditional keymaps to learn.
# https://github.com/KevinSilvester/wezterm-config
