#!/usr/bin/make -f

# Copyright (C) 2012-2015 Hunter Kaller
# Copyright (C) 2014-2017 Sergio Benjamim

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

export DEB_CXXFLAGS_MAINT_STRIP=-O2
export DEB_CFLAGS_MAINT_STRIP=-O2

ARCH=$(shell dpkg-architecture -qDEB_HOST_ARCH)

ifeq ($(ARCH), $(filter $(ARCH), amd64 i386))
	CONFIG_EXTRA_FLAGS=--enable-sse
else ifeq ($(ARCH),armhf)
# Doesn't build with --enable-neon (I don't know why).
	CONFIG_EXTRA_FLAGS=--disable-cg --enable-opengles --enable-floathard
else ifeq ($(ARCH),arm64)
	CONFIG_EXTRA_FLAGS=--disable-cg --enable-opengles
endif

%:
	dh $@

override_dh_auto_configure:
	sed 's/@DEB_HOST_MULTIARCH@/$(DEB_HOST_MULTIARCH)/g' \
		retroarch.cfg > debian/retroarch.cfg
	sed 's/@DEB_HOST_MULTIARCH@/$(DEB_HOST_MULTIARCH)/g' \
		debian/retroarch.dirs.in > debian/retroarch.dirs
	sed 's/@DEB_HOST_MULTIARCH@/$(DEB_HOST_MULTIARCH)/g' \
		debian/retroarch.install.in > debian/retroarch.install

	./configure --prefix=/usr --disable-vg \
	--disable-builtinflac --disable-builtinzlib --disable-builtinminiupnpc \
	--disable-builtinmbedtls --disable-builtinglslang \
	$(CONFIG_EXTRA_FLAGS)

#	exit 1

override_dh_auto_build:
	dh_auto_build

	# SoftFilters
	dh_auto_build -- -C gfx/video_filters/

	# Audio DSP plugins
	dh_auto_build -- -C libretro-common/audio/dsp_filters/

override_dh_auto_install:

override_dh_clean:
	dh_clean pkg/ctr/tools/makerom.exe pkg/ctr/tools/bannertool.exe \
	nvdaControllerClient64.dll deps/discord-rpc/thirdparty/rapidjson-1.1.0/docker/debian/Dockerfile

# Don't remove 
#	deps/mbedtls
# 	deps/SPIRV-Cross

	-rm -rf vita
	-rm -rf pkg/windows
	-rm -rf pkg/msvc-uwp
	-rm -rf pkg/debian
	-rm -rf deps/libFLAC
	-rm -rf deps/libz
	-rm -rf deps/vita*
	-rm -rf deps/glslang
	-rm -rf deps/wayland-protocols
	-rm -rf deps/miniupnpc

override_dh_auto_clean:
	touch config.mk
	dh_auto_clean
	dh_auto_clean -- -C gfx/video_filters/ clean
	dh_auto_clean -- -C libretro-common/audio/dsp_filters/ clean

override_dh_strip:
	dh_strip --no-automatic-dbgsym
