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

pkgbase=zsh
pkgname=('zsh' 'zsh-doc')
pkgver=5.9
pkgrel=5
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')

_patches=(
  # patches from https://src.fedoraproject.org/rpms/zsh/blob/rawhide/f/zsh.spec
  # Use regexp '/s/Patch\d+: /  /g' to replace prefixes

  # do not use egrep in tests to make them pass again
  0001-zsh-5.9-do-not-use-egrep-in-tests.patch
  # Upstream commit ab4d62eb975a4c4c51dd35822665050e2ddc6918
  0002-zsh-Use-int-main-in-test-c-codes.patch
  # upstream commit a84fdd7c8f77935ecce99ff2b0bdba738821ed79
  0003-zsh-fix-module-loading-problem-with-full-RELRO.patch
  # upstream commit 1b421e4978440234fb73117c8505dad1ccc68d46
  0004-zsh-enable-PCRE-locale-switching.patch
  # upstream commit b62e911341c8ec7446378b477c47da4256053dc0 and 10bdbd8b5b0b43445aff23dcd412f25cf6aa328a
  0005-zsh-port-to-pcre2.patch
  # upstream commit ecd3f9c9506c7720dc6c0833dc5d5eb00e4459c4
  0006-zsh-support-texinfo-7.0.patch
  # upstream commit 4c89849c98172c951a9def3690e8647dae76308f
  0007-zsh-configure-c99.patch
  # upstream commit d3edf318306e37d2d96c4e4ea442d10207722e94
  0008-zsh-deletefilelist-segfault.patch
  # upstream commit b70b241cc5ca88cc129ff9ba14f8af2e889b90e6
  0009-zsh-support-dnf5.patch
  # upstream commit 071e325c826a89b792056c3faf0c400b8c0c5738
  0010-zsh-fix-dnf5-completion-with-rpm-files.patch

  # Patches for cygwin
  add-pwd-W-option.patch
  msysize.patch
  zsh-5.0.6-1.patch
  msys-symlink-hack.patch
)

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

  ${_patches[@]}
)

sha256sums=('9b8d1ecedd5b5e81fbf1918e876752a7dd948e05c1a0dba10ab863842d45acd5'
            'SKIP'
            '6f7c091249575e68c177c5e8d5c3e9705660d0d3ca1647aea365fd00a0bd3e8a'
            'SKIP'
            '230832038c3b8f67fdb1b284ac5f68d709cdb7f1bc752b0e60657b9b9d091045'
            '06c05faa800cb7385b606fd2b3c9aa24cfac07d1dde7f0d2f017c11ede9b2ac8'
            '4445ca94d423bf8930c99f2f015b48f5cef6477be097beba88b5d049457c6084'
            'bdd4a2b643fe387d8acff42a3c1a3f10385f8c94577195fb3b6740d494776c27'
            '1e39a0e1b12c51fb0095adace1bbe612fa12c8f6507f8c4cbf16487d9f067438'
            '63c520a80cc4818db652651e348e73a1ca773128d58455d2985214cdd93ac1c2'
            'fc7ee5c381851eb7eeff474aa15973afc150a43f8ec5871d023e99ae09401368'
            '8e6b2b7e6a489c31b914d6c9c686668ea0d7bc0d2b5cf022581a126d6f917bd9'
            'bf12c202bc41997aae566af0b0ab11739f73c5e5ac17726be0a7ff1569972ce1'
            'fa6dc2ff9f901861c8b281fc998b83166c7887a06735716c33ee5413523c028c'
            '5904b94e6d8bb1a0358cc3c659f174bd30a3230baffac26b888423a901e8705e'
            '9c2ac06cf37e8479e271046c954f174fa587c2b3f9339a3332fd008fea5c8e13'
            'cca7c7175dda37996e1016815f67a509e385e3fb7e72146b076a14a3ca183e1f'
            'b3f74a10a27eff498124adc96bc8c5cced7bb15e18c2603d7c3490a81e3c2e48'
            '85a4cc64ad11f9ce65a0af994dc84067021c33f8b751df3fa82b0255e819d04c'
            '336a8e6e93b778e7aec27348619b7200e0a75e5cf14dce720afd9dd6f836ea2c'
            'de515b0d86c13c29a663b4ba0fdb338dea83f82f145cf8c4da78d30369f963e4')
validpgpkeys=('F7B2754C7DE2830914661F0EA71D9A9D4BDB27B3'
              'E96646BE08C0AF0AA0F90788A5FEEE3AC7937444'
              '7CA7ECAAF06216B90F894146ACF8146CAE8CBBC4') # dana <dana@dana.is>
prepare() {
  cd ${srcdir}/${pkgname}-${pkgver}

  patch -p0 -i "${srcdir}/Makefile.in.patch"

  for patch_filename in "${_patches[@]}"; do
    echo "Applying patch: '${patch_filename}'"
    patch --binary -Np1 -i "${srcdir}/${patch_filename}"
  done

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