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

_realname=typstudio
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.1.0dev.32
_pkgver=${pkgver/dev/-dev}
pkgrel=1
pkgdesc="desktop application for typst (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64')
url="https://github.com/Cubxity/typstudio"
license=('GPL3')
makedepends=("${MINGW_PACKAGE_PREFIX}-yarn"
             "${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-cargo-tauri"
             "${MINGW_PACKAGE_PREFIX}-jq")
options=('staticlibs' 'strip')
source=("${_realname}-${_pkgver}.tar.gz::${url}/archive/v${_pkgver}.tar.gz")
sha256sums=('971287e38857a6544765d93697589dd91b0401f9b8ce8d8dfc2ad07a2b039903')

prepare() {
  cd "${srcdir}"
  rm -rf "build-${MSYSTEM}" | true
  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
}

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"
}
