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

# Package building only when previous package is not installed
_realname=firebird
pkgbase=mingw-w64-${_realname}2-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}2-git"
pkgver=2.5.9.27152.74a4ec5c50
pkgrel=8
pkgdesc="Cross-platform relational database offering many ANSI SQL standard features - version 2.x (mingw-w64)"
url="https://www.firebirdsql.org/"
license=('custom')
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64')
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-${_realname}2" "${MINGW_PACKAGE_PREFIX}-libfbclient")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}2" "${MINGW_PACKAGE_PREFIX}-libfbclient")
replaces=("${MINGW_PACKAGE_PREFIX}-libfbclient")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=(#"${MINGW_PACKAGE_PREFIX}-btyacc" # Maybe add check in firebird presence of system btyacc
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-libatomic_ops"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "git")
_commit="74a4ec5c50f6906c01a3a8cfbfc56361469cd9fa"
source=("${_realname}"::"git+https://github.com/FirebirdSQL/firebird.git#commit=${_commit}"
        pathtools.c
        pathtools.h
        0001-Remove-hardcoded-march-to-allow-64-bit-builds.patch
        0002-Always-use-Win32-threads-for-mingw.patch
        0003-Define-AMD64-for-64-bit-mingw.patch
        0004-mingw-w64-have-isinf.patch
        0005-Fix-cast-error.patch
        0006-mingw-w64-don-t-need-hack-for-SecPkgContext_AccessTo.patch
        0007-Use-modern-ICU-libraries-when-link-with-system-wide-.patch
        0008-mingw-w64-don-t-have-win98-support.patch
        0009-Fix-redefinition-of-matherr.patch
        0010-Don-t-export-gds__print_pool-as-it-not-part-of-API-a.patch
        0011-mingw-w64-have-fenv.h.patch
        0012-Fix-cast-error-for-64-bit.patch
        0013-Fix-source-extension.patch
        0014-Move-extra-mingw-sources-definitions-for-fbintl-to-M.patch
        0015-Add-missing-sources-for-utils-building.patch
        0016-smtp_server-Add-win32-platform-files.patch
        0017-Fix-building-net_server-and-mp_server-with-mingw.patch
        0018-For-mingw-place-dll-s-into-BIN-and-generate-import-l.patch
        0019-Don-t-install-unix-files-for-Windows-platform.patch
        0020-Fix-rules-to-use-variables.patch
        0021-Auto-detect-WINDRES-with-autoconf.patch
        0022-Create-symlinks-of-fbembed-only-if-names-are-differe.patch
        0023-mod_loader-mingw-always-link-to-msvcrt.dll.patch
        0024-Relocate-paths-for-unix-like-files-layout.patch
        0025-Fix-building-gstat.patch
        0026-Fix-using-function-pointer.patch
        0027-The-operator-new-declaration-has-changed-slightly-in.patch
        0028-Fix-different-warnings.patch
        #0030-Use-nullptr.patch
        0031-Fix-compiler-flags.patch
        #0032-fix-SCL_check_access-call.patch
)
sha256sums=('SKIP'
            'ebf471173f5ee9c4416c10a78760cea8afaf1a4a6e653977321e8547ce7bf3c0'
            '1585ef1b61cf53a2ca27049c11d49e0834683dfda798f03547761375df482a90'
            'eaa4b490dfaac611001d06f9e1f0fc782340c9631808cb4ded16148f16498146'
            '6d8c2321a4fe693ac444db650b090fd841cc7c7b914bb30d0fe76559ca1be029'
            '563193c16e0004f0b4123f153b09462fde08e8a1ab9de25c6de762a7a5751b1e'
            'bae8d7bb1e5f84c431167db3606641c875d900991f62152652a1353c53ea28f6'
            '56c702d53c017af3a371a6064d3280547c0e09bddadbcf1d8761a47614ce6c76'
            'e51633c91aa54d2433cadc6726fabb1946e5b19cd928510c41112718607589a3'
            '37026200645a621283f1d2ff7f50f719cea352838d0c3bdf65dbcc7f6fc94b5f'
            'efc7fb392127fbeda7f798fca53fd4c8dafb6b9bdf231bf0fe11900fb058a7b7'
            'c50164f18a1fa8e5369b7f64ef703936fe62c7499a06a601582441f9c9da2ef4'
            '983fe70bc300265bf1e2690bdfeaab19031a4f80fd74f940948e9a56fda293fd'
            '61fda2ac6e4e0349b1a60212c2b677581a562b4f08f741bef41c764fc9d10def'
            'aec6b53a9b1319d5eceec2dbb823c7f0b675d11b9bde12bb5cc921398e09979e'
            '559656a9f6234460ed979b7c743ab8629a954a6ec6e89bd981b5f471490b12c7'
            'b6b9251c231f2551d70376b6c6e27879f3fcaf37d657e4ec81f0759a4d747df2'
            'e6bc726aae8e519300d8d9ddddebc23f13f1b3f40e5b80d97845ee82e0755d9d'
            'c34e0a04b015096434fad579c3e3de79cb08ae68a40f285d0159221bdee12365'
            '2e104b68083bd010a2d54ae23ccbe70ddfcaa220cc400acf850b5462380955bb'
            '7a84483a48d7d92177d8892c03e3291e3c693f365c7b5c0b38eb5d04d7e589cb'
            '80e7741f59815024ace8c1f662c37f5f6c3618890d83b9c94ee074f181e2ea62'
            '0439855ce3e58b3b41a0268c0135f3f3600e0ce3015706eefcd155643c590f73'
            'e927670f2ef41cdb1f13f300308d75eb7de7b680c2e40246b65a0236fe8c2fad'
            '865abc7226916fd4503a80ed5555a835d1774a4659fd32b49fdaa38e8f5d3353'
            '5896a3a0b2732a9f835c183ea3d1269e1541428ce11c27b1f15fc3d683da8310'
            '6f793e5f87afb1b4a3d3d91552f5966f7c2e30d5841137fe5becc4d44a716bff'
            '32a3520e588f0cc6648dfdf66f5a2579ff67d4db30ecdaac69bcf275b8b8c919'
            '2e2ee6f9c6d91a707b12666fa340c31b4f8edb8e910cd7ff08d6cfed5ed4c2d2'
            '3dade981ceda60919b4ac4a64350855272ea60705ba58b984c15e6d3af3a18a8'
            'ef1273dc6ba18c9a7fa112b17b8bde5b89abdb58bca0aa4c0f7c8e9989fc12a1'
            '43372147adb06ccd4c86bbfc426d28b3f4a8b573623cb686a49299a81cc7500e')

pkgver() {
  cd "${_realname}"
  source ${srcdir}/${_realname}/src/misc/writeBuildNum.sh
  printf '%s.%s.%s.%s.%s' ${MajorVer} ${MinorVer} ${RevNo} ${BuildNum} "$(git rev-parse --short "${_commit}")"
}

prepare() {
  test ! -d "${startdir}/../mingw-w64-pathtools" || {
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.c" "${srcdir}/pathtools.c" &&
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.h" "${srcdir}/pathtools.h"
  } || exit 1

  cd ${_realname}
  cp -fHv "${srcdir}"/pathtools.c src/common/pathtools.cpp
  cp -fHv "${srcdir}"/pathtools.h src/common/

  git apply ${srcdir}/0001-Remove-hardcoded-march-to-allow-64-bit-builds.patch
  git apply ${srcdir}/0002-Always-use-Win32-threads-for-mingw.patch
  git apply ${srcdir}/0003-Define-AMD64-for-64-bit-mingw.patch
  git apply ${srcdir}/0004-mingw-w64-have-isinf.patch
  git apply ${srcdir}/0005-Fix-cast-error.patch
  git apply ${srcdir}/0006-mingw-w64-don-t-need-hack-for-SecPkgContext_AccessTo.patch
  git apply ${srcdir}/0007-Use-modern-ICU-libraries-when-link-with-system-wide-.patch
  git apply ${srcdir}/0008-mingw-w64-don-t-have-win98-support.patch
  git apply ${srcdir}/0009-Fix-redefinition-of-matherr.patch
  git apply ${srcdir}/0010-Don-t-export-gds__print_pool-as-it-not-part-of-API-a.patch
  git apply ${srcdir}/0011-mingw-w64-have-fenv.h.patch
  git apply ${srcdir}/0012-Fix-cast-error-for-64-bit.patch
  git apply ${srcdir}/0013-Fix-source-extension.patch
  git apply ${srcdir}/0014-Move-extra-mingw-sources-definitions-for-fbintl-to-M.patch
  git apply ${srcdir}/0015-Add-missing-sources-for-utils-building.patch
  git apply ${srcdir}/0016-smtp_server-Add-win32-platform-files.patch
  git apply ${srcdir}/0017-Fix-building-net_server-and-mp_server-with-mingw.patch
  git apply ${srcdir}/0018-For-mingw-place-dll-s-into-BIN-and-generate-import-l.patch
  git apply ${srcdir}/0019-Don-t-install-unix-files-for-Windows-platform.patch
  git apply ${srcdir}/0020-Fix-rules-to-use-variables.patch
  git apply ${srcdir}/0021-Auto-detect-WINDRES-with-autoconf.patch
  git apply ${srcdir}/0022-Create-symlinks-of-fbembed-only-if-names-are-differe.patch
  git apply ${srcdir}/0023-mod_loader-mingw-always-link-to-msvcrt.dll.patch
  git apply ${srcdir}/0024-Relocate-paths-for-unix-like-files-layout.patch
  git apply ${srcdir}/0025-Fix-building-gstat.patch
  git apply ${srcdir}/0026-Fix-using-function-pointer.patch
  git apply ${srcdir}/0027-The-operator-new-declaration-has-changed-slightly-in.patch
  git apply ${srcdir}/0028-Fix-different-warnings.patch
  git apply ${srcdir}/0031-Fix-compiler-flags.patch

  NOCONFIGURE=1 ./autogen.sh
}

build() {
  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  cp -rf ${_realname} build-${MSYSTEM} && cd build-${MSYSTEM}

  local _debug_conf=
  if check_option "debug" "y"; then
    _debug_conf="--enable-debug"
  fi

  CFLAGS+=" -D_POSIX_C_SOURCE"
  CPPFLAGS+=" -D_POSIX_C_SOURCE"

  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-static \
    --enable-shared \
    ${_debug_conf} \
    --with-system-icu \
    --with-fbbin=${MINGW_PREFIX}/bin \
    --with-fbsbin=${MINGW_PREFIX}/bin \
    --with-fbconf=${MINGW_PREFIX}/etc/${_realname} \
    --with-fblib=${MINGW_PREFIX}/lib \
    --with-fbinclude=${MINGW_PREFIX}/include \
    --with-fbdoc=${MINGW_PREFIX}/share/${_realname}/doc \
    --with-fbudf=${MINGW_PREFIX}/share/${_realname}/udf \
    --with-fbsample=${MINGW_PREFIX}/share/${_realname}/samples \
    --with-fbsample-db=${MINGW_PREFIX}/share/${_realname}/samples/empbuild \
    --with-fbhelp=${MINGW_PREFIX}/share/${_realname}/help \
    --with-fbintl=${MINGW_PREFIX}/share/${_realname}/intl \
    --with-fbmisc=${MINGW_PREFIX}/share/${_realname}/misc \
    --with-fbsecure-db=${MINGW_PREFIX}/etc/${_realname} \
    --with-fbmsg=${MINGW_PREFIX}/etc/${_realname} \
    --with-fblog=${MINGW_PREFIX}/var \
    --with-fbglock=${MINGW_PREFIX}/var \
    --with-fbplugins=${MINGW_PREFIX}/share/${_realname} \
    ac_cv_func_localtime_r=yes \
    ac_cv_func_ctime_r=yes

  make -j1
}

package() {
  cd ${srcdir}/build-${MSYSTEM}

  mkdir -p ${pkgdir}${MINGW_PREFIX}/{bin,etc/${_realname},include,lib,share/${_realname}/{doc,help,intl,misc,plugins,samples,udf},var}
  cp -f gen/firebird/bin/*       ${pkgdir}${MINGW_PREFIX}/bin/
  cp -f gen/firebird/lib/*       ${pkgdir}${MINGW_PREFIX}/lib/
  cp -f gen/firebird/include/*   ${pkgdir}${MINGW_PREFIX}/include/
  cp -f gen/firebird/UDF/*       ${pkgdir}${MINGW_PREFIX}/share/${_realname}/udf/
  cp -rf gen/firebird/examples/* ${pkgdir}${MINGW_PREFIX}/share/${_realname}/samples/
  cp -f gen/firebird/help/*      ${pkgdir}${MINGW_PREFIX}/share/${_realname}/help/
  cp -f gen/firebird/intl/*      ${pkgdir}${MINGW_PREFIX}/share/${_realname}/intl/
  cp -f gen/firebird/plugins/*   ${pkgdir}${MINGW_PREFIX}/share/${_realname}/plugins/
  #cp -f gen/firebird/misc/*      ${pkgdir}${MINGW_PREFIX}/share/${_realname}/misc/

  cp -f gen/firebird/*.bat       ${pkgdir}${MINGW_PREFIX}/bin/
  cp -f gen/firebird/*.conf      ${pkgdir}${MINGW_PREFIX}/etc/${_realname}/
  cp -f gen/firebird/*.msg       ${pkgdir}${MINGW_PREFIX}/etc/${_realname}/
  cp -f gen/firebird/*.fdb       ${pkgdir}${MINGW_PREFIX}/etc/${_realname}/

  cp -rf doc/* ${pkgdir}${MINGW_PREFIX}/share/${_realname}/doc/

  touch ${pkgdir}${MINGW_PREFIX}/var/${_realname}.log

  # Remove build tools
  rm -f ${pkgdir}${MINGW_PREFIX}/bin/{brltable,codes,create_db,*build*,*boot*,*static*,*current*}.exe
}
