# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=firebird
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.0.0.1306
pkgrel=1
pkgdesc="Cross-platform relational database offering many ANSI SQL standard features (mingw-w64)"
url="https://www.firebirdsql.org/"
license=('spdx:Interbase-1.0 AND MPL-1.0')
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
msys2_references=(
  'archlinux: firebird'
)
msys2_repository_url='https://github.com/FirebirdSQL/firebird/'
msys2_documentation_url='https://www.firebirdsql.org/en/documentation/'
conflicts=("${MINGW_PACKAGE_PREFIX}-firebird2-git")
replaces=("${MINGW_PACKAGE_PREFIX}-firebird2-git")
depends=("${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libtomcrypt"
         "${MINGW_PACKAGE_PREFIX}-libtommath"
         "${MINGW_PACKAGE_PREFIX}-re2")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "unzip")
source=("https://github.com/FirebirdSQL/firebird/archive/refs/tags/v${pkgver%.*}.tar.gz"
        001-configure-fixes.patch
        002-mingw-makefiles-fixes.patch
        003-substitute-extauth-makefile.patch
        004-external-sources-fixes.patch
        005-firebird-sources-fixes.patch
        006-resource-file-fixes.patch
        007-pathtools-fixes.patch
        008-add-script-and-batch-files.patch
        pathtools.cpp
        pathtools.h)
sha256sums=('4da3aedbbe780f5f878f19998828fa64a9b43285499eff99c5dad71b7c72dcaf'
            'be70056bebc87a4834ff7785e32915b547b5d62cf14042851d54e5762ea05368'
            '70b4b5327e128409b9dc2bf14871062a6b36fcffb3e69d30bb6a7f0e36f95b90'
            'd99dc050c57c43e351000c161d51c0ec0420004de912547fcf04e9325cd6de27'
            '88959ee452b2950ebaac5e1a034cbb54eb95c886616a322664074704a02ba43e'
            '84714bc93ba21368b35fe9de757eafbc390742eeb4b5641d2f8330d828b72bba'
            '76f337487b62c99cff3dd2045f4143a69b0395c82d76ebf42d18d7ca3f5eca66'
            'f9babbf59dfb99a5d0048647bb64fce39f821c110e768049aa955614396382e2'
            '094896b8614981854eadcca789726e8eb646918811829f58cde43a27870156ed'
            '2f357fe2c6cdec3bfd90979616cfd075aee2de51beeaa08cd7a4192878efa64e'
            '0e6014650703817bc106825bb6a92bea72d9d790e89975c88d058fba2a26eddc')

pkgver() {
  cd ${_realname}-${pkgver%.*}
  source ${srcdir}/${_realname}-${pkgver%.*}/src/misc/writeBuildNum.sh
  printf '%s.%s.%s.%s' ${MajorVer} ${MinorVer} ${RevNo} ${BuildNum}
}

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd ${_realname}-${pkgver%.*}
  cp -f ../pathtools.h src/common
  cp -f ../pathtools.cpp src/common/os/win32

  apply_patch_with_msg \
    001-configure-fixes.patch \
    002-mingw-makefiles-fixes.patch \
    003-substitute-extauth-makefile.patch \
    004-external-sources-fixes.patch \
    005-firebird-sources-fixes.patch \
    006-resource-file-fixes.patch \
    007-pathtools-fixes.patch \
    008-add-script-and-batch-files.patch
}

build() {
  cp -rf ${_realname}-${pkgver%.*} "build-${MSYSTEM}" && cd "build-${MSYSTEM}"

  if [[ ${MSYSTEM} == CLANG* ]]; then
    CXXFLAGS="-Wno-varargs -Wno-constant-conversion -Wno-inconsistent-missing-override -Wno-tautological-constant-out-of-range-compare -Wno-c++11-narrowing"
  else
    CXXFLAGS="-Wno-class-memaccess -Wno-overflow -Wno-return-local-addr -Wno-stringop-overread -Wno-stringop-overflow -Wno-narrowing"
  fi

  ./autogen.sh \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --disable-rpath \
    --with-system-re2 \
    --with-fbbin=${MINGW_PREFIX}/bin \
    --with-fbsbin=${MINGW_PREFIX}/bin \
    --with-fbconf=${MINGW_PREFIX}/etc/${_realname} \
    --with-fbsecure-db=${MINGW_PREFIX}/etc/${_realname} \
    --with-fbmsg=${MINGW_PREFIX}/etc/${_realname} \
    --with-fblib=${MINGW_PREFIX}/lib \
    --with-fbinclude=${MINGW_PREFIX}/include \
    --with-fbdoc=${MINGW_PREFIX}/share/${_realname}/doc \
    --with-fbsample=${MINGW_PREFIX}/share/${_realname}/examples \
    --with-fbsample-db=${MINGW_PREFIX}/share/${_realname}/examples/empbuild \
    --with-fbintl=${MINGW_PREFIX}/share/${_realname}/intl \
    --with-fbmisc=${MINGW_PREFIX}/share/${_realname}/misc \
    --with-fbplugins=${MINGW_PREFIX}/share/${_realname}/plugins \
    --with-fbtzdata=${MINGW_PREFIX}/share/${_realname}/tzdata \
    --with-fblog=${MINGW_PREFIX}/share/${_realname} \
    --with-fbglock=${MINGW_PREFIX}/share/${_realname}

  make
}

package() {
  cd "build-${MSYSTEM}"

  local _target=Release
  if check_option "debug" "y"; then
    _target=Debug
  fi

  mkdir -p ${pkgdir}${MINGW_PREFIX}/{bin,etc/${_realname},include,lib,share/${_realname}/{doc,examples,intl,misc,plugins,tzdata}}
  cp -f gen/${_target}/firebird/bin/*                   ${pkgdir}${MINGW_PREFIX}/bin/
  cp -f gen/${_target}/firebird/lib/libfbclient.dll.a   ${pkgdir}${MINGW_PREFIX}/lib/
  cp -f gen/${_target}/firebird/lib/libib_util.dll.a    ${pkgdir}${MINGW_PREFIX}/lib/
  cp -rf gen/${_target}/firebird/include/*              ${pkgdir}${MINGW_PREFIX}/include/
  cp -f src/misc/pascal/ib_util.pas                     ${pkgdir}${MINGW_PREFIX}/include/
  cp -f src/include/gen/Firebird.pas                    ${pkgdir}${MINGW_PREFIX}/include/${_realname}/
  cp -f gen/${_target}/firebird/intl/*                  ${pkgdir}${MINGW_PREFIX}/share/${_realname}/intl/
  cp -f src/misc/intl.sql                               ${pkgdir}${MINGW_PREFIX}/share/${_realname}/misc/
  cp -rf src/misc/upgrade                               ${pkgdir}${MINGW_PREFIX}/share/${_realname}/misc/
  cp -rf doc/*                                          ${pkgdir}${MINGW_PREFIX}/share/${_realname}/doc/
  cp -rf gen/${_target}/firebird/examples/*             ${pkgdir}${MINGW_PREFIX}/share/${_realname}/examples/
  cp -rf gen/${_target}/firebird/plugins/*              ${pkgdir}${MINGW_PREFIX}/share/${_realname}/plugins/
  cp -rf gen/${_target}/firebird/tzdata/*               ${pkgdir}${MINGW_PREFIX}/share/${_realname}/tzdata/

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

  # Remove build tools
  rm -f ${pkgdir}${MINGW_PREFIX}/bin/{gpre_*,build*}.exe
  rm -f ${pkgdir}${MINGW_PREFIX}/bin/*.sh

  install -Dm644 builds/install/misc/IDPLicense.txt ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/IDPLicense.txt
  install -Dm644 builds/install/misc/IPLicense.txt ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/IPLicense.txt
}
