# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=flif
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_pkgname=FLIF
pkgver=0.3
pkgrel=1
pkgdesc="Free Lossless Image Format (mingw-w64)"
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/FLIF-hub/FLIF"
license=('GPL3')
conflicts=("${MINGW_PACKAGE_PREFIX}-flif-git")
depends=("${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-libpng" 
         "${MINGW_PACKAGE_PREFIX}-SDL2")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/FLIF-hub/FLIF/archive/v${pkgver//_/-}.tar.gz"
         fix-win32-build.patch)
sha256sums=('aa02a62974d78f8109cff21ecb6d805f1d23b05b2db7189cfdf1f0d97ff89498'
            'ae60d5a68670e4643a34a12011483040fb34d6b53137387ef914c84c7448dc82')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd "$srcdir/${_pkgname}-${pkgver//_/-}"
  apply_patch_with_msg fix-win32-build.patch
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_{SHARED,STATIC}_LIBS=ON \
      "${srcdir}/${_pkgname}-${pkgver//_/-}/src"
    make
}

check() {
  cd "${srcdir}"/build-${CARCH}
  make test
}

package() {
  cd "${srcdir}"/build-${CARCH}
  install -dm755 "${pkgdir}${MINGW_PREFIX}/bin"
  install -dm755 "${pkgdir}${MINGW_PREFIX}/lib"
  install -dm755 "${pkgdir}${MINGW_PREFIX}/share/man/man1/"
  install -dm755 "${pkgdir}${MINGW_PREFIX}/include/"
  install -m755 flif.exe viewflif.exe dflif.exe "${pkgdir}${MINGW_PREFIX}/bin"
  install -m755 libflif.dll "${pkgdir}${MINGW_PREFIX}/bin/"
  install -m755 libflif.dll.a "${pkgdir}${MINGW_PREFIX}/lib/"
  install -m755 libflif.a "${pkgdir}${MINGW_PREFIX}/lib/"
  install -m755 libflif_dec.dll "${pkgdir}${MINGW_PREFIX}/bin/"
  install -m755 libflif_dec.dll.a "${pkgdir}${MINGW_PREFIX}/lib/"
  install -m755 libflif_dec.a "${pkgdir}${MINGW_PREFIX}/lib/"
  install -m 644 "$srcdir/${_pkgname}-${pkgver//_/-}/src"/library/*.h "${pkgdir}${MINGW_PREFIX}/include/"
  install -m 644 "$srcdir/${_pkgname}-${pkgver//_/-}/doc/flif.1" \
          "${pkgdir}${MINGW_PREFIX}/share/man/man1"
  install -m 755 "$srcdir/${_pkgname}-${pkgver//_/-}/tools/gif2flif" \
        "${pkgdir}${MINGW_PREFIX}/bin"
  install -m 755 "$srcdir/${_pkgname}-${pkgver//_/-}/tools/apng2flif" \
        "${pkgdir}${MINGW_PREFIX}/bin"
  cd "$srcdir/${_pkgname}-${pkgver//_/-}"
  install -Dm644 "$srcdir/${_pkgname}-${pkgver//_/-}/LICENSE" \
        "${pkgdir}${MINGW_PREFIX}/share/licenses/${_pkgname}/LICENSE"
}
