#!/usr/bin/env bash
# https://github.com/wez/wezterm?tab=readme-ov-file#wezs-terminal
# https://wezfurlong.org/wezterm/
#
# wezterm - truecolor, ligatures, accelerated
#
#
# tree -L 6
#.
#├── etc
#│   └── profile.d
#│       └── wezterm.sh
#└── usr
#    ├── bin
#    │   ├── open-wezterm-here
#    │   ├── strip-ansi-escapes
#    │   ├── wezterm
#    │   ├── wezterm-gui
#    │   └── wezterm-mux-server
#    └── share
#        ├── applications
#        │   └── org.wezfurlong.wezterm.desktop
#        ├── bash-completion
#        │   └── completions
#        │       └── wezterm
#        ├── icons
#        │   └── hicolor
#        │       └── 128x128
#        │           └── apps
#        ├── metainfo
#        │   └── org.wezfurlong.wezterm.appdata.xml
#        ├── nautilus-python
#        │   └── extensions
#        │       └── wezterm-nautilus.py
#        ├── wezterm
#        └── zsh
#            └── functions
#                └── Completion
#                    └── Unix

# Upstream releases deb files
#
DEST=${1:-/etc/skel}

GITUSER=wez
PROJECT=wezterm
LATEST_RELEASE_URL="https://api.github.com/repos/${GITUSER}/${PROJECT}/releases/latest"
ARCH=$(dpkg --print-architecture)

# 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')

# Download the.deb file and its SHA256 checksum
# test for ARCH to select the correct file to download
case $ARCH in
amd64)
  EXTENSION="Debian12.deb"
  ;;
arm64)
  EXTENSION="Debian12.arm64.deb"
  ;;
armhf)
  EXTENSION="Debian12.armhf.deb"
  ;;
*)
  echo "Unable to determine system architecture."
  exit 1
  ;;
esac

wget -c -O /tmp/${PROJECT}-${TAG}.${EXTENSION} https://github.com/wez/${PROJECT}/releases/download/${TAG}/${PROJECT}-${TAG}.${EXTENSION}
wget -c -O /tmp/${PROJECT}-${TAG}.${EXTENSION}.sha256 https://github.com/wez/${PROJECT}/releases/download/${TAG}/${PROJECT}-${TAG}.${EXTENSION}.sha256

# Verify the checksum using sha256sum --check
sha256sum --check /tmp/${PROJECT}-${TAG}.${EXTENSION}.sha256

# If the checksum verification is successful, install the.deb file using dpkg
if [ $? -eq 0 ]; then
  sudo dpkg -i /tmp/${PROJECT}-${TAG}.${EXTENSION}
  sudo apt-get -f install
else
  echo "Checksums do not match. Skipping installation."
fi

# conf_print_${PROJECT}() {
# cat <<'EOF'
# name: debian12
#
# on:
#   pull_request:
#     branches:
#       - main
#     paths:
#       - "**/*.rs"
#       - "**/Cargo.lock"
#       - "**/Cargo.toml"
#       - ".github/workflows/gen_debian12.yml"
#       - "assets/fonts/**/*"
#       - "assets/icon/*"
#       - "assets/open-wezterm-here"
#       - "assets/shell-completion/**/*"
#       - "assets/shell-integration/**/*"
#       - "assets/wezterm-nautilus.py"
#       - "assets/wezterm.appdata.xml"
#       - "assets/wezterm.desktop"
#       - "ci/deploy.sh"
#       - "ci/tag-name.sh"
#       - "get-deps"
#       - "termwiz/data/wezterm.terminfo"
#
# jobs:
#   build:
#     runs-on: "ubuntu-latest"
#     container: "debian:12"
#     env:
#       CARGO_INCREMENTAL: "0"
#       SCCACHE_GHA_ENABLED: "true"
#       RUSTC_WRAPPER: "sccache"
#
#     steps:
#       - name: "set APT to non-interactive"
#         shell: bash
#         run: "echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections"
#       - name: "Update APT"
#         shell: bash
#         run: "apt update"
#       - name: "Install git"
#         shell: bash
#         run: "apt-get install -y git"
#       - name: "Install curl"
#         shell: bash
#         run: "apt-get install -y curl"
#       - name: "Update APT"
#         shell: bash
#         run: "apt update"
#       - name: "Ensure /run/sshd exists"
#         shell: bash
#         run: "mkdir -p /run/sshd"
#       - name: "Install openssh-server"
#         shell: bash
#         run: "apt-get install -y openssh-server"
#       - name: "Workaround git permissions issue"
#         shell: bash
#         run: "git config --global --add safe.directory /__w/wezterm/wezterm"
#       - name: "checkout repo"
#         uses: actions/checkout@v4
#         with:
#           submodules: "recursive"
#       - name: "Install Rust"
#         uses: dtolnay/rust-toolchain@stable
#       - name: "Compile with sccache"
#         uses: mozilla-actions/sccache-action@v0.0.4
#       - name: "Cache Rust Dependencies"
#         uses: actions/cache@v4
#         id: cache-cargo-vendor
#         with:
#           path: |
#             vendor
#             .cargo/config
#           key: "cargo-deps-${{ hashFiles('**/Cargo.lock') }}"
#       - name: "Vendor dependecies"
#         if: steps.cache-cargo-vendor.outputs.cache-hit != 'true'
#         shell: bash
#         run: "cargo vendor --locked --versioned-dirs >> .cargo/config"
#       - name: "Install System Deps"
#         shell: bash
#         run: "env CI=yes PATH=$PATH ./get-deps"
#       - name: "Build wezterm (Release mode)"
#         shell: bash
#         run: "cargo build -p wezterm --release"
#       - name: "Build wezterm-gui (Release mode)"
#         shell: bash
#         run: "cargo build -p wezterm-gui --release"
#       - name: "Build wezterm-mux-server (Release mode)"
#         shell: bash
#         run: "cargo build -p wezterm-mux-server --release"
#       - name: "Build strip-ansi-escapes (Release mode)"
#         shell: bash
#         run: "cargo build -p strip-ansi-escapes --release"
#       - name: "Install cargo-nextest from Cargo"
#         uses: baptiste0928/cargo-install@v3
#         with:
#           crate: "cargo-nextest"
#           cache-key: "debian12"
#       - name: "Test"
#         shell: bash
#         run: "cargo nextest run --all --no-fail-fast"
#       - name: "Package"
#         shell: bash
#         run: "bash ci/deploy.sh"
#       - name: "Upload artifact"
#         uses: actions/upload-artifact@v3
#         with:
#           name: "debian12"
#           path: |
#             wezterm-*.deb
#             wezterm-*.xz
# EOF
# }
# #conf_print_wezterm_actions_debian12 | tee .github/workflows/gen_debian12.yml
#
#
# # this is the debian part of deploy.sh
# conf_print_wezterm_deploy() {
# cat <<'EOF'
#       Ubuntu*|Debian*|Pop)
#         rm -rf pkg
#         mkdir -p pkg/debian/usr/bin pkg/debian/DEBIAN pkg/debian/usr/share/{applications,wezterm}
#
#         if [[ "$BUILD_REASON" == "Schedule" ]] ; then
#           pkgname=wezterm-nightly
#           conflicts=wezterm
#         else
#           pkgname=wezterm
#           conflicts=wezterm-nightly
#         fi
#
#         cat > pkg/debian/control <<EOF
# Package: $pkgname
# Version: ${TAG_NAME#nightly-}
# Conflicts: $conflicts
# Architecture: $(dpkg-architecture -q DEB_BUILD_ARCH_CPU)
# Maintainer: Wez Furlong <wez@wezfurlong.org>
# Section: utils
# Priority: optional
# Homepage: https://wezfurlong.org/wezterm/
# Description: Wez's Terminal Emulator.
#  wezterm is a terminal emulator with support for modern features
#  such as fonts with ligatures, hyperlinks, tabs and multiple
#  windows.
# Provides: x-terminal-emulator
# Source: https://wezfurlong.org/wezterm/
# EOF
#
#         cat > pkg/debian/postinst <<EOF
# #!/bin/sh
# set -e
# if [ "\$1" = "configure" ] ; then
#         update-alternatives --install /usr/bin/x-terminal-emulator x-terminal-emulator /usr/bin/open-wezterm-here 20
# fi
# EOF
#
#         cat > pkg/debian/prerm <<EOF
# #!/bin/sh
# set -e
# if [ "\$1" = "remove" ]; then
# 	update-alternatives --remove x-terminal-emulator /usr/bin/open-wezterm-here
# fi
# EOF
#
#         install -Dsm755 -t pkg/debian/usr/bin target/release/wezterm-mux-server
#         install -Dsm755 -t pkg/debian/usr/bin target/release/wezterm-gui
#         install -Dsm755 -t pkg/debian/usr/bin target/release/wezterm
#         install -Dm755 -t pkg/debian/usr/bin assets/open-wezterm-here
#         install -Dsm755 -t pkg/debian/usr/bin target/release/strip-ansi-escapes
#
#         deps=$(cd pkg && dpkg-shlibdeps -O -e debian/usr/bin/*)
#         mv pkg/debian/postinst pkg/debian/DEBIAN/postinst
#         chmod 0755 pkg/debian/DEBIAN/postinst
#         mv pkg/debian/prerm pkg/debian/DEBIAN/prerm
#         chmod 0755 pkg/debian/DEBIAN/prerm
#         mv pkg/debian/control pkg/debian/DEBIAN/control
#         sed -i '/^Source:/d' pkg/debian/DEBIAN/control  # The `Source:` field needs to be valid in a binary package
#         echo $deps | sed -e 's/shlibs:Depends=/Depends: /' >> pkg/debian/DEBIAN/control
#         cat pkg/debian/DEBIAN/control
#
#         install -Dm644 assets/icon/terminal.png pkg/debian/usr/share/icons/hicolor/128x128/apps/org.wezfurlong.wezterm.png
#         install -Dm644 assets/wezterm.desktop pkg/debian/usr/share/applications/org.wezfurlong.wezterm.desktop
#         install -Dm644 assets/wezterm.appdata.xml pkg/debian/usr/share/metainfo/org.wezfurlong.wezterm.appdata.xml
#         install -Dm644 assets/wezterm-nautilus.py pkg/debian/usr/share/nautilus-python/extensions/wezterm-nautilus.py
#         install -Dm644 assets/shell-completion/bash pkg/debian/usr/share/bash-completion/completions/wezterm
#         install -Dm644 assets/shell-completion/zsh pkg/debian/usr/share/zsh/functions/Completion/Unix/_wezterm
#         install -Dm644 assets/shell-integration/* -t pkg/debian/etc/profile.d
#
#         if [[ "$BUILD_REASON" == "Schedule" ]] ; then
#           debname=wezterm-nightly.$distro$distver
#         else
#           debname=wezterm-$TAG_NAME.$distro$distver
#         fi
#         arch=$(dpkg-architecture -q DEB_BUILD_ARCH_CPU)
#         case $arch in
#           amd64)
#             ;;
#           *)
#             debname="${debname}.${arch}"
#             ;;
#         esac
#
#         fakeroot dpkg-deb --build pkg/debian $debname.deb
#
#         if [[ "$BUILD_REASON" != '' ]] ; then
#           $SUDO apt-get install ./$debname.deb
#         fi
#
#         mv pkg/debian pkg/wezterm
#         tar cJf $debname.tar.xz -C pkg wezterm
#         rm -rf pkg
# EOF
# }

# control files taken from the binary deb package debian12 release
# conf_print_wezterm_control() {
# cat <<'EOF'
# Package: wezterm
# Version: 20240203-110809-5046fc22
# Conflicts: wezterm-nightly
# Architecture: amd64
# Maintainer: Wez Furlong <wez@wezfurlong.org>
# Section: utils
# Priority: optional
# Homepage: https://wezfurlong.org/wezterm/
# Description: Wez's Terminal Emulator.
#   wezterm is a terminal emulator with support for modern features
#   such as fonts with ligatures, hyperlinks, tabs and multiple
#   windows.
# Provides: x-terminal-emulator
# Depends: libc6 (>= 2.35), libfontconfig1 (>= 2.12.6), libgcc-s1 (>= 4.2), libssl3 (>= 3.0.0), libwayland-client0 (>= 1.11.0), libwayland-egl1 (>= 1.15.0), libx11-6, libx11-xcb1 (>= 2:1.8.4), libxcb-image0 (>= 0.2.1), libxcb-util1 (>= 0.4.0), libxcb1 (>= 1.11.1), libxkbcommon-x11-0 (>= 0.5.0), libxkbcommon0 (>= 0.6.1), zlib1g (>= 1:1.1.4)
# EOF
# }

# conf_print_wezterm_postinst() {
# 	cat <<-EOF
# 		#!/bin/sh
# 		set -e
# 		if [ "$1" = "configure" ] ; then
# 		        update-alternatives --install /usr/bin/x-terminal-emulator x-terminal-emulator /usr/bin/open-wezterm-here 20
# 		fi
# 	EOF
# }
#
# conf_print_wezterm_prerm() {
# 	cat <<-EOF
# 		#!/bin/sh
# 		set -e
# 		if [ "$1" = "remove" ]; then
# 			update-alternatives --remove x-terminal-emulator /usr/bin/open-wezterm-here
# 		fi
# 	EOF
# }
# EOF
#
# conf_print_wezterm_debian-binary() {
# 	cat <<-EOF
# 		2.0
# 	EOF
# }
# EOF
# }
# # conf_print_wezterm_deploy

WALLPAPER='${DEST}/Pictures/Wallpapers/1920x1200-Wallpaper-041.jpg'
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

mkdir -p ${DEST}/.config/wezterm/colors
# 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
