# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=allegro
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.2.10.1
pkgrel=3
pkgdesc="Portable library mainly aimed at video game and multimedia programming (mingw-w64)"
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://liballeg.org/"
msys2_repository_url='https://github.com/liballeg/allegro5'
msys2_references=(
  'archlinux: allegro'
)
license=('spdx:Zlib')
depends=("${MINGW_PACKAGE_PREFIX}-dumb"
         "${MINGW_PACKAGE_PREFIX}-flac"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-libogg"
         "${MINGW_PACKAGE_PREFIX}-libtheora"
         "${MINGW_PACKAGE_PREFIX}-libvorbis"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-openal"
         "${MINGW_PACKAGE_PREFIX}-opusfile"
         "${MINGW_PACKAGE_PREFIX}-physfs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
source=(https://github.com/liballeg/allegro5/archive/${pkgver}/${_realname}-${pkgver}.tar.gz
        https://github.com/liballeg/allegro5/commit/6e93fcaabaafd81701f4cd1b74f4b69dd598bc9b.patch)
conflicts=("${MINGW_PACKAGE_PREFIX}-allegro-static")
provides=("${MINGW_PACKAGE_PREFIX}-allegro-static")
noextract=(${_realname}-${pkgver}.tar.gz)
sha256sums=('ecef74c37aa7ecc779ec02a76eb821611b4222a41268d6ff846c3e709bae8ae5'
            'b0b265390bfc3f8a3623f9cecff137113cffface90cb18d520069fdf7225615e')

prepare() {
  rm -rf ${_realname}5-${pkgver}
  tar -xf ${_realname}-${pkgver}.tar.gz || true

  cd "${_realname}5-${pkgver}"
  patch -p1 -i "${srcdir}/6e93fcaabaafd81701f4cd1b74f4b69dd598bc9b.patch"
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DWANT_DOCS=OFF \
    -DINSTALL_PKG_CONFIG_FILES=ON \
    ../${_realname}5-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install ./
  install -Dm644 "${srcdir}"/${_realname}5-${pkgver}/LICENSE.txt \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt

  # remove path reference in cmake
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/${_realname}/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done
}
