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

pkgbase=tcl
pkgname=('tcl' 'tcl-devel' 'tcl-doc')
pkgver=8.6.12
pkgrel=3
pkgdesc="The Tcl scripting language"
arch=('i686' 'x86_64')
url="https://tcl.sourceforge.io/"
msys2_references=(
  "anitya: 4941"
  "cpe: cpe:/a:tcl:tcl"
)
license=('custom')
depends=('zlib')
makedepends=('autotools' 'gcc')
options=('staticlibs')
source=(https://downloads.sourceforge.net/sourceforge/tcl/tcl${pkgver}-src.tar.gz
        tcl-8.6.12-1.src.patch)
sha256sums=('26c995dd0f167e48b11961d891ee555f680c175f7173ff8cb829f4ebcde4c1a6'
            '322c7e17c63249fc5f6b0d89b1c77fa67406aa91f44efa4964be3f75bb271568')

prepare() {
  cd tcl${pkgver}
  # we build the tcl sqlite interface in sqlite-tcl package
  rm -rf pkgs/sqlite3*

  # taken from cygwin
  patch -p2 -i ${srcdir}/tcl-8.6.12-1.src.patch

  for _dd in itcl4.2.2 tdbc1.1.3 tdbcmysql1.1.3 tdbcodbc1.1.3 tdbcpostgres1.1.3 tdbcsqlite3-1.1.3 thread2.8.7; do
    pushd pkgs/${_dd}
      autoreconf -fiv
    popd
  done
  cd unix
  autoreconf -fiv
}

build() {
  cd tcl${pkgver}/unix

  export MSYSTEM=CYGWIN
  local BIT=
  [[ $CARCH == "x86_64" ]] && BIT="--enable-64bit"
  ./configure \
    --build="${CHOST}" \
    --prefix=/usr \
    --mandir=/usr/share/man \
    --enable-threads \
    tcl_cv_strtod_buggy=no \
    tcl_cv_sys_version=CYGWIN_NT \
    $BIT
  make
}

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

package_tcl() {
  cd tcl${pkgver}/unix
  make INSTALL_ROOT="${pkgdir}" install install-private-headers

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

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

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

  # remove buildroot traces
  _tclver=8.6
  sed -e "s#${srcdir}/tcl${pkgver}/unix#/usr/lib#" \
      -e "s#${srcdir}/tcl${pkgver}#/usr/include#" \
      -e "s#'{/usr/lib} '#'/usr/lib/tcl$_tclver'#" \
      -i "${pkgdir}/usr/lib/tclConfig.sh"

  tdbcver=tdbc1.1.3
  sed -e "s#${srcdir}/tcl${pkgver}/unix/pkgs/$tdbcver#/usr/lib/$tdbcver#" \
      -e "s#${srcdir}/tcl${pkgver}/pkgs/$tdbcver/generic#/usr/include#" \
      -e "s#${srcdir}/tcl${pkgver}/pkgs/$tdbcver/library#/usr/lib/tcl${pkgver%.*}#" \
      -e "s#${srcdir}/tcl${pkgver}/pkgs/$tdbcver#/usr/include#" \
      -i "${pkgdir}/usr/lib/$tdbcver/tdbcConfig.sh"

  itclver=itcl4.2.2
  sed -e "s#${srcdir}/tcl${pkgver}/unix/pkgs/$itclver#/usr/lib/$itclver#" \
      -e "s#${srcdir}/tcl${pkgver}/pkgs/$itclver/generic#/usr/include#" \
      -e "s#${srcdir}/tcl${pkgver}/pkgs/$itclver#/usr/include#" \
      -i "${pkgdir}/usr/lib/$itclver/itclConfig.sh"

  # 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"

  # split package (from Alpine Linux)
  cd "$pkgdir"
  for i in usr/include usr/lib/pkgconfig \
   $(find usr/lib -name '*.a' -o -name '*.dll.a') \
   $(find . -name '*.c' -o -name '*Config.sh'); do
    if [ -e "$pkgdir/$i" ] || [ -L "$pkgdir/$i" ]; then
      mkdir -p ${srcdir}/tcl-devel/${i%/*}
      mv "$pkgdir/$i" ${srcdir}/tcl-devel/${i%/*}/
      rmdir "$pkgdir/${i%/*}" 2>/dev/null || :
    fi
  done

  for i in usr/share/man/man3 ; do
    if [ -e "$pkgdir/$i" ] || [ -L "$pkgdir/$i" ]; then
      mkdir -p ${srcdir}/tcl-doc/${i%/*}
      mv "$pkgdir/$i" ${srcdir}/tcl-doc/${i%/*}/
      rmdir "$pkgdir/${i%/*}" 2>/dev/null || :
    fi
  done
}

package_tcl-devel() {
  depends=("tcl=${pkgver}")
  groups=('development')
  pkgdesc+=" (development files)"

  mv "$srcdir"/tcl-devel/* "$pkgdir"
}

package_tcl-doc() {
  depends=()
  pkgdesc+=" (documentation)"

  mv "$srcdir"/tcl-doc/* "$pkgdir"
}
