# Contributor Ricky Wu <rickleaf.wu@hotmail.com>
# Contributor Unai Martinez-Corral <unai.martinezcorral@ehu.eus>

# NOTE: This package installs the GTK2 build by default, but provides GTK3 versions of three binaries too:
# - gtkwave-gtk3.exe
# - rtlbrowse-gtk3.exe
# - twinwave-gtk3.exe
# However, mingw-w64-*-gtk3 is not a dependency. Users willing to use the GTK3 binaries need to install it explicitly.

_realname=gtkwave
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.3.110
pkgrel=1
pkgdesc='GtkWave, a fully featured GTK+ based wave viewer which reads VCD, GHW, LXT, LXT2, VZT and FST files (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url='https://gtkwave.sourceforge.io/'
license=('GPL')
groups=("${MINGW_PACKAGE_PREFIX}-eda")
depends=(
  "${MINGW_PACKAGE_PREFIX}-adwaita-icon-theme"
  "${MINGW_PACKAGE_PREFIX}-gtk2"
  "${MINGW_PACKAGE_PREFIX}-gtk-update-icon-cache"
  "${MINGW_PACKAGE_PREFIX}-tcl"
  "${MINGW_PACKAGE_PREFIX}-tcllib"
  "${MINGW_PACKAGE_PREFIX}-tk"
  "${MINGW_PACKAGE_PREFIX}-tklib"
)
makedepends=(
  'perlxml'
  'intltool'
  "${MINGW_PACKAGE_PREFIX}-gcc"
  "${MINGW_PACKAGE_PREFIX}-gtk3"
)
source=("https://gtkwave.sourceforge.io/${_realname}-gtk3-${pkgver}.tar.gz")
sha256sums=('2aedb92156bca2ddd25b138378cace6b1058e57d36d939ef0f79769006b759aa')

prepare() {
  cd "${srcdir}/${_realname}-gtk3-${pkgver}"
  ./autogen.sh
}

_build() {
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-$1"
  cd "${srcdir}/build-${MINGW_CHOST}-$1"

  [ "$1" = 'gtk3' ] && _enableGTK3='--enable-gtk3' || _enableGTK3=''

  ../"${_realname}-gtk3-${pkgver}"/configure \
    --prefix="${MINGW_PREFIX}" \
    --host="${MINGW_CHOST}" \
    --target="${MINGW_CHOST}" \
    --build="${MINGW_CHOST}" \
    --with-tcl="${MINGW_PREFIX}"/lib \
    --with-tk="${MINGW_PREFIX}"/lib \
    --disable-mime-update $_enableGTK3
  make
  cd ..
}

build() {
  _build gtk2
  _build gtk3
}

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

  _bin="${pkgdir}/${MINGW_PREFIX}"/bin
  for item in gtkwave rtlbrowse twinwave; do
    mv "${_bin}/${item}".exe "${_bin}/${item}"-gtk3.exe
  done

  cd "${srcdir}/build-${MINGW_CHOST}"-gtk2
  make DESTDIR="${pkgdir}" install
}
