# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Maintainer: Francesco Corte <francesco.corte9001@gmail.com>

pkgname=vim
_topver=9.1
_patchlevel=1766
_versiondir="${pkgname}${_topver//./}"
pkgver=${_topver}.${_patchlevel}
pkgrel=1
pkgdesc='Vi Improved, a highly configurable, improved version of the vi text editor'
arch=('i686' 'x86_64')
license=('custom:vim')
url="https://www.vim.org"
msys2_repository_url="https://github.com/vim/vim"
msys2_references=(
  "anitya: 5092"
  "archlinux: vim"
  "cpe: cpe:/a:vim:vim"
  "cpe: cpe:/a:vim_development_group:vim"
  "gentoo: app-editors/vim"
)
depends=('ncurses' 'libiconv' 'libintl' 'libxcrypt' 'perl')
groups=('editors')
makedepends=('gawk' 'perl-devel' 'python-devel' 'ruby' 'ncurses-devel' 'autotools' 'gcc'
             'libiconv-devel' 'gettext-devel' 'libxcrypt-devel') # To satisfy python3/dyn feature #3052
source=(${pkgname}-${pkgver}.tar.gz::https://github.com/vim/vim/archive/v${pkgver}/${pkgname}-${pkgver}.tar.gz
        'dot.vimrc'
        '7.3-cygwin-mouse.patch'
        '7.3-virc.patch'
        '7.3-cygwin-python-dyn.patch'
        'pretend-cygwin-msys.patch'
        'accept-crlf.patch'
        'vim-completion')
sha256sums=('fdcd657fd8e1e3964f9af39afba8523160e1026e96fefb8e3a48fe5b822d4920'
            'edd18e0756406ee7b659e4552e444c50c3a0c1e9fb4fce2ddd770c933ea6c7f5'
            'bca6e030d50c0d2468ab5c78aa0b359eb18a88a197de8406c593458ffedde922'
            '44d7738a8f801195898eeef766ff77506c717dd5d19145ade3c1c2349d4bc4fd'
            'd60db82149b68c6e3bfc7f840191af3e5cbc8af46409ec3b752407689c44f35b'
            '5fcec194d2fcb2a624358d8ce074e8d97b873b5c2fff2118491b57a4880737d9'
            'b98b4807d6c2011836191bddce1e28b22c44649e7059af646d25187d13eea549'
            'bdca6069ef0fa995718f4b59fea85e58629259bb5a385d53e52d162d1463d4ff')

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

  iconv -f ISO-8859-1 -t UTF-8 runtime/doc/eval.txt \
    > runtime/doc/eval.tmp && mv -f runtime/doc/eval.{tmp,txt}

  patch -p2 -i ${srcdir}/7.3-cygwin-mouse.patch
  patch -p2 -i ${srcdir}/7.3-virc.patch
  patch -p2 -i ${srcdir}/7.3-cygwin-python-dyn.patch
  patch -p1 -i ${srcdir}/pretend-cygwin-msys.patch
  patch -p1 -i ${srcdir}/accept-crlf.patch

  # define the place for the global (g)vimrc file (set to /etc/vimrc)
  #sed -i 's|^.*\(#define SYS_.*VIMRC_FILE.*"\) .*$|\1|' \
  #  vim-build/src/feature.h
  #sed -i 's|^.*\(#define VIMRC_FILE.*"\) .*$|\1|' \
  #  vim-build/src/feature.h

  cd src
  autoconf
}

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

  ./configure \
    --prefix=/usr \
    --build=${CHOST} \
    --with-features=huge \
    --with-tlib=ncursesw \
    --enable-cscope \
    --enable-multibyte \
    --enable-luainterp=dynamic \
    --enable-perlinterp=dynamic \
    --enable-pythoninterp=no \
    --enable-python3interp=dynamic \
    --enable-rubyinterp=dynamic \
    --disable-tclinterp \
    --disable-gpm --disable-sysmouse \
    --disable-gui --without-x \
    CPPFLAGS="${CPPFLAGS} -I/usr/include/ncursesw" \
    --with-compiledby="<https://www.msys2.org/>"

  #make auto/osdef.h
  make -j1 VIMRCLOC=/etc VIMRUNTIMEDIR=/usr/share/vim/vim${_topver/\.}
}

check() {
  # disable tests because they seem to freeze

  cd "${srcdir}"/${pkgname}-${pkgver}

  #make test
}

package() {
  cd "${srcdir}"/${pkgname}-${pkgver}

  make install -j1 \
    VIMRCLOC=/etc VIMRUNTIMEDIR=/usr/share/vim/vim${_topver/\.} \
    STRIP=: INSTALL_DATA="cp -L" INSTALL_DATA_R="cp -Lr" \
    DESTDIR="${pkgdir}"

  #for x in ex rvi vi.exe
  #do
  #  cp -f vi.exe ${pkgdir}/usr/bin/${x}
  #done

  for y in vimdiff view rview rvim
  do
    cp -f ${pkgdir}/usr/bin/vim.exe ${pkgdir}/usr/bin/${y}
  done

  mkdir -p ${pkgdir}/usr/share/bash-completion/completions
  cp -f ${srcdir}/vim-completion ${pkgdir}/usr/share/bash-completion/completions/vim

  # delete some manpages
  find "${pkgdir}"/usr/share/man -type d -name 'man1' 2>/dev/null | \
    while read _mandir; do
    cd ${_mandir}
    rm -f ex.1 # provided by (n)vi
    rm -f evim.1    # this does not make sense if we have no GUI
  done

  # fix FS#17216
  sed -i 's|messages,/var|messages,/var/log/messages.log,/var|' \
    "${pkgdir}"/usr/share/vim/${_versiondir}/filetype.vim

  # patch filetype.vim for better handling of pacman related files
  sed -i "s/rpmsave/pacsave/;s/rpmnew/pacnew/;s/,\*\.ebuild/\0,PKGBUILD*,*.install/" \
    "${pkgdir}"/usr/share/vim/${_versiondir}/filetype.vim
  sed -i "/find the end/,+3{s/changelog_date_entry_search/changelog_date_end_entry_search/}" \
    "${pkgdir}"/usr/share/vim/${_versiondir}/ftplugin/changelog.vim

  # rc files
  install -Dm644 "${srcdir}"/dot.vimrc "${pkgdir}"/etc/skel/.vimrc

  # license
  install -Dm644 "${srcdir}"/vim-${pkgver}/runtime/doc/uganda.txt \
    "${pkgdir}"/usr/share/licenses/${pkgname}/license.txt
}
