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

_realname=libsquish
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.15
pkgrel=1
pkgdesc="Open source DXT compression library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://libsquish.sourceforge.io/'
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-libpng")
options=('strip' 'staticlibs')
source=(https://sourceforge.net/projects/libsquish/files/${_realname}-${pkgver}.tgz
        0001-fix-install.patch)
sha256sums=('628796eeba608866183a61d080d46967c9dda6723bc0a3ec52324c85d2147269'
            '7ec7f612ac18a3dbade20e9ed1d7de36ad0abbec59c8147a6e5144fa03728dc7')
noextract=(${_realname}-${pkgver}.tgz)

prepare() {
  mkdir -p ${_realname}-${pkgver}
  tar -xzf ${srcdir}/${_realname}-${pkgver}.tgz -C ${srcdir}/${_realname}-${pkgver}
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/0001-fix-install.patch
}

build() {
  [[ -d "${srcdir}"/build-${MINGW_CHOST}-shared ]] && rm -rf "${srcdir}"/build-${MINGW_CHOST}-shared
  mkdir -p "${srcdir}"/build-${MINGW_CHOST}-shared && cd "${srcdir}"/build-${MINGW_CHOST}-shared

  # Build shared version
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_SQUISH_EXTRA=ON \
    ../${_realname}-${pkgver}
  make

  [[ -d "${srcdir}"/build-${MINGW_CHOST}-static ]] && rm -rf "${srcdir}"/build-${MINGW_CHOST}-static
  mkdir -p "${srcdir}"/build-${MINGW_CHOST}-static && cd "${srcdir}"/build-${MINGW_CHOST}-static
  # Build static version
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=OFF \
    -DBUILD_SQUISH_EXTRA=ON \
    ../${_realname}-${pkgver}
  make
}

package() {
  cd "${srcdir}"/build-${MINGW_CHOST}-shared
  make install DESTDIR="${pkgdir}"

  cd "${srcdir}"/build-${MINGW_CHOST}-static
  make install DESTDIR="${pkgdir}"
  
  mkdir -p ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE.txt ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt
  
  mkdir -p ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/${_realname}.pc.in ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/${_realname}.pc
  sed -s "s|@PREFIX@|${MINGW_PREFIX}|g" -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/${_realname}.pc
  sed -s "s|@LIB_PATH@|lib|g" -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/${_realname}.pc
}
