# Maintainer: Andrew Crerar <crerar@archlinux.org>
# Contributor: Mohammadreza Abdollahzadeh <morealaz at gmail dot com>
# Contributor: FadeMind <fademind@gmail.com>
# Contributor: Vlad M. <vlad@archlinux.net>
# Contributor: Sebastian Stammler <stammler.s@gmail.com>
# Contributor: Sarkasper <echo a2FzcGVyLm1lbnRlbkBnbXguY29tCg== | base64 -d>
# Contributor: Daniel Micay <danielmicay@gmail.com>
# Contributor: Michalis Georgiou <mechmg93@gmail.comr>
# Contributor: Alexander De Sousa <archaur.xandy21@spamgourmet.com>

pkgname=ttf-google-fonts-git
pkgver=r11002.cdabad3d0
pkgrel=1
epoch=1
pkgdesc="TrueType fonts from the Google Fonts project (git version)"
arch=('any')
url="https://github.com/google/fonts"
license=('custom:SIL Open Font License' 'custom:Ubuntu Font License v1.0')

depends=('adobe-source-code-pro-fonts'
  'adobe-source-sans-fonts'
  'adobe-source-serif-fonts'
  'cantarell-fonts'
  'noto-fonts'
  'noto-fonts-extra'
  'ttf-anonymous-pro'
  'ttf-caladea'
  'ttf-croscore'
  'ttf-fira-sans'
  'ttf-fira-mono'
  'ttf-inconsolata'
  'ttf-lato'
  'ttf-merriweather'
  'ttf-merriweather-sans'
  'ttf-opensans'
  'ttf-oswald'
  'ttf-quintessential'
  'ttf-roboto'
  'ttf-roboto-mono'
  'ttf-signika'
  'ttf-ubuntu-font-family')
makedepends=('git' 'fontconfig')
conflicts=('jsmath-fonts'
  'lohit-fonts'
  'ttf-andika'
  'ttf-arabeyes-fonts'
  'ttf-cardo'
  'ttf-comfortaa'
  'ttf-google-fonts-typewolf'
  'ttf-lora-cyrillic'
  'ttf-lekton'
  'ttf-medievalsharp'
  'ttf-nova'
  'ttf-oxygen'
  'ttf-oxygen-git'
  'ttf-pt-fonts'
  'ttf-source-code-pro-ibx'
  'ttf-source-sans-pro-ibx'
  'ttf-vollkorn-ibx')
provides=('jsmath-fonts'
  'lohit-fonts'
  'ttf-andika'
  'ttf-cardo'
  'ttf-carlito'
  'ttf-comfortaa'
  'ttf-lora-cyrillic'
  'ttf-lekton'
  'ttf-medievalsharp'
  'ttf-nova'
  'ttf-oxygen'
  'ttf-oxygen-git'
  'ttf-pt-fonts'
  'ttf-source-code-pro-ibx'
  'ttf-source-sans-pro-ibx'
  'ttf-vollkorn-ibx')
source=(git+"${url}".git)
sha512sums=('SKIP')

pkgver() {
  cd fonts

  printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd fonts

  # NOTE: Adobe Blank is not meant to be installed.
  # See: https://github.com/google/fonts/issues/2106#issuecomment-520067314
  rm --recursive "${srcdir}/fonts/ofl/adobeblank"
}

package() {
  # NOTE: These are the font families that already exist in the [extra] repo
  #       or should not be installed by this package, due to user reports
  #       of system instability.
  declare -A omitted_font_families=(
    [anonymous - pro]=1
    [arimo]=1
    [caladea]=1
    [cantarell]=1
    [carlito]=1
    [cousine]=1
    [dai - banna - sil]=1
    [fira - sans]=1
    [fira - mono]=1
    [inconsolata]=1
    [merriweather]=1
    [merriweather - sans]=1
    [noto - color - emoji]=1
    [noto - sans]=1
    [noto - sans - tamil]=1
    [noto - serif]=1
    [open - sans]=1
    [oswald]=1
    [quintessential]=1
    [roboto]=1
    [roboto - condensed]=1
    [roboto - mono]=1
    [source - sans - 3]=1
    [source - sans - pro]=1
    [source - serif - 4]=1
    [source - serif - pro]=1
    [tinos]=1
    [ubuntu]=1
    [ubuntu - mono]=1
  )

  while IFS= read -rd '' file; do
    font_family=$(fc-query -f '%{family[0]|downcase|translate( ,-)}\n' "$file" | sed -n '1p')

    # NOTE: Skip the rest of the loop if we're not supposed to be touching this family
    ((omitted_font_families["$font_family"])) && continue

    pkg_font_path="$pkgdir"/usr/share/fonts/"$font_family"
    install -Dm644 "$file" -t "$pkg_font_path" # TODO: Check and make sure $font_family is being created

    # NOTE: If the font's license already exists, we don't need to copy the license again.
    src_license_path="${file%/*}"/OFL.txt
    pkg_font_license="$pkgdir"/usr/share/licenses/"$pkgname"/LICENSE."$font_family"
    if [[ -f "$src_license_path" && ! -f "$pkg_font_license" ]]; then
      install -Dm644 "$src_license_path" "$pkg_font_license"
    fi
  done < <(find "$srcdir" -type f -iname \*.ttf -print0)

  # NOTE: Since the zcool xiaowei chinese font has special characters. We need to change
  # the folder name to prevent errors during package compression.
  mv "$pkgdir"/usr/share/fonts/站酷小薇体 "$pkgdir"/usr/share/fonts/zcool-xiaowei-regular
  mv "$pkgdir"/usr/share/licenses/"$pkgname"/LICENSE.站酷小薇体 "$pkgdir"/usr/share/licenses/"$pkgname"/LICENSE.zcool-xiaowei-regular
}
