
_realname=sqlitestudio
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-plugins")
pkgver=3.4.17
pkgrel=1
pkgdesc="Multi-platform SQLite database manager (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://sqlitestudio.pl/"
msys2_repository_url="https://github.com/pawelsalawa/sqlitestudio"
msys2_references=(
  "aur : sqlitestudio"
)
license=("spdx:GPL-3.0")
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-qt5-base"
         "${MINGW_PACKAGE_PREFIX}-qt5-declarative"
         "${MINGW_PACKAGE_PREFIX}-qt5-imageformats"
         "${MINGW_PACKAGE_PREFIX}-qt5-svg"
         "${MINGW_PACKAGE_PREFIX}-wineditline"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-make"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-qt5-tools"
             "${MINGW_PACKAGE_PREFIX}-tcl")
source=("https://github.com/pawelsalawa/sqlitestudio/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-destdir.patch"
        "002-wineditline-link.patch"
        "003-win32-libs.patch"
        "clang.patch"::"https://github.com/pawelsalawa/sqlitestudio/pull/5236.patch"
        "arm64.patch"::"https://github.com/pawelsalawa/sqlitestudio/pull/5237.patch")
sha256sums=('17662e1aa9bc3e70639a51a34a12ba707e52a475d2742fa8ab9ecad621ff6caf'
            '895958f1857fc9f8bfeffa88daa50e599082695d7ece974fb3dc6f1350806341'
            '6154c2f9c491a1823a2bdc79594d118ed767c1fc28946608a3a3a7ad0a31cf91'
            'db53aa419aac7722e76d813ec142cc0483f8d1a27298c241eb24512eb08b20f3'
            'd1c77713605c8599a2207e04f478e81fe22511751c0a00c5c7a45714455c3d5a'
            '625457ffbb2299f4407c04d37925cfea2a59a55352c8ab485917580086584142')

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

prepare() {
  cd ${_realname}-${pkgver}

  apply_patch_with_msg \
    001-destdir.patch \
    002-wineditline-link.patch \
    003-win32-libs.patch \
    clang.patch \
    arm64.patch
}

build() {
  mkdir -p build-${MSYSTEM}
  cd build-${MSYSTEM}

  declare -a _build_type
  if check_option "debug" "n"; then
    _build_type=release
  else
    _build_type=debug
  fi

  MSYS2_ARG_CONV_EXCL="QMAKE_CXXFLAGS=" \
  ${MINGW_PREFIX}/bin/qmake \
    QMAKE_CXXFLAGS="-DSYS_PLUGINS_DIR=${MINGW_PREFIX}/lib/sqlitestudio" \
    QMAKE_CXXFLAGS+=`pkgconf --cflags wineditline` \
    "../${_realname}-${pkgver}"/SQLiteStudio3

  # MSYS2_ARG_CONV_EXCL again for QMake called during the build
  MSYS2_ARG_CONV_EXCL="QMAKE_CXXFLAGS=" \
  ${MINGW_PREFIX}/bin/mingw32-make $_build_type 

  _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")
  mkdir plugins && cd plugins
  ${MINGW_PREFIX}/bin/qmake \
    QMAKE_CXXFLAGS+=`pkgconf --cflags python3` \
    PYTHON_VERSION=${_pyver} \
    "../../${_realname}-${pkgver}"/Plugins

  ${MINGW_PREFIX}/bin/mingw32-make $_build_type -j 1
}

package_sqlitestudio() {
  cd "${srcdir}/build-${MSYSTEM}"

  install -Dm644 bin/*.dll -t "${pkgdir}${MINGW_PREFIX}"/bin
  install -Dm644 bin/*.exe -t "${pkgdir}${MINGW_PREFIX}"/bin
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}

package_sqlitestudio-plugins() {
  pkgdesc='Official plugins for sqlitestudio (mingw-w64)'
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}"
           "${MINGW_PACKAGE_PREFIX}-python"
           "${MINGW_PACKAGE_PREFIX}-tcl")

  cd "${srcdir}/build-${MSYSTEM}"

  install -Dm644 sqlitestudio/plugins/*.dll -t "${pkgdir}${MINGW_PREFIX}"/lib/sqlitestudio
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
