# Maintainer: Naveen M K <naveen521kk@gmail.com>

_realname=starship
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.17.1
pkgrel=2
pkgdesc="The cross-shell prompt for astronauts (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://starship.rs"
license=('spdx:ISC')
depends=("${MINGW_PACKAGE_PREFIX}-libgit2")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-cmake")
optdepends=("${MINGW_PACKAGE_PREFIX}-ttf-font-nerd: Nerd Font Symbols preset")
options=('!strip' '!lto') # done at build stage
source=("https://github.com/starship/starship/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "https://github.com/starship/starship/pull/5690.patch"
        "https://github.com/starship/starship/pull/5694.patch")
sha256sums=('2b2fc84feb0197104982e8baf17952449375917da66b7a98b3e3fd0be63e5dba'
            '2969b3fab871af21f351674d241c58d5821542e5a00a2bf24c249ee8fffd2d07'
            '48587e5219fe7616f99f788df22ef01cc07f3d45f81c1edd5c985ae3f8d9fb1d')
noextract=("${_realname}-${pkgver}.tar.gz")

prepare() {
  tar -xzf "${srcdir}"/${_realname}-${pkgver}.tar.gz -C ${srcdir} || true
  cd "${srcdir}/${_realname}-${pkgver}"

  # https://github.com/starship/starship/pull/5690
  patch -Np1 -i "${srcdir}/5690.patch"
  # https://github.com/starship/starship/pull/5694
  patch -Np1 -i "${srcdir}/5694.patch"

  cargo fetch --locked
}

build() {
  cp -r ${_realname}-${pkgver} build-${MSYSTEM}
  cd "${srcdir}/build-${MSYSTEM}"
  WINAPI_NO_BUNDLED_LIBRARIES=1 \
    ${MINGW_PREFIX}/bin/cargo build \
    --release \
    --frozen
}

package() {
  WINAPI_NO_BUNDLED_LIBRARIES=1 \
    ${MINGW_PREFIX}/bin/cargo install \
    --frozen \
    --offline \
    --no-track \
    --path "build-${MSYSTEM}" \
    --root "${pkgdir}${MINGW_PREFIX}"

  install -Dm 644 build-${MSYSTEM}/LICENSE -t "${pkgdir}${MINGW_PREFIX}"/share/licenses/starship/
  install -dm 755 "${pkgdir}${MINGW_PREFIX}"/share/{bash-completion/completions,fish/vendor_completions.d,zsh/site-functions}/
  ./build-${MSYSTEM}/target/release/starship completions bash >"${pkgdir}${MINGW_PREFIX}"/share/bash-completion/completions/starship
  ./build-${MSYSTEM}/target/release/starship completions fish >"${pkgdir}${MINGW_PREFIX}"/share/fish/vendor_completions.d/starship.fish
  ./build-${MSYSTEM}/target/release/starship completions zsh >"${pkgdir}${MINGW_PREFIX}"/share/zsh/site-functions/_starship
}
