#!/bin/bash

# Font URL
fontUrl="https://raw.githubusercontent.com/google/material-design-icons/master/font/MaterialIcons-Regular.ttf"
fontFile="$(basename "$fontUrl")"

# Codepoints can be found at https://fonts.google.com/icons?icon.set=Material+Icons
unicodePoints=(
	"61-7a,5f" # ascii lowercase + underscore
	"e2c4"     # file_download
	"e037"     # play_arrow
	"e034"     # pause
	"e04f"     # volume_off
	"e04e"     # volume_mute
	"e04d"     # volume_down
	"e050"     # volume_up
	"e01f"     # fast_forward
	"e83a"     # star_border
	"e838"     # star
	"e31b"     # keyboard_return
)
unicodeStr=$(
	IFS=,
	echo "${unicodePoints[*]}"
)

# Command extracts the needed iconfont codepoints and compresses as woff2 for small bundled size
fontToolsBinary="fonttools"
fontToolsArgs=("subset" "$fontFile" "--output-file=../static/fonts/MaterialIcons-Regular-subset.woff2" "--no-layout-closure" "--unicodes=${unicodeStr}" "--flavor=woff2" "--verbose")

# Check if file exists
if [ -f "$fontFile" ]; then
	echo "Font file already exists. Skipping download."
else
	echo "Font file not found. Downloading..."
	curl -s -o "$fontFile" "$fontUrl"
fi

# Check if fonttools binary exists
if ! command -v "$fontToolsBinary" &>/dev/null; then
	echo "Error: $fontToolsBinary not found. Please install it and try again."
else
	echo "$fontToolsBinary is installed. Running command..."
	echo "${fontToolsArgs[@]}"
	"$fontToolsBinary" "${fontToolsArgs[@]}"
fi
