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

_realname=zopfli
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.0.3
pkgrel=2
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/google/zopfli"
license=('apache')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake")
source=("https://github.com/google/zopfli/archive/${_realname}-${pkgver}.tar.gz"
        "7554e4d34e7000b0595aa606e7d72357cf46ba86.patch"
        "win32-fixes.patch")
sha256sums=('e955a7739f71af37ef3349c4fa141c648e8775bceb2195be07e86f8e638814bd'
            '3a3c002af41a8b066984362216099152ee127f2a31335bc6a3360f96d3a88366'
            '3adb302978fb0c47adf0eeb641b6674add71fd6af9a22497fbeffb991e78dddd')

# 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}/zopfli-zopfli-${pkgver}"
  del_file_exists CMakeLists.txt

#  sed -i -e "/^CFLAGS = / s/$/ $CFLAGS/" \
#    -e "/^CXXFLAGS = / s/$/ $CXXFLAGS/" Makefile
  apply_patch_with_msg 7554e4d34e7000b0595aa606e7d72357cf46ba86.patch \
     win32-fixes.patch
}

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

  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_LIBS=ON \
      ../zopfli-zopfli-${pkgver}
  make

  [[ -d "${srcdir}"/build-${CARCH}-static ]] && rm -rf "${srcdir}"/build-${CARCH}-static
  mkdir -p "${srcdir}"/build-${CARCH}-static && cd "${srcdir}"/build-${CARCH}-static
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=OFF \
      ../zopfli-zopfli-${pkgver}
  make
}

package() {
  #shared
  cd "${srcdir}"/build-${CARCH}-shared
  # -- zopfli
  install -Dm755 "zopfli.exe" "${pkgdir}${MINGW_PREFIX}/bin/zopfli.exe"
  install -Dm644 "../zopfli-zopfli-${pkgver}/src/zopfli/zopfli.h" "${pkgdir}${MINGW_PREFIX}/include/zopfli.h"
  install -Dm755 "libzopfli.dll" "${pkgdir}${MINGW_PREFIX}/bin/libzopfli.dll"
  install -Dm644 "libzopfli.dll.a" "${pkgdir}${MINGW_PREFIX}/lib/libzopfli.dll.a"
  # -- zopfipng
  install -Dm755 "zopflipng" "${pkgdir}${MINGW_PREFIX}/bin/zopflipng"

  install -Dm644 "../zopfli-zopfli-${pkgver}/src/zopflipng/zopflipng_lib.h" "${pkgdir}${MINGW_PREFIX}/include/zopflipng_lib.h"

  install -Dm755 "libzopflipng.dll" "${pkgdir}${MINGW_PREFIX}/bin/libzopflipng.dll"
  install -Dm755 "libzopflipng.dll.a" "${pkgdir}${MINGW_PREFIX}/lib/libzopflipng.dll.a"

  #static
  cd "${srcdir}"/build-${CARCH}-static
  #  -- zopfli
  install -Dm644 "libzopfli.a" "${pkgdir}${MINGW_PREFIX}/lib/libzopfli.a"
  install -Dm644 "../zopfli-zopfli-${pkgver}/README" "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/README"
  # -- zopfipng
  install -Dm755 "libzopflipng.a" "${pkgdir}${MINGW_PREFIX}/lib/libzopflipng.a"
  install -Dm644 "../zopfli-zopfli-${pkgver}/README.zopflipng" "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/README.zopflipng"
}
