#!/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.
#

PKGNAME=nano
PKGVERSION=4.3
PKGREVISION=1
PKGLICENSE="GPLv3"
PKGSUMMARY="GNU nano editor"
PKGDOWNLOAD="https://www.nano-editor.org/dist/v4/$PKGNAME-$PKGVERSION.tar.xz"
PKGDEPENDS="ncurses"
PKGOPTIONS="BROWSER
            COLOR
            COMMENT
            EXTRA
            HELP
            HISTORIES
            JUSTIFY
            LIBMAGIC
            LINENUMBERS
            MOUSE
            MULTIBUFFER
            NANORC
            NLS
            SPELLER
            TABCOMP
            UTF8
            WORDCOMP
            WRAPPING"

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${BROWSER:=yes}
: ${COLOR:=yes}
: ${COMMENT:=yes}
: ${EXTRA:=yes}
: ${HELP:=yes}
: ${HISTORIES:=yes}
: ${JUSTIFY:=yes}
: ${LIBMAGIC:=yes}
: ${LINENUMBERS:=yes}
: ${MOUSE:=yes}
: ${MULTIBUFFER:=yes}
: ${NANORC:=yes}
: ${NLS:=yes}
: ${SPELLER:=yes}
: ${TABCOMP:=yes}
: ${UTF8:=yes}
: ${WORDCOMP:=yes}
: ${WRAPPING:=yes}

if [ "$BROWSER" = "yes" ]; then
	with_browser="--enable-browser"
else
	with_browser="--disable-browser"
fi

if [ "$COLOR" = "yes" ]; then
	with_color="--enable-color"
else
	with_color="--disable-color"
fi

if [ "$COMMENT" = "yes" ]; then
	with_comment="--enable-comment"
else
	with_comment="--disable-comment"
fi

if [ "$EXTRA" = "yes" ]; then
	with_extra="--enable-extra"
else
	with_extra="--disable-extra"
fi

if [ "$HELP" = "yes" ]; then
	with_help="--enable-help"
else
	with_help="--disable-help"
fi

if [ "$HISTORIES" = "yes" ]; then
	with_histories="--enable-histories"
else
	with_histories="--disable-histories"
fi

if [ "$JUSTIFY" = "yes" ]; then
	with_justify="--enable-justify"
else
	with_justify="--disable-justify"
fi

if [ "$LIBMAGIC" = "yes" ]; then
	PKGDEPENDS="zlib file $PKGDEPENDS"
	with_libmagic="--enable-libmagic"
else
	with_libmagic="--disable-libmagic"
fi

if [ "$LINENUMBERS" = "yes" ]; then
	with_linenumbers="--enable-linenumbers"
else
	with_linenumbers="--disable-linenumbers"
fi

if [ "$MOUSE" = "yes" ]; then
	with_mouse="--enable-mouse"
else
	with_mouse="--disable-mouse"
fi

if [ "$MULTIBUFFER" = "yes" ]; then
	with_multibuffer="--enable-multibuffer"
else
	with_multibuffer="--disable-multibuffer"
fi

if [ "$NANORC" = "yes" ]; then
	with_nanorc="--enable-nanorc"
else
	with_nanorc="--disable-nanorc"
fi

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

if [ "$SPELLER" = "yes" ]; then
	with_speller="--enable-speller"
else
	with_speller="--disable-speller"
fi

if [ "$TABCOMP" = "yes" ]; then
	with_tabcomp="--enable-tabcomp"
else
	with_tabcomp="--disable-tabcomp"
fi

if [ "$UTF8" = "yes" ]; then
	with_utf8="--enable-utf8"
else
	with_utf8="--disable-utf8"
fi

if [ "$WORDCOMP" = "yes" ]; then
	with_wordcomp="--enable-wordcomp"
else
	with_wordcomp="--disable-wordcomp"
fi

if [ "$WRAPPING" = "yes" ]; then
	with_wrapping="--enable-wrapping"
else
	with_wrapping="--disable-wrapping"
fi

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

	CC="$CC" \
	CFLAGS="$CFLAGS" \
	LDFLAGS="$LDFLAGS" \
	LIBS="$LIBS" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix= \
		$with_browser \
		$with_color \
		$with_comment \
		$with_extra \
		$with_help \
		$with_histories \
		$with_justify \
		$with_libmagic \
		$with_linenumbers \
		$with_mouse \
		$with_multibuffer \
		$with_nanorc \
		$with_nls \
		$with_speller \
		$with_tabcomp \
		$with_utf8 \
		$with_wordcomp \
		$with_wrapping
	make
	make install DESTDIR=$DESTDIR

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