# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=tk
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_basever=8.6.11
_patch=.1
pkgver=${_basever}${_patch}
pkgrel=2
pkgdesc="A windowing toolkit for use with tcl (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://tcl.sourceforge.io/"
license=("custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-tcl>=${_basever}")
options=('staticlibs') # '!strip' 'debug')
source=("https://downloads.sourceforge.net/sourceforge/tcl/${_realname}${pkgver}-src.tar.gz"
        003-fix-forbidden-colon-in-paths.mingw.patch
        004-install-man.mingw.patch
        006-prevent-tclStubsPtr-segfault.patch
        008-dont-link-shared-with--static-libgcc.patch
        010-win-non-x86.patch
        011-win-arm64.patch)
sha256sums=('006cab171beeca6a968b6d617588538176f27be232a2b334a0e96173e89909be'
            '5347487af0e736dbb51425b22ed308840faf75b44c070623baa55f78dac3d053'
            '8516749dd73c084ece7b9df6d1ba5708e652e8ba39cad59120c7f909f61747f0'
            '0029fde6782ce7635e9957412d3e36a7e76d304399d57d64a42818f93e705621'
            'a16406e8519ab681bba0915e35b23a7f91cf978934b0a4ebdc7e949e87f0c877'
            '2892e65d77b87c8336591d1fc10e35ad94ce73cf8c0a8f1968ec7b8bccce0b1a'
            '51dc29f822ad7af721d1dd6d13700856c21da30c70c9c2aa688f69a950965113')

prepare() {
  cd "${srcdir}/${_realname}${_basever}"
  patch -p1 -i "${srcdir}"/003-fix-forbidden-colon-in-paths.mingw.patch
  patch -p1 -i "${srcdir}"/004-install-man.mingw.patch
  patch -p1 -i "${srcdir}"/006-prevent-tclStubsPtr-segfault.patch
  patch -p1 -i "${srcdir}"/008-dont-link-shared-with--static-libgcc.patch
  patch -p1 -i "${srcdir}"/010-win-non-x86.patch
  patch -p1 -i "${srcdir}"/011-win-arm64.patch

  cd win && autoreconf -fi
}

build() {
   local extra_config=
  if check_option "debug" "y"; then
    extra_config="--enable-symbols"
  fi
  local enable64bit=
  [[ "${MINGW_CHOST}" = 'x86_64-w64-mingw32' ]] && enable64bit='--enable-64bit'
  [[ "${MINGW_CHOST}" = 'aarch64-w64-mingw32' ]] && enable64bit='--enable-64bit=arm64'

  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST}
  cd "${srcdir}/build-${MINGW_CHOST}"
  ${srcdir}/tk${_basever}/win/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --with-tcl=${MINGW_PREFIX}/lib \
    ${extra_config} \
    ${enable64bit}

  make
}

package() {
  local debug_suffix=
  if check_option "debug" "y"; then
    debug_suffix="g"
  fi
  cd "${srcdir}/build-${MINGW_CHOST}"
  make install INSTALL_ROOT="${pkgdir}"
  cp "${pkgdir}${MINGW_PREFIX}/bin/wish86${debug_suffix}.exe" "${pkgdir}${MINGW_PREFIX}/bin/wish.exe"
  find "${pkgdir}${MINGW_PREFIX}" -name '*.def' -o -name '*.exp' | xargs -rtl1 rm
  find "${pkgdir}${MINGW_PREFIX}" -name '*.sh' -o -name '*.dll' -o -name '*.exe' | xargs -rtl1 chmod 755

  ln -s "${pkgdir}${MINGW_PREFIX}/lib/libtk86${debug_suffix}.dll.a" "${pkgdir}${MINGW_PREFIX}/lib/libtk.dll.a"
  mkdir -p "${pkgdir}${MINGW_PREFIX}/include/tk${_basever%.*}/tk-private/"{generic/ttk,win}

  cd ${srcdir}/tk${_basever}
  find generic win -name "*.h" -exec cp -p '{}' "${pkgdir}${MINGW_PREFIX}"/include/tk${_basever%.*}/tk-private/'{}' ';'
  ( cd "${pkgdir}${MINGW_PREFIX}/include"
    for header in *.h ; do
      cp -f ${header} ${pkgdir}${MINGW_PREFIX}/include/tk${_basever%.*}/tk-private/generic/
    done
  )
  chmod a-x "${pkgdir}${MINGW_PREFIX}/lib/"*/pkgIndex.tcl

  local _libver=${_basever%.*}
  _libver=${_libver//./}
  sed -i \
    -e "s|^\(TK_BUILD_LIB_SPEC\)='.*|\1='-Wl,${MINGW_PREFIX}/lib/libtk${_libver}.dll.a'|" \
    -e "s|^\(TK_SRC_DIR\)='.*'|\1='${MINGW_PREFIX}/include/tk${_basever%.*}/tk-private'|" \
    -e "s|^\(TK_BUILD_STUB_LIB_SPEC\)='.*|\1='-Wl,${MINGW_PREFIX}/lib/libtkstub${_libver}.a'|" \
    -e "s|^\(TK_BUILD_STUB_LIB_PATH\)='.*|\1='${MINGW_PREFIX}/lib/libtkstub${_libver}.a'|" \
    -e "s|^\(TK_STUB_LIB_SPEC\)='.*|\1='-L${MINGW_PREFIX}/lib -ltkstub${_libver}'|" \
    ${pkgdir}${MINGW_PREFIX}/lib/tkConfig.sh

  # Add missing entry to tkConfig.sh
  echo "# String to pass to the compiler so that an extension can" >> ${pkgdir}${MINGW_PREFIX}/lib/tkConfig.sh
  echo "# find installed Tcl headers." >> ${pkgdir}${MINGW_PREFIX}/lib/tkConfig.sh
  echo "TK_INCLUDE_SPEC='-I${MINGW_PREFIX}/include/tk${_basever%.*}'" >> ${pkgdir}${MINGW_PREFIX}/lib/tkConfig.sh

  rm "${pkgdir}${MINGW_PREFIX}/lib/tk${_basever%.*}/tkAppInit.c"
}
