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

pkgname=vim
_topver=8.2
_patchlevel=3182
_versiondir="${pkgname}${_topver//./}"
pkgver=${_topver}.${_patchlevel}
pkgrel=1
arch=('i686' 'x86_64')
license=('custom:vim')
url="http://www.vim.org"
depends=('ncurses')
groups=('editors')
makedepends=('gawk' 'perl-devel' 'python-devel' 'ruby' 'libiconv' 'ncurses-devel')
source=(${pkgname}-${pkgver}.tar.gz::https://github.com/vim/vim/archive/v${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=('b73e285a56e634f7fe7a17f088b182fc3ac186bd37e34df0b912df6d19615495'
            'edd18e0756406ee7b659e4552e444c50c3a0c1e9fb4fce2ddd770c933ea6c7f5'
            'bca6e030d50c0d2468ab5c78aa0b359eb18a88a197de8406c593458ffedde922'
            '44d7738a8f801195898eeef766ff77506c717dd5d19145ade3c1c2349d4bc4fd'
            'd60db82149b68c6e3bfc7f840191af3e5cbc8af46409ec3b752407689c44f35b'
            '5fcec194d2fcb2a624358d8ce074e8d97b873b5c2fff2118491b57a4880737d9'
            '6b72d475e45050d49f13d06ff7eb0a56e846005b8047a6a1d7fee75c4cb38d4e'
            '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 -j1 VIMRCLOC=/etc DESTDIR="${pkgdir}" install

  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

  # rgb.txt file
  install -Dm644 "${srcdir}"/vim-${pkgver}/runtime/rgb.txt \
    "${pkgdir}"/usr/share/vim/${_versiondir}/rgb.txt

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