# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Lars Kanis <lars@greiz-reinsdorf.de>

_realname=freetds
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.2.6
pkgrel=1
pkgdesc="A free implementation of Sybase's DB-Library, CT-Library and ODBC libraries (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.freetds.org/"
license=(LGPL2)
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-libiconv")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-libtool")
options=('strip' '!libtool' 'staticlibs')
source=(https://www.freetds.org/files/stable/freetds-${pkgver}.tar.bz2
        0001-Don-t-use-MSYS2-file-libws2_32.patch
        0002-Enable-inet_pton-definition-in-MINGW-i686-build.patch)
sha256sums=('66c085ef9de8a86b030df8af8f1689c2bb7dcfd2f998d3393e96943fdafff004'
            '27e97c6bab28f00d5f6b00cb4a2f604e9c1919d08ee9fbd12b244461aff9f2d4'
            'f0ac77b00b14db339fb772730ca36c97e8ff69ccfed2fe4291cad6daba47c586')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/0001-Don-t-use-MSYS2-file-libws2_32.patch
  patch -p1 -i ${srcdir}/0002-Enable-inet_pton-definition-in-MINGW-i686-build.patch
}

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

  ../freetds-${pkgver}/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --enable-static \
    --enable-shared \
    --with-openssl \
    --enable-sspi

  make
}

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