#!/bin/bash
set -e

REPO_DIR="$(mktemp -d)"
OUT_DIR="[% dest_dir %]/noto-fonts-[% c("var/noto_git_hash") %]-[% c("version") %]"

pushd "$REPO_DIR"
git init
git remote add origin https://github.com/notofonts/notofonts.github.io.git
git fetch --depth 1 origin "[% c('var/noto_git_hash') %]"
git checkout "[% c('var/noto_git_hash') %]"

rm -rf "$OUT_DIR"
mkdir "$OUT_DIR"
[%
  platforms = [c("var/noto_fonts_common"), c("var/noto_fonts_linux"), c("var/noto_fonts_mac"), c("var/noto_fonts_windows")];
  FOREACH platform = platforms;
    FOREACH family = platform;
%]
  font_src="fonts/[% family %]/hinted/ttf"
  font_dst="$OUT_DIR/[% family %]"
  mkdir -p $font_dst
  # Use the wildcard for cases such as NotoSansTifinagh.
  # But then to avoid NotoSans* matching everything on Windows, we create a
  # directory for each family.
  find $font_src -name '*-Regular.ttf' -and -not -name '*UI-Regular.ttf' -exec cp {} $font_dst/ \;
[%
    END;
  END; %]

popd
rm -rf "$REPO_DIR"

echo "Finished downloading the needed Noto fonts"
