# Template file for 'vim'
pkgname=vim
_distver=7.4
_patchver=763
version=${_distver}.${_patchver}
revision=1
hostmakedepends="mercurial pkg-config"
makedepends="ncurses-devel acl-devel libXt-devel gtk+-devel perl
 ruby-devel python-devel python3.4-devel lua-devel"
depends="vim-common>=$version"
short_desc="Vim editor (vi clone)"
maintainer="Juan RP <xtraeme@voidlinux.eu>"
homepage="http://www.vim.org"
license="GPL-2"

subpackages="vim-common vim-x11 gvim"
# XXX vim-huge cannot be cross compiled for now.
if [ -z "$CROSS_BUILD" ]; then
	subpackages+=" vim-huge vim-huge-python3"
fi

do_fetch() {
	local url="http://code.google.com/p/vim/"
	hg clone -u v${_distver/./-}-${_patchver} ${url} ${pkgname}-${version}
}

pre_configure() {
	for f in ${subpackages/vim-common/}; do
		mkdir -p /tmp/${f}
		cp -a $wrksrc/* /tmp/${f}
		mv /tmp/${f} $wrksrc/${f}
	done
}

do_configure() {
	args="--enable-cscope --enable-multibyte --with-tlib=ncursesw
		--with-ex-name=vim-ex --with-view-name=vim-view"

	nohuge_args="--disable-perlinterp --disable-pythoninterp
		--disable-rubyinterp --disable-workshop
		--disable-netbeans --disable-sniff --disable-gpm
		--disable-hangulinput"

	if [ "$CROSS_BUILD" ]; then
		configure_args+="
			--host=${XBPS_CROSS_TRIPLET}
			vim_cv_toupper_broken=no vim_cv_terminfo=yes
			vim_cv_tty_group=world vim_cv_getcwd_broken=no
			vim_cv_stat_ignores_slash=yes
			vim_cv_memmove_handles_overlap=yes
			ac_cv_small_wchar_t=no"
	fi

	# Normal version.
	cd $wrksrc
	./configure ${configure_args} ${args} ${nohuge_args} --disable-xim --enable-gui=no --with-x=no

	# X11 version.
	cd $wrksrc/vim-x11
	./configure ${configure_args} ${args} ${nohuge_args} --enable-gui=no --with-x

	# GTK+ version.
	cd $wrksrc/gvim
	./configure ${configure_args} ${args} ${nohuge_args} --enable-gui=gtk2 --with-x --with-vim-name=gvim

	# Huge versions
	if [ -z "$CROSS_BUILD" ]; then
		cd $wrksrc/vim-huge
		./configure ${configure_args} ${args} --with-x=no --enable-gui=no \
			--with-features=huge --enable-perlinterp --enable-pythoninterp \
			--enable-rubyinterp --enable-luainterp

		cd $wrksrc/vim-huge-python3
		./configure ${configure_args} ${args} --with-x=no --enable-gui=no \
			--with-features=huge --enable-perlinterp --disable-pythoninterp --enable-python3interp \
			--enable-rubyinterp --enable-luainterp
	fi
}

do_build() {
	cd $wrksrc
	make ${makejobs}

	for f in ${subpackages/vim-common/}; do
		cd $wrksrc/$f
		make ${makejobs}
	done

}

do_install() {
	make DESTDIR=$DESTDIR install
	# Create vim-ex symlink.
	cd ${DESTDIR}/usr/bin
	ln -sf vim vim-ex
	rm -f ${DESTDIR}/usr/bin/{ex,view}

	find ${DESTDIR}/usr/share/man -type f -name evim.1 -delete
}

vim-common_package() {
	short_desc+=" - common files"
	noarch=yes
	replaces="vim<=7.2_2"
	pkg_install() {
		vmove usr/share/vim
		vinstall ${FILESDIR}/vimrc 644 usr/share/vim
	}
}
vim-x11_package() {
	depends="vim-common-${version}_${revision}"
	provides="vim-${version}_${revision}"
	replaces="vim>=0"
	short_desc+=" - X11 version"
	pkg_install() {
		cd $wrksrc/vim-x11
		make DESTDIR=$PKGDESTDIR install
		rm -f $PKGDESTDIR/usr/bin/{ex,view}
		rm -rf $PKGDESTDIR/usr/share/vim
	}
}
gvim_package() {
	depends="vim-common-${version}_${revision} desktop-file-utils hicolor-icon-theme"
	short_desc+=" - GTK+ GUI"
	pkg_install() {
		cd $wrksrc/gvim
		sed -i "s|/locolor|/hicolor|g" src/Makefile
		for d in 16 32 48; do
			install -d ${PKGDESTDIR}/usr/share/icons/hicolor/${d}x${d}/apps
		done
		make -C src DESTDIR=${PKGDESTDIR} installvimbin install-icons
		vinstall ${FILESDIR}/gvim.desktop 644 usr/share/applications
	}
}
vim-huge_package() {
	depends="python vim-common-${version}_${revision}"
	short_desc+=" - Huge (all features enabled, using Python 2)"
	provides="vim-${version}_${revision}"
	replaces="vim>=0"
	pkg_install() {
		cd $wrksrc/vim-huge
		make DESTDIR=$PKGDESTDIR install
		rm -f $PKGDESTDIR/usr/bin/{ex,view}
		rm -rf $PKGDESTDIR/usr/share/vim
	}
}
vim-huge-python3_package() {
	depends="python3.4 vim-common-${version}_${revision}"
	short_desc+=" - Huge (all features enabled, using Python 3)"
	provides="vim-${version}_${revision}"
	replaces="vim>=0"
	pkg_install() {
		cd $wrksrc/vim-huge-python3
		make DESTDIR=$PKGDESTDIR install
		rm -f $PKGDESTDIR/usr/bin/{ex,view}
		rm -rf $PKGDESTDIR/usr/share/vim
	}
}
