#!/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=sdl2-image
PKGVERSION=2.0.5
PKGREVISION=1
PKGLICENSE="zlib"
PKGSUMMARY="image addon for SDL2"
PKGDOWNLOAD="https://www.libsdl.org/projects/SDL_image/release/SDL2_image-$PKGVERSION.tar.gz"
PKGDEPENDS="sdl2"
PKGOPTIONS="BMP
            GIF
            JPEG
            LBM
            PCX
            PNG
            PNM
            SVG
            TGA
            TIFF
            WEBP
            XCF
            XPM
            XV"

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${BMP:=yes}
: ${GIF:=yes}
: ${JPEG:=yes}
: ${LBM:=yes}
: ${PCX:=yes}
: ${PNG:=yes}
: ${PNM:=yes}
: ${SVG:=yes}
: ${TGA:=yes}
: ${TIFF:=yes}
: ${WEBP:=yes}
: ${XCF:=yes}
: ${XPM:=yes}
: ${XV:=yes}

if [ "$BMP" = "yes" ]; then
	with_bmp="--enable-bmp"
else
	with_bmp="--disable-bmp"
fi

if [ "$GIF" = "yes" ]; then
	with_gif="--enable-gif"
else
	with_gif="--disable-gif"
fi

if [ "$JPEG" = "yes" ]; then
	PKGDEPENDS="libjpeg-turbo $PKGDEPENDS"
	with_jpeg="--enable-jpg"
else
	with_jpeg="--disable-jpg"
fi

if [ "$LBM" = "yes" ]; then
	with_lbm="--enable-lbm"
else
	with_lbm="--disable-lbm"
fi

if [ "$PCX" = "yes" ]; then
	with_pcx="--enable-pcx"
else
	with_pcx="--disable-pcx"
fi

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

if [ "$PNM" = "yes" ]; then
	with_pnm="--enable-pnm"
else
	with_pnm="--disable-pnm"
fi

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

if [ "$TGA" = "yes" ]; then
	with_tga="--enable-tga"
else
	with_tga="--disable-tga"
fi

if [ "$TIFF" = "yes" ]; then
	PKGDEPENDS="libtiff $PKGDEPENDS"
	with_tiff="--enable-tif"
else
	with_tiff="--disable-tif"
fi

if [ "$XCF" = "yes" ]; then
	with_xcf="--enable-xcf"
else
	with_xcf="--disable-xcf"
fi

if [ "$XPM" = "yes" ]; then
	with_xpm="--enable-xpm"
else
	with_xpm="--disable-xpm"
fi

if [ "$XV" = "yes" ]; then
	with_xv="--enable-xv"
else
	with_xv="--disable-xv"
fi

if [ "$WEBP" = "yes" ]; then
	PKGDEPENDS="libwebp $PKGDEPENDS"
	with_webp="--enable-webp"
else
	with_webp="--disable-webp"
fi

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

	CC="$CC" \
	CFLAGS="$CFLAGS" \
	LDFLAGS="$LDFLAGS" \
	LIBS="$LIBS" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix= \
		$with_bmp \
		$with_gif \
		$with_jpeg \
		$with_lbm \
		$with_pcx \
		$with_png \
		$with_pnm \
		$with_svg \
		$with_tga \
		$with_tiff \
		$with_webp \
		$with_xcf \
		$with_xpm \
		$with_xv
	make
	make install DESTDIR=$DESTDIR
	find $DESTDIR -type f -name "*.la" -delete

	cd ..
	rm -rf SDL2_image-$PKGVERSION
}
