#!/bin/sh
#
# Copyright (c) 2019-2021 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=sfml
PKGVERSION=2.5.1
PKGREVISION=1
PKGLICENSE="ZLIB"
PKGSUMMARY="Simple and Fast Multimedia Library"
PKGDOWNLOAD="https://www.sfml-dev.org/files/SFML-$PKGVERSION-sources.zip"
PKGDEPENDS="cmake:build unzip:build"
PKGOPTIONS="WINDOW GRAPHICS AUDIO NETWORK"

: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${AUDIO:=yes}
: ${GRAPHICS:=yes}
: ${NETWORK:=yes}
: ${WINDOW:=yes}

if [ "$AUDIO" = "yes" ]; then
	PKGDEPENDS="flac libogg libvorbis openal-soft $PKGDEPENDS"
	with_audio="-DSFML_BUILD_AUDIO=On"
else
	with_audio="-DSFML_BUILD_AUDIO=Off"
fi

# Both Window and Graphics component require mesa
if [ "$GRAPHICS" = "yes" ] || [ "$WINDOW" = "yes" ]; then
	PKGDEPENDS="mesa libx11 libxrandr $PKGDEPENDS"
fi

if [ "$GRAPHICS" = "yes" ]; then
	PKGDEPENDS="freetype libjpeg-turbo libpng $PKGDEPENDS"
	with_graphics="-DSFML_BUILD_GRAPHICS=On"
else
	with_graphics="-DSFML_BUILD_GRAPHICS=Off"
fi

if [ "$NETWORK" = "yes" ]; then
	with_network="-DSFML_BUILD_NETWORK=On"
else
	with_network="-DSFML_BUILD_NETWORK=Off"
fi

if [ "$WINDOW" = "yes" ]; then
	PKGDEPENDS="eudev $PKGDEPENDS"
	with_window="-DSFML_BUILD_WINDOW=On"
else
	with_window="-DSFML_BUILD_WINDOW=Off"
fi

build()
{
	rm -rf SFML-$PKGVERSION
	unzip SFML-$PKGVERSION-sources.zip
	cd SFML-$PKGVERSION

	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= \
		-DSFML_USE_SYSTEM_DEPS=On \
		-DSFML_PKGCONFIG_INSTALL_PREFIX=/lib/pkgconfig \
		$with_audio \
		$with_graphics \
		$with_network \
		$with_window
	make VERBOSE=1
	make install DESTDIR=$DESTDIR
	cd ..

	cd ..
	rm -rf SFML-$PKGVERSION
}
