# Maintainer: Lucio Perusini <lucioperusini@gmail.com>

_realname=sqlitebrowser
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=3.13.1
pkgrel=1
pkgdesc='SQLite Database browser is a Qt GUI editor for SQLite databases (mingw-w64)'
url='https://sqlitebrowser.org/'
msys2_repository_url="https://github.com/sqlitebrowser/sqlitebrowser"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
license=('spdx:MPL-2.0 OR GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-qt5-base"
         "${MINGW_PACKAGE_PREFIX}-sqlcipher"
         "${MINGW_PACKAGE_PREFIX}-sqlite3")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-qt5-tools")
source=("https://github.com/sqlitebrowser/sqlitebrowser/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-sqlitebrowser-install.patch"
        "0002-sqlitebrowser-linking.patch")
sha256sums=('1044ba67d649cabc459eb887c016f70d6a404ad651390ab33bf7859dc7f1b67b'
            'f42dca177ec7a51ca847ee8f89430acd415e4be8e94f5db84acd62cb066f7b81'
            '5e2851cc51dde429d8383675e3b8e178ef5a123b54413f7bda4307061c01eaf2')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  patch -Np1 -i "${srcdir}"/0001-sqlitebrowser-install.patch
  patch -Np1 -i "${srcdir}"/0002-sqlitebrowser-linking.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  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=" \
  ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${extra_config[@]}" \
    -DBUILD_STABLE_VERSION=ON \
    -Dsqlcipher=ON \
    -Wno-dev \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
}
