# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>

_realname=edit
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.2.1
pkgrel=1
pkgdesc="Terminal text editor from Microsoft (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://github.com/microsoft/edit'
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-rustup"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("${url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "67c401648fbd51de503d99ce4fca27774638f254.patch")
sha256sums=('41c719b08212fa4ab6e434a53242b2718ba313e8d24d090f244bb857d6a9d0fd'
            'd6023b09728eb50fe68743ea0bed67892681d0121711ce7283a4b595e06771cd')

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

  patch -p1 -i ../67c401648fbd51de503d99ce4fca27774638f254.patch

  export RUSTUP_TOOLCHAIN=nightly-${RUST_CHOST}
  cargo fetch --locked --target "${RUST_CHOST}"
}

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

  export RUSTUP_TOOLCHAIN=nightly-${RUST_CHOST}
  rustup component add rust-src --toolchain nightly-${RUST_CHOST}
  # FIXME: split debuginfo is not supported for mingw host anyway
  # https://github.com/rust-lang/rust/blob/60dabef95a3de3ec974dcb50926e4bfe743f078f/compiler/rustc_target/src/spec/base/windows_gnu.rs#L111
  export CARGO_PROFILE_RELEASE_DEBUG=0
  cargo build \
    --config .cargo/release-nightly.toml \
    --release \
    --locked
}

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

  cargo install \
    --config .cargo/release-nightly.toml \
    --offline \
    --no-track \
    --frozen \
    --path . \
    --root "${pkgdir}${MINGW_PREFIX}"

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