#!/bin/sh
#
# Copyright (c) 2019 David Demelier <markand@malikania.fr>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

# TODO: add LUA, SELINUX options

PKGNAME=vim
PKGVERSION=8.1.1817
PKGREVISION=1
PKGLICENSE="CUSTOM"
PKGSUMMARY="vi improved"
PKGDOWNLOAD="https://github.com/vim/vim/archive/v$PKGVERSION.tar.gz"
PKGOPTIONS="ACL GPM GTK LIBCANBERRA MULTIBYTE NLS PERL PYTHON RUBY TCL TERMINAL"
PKGDEPENDS=""

: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${ACL:=yes}
: ${GPM:=no}            # TODO: set to yes once gpm is there.
: ${GTK:=yes}
: ${LIBCANBERRA:=no}
: ${MULTIBYTE:=yes}
: ${NLS:=yes}
: ${PERL:=yes}
: ${PYTHON:=yes}
: ${RUBY:=yes}
: ${TCL:=yes}
: ${TERMINAL:=yes}

if [ "$ACL" = "yes" ]; then
	PKGDEPENDS="acl $PKGDEPENDS"
	with_acl="--enable-acl"
else
	with_acl="--disable-acl"
fi

if [ "$GPM" = "yes" ]; then
	PKGDEPENDS="system/gpm $PKGDEPENDS"
	with_gpm="--enable-gpm"
else
	with_gpm="--disable-gpm"
fi

if [ "$GTK" = "yes" ]; then
	PKGDEPENDS="gtk $PKGDEPENDS"
	with_gui="--enable-gui=gtk3"
else
	with_gui="--enable-gui=no"
fi

if [ "$LIBCANBERRA" = "yes" ]; then
	PKGDEPENDS="libcanberra $PKGDEPENDS"
	with_libcanberra="--enable-canberra"
else
	with_libcanberra="--disable-canberra"
fi

if [ "$MULTIBYTE" = "yes" ]; then
	with_multibyte="--enable-multibyte"
else
	with_multibyte="--disable-multibyte"
fi

if [ "$NLS" = "yes" ]; then
	PKGDEPENDS="gettext $PKGDEPENDS"
	with_nls="--enable-nls"
else
	with_nls="--disable-nls"
fi

if [ "$PERL" = "yes" ]; then
	PKGDEPENDS="perl $PKGDEPENDS"
	with_perl="--enable-perlinterp=dynamic"
else
	with_perl="--disable-perlinterp"
fi

if [ "$PYTHON" = "yes" ]; then
	PKGDEPENDS="python $PKGDEPENDS"
	with_python="--enable-python3interp=dynamic"
else
	with_python="--disable-python3interp"
fi

if [ "$RUBY" = "yes" ]; then
	PKGDEPENDS="ruby $PKGDEPENDS"
	with_ruby="--enable-rubyinterp=dynamic"
else
	with_ruby="--disable-rubyinterp"
fi

if [ "$TCL" = "yes" ]; then
	PKGDEPENDS="tcl $PKGDEPENDS"
	with_tcl="--enable-tclinterp=dynamic"
else
	with_tcl="--disable-tclinterp"
fi

if [ "$TERMINAL" = "yes" ]; then
	with_terminal="--enable-terminal"
else
	with_terminal="--disable-terminal"
fi

build()
{
	rm -rf $PKGNAME-$PKGVERSION
	tar xvf v$PKGVERSION.tar.gz
	cd $PKGNAME-$PKGVERSION

	CC="$CC" \
	CFLAGS="$CFLAGS" \
	LDFLAGS="$LDFLAGS" \
	LIBS="$LIBS" \
	./configure \
		--prefix= \
		--disable-selinux \
		--disable-luainterp \
		--disable-pythoninterp \
		$with_acl \
		$with_gpm \
		$with_gui \
		$with_libcanberra \
		$with_multibyte \
		$with_nls \
		$with_perl \
		$with_python \
		$with_ruby \
		$with_tcl \
		$with_terminal
	make
	make install DESTDIR=$DESTDIR

	cd ..
	rm -rf $PKGNAME-$PKGVERSION
}
