# Maintainer: Naveen M K <naveen521kk at gmail.com>
# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=skia
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=130.r73211.b988efa0
pkgrel=4
pkgdesc="Skia is a complete 2D graphic library for drawing Text, Geometries, and Images (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url="https://skia.org"
msys2_repository_url="https://skia.googlesource.com/skia"
msys2_documentation_url="https://skia.org/docs"
license=("spdx:BSD-3-Clause")
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-harfbuzz"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-gn"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-python"
             "git")
_commit="b988efa06f8aa3bfeaa18c5b8c716ff244ab43cf"
source=("${_realname}.tar.gz::https://github.com/google/skia/archive/${_commit}.tar.gz"
        "bare-clones/zlib::git+https://chromium.googlesource.com/chromium/src/third_party/zlib.git#commit=646b7f569718921d7d4b5b8e22572ff6c76f2596"
        "0001-add-pkgconfig-support.patch"
        "0002-add-mingw-toolchain-skia.patch"
        "0003-fix-dwrite-function-mingw.patch"
        "0004-misc-mingw-fixes.patch"
        "0005-external-packages-update.patch"
        "0006-integrates-svg-into-skia-lib.patch"
        "skia.pc")
sha256sums=('6ae175029bcf3e7e7368aa9dc0f744b59f4c0e0e096dc204a65c5560761f6f6e'
            '967acb8025f9af3f1a5d4d4e9ab671a65fd9b1a52e93081683f84167077c979b'
            '7440572487b8bc33ac7a8445de0cc4148e0d47b538ba46f4952ba8cc97e0fa52'
            '252f49523d53a2b74d0efb60678de1ca491df28c71ee4d1504684a1414ec3503'
            '107526f649a31ff87033608cb1f55c0922d6bd40fa3c8c769ed0320b9230cde6'
            '5d934a3a9deeea37b4f67daa83f9c4716e6b90b7f8829ca6588d0a0829561b0c'
            '9ad4b876437bc83df8baf72051c9e919529a754b681c434316f119e8440d8996'
            '1ead6b6e2e706b20f070ea3f86ca05648fedcc6afa70d415c5e39bd3712a1e7e'
            'f0b20d04cee41207a6f974d468e05be63dc3f7ed02992e118660b5bf279e757b')

apply_patch_with_msg() {
  for _patch in "$@"; do
      msg2 "Applying $_patch"
      patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

pkgver() {
  git clone --filter=blob:none --revision "${_commit}" https://skia.googlesource.com/skia.git skia_temp
  cd skia_temp
  local _milestone=$(grep -Eo "Milestone ([0-9]+)" RELEASE_NOTES.md | head -1 | cut -f 2 -d ' ')
  printf "%s.r%s.%s" "${_milestone}" "$(git rev-list --count HEAD)" "$(git rev-parse --short=8 "${_commit}")"
  cd ..
  rm -Rf skia_temp
}

prepare() {
  mv "${_realname}-${_commit}" "${_realname}"
  cd ${_realname}
  apply_patch_with_msg \
    0001-add-pkgconfig-support.patch \
    0002-add-mingw-toolchain-skia.patch \
    0003-fix-dwrite-function-mingw.patch \
    0004-misc-mingw-fixes.patch \
    0005-external-packages-update.patch \
    0006-integrates-svg-into-skia-lib.patch

  mkdir -p third_party/externals/zlib
  cp -r "${srcdir}"/zlib/google third_party/externals/zlib

  # gn is expected to be in skia/bin
  cp ${MINGW_PREFIX}/bin/gn.exe bin/gn.exe

  sed -e "s|@PREFIX@|${MINGW_PREFIX}|g" \
      -e "s|@VERSION@|${pkgver}|g" -i "${srcdir}"/skia.pc
}

build() {
  local _buildtype=Release
  local _debug=false
  local _official=true
  if check_option "debug" "y"; then
      _buildtype=Debug
      _debug=true
      _official=false
  fi

  local _arch=x64
  if [[ ${CARCH} == aarch64 ]]; then
      _arch=arm64
  fi

  cd ${_realname}
  gn gen out/${_buildtype}-${MSYSTEM} --args="
    target_cpu=\"${_arch}\"
    cc=\"${CC}\"
    cxx=\"${CXX}\"
    is_debug=${_debug}
    is_official_build=${_official}
    is_component_build=true
    skia_use_dng_sdk=false
    skia_use_wuffs=false
    skia_use_xps=false"

  ninja -C out/${_buildtype}-${MSYSTEM}
}

package() {
  cd ${_realname}

  local _buildtype=Release
  if check_option "debug" "y"; then
    _buildtype=Debug
  fi

  install -d "${pkgdir}"/${MINGW_PREFIX}/bin
  install -Dm755 out/${_buildtype}-${MSYSTEM}/*.dll "${pkgdir}"/${MINGW_PREFIX}/bin/

  install -d "${pkgdir}"/${MINGW_PREFIX}/lib
  install -Dm755 out/${_buildtype}-${MSYSTEM}/*.dll.a "${pkgdir}"/${MINGW_PREFIX}/lib/

  install -d "${pkgdir}"/${MINGW_PREFIX}/include/skia
  cp --parents `find ./include -name \*.h` "$pkgdir"/${MINGW_PREFIX}/include/skia
  cp --parents `find ./modules -name \*.h` "$pkgdir"/${MINGW_PREFIX}/include/skia

  install -d "${pkgdir}"/${MINGW_PREFIX}/lib/pkgconfig
  install -Dm644 "${srcdir}/skia.pc" "${pkgdir}"/${MINGW_PREFIX}/lib/pkgconfig/

  install -d "${pkgdir}"/${MINGW_PREFIX}/share/licenses/skia
  install -Dm644 LICENSE* "${pkgdir}"/${MINGW_PREFIX}/share/licenses/${_realname}/
}
