#!/usr/bin/env bash
#
# Adapted from https://gist.github.com/xqm32/17777d035930d622d0ff7530bfab61fd
#

set -euo pipefail

# Enable verbosity with VERBOSE=1
VERBOSE="${VERBOSE:-0}"

A_DIR="$HOME/.config/alacritty"

# Function to print usage information
usage()
{
  echo "Usage: $0 <theme>"
  echo "Available themes: (dark|night) to turn dark mode on, (day|light) to turn off"
  exit 1
}

# Function to print messages if VERBOSE is enabled
# $1 - message (string)
msg()
{
  [[ "$VERBOSE" -eq 1 ]] && echo "$1"
  return 0
}

# Function to set the alacritty theme
# $1 - theme (string)
set_alacritty_theme()
{
  local theme=$1
  local theme_file="$A_DIR/theme-$theme.toml"
  msg "Setting alacritty theme to $theme ($theme_file)"
  cp -f "$theme_file" "$A_DIR/theme-active.toml"
  return 0
}

# Function to notify alacritty about the changes
notify_alacritty()
{
  msg "Notifying alacritty about the changes"
  touch "$A_DIR/alacritty.toml"
  return 0
}

# Main function
main()
{
  if [ "$#" -ne 1 ]; then
    usage
  fi

  local alacritty_theme=$1

  case "$alacritty_theme" in
    day | light)
      set_alacritty_theme "day"
      ;;
    *)
      set_alacritty_theme "night"
      ;;
  esac

  notify_alacritty
  msg "Theme set successfully!"
  exit 0
}

main "$@"
