include ../../depends/Makefile.include

BUILD_TYPE:=@CMAKE_BUILD_TYPE@
BUILD_TYPE_LC:=$(shell echo $(BUILD_TYPE) | tr A-Z a-z)

OBJS = libplist.so libshairplay.so \
  libass.so \
  libbluray.so libsmbclient.so

PLATFORM_OBJS =
EXCLUDED_ADDONS =

CMAKE_SOURCE_DIR = $(shell cd $(CURDIR)/../../..; pwd)
APP_PACKAGE_DIR = $(subst .,/,@APP_PACKAGE@)
COPYDIRS = system addons media
GCC_VERSION=$(shell $(CC) -dumpversion)
ZIP=zip

SIGN_STORE=~/.android/debug.keystore
SIGN_STOREPASS=android
SIGN_KEY=androiddebugkey
SIGN_KEYPASS=

ifneq ($(strip $(SIGN_KEYPASS)),)
SIGN_KEYPASS_OPT=-keypass $(SIGN_KEYPASS)
endif

X86OVERRIDES=XBMC_OVERRIDE_HOST=i686-android-linux XBMC_OVERRIDE_TOOLCHAIN=$(XBMC_X86_TOOLCHAIN)
ARMOVERRIDES=XBMC_OVERRIDE_HOST=arm-linux-androideabi XBMC_OVERRIDE_TOOLCHAIN=$(XBMC_ARM_TOOLCHAIN)

#this fixes a android ndk fuckup where the paths to
#prebuilt stuff follow different name schemes for
#arm and x86
ifeq ($(findstring i686,$(CPU)),i686)
  ARCH=x86
  CPU=x86
endif
ifeq ($(findstring arm64,$(CPU)),arm64)
  ARCH=arm64
  CPU=arm64-v8a
else ifeq ($(findstring arm,$(CPU)),arm)
  ARCH=arm
  CPU=armeabi-v7a
endif

# libc++
STLLIB=$(TOOLCHAIN)/$(HOST)/lib/libc++_shared.so
# https://android.googlesource.com/platform/ndk/+/fe0fe5dc32682d1811d32492c3055becb512c147/build/tools/make_standalone_toolchain.py#308
ifeq ($(findstring armeabi-v7a,$(CPU)),armeabi-v7a)
  STLLIB=$(TOOLCHAIN)/$(HOST)/lib/armv7-a/libc++_shared.so
endif

#older ndk x86
GDBPATH=$(NDKROOT)/toolchains/$(ARCH)-$(GCC_VERSION)/prebuilt/gdbserver

ifneq ($(shell test -f $(GDBPATH) && echo 1),1)
#older ndk arm
GDBPATH=$(NDKROOT)/toolchains/$(HOST)-$(GCC_VERSION)/prebuilt/gdbserver
endif

ifneq ($(shell test -f $(GDBPATH) && echo 1),1)
#vanilla ndk >= 8
GDBPATH=$(NDKROOT)/prebuilt/android-$(ARCH)/gdbserver/gdbserver
endif

ifeq ($(ARCH),aarch64)
GDBPATH=$(NDKROOT)/prebuilt/android-arm64/gdbserver/gdbserver
endif

# check for java 1.8
JAVA8=$(shell java -version 2>&1 | grep -q '1.8.[0-9]' && echo yes)
ifeq ($(JAVA8),yes)
  JAVAC_EXTRA_ARGS = -source 1.7 -target 1.7
endif

SRCLIBS = $(addprefix $(PREFIX)/lib/,$(OBJS))
DSTLIBS = $(CPU)/lib/lib@APP_NAME_LC@.so $(addprefix $(CPU)/lib/,$(OBJS)) $(addprefix $(CPU)/lib/,$(PLATFORM_OBJS))
libs= $(DSTLIBS)

all: apk

apk: apk-clean sharedapk package apk-sign

obb: apk-clean sharedobb

apk-unsigned: apk-clean sharedapk package

apk-obb: apk-clean sharedobb package apk-sign

apk-obb-unsigned: apk-clean sharedobb package

apk-noobb: apk-clean package apk-sign

xbmc/assets:
	mkdir -p xbmc/assets

shared:
	mkdir -p assets
	cp -rfp $(PREFIX)/share/@APP_NAME_LC@/* ./assets
	cp -rfp $(DEPENDS_PATH)/share/kodi/* ./assets || true
	find `pwd`/assets/ -depth -name ".git" -exec rm -rf {} \;
	find `pwd`/assets/ -name "*.so" -exec rm {} \;
	find `pwd`/assets/addons/skin.*/media/* -depth -not -iname "Textures.xbt" -exec rm -rf {} \;
	cd `pwd`/assets/addons; rm -rf $(EXCLUDED_ADDONS)
	mkdir -p assets/system/certs
	cp $(CMAKE_SOURCE_DIR)/tools/depends/target/openssl/cacert.pem assets/system/certs
	cp $(CMAKE_SOURCE_DIR)/privacy-policy.txt assets

sharedapk: shared | xbmc/assets
	cp -rfp assets/* ./xbmc/assets

sharedobb: shared
	rm -f $(CMAKE_SOURCE_DIR)/main.@APP_NAME_LC@.obb
	$(ZIP) -9 -q -r $(CMAKE_SOURCE_DIR)/main.@APP_NAME_LC@.obb assets
	@echo "$(CMAKE_SOURCE_DIR)/main.@APP_NAME_LC@.obb created"

python: | xbmc/assets
	mkdir -p xbmc/assets/python2.7/lib/
	cp -rfp $(PREFIX)/lib/python2.7 xbmc/assets/python2.7/lib/
	cd xbmc/assets/python2.7/lib/python2.7/; rm -rf test config lib-dynload

res:
	mkdir -p xbmc/res xbmc/res/raw xbmc/res/values images
	@echo "native_arch=$(CPU)" > xbmc/res/raw/xbmc.properties
	cp -fp $(CMAKE_SOURCE_DIR)/media/splash.* xbmc/res/drawable/
	cp -fp media/drawable-hdpi/ic_launcher.png xbmc/res/drawable-hdpi/ic_launcher.png
	cp -fp media/drawable-ldpi/ic_launcher.png xbmc/res/drawable-ldpi/ic_launcher.png
	cp -fp media/drawable-mdpi/ic_launcher.png xbmc/res/drawable-mdpi/ic_launcher.png
	cp -fp media/drawable-xhdpi/ic_launcher.png xbmc/res/drawable-xhdpi/ic_launcher.png
	cp -fp media/drawable-xxhdpi/ic_launcher.png xbmc/res/drawable-xxhdpi/ic_launcher.png
	cp -fp $(CMAKE_SOURCE_DIR)/media/splash.* xbmc/res/drawable-xxxhdpi/
	cp -fp media/drawable-xxxhdpi/ic_launcher.png xbmc/res/drawable-xxxhdpi/ic_launcher.png
	cp -fp media/drawable-xhdpi/banner.png xbmc/res/drawable-xhdpi/banner.png
	cp -fp $(CMAKE_SOURCE_DIR)/media/icon80x80.png xbmc/res/drawable/ic_recommendation_80dp.png
	cp xbmc/strings.xml xbmc/res/values/
	cp xbmc/colors.xml xbmc/res/values/
	cp xbmc/searchable.xml xbmc/res/xml/

libs: $(PREFIX)/lib/@APP_NAME_LC@/lib@APP_NAME_LC@.so
	rm -rf xbmc/lib/$(CPU) xbmc/obj/local/$(CPU)
	mkdir -p xbmc/lib/$(CPU) xbmc/assets/python2.7/lib/ xbmc/obj/local/$(CPU)
	cp -fpL $(SRCLIBS) xbmc/obj/local/$(CPU)/
	cp -fp $(PREFIX)/lib/@APP_NAME_LC@/lib@APP_NAME_LC@.so xbmc/obj/local/$(CPU)/
	find $(PREFIX)/lib/@APP_NAME_LC@/addons -name "*.so" -exec cp -fp {} xbmc/obj/local/$(CPU)/ \; || true
	find $(PREFIX)/share/@APP_NAME_LC@/addons -name "*.so" -exec cp -fp {} xbmc/obj/local/$(CPU)/ \; || true
	find $(DEPENDS_PATH)/share/kodi/addons -name "*.so" -exec cp -fp {} xbmc/obj/local/$(CPU)/ \; || true
	find $(DEPENDS_PATH)/lib/kodi/addons -name "*.so" -exec cp -fp {} xbmc/obj/local/$(CPU)/ \; || true
	find $(PREFIX)/lib/@APP_NAME_LC@/system -name "*.so" -exec cp -fp {} xbmc/obj/local/$(CPU)/ \;
	DIR=${CURDIR}; cd $(PREFIX)/lib/python2.7/site-packages; for i in `find Cryptodome -name \*.so` ; do FN=`echo $$i | cut -c1- | tr "/" "_"` ; mv $$i $$DIR/xbmc/obj/local/$(CPU)/$$FN ; done
	cd xbmc/obj/local/$(CPU)/; find . -name "*.so" -not -name "lib*.so" | sed "s/\.\///" | xargs -I@ mv @ lib@
	cp -fp xbmc/obj/local/$(CPU)/*.so xbmc/lib/$(CPU)/
	$(STRIP) --strip-unneeded xbmc/lib/$(CPU)/*.so
	(test -s $(STLLIB) && install -p $(STLLIB) ./xbmc/lib/$(CPU)/) || true
	install -p $(GDBPATH) ./xbmc/lib/$(CPU)/gdbserver
	echo "set solib-search-path ./obj/local/$(CPU)" > ./xbmc/lib/$(CPU)/gdb.setup
	echo "directory $(TOOLCHAIN)/sysroot/usr/include $(NDKROOT)/sources/android/native_app_glue" \
             "$(NDKROOT)/sources/cxx-stl/gnu-libstdc++/$(GCC_VERSION)/include $(CORE_SOURCE_DIR)  $(PREFIX)/include jni" >> ./xbmc/lib/$(CPU)/gdb.setup

java: res
	mkdir -p xbmc/java/$(APP_PACKAGE_DIR) xbmc/obj
	@cp -R xbmc/src/* xbmc/java/$(APP_PACKAGE_DIR)/

package: libs python java
	@echo "Gradle build..."
	ANDROID_HOME=$(SDKROOT) ./gradlew assemble$(BUILD_TYPE)Unsigned
	@cp xbmc/build/outputs/apk/$(BUILD_TYPE_LC)Unsigned/xbmc-$(BUILD_TYPE_LC)Unsigned-unsigned.apk images/@APP_NAME_LC@app-debug-$(CPU)-unaligned.apk

apk-sign:
	@echo "Signing..."
	@jarsigner -sigalg MD5withRSA -digestalg SHA1 -keystore $(SIGN_STORE) -storepass $(SIGN_STOREPASS) $(SIGN_KEYPASS_OPT) images/@APP_NAME_LC@app-debug-$(CPU)-unaligned.apk $(SIGN_KEY)
	@$(ZIPALIGN) -f 4 images/@APP_NAME_LC@app-debug-$(CPU)-unaligned.apk $(CMAKE_SOURCE_DIR)/@APP_NAME_LC@app-$(CPU)-debug.apk
	@rm images/@APP_NAME_LC@app-debug-$(CPU)-unaligned.apk
	@echo "$(CMAKE_SOURCE_DIR)/@APP_NAME_LC@app-$(CPU)-debug.apk created"

$(PREFIX)/lib/xbmc/lib@APP_NAME_LC@.so: $(SRCLIBS)
	$(MAKE) -C ../../depends/target/xbmc

$(SRCLIBS):

apk-clean:
	rm -rf images
	rm -rf xbmc/java
	rm -rf xbmc/lib
	rm -rf xbmc/assets
	rm -rf xbmc/obj
	rm -rf xbmc/res/raw
	rm -rf xbmc/res/values
	rm -f xbmc/res/drawable/splash.*
	rm -f xbmc/res/drawable-xxxhdpi/splash.*
	rm -rf assets

.PHONY: force libs assets python sharedapk sharedobb res package

