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

pkgbase=zsh
pkgname=('zsh' 'zsh-doc')
pkgver=5.9.2
pkgrel=1
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=('spdx:Zsh')
makedepends=('ncurses-devel' 'pcre2-devel' 'libiconv-devel' 'autotools' 'gcc' 'groff' 'texinfo')

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

  # Patches for cygwin
  add-pwd-W-option.patch
  msysize.patch
  zsh-5.0.6-1.patch
  msys-symlink-hack.patch
  fix-no-mmap-fallback.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=('36fa734374b44783582cec09bcd67822e2f992c779ec1624ab5596df078d2f81'
            'SKIP'
            '020ee644be1749507b282e619cdcd95c56ff36144e79b7a3c245458aacd9458f'
            'SKIP'
            '230832038c3b8f67fdb1b284ac5f68d709cdb7f1bc752b0e60657b9b9d091045'
            '06c05faa800cb7385b606fd2b3c9aa24cfac07d1dde7f0d2f017c11ede9b2ac8'
            '4445ca94d423bf8930c99f2f015b48f5cef6477be097beba88b5d049457c6084'
            'b3f74a10a27eff498124adc96bc8c5cced7bb15e18c2603d7c3490a81e3c2e48'
            '85a4cc64ad11f9ce65a0af994dc84067021c33f8b751df3fa82b0255e819d04c'
            '336a8e6e93b778e7aec27348619b7200e0a75e5cf14dce720afd9dd6f836ea2c'
            'de515b0d86c13c29a663b4ba0fdb338dea83f82f145cf8c4da78d30369f963e4'
            '24cc021cafb508d972057ad354773465603103622c312870e9f090009f6e4b19')
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 \
    --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')
  backup=('etc/zsh/zprofile' 'etc/zsh/zshenv')
  install=zsh.install

  cd "${srcdir}/${pkgbase}-${pkgver}"
  make DESTDIR="${pkgdir}" install
  install -Dm644 "${srcdir}/zprofile" "${pkgdir}/etc/zsh/zprofile"
  install -Dm644 "${srcdir}/zshenv" "${pkgdir}/etc/zsh/zshenv"
  install -Dm644 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 -Dm644 LICENCE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
}
