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

_realname=keepawake
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-lib${_realname}")
pkgver=0.6.0
pkgrel=1
pkgdesc="Keep your computer awake (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/segevfiner/keepawake-rs"
msys2_references=(
  'aur'
  'purl: pkg:cargo/keepawake'
)
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-cargo-c")
options=('!strip') # done while building
source=("https://github.com/segevfiner/keepawake-rs/archive/v${pkgver}/${_realname}-rs-${pkgver}.tar.gz")
sha256sums=('30ce19e32f0bf7cd6aa83c1989a616bc2bde8c6696c6440d37a8e40608d093e5')

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

  cargo fetch --locked
}

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

  cargo build --release --frozen --features bin

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    cargo cbuild --meson-paths --release --frozen --prefix="${MINGW_PREFIX}"
}

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

  cargo test --release --frozen --features bin
}

package_keepawake() {
  cd "${_realname}-rs-${pkgver}"

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

  local _complete="target/release/keepawake --completions"
  $_complete bash | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/keepawake"
  $_complete fish | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d/keepawake.fish"
  $_complete zsh | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_keepawake"
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}

package_libkeepawake() {
  cd "${_realname}-rs-${pkgver}"

  pkgdesc+=" (C API)"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    cargo cinstall \
      --meson-paths \
      --release \
      --frozen \
      --prefix="${MINGW_PREFIX}" \
      --destdir="${pkgdir}"

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

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# 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
# template end;
