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

pkgname=vim
_topver=9.2
_patchlevel=1054
_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')
url='https://www.vim.org'
msys2_repository_url='https://github.com/vim/vim'
msys2_references=(
  'anitya: 5092'
  'archlinux: vim'
  'aur: vim-cli-git'
  'cpe: cpe:/a:vim:vim'
  'cpe: cpe:/a:vim_development_group:vim'
  'gentoo: app-editors/vim'
)
license=('spdx:Vim')
groups=('editors')
depends=(
  'libiconv'
  'libintl'
  'libxcrypt'
  'ncurses'
)
optdepends=(
  'python: Python language support'
  'ruby: Ruby language support'
  'lua: Lua language support'
  'perl: Perl language support'
)
makedepends=(
  'autotools'
  'gawk'
  'gcc'
  'gettext-devel'  # To satisfy python3/dyn feature #3052
  'libiconv-devel' # To satisfy python3/dyn feature #3052
  'libxcrypt-devel'
  'ncurses-devel'
  'perl-devel'
  'python-devel'
  'ruby'
)
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=('b0ad7bba02f4de81cf3ecbf23ef1ef3f69e9e9029203d6c9f7858e18d0669470'
            'edd18e0756406ee7b659e4552e444c50c3a0c1e9fb4fce2ddd770c933ea6c7f5'
            'bca6e030d50c0d2468ab5c78aa0b359eb18a88a197de8406c593458ffedde922'
            '44d7738a8f801195898eeef766ff77506c717dd5d19145ade3c1c2349d4bc4fd'
            'd60db82149b68c6e3bfc7f840191af3e5cbc8af46409ec3b752407689c44f35b'
            '5fcec194d2fcb2a624358d8ce074e8d97b873b5c2fff2118491b57a4880737d9'
            '0872ab2c21cf258b1eebfc550457a51c71dbc0fd402b15122d2529e00cdb43c7'
            'bdca6069ef0fa995718f4b59fea85e58629259bb5a385d53e52d162d1463d4ff')

prepare() {
  cd "${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 "${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 "${pkgname}-${pkgver}"

  #make test
}

package() {
  cd "${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"
}
