NAME=( vim  vim-gvim )
VERSION=( 7.0  7.0  5.6 )
SOURCE=(ftp://ftp.vim.org/pub/vim/unix/vim-$VERSION.tar.bz2
	ftp://ftp.vim.org/pub/vim/extra/vim-$VERSION-lang.tar.gz
	http://dl.sf.net/ctags/ctags-${VERSION[2]}.tar.gz
	ftp://ftp.vim.org/pub/vim/patches/$VERSION/MD5 )
ROOT_REQUIRED=0
OPTIMIZE=${OPTIMIZE:-"-Os -pipe"}
BUILD=${BUILD:-1}

# This is the directory in the VIM source archive to cd into.
DIRVER=${VERSION//./}

# Patch level, see ftp://ftp.vim.org/pub/vim/patches/7.0/ for exact number:
VIM_PATCHLEVEL=158

# Generate patch URLs.
for ((I=1; I <= $VIM_PATCHLEVEL; I++)); do
	PATCH[$((I - 1))]=ftp://ftp.vim.org/pub/vim/patches/$VERSION/$VERSION.$(printf %03g "$I")
done

# Set the full version numbers:
VERSION[0]=${VERSION[0]}.$VIM_PATCHLEVEL

build() {
	# Check the MD5 sums of the patches since this cannot be done
	# automatically by tukbuild. FIXME: tukbuild 0.5 doesn't have
	# a saner mechanism to know where the downloaded files are
	# actually located.
	cd "$(dirname "${PATCH[1]}")"
	md5sum -c "${SOURCE[3]}"
	cd -

	_explode_all

	# ctags was once a part of vim,
	# but now we have to bundle it in
	cd $TMP/ctags-${VERSION[2]}
	$CONFIGURE
	make
	for J in 0 1; do
		mkdir -p ${PKG[J]}$BINDIR
		cat ctags > ${PKG[J]}$BINDIR/ctags
		chmod 755 ${PKG[J]}$BINDIR/ctags
		mkdir -p ${PKG[J]}$MANDIR/man1
		cat ctags.1 > ${PKG[J]}$MANDIR/man1/ctags.1
	done
	_doc 0 ctags-${VERSION[2]}  COPYING EXTENDING.html FAQ \
			INSTALL INSTALL.oth NEWS README

	# ViM
	cd $TMP/vim$DIRVER

	# Official patches; some of these don't apply to *NIX
	# versions so ignore errors:
	(
		set +e
		for I in "${PATCH[@]}"; do
			_patch $I -p0 --batch
		done
	)

	# build the non-X version of vim
	$CONFIGURE \
		--without-x \
		--disable-gui \
		--with-features=big
	make
	make install DESTDIR=${PKG[0]}

	# ?
	ln -sf vim $PKG/usr/bin/eview
	ln -sf vim $PKG/usr/bin/evim


	_doc 0 vim-$VERSION  README.txt
	find $PKG$DOCDIR/vim-$VERSION -type f | xargs chmod 644
	rm -rf $PKG$DOCDIR/vim-$VERSION/doc
	ln -sf $DATADIR/vim/vim$DIRVER $PKG$DOCDIR/vim-$VERSION/doc

	# FIXME: Uhh, this will be removed soon. Too dirty.
	cat << "EOF" > "${PKG[0]}/install/doinst.sh"
# If there's no vi link, take over:
if [ ! -r usr/bin/vi ]; then
  ( cd usr/bin ; ln -sf vim vi )
fi

EOF


	make clean
	$CONFIGURE \
		--with-x \
		--enable-gui=gtk2 \
		--with-features=big
	make
	make install DESTDIR=${PKG[1]}

	rm -rf ${PKG[1]}$DOCDIR
	cp -a ${PKG[0]}$DOCDIR ${PKG[1]}$DOCDIR
	cp -a ${PKG[0]}/install/* ${PKG[1]}/install

	_chfix
	_chgrp_bin


	VERSION=${VERSION}

	_desc 0 << "EOF"
vim (Vi IMproved)

Vim is an almost compatible version of the UNIX editor vi.  Many new
features have been added:  multi level undo, command line history, 
filename completion, block operations, and more.

http://www.vim.org/
EOF

	_desc 1 << "EOF"
xvim (X/GTK2 enabled version of vim)

Vim is an almost compatible version of the UNIX editor vi.  Many new
features have been added:  multi level undo, command line history,
filename completion, block operations, and more.

http://www.vim.org/
EOF
}
