#!/bin/bash
#
# Building bugs with CMake and the Gradle externalNativeBuild plugin
# The STATIC build is not consistent and stopped compiling. So I found a work around.
# The SHARED build is consistent, and if I do that first, then STATIC will work **once**
# This following process will build repeated, but only release version (don't care to troublshoot)


# build SHARED library
build_shared() {
	cp src/main/cpp/wolfssl-oem/CMakeLists_SHARED.txt  src/main/cpp/wolfssl-oem/CMakeLists.txt
	gradle clean
	rm -rf ./externalNativeBuild
	gradle build
}

# build STATIC library
build_static() {
	cp src/main/cpp/wolfssl-oem/CMakeLists_STATIC.txt  src/main/cpp/wolfssl-oem/CMakeLists.txt
	gradle build
}

get_key_status() {
	echo
	echo $1
	echo "Press any key to continue..."
	read -rsn1	
}

make_dirs() {
	# gen date stamp
	UNIQ_DIR=`date +%Y-%m-%d.%H_%M_%S`
	DEST_DIR="dist/wolfssl-lib_${UNIQ_DIR}/"
	# release
	mkdir -p ${DEST_DIR}/release/arm64-v8a
	mkdir -p ${DEST_DIR}/release/armeabi-v7a
	mkdir -p ${DEST_DIR}/release/x86
	mkdir -p ${DEST_DIR}/release/x86_64
	# debug
	# mkdir -p ${DEST_DIR}/debug/arm64-v8a
	# mkdir -p ${DEST_DIR}/debug/armeabi-v7a
	# mkdir -p ${DEST_DIR}/debug/x86
	# mkdir -p ${DEST_DIR}/debug/x86_64
}

copy_files() {
	cp  .externalNativeBuild/cmake/release/arm64-v8a/src/main/cpp/wolfssl-oem/libwolfssl.a  	 ${DEST_DIR}/release/arm64-v8a/
	cp  .externalNativeBuild/cmake/release/armeabi-v7a/src/main/cpp/wolfssl-oem/libwolfssl.a   ${DEST_DIR}/release/armeabi-v7a/
	cp  .externalNativeBuild/cmake/release/x86/src/main/cpp/wolfssl-oem/libwolfssl.a   				 ${DEST_DIR}/release/x86/
	cp  .externalNativeBuild/cmake/release/x86_64/src/main/cpp/wolfssl-oem/libwolfssl.a   		 ${DEST_DIR}/release/x86_64/
	cp  ./build/intermediates/cmake/release/obj/arm64-v8a/libwolfssl.so                        ${DEST_DIR}/release/arm64-v8a/
	cp  ./build/intermediates/cmake/release/obj/armeabi-v7a/libwolfssl.so                      ${DEST_DIR}/release/armeabi-v7a/
	cp  ./build/intermediates/cmake/release/obj/x86/libwolfssl.so                              ${DEST_DIR}/release/x86/
	cp  ./build/intermediates/cmake/release/obj/x86_64/libwolfssl.so                           ${DEST_DIR}/release/x86_64/
	#
	# debug not building right now...not sure I care so ignoring to move on...
	# 
	# cp  .externalNativeBuild/cmake/debug/arm64-v8a/src/main/cpp/wolfssl-oem/libwolfssl.a  	    ${DEST_DIR}/debug/arm64-v8a/
	# cp  .externalNativeBuild/cmake/debug/armeabi-v7a/src/main/cpp/wolfssl-oem/libwolfssl.a  	  ${DEST_DIR}/debug/armeabi-v7a/
	# cp  .externalNativeBuild/cmake/debug/x86/src/main/cpp/wolfssl-oem/libwolfssl.a    				  ${DEST_DIR}/debug/x86/
	# cp  .externalNativeBuild/cmake/debug/x86_64/src/main/cpp/wolfssl-oem/libwolfssl.a  				${DEST_DIR}/debug/x86_64/
}


build_shared
# get_key_status "Finished STATIC lib build."
build_static
# get_key_status "Finished SHARED lib build."
make_dirs
copy_files

