# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgbase=zsh
pkgname=('zsh' 'zsh-doc')
pkgver=5.8
pkgrel=5
arch=('i686' 'x86_64')
url='https://www.zsh.org/'
license=('custom')
makedepends=('ncurses-devel' 'pcre-devel' 'libiconv-devel' 'libgdbm-devel')
source=("https://www.zsh.org/pub/zsh-${pkgver}.tar.xz"{,.asc}
        "https://www.zsh.org/pub/zsh-${pkgver}-doc.tar.xz"{,.asc}
        'config.guess::https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD'
        'config.sub::https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=HEAD'
        'zprofile'
        'zshenv'
        Makefile.in.patch
        add-pwd-W-option.patch
        msysize.patch
        zsh-5.0.6-1.patch
        msys-symlink-hack.patch
        make-zpty-module-work-on-Cygwin.patch)
sha256sums=('dcc4b54cc5565670a65581760261c163d720991f0d06486da61f8d839b52de27'
            'SKIP'
            '9b4e939593cb5a76564d2be2e2bfbb6242509c0c56fd9ba52f5dba6cf06fdcc4'
            'SKIP'
            'a41df3c465f3704faf331f052c4c3975f656436902d1778e81e16b5b511fb5ed'
            'd15bed73e5596867e584b20fc21b1c5a1a84203b25f50fddd543b77a3e8d1876'
            '230832038c3b8f67fdb1b284ac5f68d709cdb7f1bc752b0e60657b9b9d091045'
            '06c05faa800cb7385b606fd2b3c9aa24cfac07d1dde7f0d2f017c11ede9b2ac8'
            '971e48433ec40e0c2fb64584b5555367b4f997156aa9acbd06c67d7bdacd6c59'
            'b3f74a10a27eff498124adc96bc8c5cced7bb15e18c2603d7c3490a81e3c2e48'
            'b879d33cc60fc0c44361189eef1ee95a3bd0b8f9e8b32c81e439950483c151aa'
            '336a8e6e93b778e7aec27348619b7200e0a75e5cf14dce720afd9dd6f836ea2c'
            'de515b0d86c13c29a663b4ba0fdb338dea83f82f145cf8c4da78d30369f963e4'
            '7fae853240d14e6b2c271f820ca983149407900d7b8e7eed53b641608b6d6aec')
validpgpkeys=('F7B2754C7DE2830914661F0EA71D9A9D4BDB27B3'
              'E96646BE08C0AF0AA0F90788A5FEEE3AC7937444'
              '7CA7ECAAF06216B90F894146ACF8146CAE8CBBC4')
prepare() {
  cd ${srcdir}/${pkgname}-${pkgver}

  patch -p0 -i "${srcdir}/Makefile.in.patch"
  patch -p1 -i "${srcdir}/add-pwd-W-option.patch"
  patch -p1 -i "${srcdir}/msysize.patch"
  patch -p1 -i "${srcdir}/zsh-5.0.6-1.patch"
  patch -p1 -i "${srcdir}/msys-symlink-hack.patch"
  # https://sourceforge.net/p/zsh/code/ci/bf8ca5f019fa9fd585a7480a3d4198719369df92
  patch -p1 -i "${srcdir}/make-zpty-module-work-on-Cygwin.patch"

  autoreconf -fiv

  mv -f ${srcdir}/config.guess ${srcdir}/config.sub ${srcdir}/${pkgname}-${pkgver}/
  # Set correct keymap path
  sed -i 's#/usr/share/keymaps#/usr/share/kbd/keymaps#g' Completion/Unix/Command/_loadkeys

  # Remove unneeded and conflicting completion scripts
  for _fpath in AIX BSD Cygwin Darwin Debian Mandriva openSUSE Redhat Solaris; do
    rm -rf Completion/$_fpath
    sed "s#\s*Completion/$_fpath/\*/\*##g" -i Src/Zle/complete.mdd
  done
  rm -f  Completion/Linux/Command/_{pkgtool,rpmbuild,yast}
  rm -f  Completion/Unix/Command/_{osc,systemd}
}

build() {
  cd ${srcdir}/${pkgname}-${pkgver}

  ./configure --prefix=/usr \
    --build=${CHOST} \
    --host=${CHOST} \
    --docdir=/usr/share/doc/zsh \
    --enable-etcdir=/etc/zsh \
    --enable-zshenv=/etc/zsh/zshenv \
    --enable-zlogin=/etc/zsh/zlogin \
    --enable-zlogout=/etc/zsh/zlogout \
    --enable-zprofile=/etc/zsh/zprofile \
    --enable-zshrc=/etc/zsh/zshrc \
    --enable-maildir-support \
    --with-term-lib='ncursesw' \
    --enable-multibyte \
    --enable-function-subdirs \
    --enable-fndir=/usr/share/zsh/functions \
    --enable-scriptdir=/usr/share/zsh/scripts \
    --with-tcsetpgrp \
    --enable-pcre \
    --disable-cap \
    --enable-zsh-secure-free
  make
}

package_zsh() {
  pkgdesc='A very advanced and programmable command interpreter (shell) for UNIX'
  depends=('ncurses' 'pcre' 'libiconv' 'gdbm')
  backup=('etc/zsh/zprofile' 'etc/zsh/zshenv')
  install=zsh.install

  cd "${srcdir}/${pkgbase}-${pkgver}"
  make DESTDIR="${pkgdir}/" install
  install -D -m644 "${srcdir}/zprofile" "${pkgdir}/etc/zsh/zprofile"
  install -D -m644 "${srcdir}/zshenv" "${pkgdir}/etc/zsh/zshenv"
  install -D -m644 LICENCE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
}

package_zsh-doc() {
  pkgdesc='Info, HTML and PDF format of the ZSH documentation'
  install=zsh-doc.install

  cd "${srcdir}/${pkgbase}-${pkgver}"
  make DESTDIR="${pkgdir}/" install.info
  install -D -m644 LICENCE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
}
