pkgname="zsh"
pkgver="5.9"
pkgdesc="A very advanced and programmable command interpreter (shell) for UNIX"
homepage="https://www.zsh.org/"
license=("custom")
sources=("${pkgname}-${pkgver}.tar.xz")
#urls=("https://www.zsh.org/pub/${sources[0]}")
urls=("https://downloads.sourceforge.net/project/zsh/zsh/${pkgver}/${sources[0]}")
md5sums=("182e37ca3fe3fa6a44f69ad462c5c30e")


src_prepare() {
  tar -xf ${distdir}/${sources[0]} --strip-components=1
  sed -e 's/set_from_init_file/texinfo_&/' -i Doc/Makefile.in
  sed -e 's/^main/int &/'      \
      -e 's/exit(/return(/'    \
      -i aczsh.m4 configure.ac &&

  sed -e 's/test = /&(char**)/' \
      -i configure.ac           &&

  autoconf

  sed 's|/etc/z|/etc/zsh/z|g' -i Doc/*.*
}

src_build() {
  ./configure --prefix=/usr              \
              --sysconfdir=/etc/zsh      \
              --enable-etcdir=/etc/zsh   \
              --enable-cap               \
              --enable-gdbm              \
              --with-term-lib='ncursesw' \
              --enable-multibyte        &&
  make                                  &&

  makeinfo Doc/zsh.texi --html      -o Doc/html&&
  makeinfo Doc/zsh.texi --plaintext -o zsh.txt &&
  makeinfo Doc/zsh.texi --html --no-split --no-headers -o zsh.html
}

src_check(){
  HOME="${srcdir}" make check
}

src_install(){
  make DESTDIR="${pkgdir}/" install                                                     &&
  install -D -m644 LICENCE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"            &&
  make DESTDIR="${pkgdir}/" infodir="/usr/share/info" install.info             &&
  make DESTDIR="${pkgdir}/" htmldir="/usr/share/doc/zsh-5.9/html" install.html &&
  install -v -m644 zsh.{html,txt} Etc/FAQ "${pkgdir}/usr/share/doc/zsh-5.9"
}

src_postinstall() {
  leaf_add_shell /bin/zsh
}
src_postremove() {
  leaf_remove_shell /bin/zsh
}
