# Maintainer: Laurent Carlier <lordheavym@gmail.com>
# Contributor : sebikul <sebikul@gmail.com>
# @{about}@

@{disable dev devel gui gui-opengl gui-webview gb-db-sqlite2 gb-qt4* gb-gtk gb-gtk-* gb-pdf gb-sdl gb-sdl-sound gb-qt5-webkit gb-qt5-webview gb-desktop-gnome-keyring}@
@{merge gb-qt5 gb-qt5-x11 gb-qt5-wayland}@
@{merge gb-qt6 gb-qt6-x11 gb-qt6-wayland}@
@{merge gb-gtk3 gb-gtk3-x11 gb-gtk3-wayland}@

# Fix for error: bsdtar: Failed to set default locale
#                perl: warning: Setting locale failed.
# LANG=en_US.UTF-8 # not available, use C or POSIX
LANG=C
LC_ALL=${LANG}

pkgbase=gambas3
pkgname=('gambas3' @{package-list}@)
pkgver=@{version}@
pkgrel=1
pkgdesc="A free development environment based on a Basic interpreter."
arch=('x86_64')
url="@{website-url}@"
license=('GPL-2.0-or-later')
groups=('gambas3')
makedepends=('intltool' 'postgresql' 'libffi' 'bzip2' 'glib2' 'v4l-utils'
         'zlib' 'mesa' 'glew' 'xdg-utils' 'imlib2' 'gdk-pixbuf2' 'gtk3' 'webkit2gtk-4.1'
         'postgresql-libs' 'mariadb-libs' 'unixodbc' 'sqlite' 'librsvg'
         'curl' 'poppler-glib' 'sdl2' 'sdl2_mixer' 'sdl2_image' 'sdl2_ttf' 'libxtst'
         'pcre2' 'libx11' 'qt5-x11extras' 'qt5-svg' 'libxcursor' 'libsm' 'dbus' 'libxml2'
         'libxslt' 'gsl' 'gst-plugins-base-libs' 'alure' 'ncurses'
         'gmime3' 'libxcrypt' 'qt6-svg' 'qt6-webengine' 'git' 'bubblewrap' 'cblas' 'mongo-c-driver')
options=('!emptydirs' 'libtool')
source=("gambas-${pkgver}.tar.gz")
noextract=("gambas-${pkgver}.tar.gz")
sha512sums=('SKIP')

prepare() {
  cd ${srcdir}
  # use tar, because with bsdtar (and obs_scm) we get:
  #    bsdtar: Pathname can't be converted from UTF-8 to current locale.
  tar xf gambas-${pkgver}.tar.gz

  cd gambas-${pkgver}

  ./reconf-all
}

build() {
  cd ${srcdir}/gambas-${pkgver}

  CXXFLAGS+=" -std=gnu++20" \
  GAMBAS_CONFIG_FAILURE=1 ./configure --prefix=/usr \
    --disable-sqlite2 \
    --disable-qt4  \
    --disable-gtk \
    --disable-pdf \
    --disable-sdl \
    --disable-sdlsound \
    --disable-qt5webkit \
    --disable-qt5webview \
    --disable-keyring

  make bindir=${pkgdir}/usr/bin

  mkdir -p ${srcdir}/fakeinstall
  make -j1 XDG_UTILS='' DESTDIR=${srcdir}/fakeinstall install
}

_install() {
  local src f dir
  for src; do
    f="${src#fakeinstall/}"
    dir="${pkgdir}/${f%/*}"
    install -m755 -d "${dir}"
    mv -v "${src}" "${dir}/"
  done
}

package_gambas3() {
  depends=(@{dependencies all}@)
  pkgdesc=@{desc all}@

  cp -pv ${srcdir}/gambas-${pkgver}/README.md ${srcdir}/fakeinstall/usr/share/gambas3/

  _install fakeinstall/usr/share/gambas3/README.md
}

@{package gb.cairo
  depends=('cairo')
}@

@{package gb.compress.bzlib2
  depends=('bzip2')
}@

@{package gb.compress.zlib
  depends=('zlib')
}@

@{package gb.compress.zstd
  depends=('zstd')
}@

@{package gb.crypt
  depends=('libxcrypt')
}@

@{package gb.db.mysql
  depends=('mariadb-libs')
}@

@{package gb.db.odbc
  depends=('unixodbc')
}@

@{package gb.db.postgresql
  depends=('postgresql-libs')
}@

@{package gb.db.sqlite3
  depends=('sqlite' 'gcc-libs')
}@

@{package gb.dbus
  depends=('dbus')
}@

@{package gb.desktop
  depends=('xdg-utils')
}@

@{package gb.desktop.x11
  depends=('libsm' 'libxtst')
}@

@{package gb.gmp
  depends=('gmp')
}@

@{package gb.gsl
  depends=('gsl')
}@

@{package gb.gtk
  depends=('gtk')
  provides=('gambas3-gui')
}@

@{package gb.gtk.opengl
  provides=('gambas3-gui-opengl')
}@

@{package gb.gtk3
  depends=('gtk3')
  provides=('gambas3-gui')
}@

@{package gb.gtk3.opengl
  provides=('gambas3-gui-opengl')
}@

@{package gb.gtk3.webview
  depends=('webkit2gtk-4.1')
  provides=('gambas3-gui-webview')
}@

@{package gb.httpd
  depends=('libxcrypt')
}@

@{package gb.image.effect
  depends=('gcc-libs')
}@

@{package gb.image.imlib
  depends=('imlib2')
}@

@{package gb.image.io
  depends=('gdk-pixbuf2')
}@

@{package gb.libxml
  depends=('libxml2')
}@

@{package gb.media
  depends=('gst-plugins-base-libs')
}@

@{package gb.mime
  depends=('gmime3')
}@

@{package gb.ncurses
  depends=('ncurses')
}@

@{package gb.net.curl
  depends=('curl')
}@

@{package gb.openal
  depends=('alure')
}@

@{package gb.opengl
  depends=('glew')
}@

@{package gb.openssl
  depends=('openssl')
}@

@{package gb.pcre
  depends=('pcre2')
}@

@{package gb.poppler
  depends=('poppler-glib')
  replaces=('gambas3-gb-pdf')
}@

@{package gb.qt5
  depends=('qt5-base' 'qt5-svg' 'qt5-x11extras')
  provides=('gambas3-gui')
}@

@{package gb.qt5.opengl
  provides=('gambas3-gui-opengl')
}@

@{package gb.qt6
  depends=('qt6-base' 'qt6-svg' 'libx11')
  provides=('gambas3-gui')
}@

@{package gb.qt6.opengl
  provides=('gambas3-gui-opengl')
}@

@{package gb.qt6.webview
  depends=('qt6-webengine')
  provides=('gambas3-gui-webview')
}@

@{package gb.scanner
  depends=('sane')
}@

@{package gb.sdl2.audio
  depends=('sdl2_mixer')
}@

@{package gb.sdl2
  depends=('sdl2_ttf' 'sdl2_image' 'glew')
}@

@{package gb.v4l
  depends=('v4l-utils' 'libjpeg-turbo' 'libpng')
}@

@{package gb.xml.xslt
  depends=('libxslt')
}@

@{package gb.xml
  depends=('gcc-libs')
}@

package_gambas3-runtime() {
  depends=('libffi' 'xdg-utils')
  optdepends=('gcc: The JIT compiler needs it' 'clang: The JIT compiler can use either clang or gcc')
  pkgdesc=@{desc runtime}@
  provides=('gambas3-gb-jit')
  conflicts=('gambas3-gb-hash')
  replaces=('gambas3-gb-hash')

  _install fakeinstall/usr/bin/gb{r,x}3
  _install fakeinstall/usr/lib/gambas3/*
  _install fakeinstall/usr/share/gambas3/info/*
  _install fakeinstall/usr/share/man/man1/gb{r,x}3*

  install -dm755 ${pkgdir}/usr/share/gambas3/{mime,icons}

  install -m644 ${srcdir}/gambas-${pkgver}/main/mime/* ${pkgdir}/usr/share/gambas3/mime/
  install -m644 ${pkgdir}/usr/share/gambas3/mime/application-x-gambas3.png ${pkgdir}/usr/share/gambas3/icons/
}

package_gambas3-dev-tools() {
  depends=('gambas3-runtime')
  pkgdesc=@{desc dev-tools}@
  conflicts=('gambas3-devel')
  replaces=('gambas3-devel')

  _install fakeinstall/usr/bin/gb{a,c,i,h}3*
  _install fakeinstall/usr/share/man/man1/gb{a,c,i,h}3*
}

package_gambas3-ide() {
  depends=('gettext' @{dependencies ide}@)
  optdepends=('pngquant: Optimize the PNG files edited by the IDE' 'translate-shell: Automatic translation')
  pkgdesc=@{desc ide}@
  conflicts=('gambas3-examples')
  replaces=('gambas3-examples')

  _install fakeinstall/usr/bin/gambas3*
  _install fakeinstall/usr/share/appdata
  _install fakeinstall/usr/share/metainfo
  _install fakeinstall/usr/share/gambas3/template
  _install fakeinstall/usr/share/man/man1/gambas3.1

  install -dm755 ${pkgdir}/usr/share/applications
  install -dm755 ${pkgdir}/usr/share/pixmaps

  install -m644 ${srcdir}/gambas-${pkgver}/app/desktop/gambas3.desktop ${pkgdir}/usr/share/applications/
  install -m644 ${srcdir}/gambas-${pkgver}/app/desktop/gambas3.png ${pkgdir}/usr/share/pixmaps/
  mv "$pkgdir"/usr/share/{appdata,metainfo}
}

package_gambas3-scripter() {
  depends=('gambas3-dev-tools')
  provides=('gambas3-script')
  pkgdesc=@{desc scripter}@

  _install fakeinstall/usr/bin/gb{s,w}3*
  _install fakeinstall/usr/share/man/man1/gb{s,w}3*

  install -dm755 ${pkgdir}/usr/share/pixmaps
  install -dm755 ${pkgdir}/usr/share/mime/packages

  install -m644 ${srcdir}/gambas-${pkgver}/app/mime/*.png ${pkgdir}/usr/share/pixmaps
  install -m644 ${srcdir}/gambas-${pkgver}/app/mime/*.xml ${pkgdir}/usr/share/mime/packages
}

@{package}@

