# Maintainer: Diego Sogari <diego.sogari@gmail.com>

_realname=soci
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.1.2
pkgrel=1
pkgdesc="SOCI - The C++ Database Access Library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://soci.sourceforge.io/"
msys2_repository_url="https://github.com/SOCI/soci"
msys2_references=(
  'aur: soci'
)
license=('spdx:BSL-1.0')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-firebird"
             "${MINGW_PACKAGE_PREFIX}-libmariadbclient"
             "${MINGW_PACKAGE_PREFIX}-postgresql"
             "${MINGW_PACKAGE_PREFIX}-sqlite3")
optdepends=("${MINGW_PACKAGE_PREFIX}-firebird: support for Firebird databases"
            "${MINGW_PACKAGE_PREFIX}-libmariadbclient: support for MySQL databases"
            "${MINGW_PACKAGE_PREFIX}-postgresql: support for PostGreSQL databases"
            "${MINGW_PACKAGE_PREFIX}-sqlite3: support for SQLite databases")
source=("https://github.com/SOCI/soci/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        001-fix-undefined-reference.patch)
sha256sums=('c0974067e57242f21d9a85677c5f6cc7848fba3cbd5ec58d76c95570a5a7a15b'
            '93b96f408da20a4775d75483d35ea1f64a7283e7f80fc26881010baebf42c7ad')

prepare() {
  cd "${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}"/001-fix-undefined-reference.patch
}

build() {
  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=" \
  cmake -Wno-dev \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -S ${_realname}-${pkgver} \
    -B build-${MSYSTEM}

  cmake --build build-${MSYSTEM}
}

package() {
  DESTDIR="${pkgdir}" cmake --install build-${MSYSTEM}

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/soci*/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE_1_0.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
