# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=typstudio
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.1.0dev.32
_pkgver=${pkgver/dev/-dev}
pkgrel=2
pkgdesc="desktop application for typst (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url="https://github.com/Cubxity/typstudio"
license=('spdx:GPL-3.0-or-later')
makedepends=("${MINGW_PACKAGE_PREFIX}-yarn"
             "${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-cargo-tauri"
             "${MINGW_PACKAGE_PREFIX}-jq"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-gtk3"
             "${MINGW_PACKAGE_PREFIX}-libsoup")
source=("${url}/archive/v${_pkgver}/${_realname}-${_pkgver}.tar.gz")
sha256sums=('971287e38857a6544765d93697589dd91b0401f9b8ce8d8dfc2ad07a2b039903')

prepare() {
  cp -r "${_realname}-${_pkgver}" "build-${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}"

  local conf=src-tauri/tauri.conf.json

  echo $(jq '.build.beforeBuildCommand |= "yarn run build"' $conf) > $conf
  echo $(jq '.tauri.bundle.active |= false' $conf) > $conf

  "${MINGW_PREFIX}/bin/yarn" install

  "${MINGW_PREFIX}/bin/cargo" fetch \
    --locked \
    --manifest-path src-tauri/Cargo.toml \
    --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  msg "Tauri build for ${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}"

  "${MINGW_PREFIX}/bin/cargo-tauri" build
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  install -Dm755 "src-tauri/target/release/${_realname}.exe" "${pkgdir}${MINGW_PREFIX}/bin/${_realname}.exe"
  install -Dm755 "src-tauri/target/release/WebView2Loader.dll" "${pkgdir}${MINGW_PREFIX}/bin/WebView2Loader.dll"
  install -Dm644 "COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
