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

_realname=evcxr
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}_repl"
         "${MINGW_PACKAGE_PREFIX}-${_realname}_jupyter")
pkgver=0.21.1
pkgrel=1
pkgdesc='An evaluation context for Rust (mingw-w64)'
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://github.com/evcxr/evcxr'
license=('spdx:Apache-2.0 OR MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-jq")
source=("${url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('904b397ac402d71d32587971477529acf6226ea1ae8c08c4558c2f670793fbda')

prepare() {
  cd "${_realname}-${pkgver}"

  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cd "${_realname}-${pkgver}"

  cargo build --release --frozen
}

check() {
  cd "${_realname}-${pkgver}"

  cargo test --release --frozen | true
}

package_evcxr_repl() {
  cd "${_realname}-${pkgver}"

  pkgdesc="Evcxr Rust REPL (mingw-w64)"

  cargo install \
    --offline \
    --no-track \
    --frozen \
    --path evcxr_repl \
    --root "${pkgdir}${MINGW_PREFIX}"

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}_repl/LICENSE"
}

package_evcxr_jupyter() {
  cd "${_realname}-${pkgver}"

  pkgdesc="Evcxr Rust Jupyter Kernel (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-rust"
           "${MINGW_PACKAGE_PREFIX}-python-jupyterlab")

  cargo install \
    --offline \
    --no-track \
    --frozen \
    --path evcxr_jupyter \
    --root "${pkgdir}${MINGW_PREFIX}"

  local JUPYTER_PATH="${pkgdir}${MINGW_PREFIX}"/share/jupyter
  local conf="${JUPYTER_PATH}"/kernels/rust/kernel.json

  JUPYTER_PATH=$JUPYTER_PATH \
    ./target/release/evcxr_jupyter --install

  echo $(jq '.argv[0] = "evcxr_jupyter.exe"' $conf) > $conf

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}_jupyter/LICENSE"
}

# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
