# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>
# Contributor: Brisingr Aerowing <ztgreve@live.com>

_realname=sqlite3
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_sqlite_year=2021
_amalgamationver=3360000
_docver=${_amalgamationver}
pkgver=3.36.0
pkgrel=1
pkgdesc="A C library that implements an SQL database engine (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=(PublicDomain)
url="https://www.sqlite.org"
provides=(${MINGW_PACKAGE_PREFIX}-sqlite=${pkgver}
          ${MINGW_PACKAGE_PREFIX}-sqlite-analyzer=${pkgver}
          ${MINGW_PACKAGE_PREFIX}-sqlite-docs=${pkgver})
conflicts=(${MINGW_PACKAGE_PREFIX}-sqlite-analyzer)
replaces=(${MINGW_PACKAGE_PREFIX}-sqlite-analyzer)
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-readline"
         "${MINGW_PACKAGE_PREFIX}-tcl")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
source=(https://www.sqlite.org/${_sqlite_year}/sqlite-src-${_amalgamationver}.zip
        https://www.sqlite.org/${_sqlite_year}/sqlite-doc-${_docver}.zip
        Makefile.ext.in
        README.md.in
        LICENSE)
sha256sums=('25a3b9d08066b3a9003f06a96b2a8d1348994c29cc912535401154501d875324'
            '79f03bf2b4afd845b66e7d07c7fa08c46a30c2484a4a5f8508db653c9da5d32d'
            '4a8a87289253529cf04c916e5743c8727a5506b5185bc9bd4070b42037e8ae20'
            '5ca42f1f92abfb61bacc9ff60f5836cc56e2ce2af52264f918cb06c3d566d562'
            '0b76663a90e034f3d7f2af5bfada4cedec5ebc275361899eccc5c18e6f01ff1f')
options=('!strip' 'staticlibs' '!buildflags')

sqlite_tools="sqlite3_analyzer.exe dbhash.exe sqldiff.exe"

prepare() {
  cd "${srcdir}/sqlite-src-${_amalgamationver}"
}

build() {
  test -d ${srcdir}/build-${_amalgamationver}-${MINGW_CHOST} && \
    rm -rf ${srcdir}/build-${_amalgamationver}-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${_amalgamationver}-${MINGW_CHOST} && \
    cd ${srcdir}/build-${_amalgamationver}-${MINGW_CHOST}

  export lt_cv_deplibs_check_method='pass_all'

  SQLITE_OPTIONS="\
            -DSQLITE_ENABLE_COLUMN_METADATA=1 \
            -DSQLITE_USE_MALLOC_H=1 \
            -DSQLITE_USE_MSIZE=1 \
            -DSQLITE_DISABLE_DIRSYNC=1 \
            -DSQLITE_ENABLE_DBSTAT_VTAB=1 \
            -DSQLITE_SOUNDEX=1 \
            -DSQLITE_ENABLE_MATH_FUNCTIONS=1 \
  "

  CFLAGS+=" -fexceptions -fno-strict-aliasing ${SQLITE_OPTIONS}"

  ../sqlite-src-${_amalgamationver}/configure -C \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --enable-threadsafe \
    --disable-editline \
    --enable-readline \
    --enable-all \
    --enable-session \
    --with-readline-inc=-I${MINGW_PREFIX}/include \
    --with-tcl=${MINGW_PREFIX}/lib \
    TCLLIBDIR=${MINGW_PREFIX}/lib/sqlite${pkgver}

  make

  # build additional tools
  make $sqlite_tools

  # build extensions
  ./config.status --file=ext/misc/Makefile:../Makefile.ext.in
  make -C ext/misc
}

check() {
  cd "${srcdir}/build-${_amalgamationver}-${MINGW_CHOST}"
  # This test run lasts very loooong ... despite the target name
  make quicktest || true
}

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

  for t in $sqlite_tools; do
    install -Dm755 .libs/$t "${pkgdir}${MINGW_PREFIX}/bin/$t"
  done
  mv ${pkgdir}${MINGW_PREFIX}/lib/bin/*.* ${pkgdir}${MINGW_PREFIX}/lib/sqlite${pkgver}/
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/bin

  # Install extensions
  make -C ext/misc DESTDIR="${pkgdir}" install
  cat "${srcdir}/README.md.in" | sed        \
    -e "s|@MINGW_PREFIX@|${MINGW_PREFIX}|g" \
    -e "s|@MSYSTEM@|${MSYSTEM}|g;"          \
    -e "s|@VERSION@|${pkgver}|g;"           \
    > "${pkgdir}${MINGW_PREFIX}/share/sqlite/extensions/README.md"

  # Install docs
  cd ${srcdir}/sqlite-doc-${_docver}
  mkdir -p ${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}
  cp -R *  ${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/

  install -Dm644 "${srcdir}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
