#!/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=wesnoth
PKGVERSION=1.14.7
PKGREVISION=1
PKGLICENSE="GPL-2.0-only"
PKGSUMMARY="turn-based strategy game in fantasy world"
PKGDOWNLOAD="https://sourceforge.net/projects/wesnoth/files/$PKGNAME-${PKGVERSION%.*}/$PKGNAME-$PKGVERSION/$PKGNAME-$PKGVERSION.tar.bz2"
PKGDEPENDS="sdl2-mixer
            cmake:build
            fontconfig
            pango
            sdl2-ttf
            cairo
            sdl2-image
            boost
            sdl2"
PKGOPTIONS="FRIBIDI NLS NOTIFICATIONS"

: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${FRIBIDI:=yes}
: ${NLS:=yes}
: ${NOTIFICATIONS:=yes}

if [ "$FRIBIDI" = "yes" ]; then
	PKGDEPENDS="fribidi $PKGDEPENDS"
	with_fribidi="-DENABLE_FRIBIDI=On"
else
	with_fribidi="-DENABLE_FRIBIDI=Off"
fi

if [ "$NLS" = "yes" ]; then
	PKGDEPENDS="gettext $PKGDEPENDS"
	with_nls="-DENABLE_NLS=On"
else
	with_nls="-DENABLE_NLS=Off"
fi

if [ "$NOTIFICATIONS" = "yes" ]; then
	PKGDEPENDS="dbus $PKGDEPENDS"
	with_notifications="-DENABLE_NOTIFICATIONS=On"
else
	with_notifications="-DENABLE_NOTIFICATIONS=Off"
fi

rm -rf $PKGNAME-$PKGVERSION
tar xvf $PKGNAME-$PKGVERSION.tar.bz2
cd $PKGNAME-$PKGVERSION

# -DENABLE_SERVER=Off: broken with libc++ yet.
patch -p0 < ../patch-clang.patch
patch -p0 < ../patch-boost.patch
mkdir build && cd build
cmake .. \
	-DBUILD_SHARED_LIBS=On \
	-DCMAKE_BUILD_TYPE=Release \
	-DCMAKE_CXX_COMPILER="$CXX" \
	-DCMAKE_CXX_FLAGS="$CXXFLAGS" \
	-DCMAKE_C_COMPILER="$CC" \
	-DCMAKE_C_FLAGS="$CFLAGS" \
	-DCMAKE_INSTALL_PREFIX=/usr \
	-DENABLE_SERVER=Off \
	$with_fribidi \
	$with_nls \
	$with_notifications
make -j2
make install DESTDIR=$DESTDIR
cd ..

cd ..
rm -rf $PKGNAME-$PKGVERSION
