# 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.118
pkgrel=1
pkgdesc='A wave viewer which reads LXT, LXT2, VZT, GHW and VCD/EVCD files (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://gtkwave.sourceforge.net/'
license=('spdx:GPL-2.0-or-later')
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=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-autotools"
  "${MINGW_PACKAGE_PREFIX}-gtk3"
  "${MINGW_PACKAGE_PREFIX}-gperf"
  'perlxml'
  'intltool'
)
source=("https://downloads.sourceforge.net/project/${_realname}/${_realname}-gtk3-${pkgver}/${_realname}-gtk3-${pkgver}.tar.gz")
sha256sums=('0f4330c028a2ab3d2f4d4ceeaf6db6925db010c4b6fd52d11022d0e5dea0486a')

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

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

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

  "${srcdir}/${_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
}

build() {
  _build gtk2
  _build gtk3
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"-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-${MSYSTEM}"-gtk2
  make DESTDIR="${pkgdir}" install
  install -Dm644 "${srcdir}/${_realname}-gtk3-${pkgver}/LICENSE.TXT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.TXT"
}
