#!/bin/busybox 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=cairo
PKGVERSION=1.16.0
PKGREVISION=1
PKGLICENSE="LGPLv21 MPLv11"
PKGSUMMARY="vector graphics library"
PKGDOWNLOAD="https://cairographics.org/releases/$PKGNAME-$PKGVERSION.tar.xz"
PKGDEPENDS="pixman"
PKGOPTIONS="DRM EGL FONTCONFIG GALLIUM GLES2 GLES3 GLIB PDF PNG SVG TTF X"

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${DRM:=no}
: ${EGL:=no}
: ${FONTCONFIG:=yes}
: ${GALLIUM:=no}
: ${GLES2:=no}
: ${GLES3:=no}
: ${GLIB:=yes}
: ${PDF:=yes}
: ${PNG:=yes}
: ${SVG:=yes}
: ${TTF:=yes}
: ${X:=yes}

if [ "$DRM" = "yes" ]; then
	PKGDEPENDS="eudev $PKGDEPENDS"
	with_drm="--enable-drm"
else
	with_drm="--disable-drm"
fi

if [ "$FONTCONFIG" = "yes" ]; then
	PKGDEPENDS="fontconfig $PKGDEPENDS"
	with_fontconfig="--enable-fc"
else
	with_fontconfig="--disable-fc"
fi

# All these options require mesa.
if [ "$EGL" = "yes" ] || [ "$GALLIUM" = "yes" ] || [ "$GLES2" = "yes" ] || [ "$GLES3" = "yes" ]; then
	PKGDEPENDS="mesa $PKGDEPENDS"

	if [ "$EGL" = "yes" ]; then
		with_egl="--enable-egl"
	else
		with_egl="--disable-egl"
	fi

	if [ "$GLES2" = "yes" ]; then
		with_gles2="--enable-gles2"
	else
		with_gles2="--disable-gles2"
	fi

	if [ "$GLES3" = "yes" ]; then
		with_gles3="--enable-gles3"
	else
		with_gles3="--disable-gles3"
	fi
fi

if [ "$GLIB" = "yes" ]; then
	PKGDEPENDS="glib $PKGDEPENDS"
	with_glib="--enable-gobject"
else
	with_glib="--disable-gobject"
fi

if [ "$PDF" = "yes" ]; then
	PKGDEPENDS="graphics/poppler $PKGDEPENDS"
	with_pdf="--enable-pdf"
else
	with_pdf="--disable-pdf"
fi

if [ "$PNG" = "yes" ]; then
	PKGDEPENDS="libpng $PKGDEPENDS"
	with_png="--enable-png"
else
	with_png="--disable-png"
fi

if [ "$SVG" = "yes" ]; then
	PKGDEPENDS="librsvg $PKGDEPENDS"
	with_svg="--enable-svg"
else
	with_svg="--disable-svg"
fi

if [ "$TTF" = "yes" ]; then
	PKGDEPENDS="freetype $PKGDEPENDS"
	with_ttf="--enable-ft"
else
	with_ttf="--disable-ft"
fi

if [ "$X" = "yes" ]; then
	PKGDEPENDS="libxrender libxext libx11 libxcb"
	with_x="--enable-xlib $with_x"
	with_x="--enable-xlib-xrender $with_x"
	with_x="--enable-xcb=yes $with_x"
	with_x="--enable-xlib-xcb $with_x"
	with_x="--with-x $with_x"
else
	with_x="--enable-xlib $with_x"
	with_x="--enable-xlib-xrender $with_x"
	with_x="--enable-xcb $with_x"
	with_x="--enable-xlib-xcb $with_x"
	with_x="--with-x $with_x"
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= \
		--disable-valgrind \
		--enable-shared \
		$with_drm \
		$with_fontconfig \
		$with_egl \
		$with_gles2 \
		$with_gles3 \
		$with_glib \
		$with_pdf \
		$with_png \
		$with_svg \
		$with_ttf \
		$with_x
	make
	make install DESTDIR=$DESTDIR
	find $DESTDIR -type f -name "*.la" -delete

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