# Maintainer (MSYS2): Ray Donnelly <mingw.android@gmail.com>
# Maintainer (ArchLinux): Eric Bélanger <eric@archlinux.org>

pkgname=tcl
pkgver=8.6.10
pkgrel=1
pkgdesc="The Tcl scripting language"
arch=('i686' 'x86_64')
url="https://tcl.sourceforge.io/"
license=('custom')
depends=('zlib')
options=('staticlibs')
source=(https://downloads.sourceforge.net/sourceforge/tcl/tcl${pkgver}-src.tar.gz
        8.6.6-cygwin-unix.patch
        tcl-8.5.18-msysize.patch
        tcl.m4)
sha256sums=('5196dbf6638e3df8d5c87b5815c8c2b758496eb6f0e41446596c9a4e638d87ed'
            'a9abbd94cdc4354a04cd5dc5bbf599cc3a71cc84b40e08f9607f90e12d7bb3cd'
            'c690f41ae5a16ce62ade62d46016387b2d326c5ad4a3888e02f94f1aaf78083d'
            '2162a7f2a58cc5d31de1a6e3c47907b2bdf5b12aa44d9a4787ddd01c7745a60c')

prepare() {
  cd tcl${pkgver}
  # we build the tcl sqlite interface in sqlite-tcl package
  rm -rf pkgs/sqlite3*
  patch -p2 -i ${srcdir}/8.6.6-cygwin-unix.patch
  patch -p1 -i ${srcdir}/tcl-8.5.18-msysize.patch

  for _dd in itcl4.2.0 tdbc1.1.1 tdbcmysql1.1.1 tdbcodbc1.1.1 tdbcpostgres1.1.1 tdbcsqlite3-1.1.1 thread2.8.5; do
    pushd pkgs/${_dd}
      cp -f ${srcdir}/tcl.m4 tclconfig/tcl.m4
      autoreconf -fiv
    popd
  done
  cd unix
  autoreconf -fiv
}

build() {
  cd tcl${pkgver}/unix
  local BIT=
  [[ $CARCH == "x86_64" ]] && BIT="--enable-64bit"
  ./configure \
    --prefix=/usr \
    --mandir=/usr/share/man \
    --enable-threads \
    tcl_cv_strtod_buggy=no \
    tcl_cv_sys_version=MSYS_NT \
    $BIT
  make
}

# check() {
#   cd tcl${pkgver}/unix
#   make test
# }

package() {
  cd tcl${pkgver}/unix
  make INSTALL_ROOT="${pkgdir}" install

  install -Dm644 libtcl${pkgver%.*}.dll.a "${pkgdir}"/usr/lib/libtcl${pkgver%.*}.dll.a
  #mv ${pkgdir}/usr/lib/libtcl${pkgver%.*}.dll ${pkgdir}/usr/bin/

  install -Dm644 ../license.terms "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"

  # install build system macros
  install -Dm644 tcl.m4 "${pkgdir}"/usr/share/aclocal/tcl-sc.m4
  install -Dm644 ${srcdir}/tcl.m4 "${pkgdir}"/usr/share/aclocal/tcl-tea.m4

  sed -i \
    -e "s|^\(TCL_BUILD_LIB_SPEC\)='.*|\1='-Wl,/usr/lib/libtcl${pkgver%.*}.dll.a'|" \
    -e "s|^\(TCL_SRC_DIR\)='.*'|\1='/usr/include/tcl${pkgver%.*}'|" \
    -e "s|^\(TCL_BUILD_STUB_LIB_SPEC\)='.*|\1='-Wl,/usr/lib/libtclstub${pkgver%.*}.a'|" \
    -e "s|^\(TCL_BUILD_STUB_LIB_PATH\)='.*/unix|\1='/usr/lib|" \
    -e "s|^\(TCL_STUB_LIB_SPEC\)='.*|\1='-Wl,-ltclstub${pkgver%.*}'|" \
    ${pkgdir}/usr/lib/tclConfig.sh

  # Install private headers
  mkdir -p "${pkgdir}/usr/include/tcl${pkgver%.*}/"{generic,unix}
  cd ${srcdir}/tcl${pkgver}
  find generic unix -name "*.h" -exec cp -p '{}' "${pkgdir}"/usr/include/tcl${pkgver%.*}/'{}' ';'
  ( cd "${pkgdir}/usr/include"
    for header in *.h ; do
      cp -f ${header} ${pkgdir}/usr/include/tcl${pkgver%.*}/generic/
    done
  )

  # make the default tcl version
  ln -s "${pkgdir}/usr/bin/tclsh${pkgver%.*}.exe" "${pkgdir}/usr/bin/tclsh.exe"
  ln -s "${pkgdir}/usr/lib/libtcl${pkgver%.*}.dll.a" "${pkgdir}/usr/lib/libtcl.dll.a"
  ln -s "${pkgdir}/usr/lib/libtclstub${pkgver%.*}.a" "${pkgdir}/usr/lib/libtclstub.a"
  ln -s "${pkgdir}/usr/lib/tclConfig.sh" "${pkgdir}/usr/lib/tcl${pkgver%.*}/tclConfig.sh"
}
