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

pkgbase=zsh
pkgname=('zsh' 'zsh-doc')
pkgver=5.9
pkgrel=4
arch=('i686' 'x86_64')
url='https://www.zsh.org/'
msys2_repository_url='https://sourceforge.net/p/zsh/code'
msys2_references=(
  "anitya: 5307"
  "cpe: cpe:2.3:a:zsh:zsh"
  "cpe: cpe:2.3:a:zsh_project:zsh"
)
license=('custom')
makedepends=('ncurses-devel' 'pcre2-devel' 'libiconv-devel' 'libgdbm-devel' 'autotools' 'gcc' 'groff')
source=("https://www.zsh.org/pub/zsh-${pkgver}.tar.xz"{,.asc}
        "https://www.zsh.org/pub/zsh-${pkgver}-doc.tar.xz"{,.asc}
        'zprofile'
        'zshenv'
        Makefile.in.patch
        add-pwd-W-option.patch
        msysize.patch
        zsh-5.0.6-1.patch
        msys-symlink-hack.patch
        0004-pcre2.patch)
sha256sums=('9b8d1ecedd5b5e81fbf1918e876752a7dd948e05c1a0dba10ab863842d45acd5'
            'SKIP'
            '6f7c091249575e68c177c5e8d5c3e9705660d0d3ca1647aea365fd00a0bd3e8a'
            'SKIP'
            '230832038c3b8f67fdb1b284ac5f68d709cdb7f1bc752b0e60657b9b9d091045'
            '06c05faa800cb7385b606fd2b3c9aa24cfac07d1dde7f0d2f017c11ede9b2ac8'
            '971e48433ec40e0c2fb64584b5555367b4f997156aa9acbd06c67d7bdacd6c59'
            'b3f74a10a27eff498124adc96bc8c5cced7bb15e18c2603d7c3490a81e3c2e48'
            '85a4cc64ad11f9ce65a0af994dc84067021c33f8b751df3fa82b0255e819d04c'
            '336a8e6e93b778e7aec27348619b7200e0a75e5cf14dce720afd9dd6f836ea2c'
            'de515b0d86c13c29a663b4ba0fdb338dea83f82f145cf8c4da78d30369f963e4'
            'ea68214d5be0514aa1b19e93fc9f44de878b9428e920227230a8fa3a75b0bd18')
validpgpkeys=('F7B2754C7DE2830914661F0EA71D9A9D4BDB27B3'
              'E96646BE08C0AF0AA0F90788A5FEEE3AC7937444'
              '7CA7ECAAF06216B90F894146ACF8146CAE8CBBC4') # dana <dana@dana.is>
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"

  # pcre2 backport from Arch:
  # https://gitlab.archlinux.org/archlinux/packaging/packages/zsh/-/blob/main/0004-pcre2.patch
  patch -p1 -i "${srcdir}/0004-pcre2.patch"

  autoreconf -fiv

  # 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' 'libpcre2_8' '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'

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