# Maintainer: Ali Mohammad Pur <totally@fakegmail.ch>
# Contributor: Michał Wojdyła < micwoj9292 at gmail dot com >
# Contributor: Fredrick R. Brennan <copypaste@kittens.ph>
# Contributor: nyorain <nyorain at gmail dot com>

pkgname=('skia-static')
pkgver=148
pkgrel=1
pkgdesc="A complete 2D graphic library for drawing Text, Geometries, and Images; Static build."
arch=('x86_64')
url="https://github.com/google/skia"
license=('BSD-3-Clause')
depends=('zlib' 'libglvnd' 'freetype2' 'expat' 'libpng' 'libjpeg-turbo' 'fontconfig' 'harfbuzz' 'gcc-libs' 'libwebp' 'glibc')
makedepends=('git' 'rsync' 'python' 'procps-ng' 'ninja' 'depot-tools-git')
provides=(skia)
conflicts=(skia)
source=(skia.pc)
sha256sums=('SKIP')

prepare() {
  # to make sure depot tools are in path
  cd "$srcdir"
  SRC_DIR="$(readlink -f "$srcdir")"
  rsync -Pa /opt/depot_tools/ "$SRC_DIR/depot_tools/"

  export PATH="$SRC_DIR/depot_tools:$PATH"

  # download/sync skia
  cd "$srcdir"
  gclient config https://skia.googlesource.com/skia.git < <(yes)
  gclient sync -D --with_tags -r "chrome/m$pkgver" < <(yes)
  gclient runhooks < <(yes y)

  # generate the ninja build files using gn
  cd skia
  tools/git-sync-deps
  bin/gn gen out/Static --args='is_official_build=true is_debug=false skia_use_vulkan=true extra_cflags_cc=["-USK_HIDE_PATH_EDIT_METHODS"]'
}

build() {
  cd skia
  export PATH="$srcdir/depot_tools:$PATH"
  ninja -C ./out/Static
}

package() {
  cd "$srcdir/skia"

  # License
  install -D -m644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"

  # Library
  libs=(libskia.a)
  for lib in "${libs[@]}"; do
    install -D -m644 out/Static/"$lib" "$pkgdir/usr/lib/$lib"
  done

  # Headers
  find include -type f -and -name "*.h" -exec install -v -D -m644 {} "$pkgdir/usr/include/"{} \; -print
  find modules -type f -and -name "*.h" -exec install -v -D -m644 {} "$pkgdir/usr/include/"{} \; -print
  find src -type f -and -name "*.h" -exec install -v -D -m644 {} "$pkgdir/usr/include/"{} \; -print

  # pkg-conf file
  install -D -m644 "$srcdir/skia.pc" "$pkgdir/usr/lib/pkgconfig/skia.pc"

  # Reset git
  git reset --hard origin/main
}
