pkgname=tcl
_basever=8.6
pkgver=${_basever}.16
pkgdesc='Powerful, easy-to-learn dynamic programming language'
homepage='http://tcl.sourceforge.net/'
license=(TCL)
_archive="$pkgname$pkgver"
sources=("${_archive}-src.tar.gz"
  "${_archive}-html.tar.gz"
)
urls=("https://downloads.sourceforge.net/sourceforge/tcl/${sources[0]}"
  "https://downloads.sourceforge.net/sourceforge/tcl/${sources[0]}"
)
md5sums=("eaef5d0a27239fb840f04af8ec608242"
  "750c221bcb6f8737a6791c1fbe98b684"
)


src_prepare() {
  tar -xf ${distdir}/${sources[0]} --strip-components=1
}

src_build() {
  SRCDIR=$(pwd)
  cd unix
  ./configure --prefix=/usr         \
            --mandir=/usr/share/man \
            --disable-rpath
  make

  tdbcver=1.1.10
  itcver=4.3.2
  sed -e "s|$SRCDIR/unix|/usr/lib|" \
      -e "s|$SRCDIR|/usr/include|"  \
      -i tclConfig.sh

  sed -e "s|$SRCDIR/unix/pkgs/tdbc${tdbcver}|/usr/lib/tdbc${tdbcver}|" \
      -e "s|$SRCDIR/pkgs/tdbc${tdbcver}/generic|/usr/include|"    \
      -e "s|$SRCDIR/pkgs/tdbc${tdbcver}/library|/usr/lib/tcl${_basever}|" \
      -e "s|$SRCDIR/pkgs/tdbc${tdbcver}|/usr/include|"            \
      -i pkgs/tdbc${tdbcver}/tdbcConfig.sh

  sed -e "s|$SRCDIR/unix/pkgs/itcl${itcver}|/usr/lib/itcl${itcver}|" \
      -e "s|$SRCDIR/pkgs/itcl${itcver}/generic|/usr/include|"    \
      -e "s|$SRCDIR/pkgs/itcl${itcver}|/usr/include|"            \
      -i pkgs/itcl${itcver}/itclConfig.sh

  unset SRCDIR
}

src_check() {
  cd "${srcdir}/unix"
  make test
}

src_install() {
  cd "${srcdir}/unix"
  make INSTALL_ROOT="${pkgdir}" install install-private-headers
  chmod -v u+w "${pkgdir}/usr/lib/libtcl${_basever}.so"
  ln -sfv tclsh${_basever} "${pkgdir}/usr/bin/tclsh"
  mv "${pkgdir}"/usr/share/man/man3/{Thread,Tcl_Thread}.3
  
  cd ..
  tar -xf ${distdir}/${sources[1]} --strip-components=1
  mkdir -v -p "${pkgdir}/usr/share/doc/${pkgname}-${pkgver}"
  cp -v -r  ./html/* "${pkgdir}/usr/share/doc/${pkgname}-${pkgver}"
}
