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

_realname=libgeotiff
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.6.0
pkgrel=1
pkgdesc="TIFF based interchange format for georeferenced raster imagery (mingw-w64)"
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://trac.osgeo.org/geotiff/"
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-proj"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-libtool")
options=('staticlibs' 'strip')
source=("https://download.osgeo.org/geotiff/${_realname}/${_realname}-${pkgver}.tar.gz"
        'libgeotiff_buildsys.patch')
sha256sums=('9311017e5284cffb86f2c7b7a9df1fb5ebcdc61c30468fb2e6bca36e4272ebca'
            'fb7e213559f779905c913389ad715e3aaf849c0e8c8ef72df86d1ca313d9393b')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/libgeotiff_buildsys.patch

  # fix wrongly encoded files from tarball
  for f in $(find . -type f); do
    if file $f | grep -q ISO-8859 ; then
      /bin/iconv -f ISO-8859-1 -t UTF-8 $f > ${f}.tmp && \
        mv -f ${f}.tmp $f
    fi
    if file $f | grep -q CRLF ; then
      sed -i -e 's|\r$||g' $f
    fi
  done

  #autoreconf -fiv
  ./autogen.sh
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"
  
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --with-proj \
    --with-jpeg \
    --with-zip

  make
}

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

  mkdir -p ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/

cat > ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/${_realname}.pc <<EOF
prefix=${MINGW_PREFIX}
exec_prefix=\${prefix}
libdir=\${prefix}/lib
includedir=\${prefix}/include

Name: ${_realname}
Description: GeoTIFF file format library
Version: ${pkgver}
Libs: -L\${libdir} -lgeotiff
Cflags: -I\${includedir}
EOF

  find "${pkgdir}${MINGW_PREFIX}" -name '*.def' -o -name '*.exp' | xargs -rtl1 rm
}
