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

# TODO: add OSS options

PKGNAME=retroarch
PKGVERSION=1.7.7
PKGREVISION=1
PKGLICENSE="GPLv3"
PKGSUMMARY="official frontend for libretro cores"
PKGDOWNLOAD="https://github.com/libretro/RetroArch/releases/download/v$PKGVERSION/RetroArch-$PKGVERSION.tar.xz"
PKGOPTIONS="ALSA
            DBUS
            FFMPEG
            FLAC
            FREETYPE
            JACK
            KMS
            OPENAL
            OPENGL
            PULSEAUDIO
            QT
            SDL2
            UDEV
            USB
            WAYLAND
            X
            ZLIB"
PKGDEPENDS=""
PKGPROTECT="etc/retroarch.cfg"

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${LDFLAGS:=}
: ${ALSA:=yes}
: ${DBUS:=no}
: ${FFMPEG:=yes}
: ${FLAC:=yes}
: ${FREETYPE:=yes}
: ${JACK:=yes}
: ${KMS:=yes}
: ${OPENAL:=yes}
: ${OPENGL:=yes}
: ${PULSEAUDIO:=yes}
: ${QT:=yes}
: ${SDL2:=yes}
: ${UDEV:=yes}
: ${USB:=yes}
: ${WAYLAND:=yes}
: ${X:=yes}
: ${ZLIB:=yes}

if [ "$ALSA" = "yes" ]; then
	PKGDEPENDS="audio/alsa-lib"
	with_alsa="--enable-alsa"
else
	with_alsa="--disable-alsa"
fi

if [ "$DBUS" = "yes" ]; then
	PKGDEPENDS="network/dbus $PKGDEPENDS"
	with_dbus="--enable-dbus"
else
	with_dbus="--disable-dbus"
fi

if [ "$FFMPEG" = "yes" ]; then
	PKGDEPENDS="multimedia/ffmpeg $PKGDEPENDS"
	with_ffmpeg="--enable-ffmpeg"
else
	with_ffmpeg="--disable-ffmpeg"
fi

if [ "$FLAC" = "yes" ]; then
	PKGDEPENDS="audio/flac $PKGDEPENDS"
	with_flac="--enable-flac"
else
	with_flac="--disable-flac"
fi

if [ "$FREETYPE" = "yes" ]; then
	PKGDEPENDS="fonts/freetype $PKGDEPENDS"
	with_freetype="--enable-freetype"
else
	with_freetype="--disable-freetype"
fi

if [ "$JACK" = "yes" ]; then
	PKGDEPENDS="audio/jack $PKGDEPENDS"
	with_jack="--enable-jack"
else
	with_jack="--disable-jack"
fi

if [ "$KMS" = "yes" ]; then
	PKGDEPENDS="graphics/mesa graphics/libdrm $PKGDEPENDS"
	with_kms="--enable-kms"
else
	with_kms="--disable-kms"
fi

if [ "$OPENAL" = "yes" ]; then
	PKGDEPENDS="audio/openal-soft $PKGDEPENDS"
	with_openal="--enable-al"
else
	with_openal="--disable-al"
fi

if [ "$OPENGL" = "yes" ]; then
	if [ "$KMS" != "yes" ]; then
		PKGDEPENDS="graphics/mesa $PKGDEPENDS"
	fi

	with_opengl="--enable-opengl"
else
	with_opengl="--disable-opengl"
fi

if [ "$PULSEAUDIO" = "yes" ]; then
	PKGDEPENDS="audio/pulseaudio $PKGDEPENDS"
	with_pulseaudio="--enable-pulse"
else
	with_pulseaudio="--disable-pulse"
fi

if [ "$QT" = "yes" ];then
	PKGDEPENDS="qt/qtbase $PKGDEPENDS"
	with_qt="--enable-qt"
else
	with_qt="--disable-qt"
fi

if [ "$SDL2" = "yes" ]; then
	PKGDEPENDS="multimedia/sdl2 $PKGDEPENDS"
	with_sdl2="--enable-sdl2"
else
	with_sdl2="--disable-sdl2"
fi

if [ "$UDEV" = "yes" ]; then
	PKGDEPENDS="core/eudev $PKGDEPENDS"
	with_udev="--enable-udev"
else
	with_udev="--disable-udev"
fi

if [ "$USB" = "yes" ]; then
	PKGDEPENDS="lib/libusb $PKGDEPENDS"
	with_usb="--enable-libusb"
else
	with_usb="--disable-libusb"
fi

if [ "$WAYLAND" = "yes" ]; then
	PKGDEPENDS="graphics/wayland graphics/wayland-protocols $PKGDEPENDS"
	with_wayland="--enable-wayland"
else
	with_wayland="--disable-wayland"
fi

if [ "$X" = "yes" ]; then
	PKGDEPENDS="x11/libxcb
	            x11/libxext
	            x11/libxinerama
	            x11/libxrandr
	            x11/libxshm
	            x11/libxxf86vm $PKGDEPENDS"
	with_x="--enable-x11 --enable-xinerama --enable-xrandr"
else
	with_x="--disable-x11 --disable-xinerama --disable-xrandr"
fi

if [ "$ZLIB" = "yes" ]; then
	PKGDEPENDS="compression/zlib $PKGDEPENDS"
	with_zlib="--enable-zlib"
else
	with_zlib="--disable-zlib"
fi

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

	# Disable core updater because they are not binary compatible and
	# shipped with Vanilla Linux.
	sed -i -e "s|# menu_show_core_updater.*|menu_show_core_updater = false|" retroarch.cfg

	# Adapt core and their info directories respectively.
	sed -i \
		-e "s|# libretro_directory.*|libretro_directory = /lib/libretro|" \
		-e "s|# libretro_info_path.*|libretro_info_path = /share/libretro/info|" retroarch.cfg

	# Adapt assets directory.
	sed -i -e "s|# assets_directory.*|assets_directory = /share/retroarch/assets|" retroarch.cfg

	# --prefix=/.: does not accept an empty prefix.
	CC="$CC" \
	CFLAGS="$CFLAGS" \
	LDFLAGS="$LDFLAGS" \
	PKG_CONF_PATH=/bin/pkgconf \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/. \
		--disable-sdl \
		--disable-systemd \
		$with_alsa \
		$with_dbus \
		$with_ffmpeg \
		$with_freetype \
		$with_jack \
		$with_kms \
		$with_openal \
		$with_opengl \
		$with_pulseaudio \
		$with_qt \
		$with_sdl2 \
		$with_udev \
		$with_usb \
		$with_wayland \
		$with_x \
		$with_zlib
	make CC="$CC" CXX="$CXX"
	make install DESTDIR=$DESTDIR

	cd ..
	rm -rf RetroArch-$PKGVERSION
}
