#!/bin/bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
FONTS_DIR="$SCRIPT_DIR/fonts"
PLUGIN_FILE="$SCRIPT_DIR/Quran Publication 4.0.2023_ID.ccx"
USER_FONTS_DIR="$HOME/Library/Fonts"

echo "Installing fonts to: $USER_FONTS_DIR"
mkdir -p "$USER_FONTS_DIR"

if [ ! -d "$FONTS_DIR" ]; then
  echo "Fonts folder not found: $FONTS_DIR"
  exit 1
fi

FONT_COUNT=0
while IFS= read -r -d '' font_file; do
  cp -f "$font_file" "$USER_FONTS_DIR/"
  FONT_COUNT=$((FONT_COUNT + 1))
done < <(find "$FONTS_DIR" -type f \( -iname "*.ttf" -o -iname "*.otf" -o -iname "*.ttc" \) -print0)

echo "Installed $FONT_COUNT font files."

if [ ! -f "$PLUGIN_FILE" ]; then
  echo "Plugin package not found: $PLUGIN_FILE"
  exit 1
fi

echo "Opening plugin package..."
open "$PLUGIN_FILE"
echo "Done."
