#!/usr/bin/env bash

# Volantes cursors, based on KDE Breeze
# Copyright (c) 2016 Keefer Rourke <keefer.rourke@gmail.com>
# Copyright (c) 2020 Sergei Eremenko <https://github.com/SmartFinn>

set -eu

build_svgs() {
  local src_dir="$1"

  if [[ -n "$2" ]]; then
    flavour="-f $2"
  fi

  for file in $src_dir/templates/*.tera; do
    [ -f "$file" ] || continue
    whiskers $file $flavour
  done
}

SCRIPT_DIR="$(dirname "$0")"
SRC_DIR="$SCRIPT_DIR/src"
OUT_DIR="$SCRIPT_DIR/dist"
PNG_DIR="$SCRIPT_DIR/pngs"
SVGS_DIR="$SCRIPT_DIR/svgs"
HL_DIR="$SCRIPT_DIR/hl"

AUTHORS="$SCRIPT_DIR/AUTHORS"
LICENSE="$SCRIPT_DIR/LICENSE"

ACCENTS="blue dark flamingo green lavender light maroon mauve peach pink red rosewater sapphire sky teal yellow"

while getopts "f:a:" option; do
  case $option in
  f)
    FLAVOR="$OPTARG"
    ;;
  a)
    ACCENTS="$OPTARG"
    ;;
  \?)
    echo "Invalid option: -$OPTARG"
    exit 1
    ;;
  esac
done
echo "[INFO] - Flavor: '$FLAVOR'"
echo "[INFO] - Accent(s): '$ACCENTS'"

echo "[INFO] - Generating SVGs..."
build_svgs $SRC_DIR $FLAVOR
echo "[INFO] - Generating SVGs complete."

echo "[INFO] - Building cursor formats; xcursor, hyprcursor, plasmasvg..."
for accent in $ACCENTS; do
  theme_name="catppuccin-$FLAVOR-$accent-cursors"
  theme_src_dir="$SVGS_DIR/$theme_name"
  theme_out_dir="$OUT_DIR/$theme_name"
  theme_png_dir="$PNG_DIR/$theme_name"
  theme_hl_dir="$HL_DIR/$theme_name"

  scripts/build-cursors "$theme_src_dir" "$theme_png_dir" "$theme_hl_dir" "$theme_out_dir"

  cp -f "$AUTHORS" "$theme_out_dir"
  cp -f "$LICENSE" "$theme_out_dir"
  cp -f "$theme_src_dir/index.theme" "$theme_out_dir"
  cp -f "$theme_hl_dir/manifest.hl" "$theme_out_dir"
done
echo "[INFO] - Finished building cursor formats; xcursor, hyprcursor, plasmasvg"
