# Maintainer: Kaiting Chen <kaiting.chen@kiwilight.com>

# Add Ruby, Tcl, and Python support
# Use /usr/share/vim for everything
# Install color schemes
# Install Marty Grenfell scripts

pkgdesc='Vi Improved, a highly configurable, improved version of the vi text editor'
url="http://www.vim.org"

pkgname='vim-full'
_version='7.2'
_patch='284'
pkgver=${_version}.${_patch}
pkgrel='1'
arch=('i686')
license=('custom:vim')
replaces=('vim=7.2' 'vi' 'nvi')
provides=('vim=7.2' 'vi' 'nvi')
depends=('gpm' 'coreutils' 'perl' 'ruby' 'python'
  'vim-surround' 'vim-matchit' 'vim-vcscommand'
  'vim-project' 'vim-rails'
  'vim-supertab' 'vim-taglist' 'vimpager')
makedepends=('wget' 'sed' 'grep' 'gettext' 'perl')

backup=(etc/vimrc)
install=${pkgname}.install

md5sums=('f0901284b338e448bfd79ccca0041254'
         'd8884786979e0e520c112faf2e176f05'
         '35e04482f07c57221c9a751aaa3b8dac'
         '29c9c0125552aaffb23df8084de288fc'
         '3bd594e85610d8d723084f5544d4b3fd'
         'a359ce8f9bf5ccf238a53df5dd94725a'
         '7b9ce214633c0dbb3a81eaed0fdf82d1'
         'd70e5c834e6915371eb45238eb3c8945'
         'efbd5986e691ce8c876fb86e8f5961ea'
         'f4639ddafc68f49395c25fb4caf0d6f7'
         '2d0fd8cb3ee4ec7052c23d0e1060eca0'
         '10353a61aadc3f276692d0e17db1478e'
         '083622bede99d6500670e4527a453154')

_mirror='http://www.gtlib.gatech.edu/pub'
_source1='ftp://ftp.vim.org/pub/vim'
_source2="${_mirror}/gentoo/distfiles"
_source3='http://www.vim.org/scripts'
source=("${_source1}/unix/vim-${_version}.tar.bz2"
  "${_source1}/extra/vim-${_version}-lang.tar.gz"
  "${_source1}/extra/vim-${_version}-extra.tar.gz"
  "${_source2}/colorschemes-20090118.tar.bz2"
  "${_source3}/download_script.php?src_id=10775"
  "${_source3}/download_script.php?src_id=11500"
  "${_source3}/download_script.php?src_id=11172"
  "${_source3}/download_script.php?src_id=10318"
  LICENSE.nt
  LICENSE.csp vimrc default.vim vim-ruby19.patch)

build() {
  _srcdir="vim$(echo ${_version} | sed 's/\.//')"
  cd ${srcdir}/${_srcdir}

  # apply all of the available patches on the site
  _md5sums="${_source1}/patches/${_version}/MD5SUMS"
  curl ${_md5sums} | sed -e '/.gz$/d' > MD5SUMS
  _srcurl="${_source1}/patches/${_version}/"
  # download the patches in parallel
  cat MD5SUMS | awk '{ print $2 }' | sed -e \
    "s|^|${_srcurl}|" | xargs -P 0 -r -n 1 wget -nv
  md5sum -c MD5SUMS > /dev/null || return 1
  for file in $(cat MD5SUMS | awk '{ print $2 }')
    do patch -p0 < $file > vim.full.patch.log; done
  patch -p1 < ../vim-ruby19.patch

  # remove aap files not needed for our scheme
  find -name \*.aap -exec rm {} \;

  # apply these settings for the vimrc file
  for conf in 'VIMRC_FILE' 'GVIMRC_FILE' \
    'SYS_VIMRC_FILE' 'SYS_GVIMRC_FILE'; do
    sed -ie "/#define $conf/s:/\* ::" src/feature.h
    sed -ie "/#define $conf/s: \*/::" src/feature.h
  done

  ./configure --prefix=/usr \
    --localstatedir=/var/lib/vim \
    --mandir=/usr/share/man \
    --with-features=huge \
    --enable-gpm --enable-acl \
    --with-x=yes --enable-gui=gtk2 \
    --enable-multibyte \
    --enable-cscope \
    --enable-perlinterp \
    --enable-rubyinterp \
    --enable-tclinterp \
    --enable-pythoninterp \
    --with-compiledby="Kaiting Chen" \
    --with-global-runtime=/usr/share/vim

  make || return 1
  make VIMRCLOC=/etc DESTDIR=${pkgdir} install

  cd ${pkgdir}/usr/bin

  # create the archlinux symlinks
  relink='vim rview rvim vimdiff'
  mv vim vim-normal
  for i in $relink; do
    ln -sf vim-normal $i; done

  # update the runtime files
  cd ${pkgdir}/usr/share/vim/${_srcdir}
  rsync -rlDEzc --delete \
    --exclude='dos' \
    --exclude='doc/tags' \
    ftp.nluug.nl::Vim/runtime/ .

  find -name \*.aap -exec rm {} \;

  # merge the runtime directory
  cd ${pkgdir}/usr/share/vim
  mv ${_srcdir}/* .
  rmdir $_srcdir
  ln -s . $_srcdir
  ln -s . vimfiles

  # install the vim color sampler pack from scripts
  _file='download_script.php?src_id=10775'
  cd ${srcdir}
  mv $_file ColorSamplerPack.zip
  unzip -qqo ColorSamplerPack.zip

  cd colors; for i in *; do
    [ ! -e "${pkgdir}/usr/share/vim/colors/$i" ] &&
    install -m644 $i ${pkgdir}/usr/share/vim/colors
  done

  # install the gentoo color schemes
  cd "${srcdir}/colorschemes-20090118"
  for i in patches/*; do
    patch -p0 < $i; done
  cd colors; for i in *; do
    [ ! -e "${pkgdir}/usr/share/vim/colors/$i" ] &&
    install -m644 $i ${pkgdir}/usr/share/vim/colors
  done

  # install the Marty Grenfell plugins
  _file='download_script.php?src_id=11172'
  mv "${srcdir}/${_file}" ${srcdir}/syntastic.zip
  cd ${pkgdir}/usr/share/vim
  unzip ${srcdir}/syntastic.zip
  _file='download_script.php?src_id=11500'
  mv "${srcdir}/${_file}" ${srcdir}/nerdtree.zip
  cd ${pkgdir}/usr/share/vim
  unzip ${srcdir}/nerdtree.zip
  _file='download_script.php?src_id=10318'
  mv "${srcdir}/${_file}" ${srcdir}/commenter.zip
  cd ${pkgdir}/usr/share/vim
  unzip ${srcdir}/commenter.zip

  # move some directories around
  _rx1='s:syntax_checkers:syntastic:g'
  _rx2='s:nerdtree_plugin:nerdtree:g'
  mv nerdtree_plugin nerdtree
  mv syntax_checkers syntastic
  sed -i -e "$_rx1" plugin/syntastic.vim
  sed -i -e "$_rx2" plugin/NERD_tree.vim

  # install licenses and vimrc files and such
  install -D -m644 ${srcdir}/LICENSE.csp \
    ${pkgdir}/usr/share/licenses/vim/LICENSE.csp
  install -D -m644 ${srcdir}/LICENSE.nt \
    ${pkgdir}/usr/share/licenses/vim/LICENSE.nt
  cd ${srcdir}/${_versiondir}
  install -dm755 ${pkgdir}/usr/share/licenses/vim
  ln -s ../../vim/doc/uganda.txt \
    ${pkgdir}/usr/share/licenses/vim/license.txt
  install -Dm644 ${srcdir}/default.vim \
    ${pkgdir}/usr/share/vim/default.vim
  install -Dm644 ${srcdir}/vimrc ${pkgdir}/etc/vimrc
}