LIB_DIR=$(shell readlink -f .)
CROSS_COMPILER_DIR=$(shell readlink -f ../../buildroot/output/host/usr/bin)
CROSS_COMPILER=arm-none-linux
CROSS_COMPILER_PREFIX=$(CROSS_COMPILER)-gnueabi-
CC=$(CROSS_COMPILER_DIR)/$(CROSS_COMPILER_PREFIX)gcc
CXX=$(CROSS_COMPILER_DIR)/$(CROSS_COMPILER_PREFIX)g++
LD=$(CROSS_COMPILER_DIR)/$(CROSS_COMPILER_PREFIX)ld
AR=$(CROSS_COMPILER_DIR)/$(CROSS_COMPILER_PREFIX)ar
STRIP=$(CROSS_COMPILER_DIR)/$(CROSS_COMPILER_PREFIX)strip
RANLIB=$(CROSS_COMPILER_DIR)/$(CROSS_COMPILER_PREFIX)ranlib
CROSS_COMPILER_FLAGS=-mcpu=arm926ej-s -mtune=arm926ej-s -ffunction-sections -fdata-sections -O2 -DNDEBUG -fPIE -fstack-protector
CFLAGS=$(CROSS_COMPILER_FLAGS)
CXXFLAGS=$(CROSS_COMPILER_FLAGS) -fpermissive
MFLAGS=--jobs $(shell nproc --all)
CONFIGFILE=$(LIB_DIR)/../src/isp20/include/IsomFrameworkConfig.h

ifeq ($(DEBUG),yes)
CFLAGS += -DDEBUGGING
CXXFLAGS += -DDEBUGGING
endif


.PHONY: help
help:
	@echo "Makefile for libraries"
	@echo " all"
	@echo " clean"
	@echo " distclean"



.PHONY: all
all: make_prov make_crypto make_device

.PHONY: clean
clean: clean_json clean_openssl clean_poco clean_protobuf clean_device clean_prov clean_crypto

.PHONY: distclean
distclean: distclean_json distclean_openssl distclean_poco distclean_protobuf distclean_device distclean_prov distclean_crypto


#Cryptoauthlib

.PHONY: make_crypto
make_crypto: ./crypto/.make_crypto

./crypto/.get_crypto:
	rm -rf crypto/src
	rm -rf crypto/host
	mkdir -p crypto/src
	mkdir -p crypto/host
	git clone --single-branch -b pkcs11 https://github.com/MicrochipTech/cryptoauthlib ./crypto/src/cryptoauthlib
	git clone https://github.com/OpenSC/libp11.git ./crypto/src/libp11
	sh -c 'cd crypto/src/libp11; git checkout tags/libp11-0.4.10'
	echo > ./crypto/.get_crypto

./crypto/.make_crypto: ./crypto/.get_crypto
	#building libp11 library
	sh -c 'cd ./crypto/src/libp11; make distclean; ./bootstrap; ./configure CC=$(CC) CFLAGS="$(CFLAGS)" --host=arm-none-linux-gnueabi OPENSSL_LIBS="$(LIB_DIR)/isom/openssl/host/usr/lib/libcrypto.so.1.1" OPENSSL_CFLAGS="-I$(LIB_DIR)/isom/openssl/host/usr/include"; make; make install DESTDIR=$(LIB_DIR)/crypto/host'
	#building cryptoauthlib library
	sh -c 'cd ./crypto/src/cryptoauthlib; make clean; cmake . -DATCA_HAL_I2C=on -DATCA_PKCS11=on -DCMAKE_INSTALL_PREFIX=$(LIB_DIR)/crypto/host -DCMAKE_TOOLCHAIN_FILE=$(CROSS_COMPILER_DIR)/../share/buildroot/toolchainfile.cmake -DCMAKE_C_COMPILER=$(CC) -DCMAKE_CXX_COMPILER=$(CXX) -DCMAKE_C_FLAGS="$(CFLAGS)"; make; make install DESTDIR=$(LIB_DIR)/crypto/host'
	# copy config to location	
	mkdir ../../buildroot/output/target/etc/cryptoauthlib
	mkdir ../../buildroot/output/target/var/lib/cryptoauthlib
	cp ./crypto/config/cryptoauthlib.conf ../../buildroot/output/target/etc/cryptoauthlib/
	cp ./crypto/config/0.conf ../../buildroot/output/target/var/lib/cryptoauthlib/
	echo > ./crypto/.make_crypto

.PHONY: clean_crypto
clean_crypto:
	make -C ./crypto/src/libp11 clean;
	make -C ./crypto/src/libp11 distclean;
	make -C ./crypto/src/cryptoauthlib clean;
	make -C ./crypto/src/cryptoauthlib distclean;

.PHONY: dist_clean
distclean_crypto:
	rm -f ./crypto/.make_crypto
	rm -f ./crypto/.get_crypto

# JSON
# https://acswiki.honeywell.com/display/FRAMEWORK/External+Libraries#externalLibraries-2286824

.PHONY: make_json
make_json: ./isom/json/.make_json

./isom/json/.get_json:
	rm -rf ./isom/json
	mkdir -p ./isom/json/src
	git clone https://bitbucket.honeywell.com/scm/isom/exlibs.libjson.git ./isom/json/src
	sh -c 'cd ./isom/json/src; git checkout tags/libjson-7.6.1-isom-6'
	#rm -rf ./isom/json/libjson_7.6.1.zip
	#wget -q https://sourceforge.net/projects/libjson/files/libjson_7.6.1.zip/download -O ./isom/json/libjson_7.6.1.zip
	#mv -f ./isom/json/src/libjson/* ./isom/json/src/
	#patch -d ./isom/json/src -N -p2 -i ./json/JSONOptions.patch
	#patch -d ./isom/json/src -N -p2 -i ./json/JSONMemory_StrayGrapheme.patch
	#patch -d ./isom/json/src -N -p2 -i ./json/JSONStream_SpaceAndComments.patch
	#patch -d ./isom/json/src -N -p2 -i ./json/makefile.patch
	echo > ./isom/json/.get_json

./isom/json/.make_json: ./isom/json/.get_json
	rm -rf ./isom/json/host
	mkdir -p ./isom/json/host
	#make -C ./isom/json/src -f makefile distclean
	make -C ./isom/json/src -f makefile clean
	make -C ./isom/json/src -f makefile all CXX="$(CXX)" cxxflags_default="$(CFLAGS)" AR="$(AR)" RANLIB="$(RANLIB)"
	make -C ./isom/json/src -f makefile install prefix=$(LIB_DIR)/isom/json/host libdir=. includedir=Inc libname_hdr=Json
	echo > ./isom/json/.make_json

.PHONY: clean_json
clean_json:
	make -C ./isom/json/src -f makefile clean
	rm -f ./isom/json/.make_json

.PHONY: distclean_json
distclean_json:
	rm -f ./isom/json/.get_json
	rm -f ./isom/json/.make_json



# TLS
# https://acswiki.honeywell.com/display/FRAMEWORK/External+Libraries#externalLibraries-401446722

.PHONY: make_tls
make_tls: ./isom/openssl/.make_tls

./isom/openssl/.get_tls:
	rm -rf ./isom/openssl/host
	rm -rf ./isom/openssl/src
	wget -q https://www.openssl.org/source/openssl-1.1.1g.tar.gz -O ./isom/openssl/openssl-1.1.1g.tar.gz
	mkdir -p ./isom/openssl/src
	tar -xzf ./isom/openssl/openssl-1.1.1g.tar.gz -C ./isom/openssl/src --strip-components=1
	#patch -d ./tls/src -N -p2 -i ../Makefiles.patch
	echo > ./isom/openssl/.get_tls

./isom/openssl/.make_tls: ./isom/openssl/.get_tls
	rm -rf ./isom/openssl/host
	mkdir -p ./isom/openssl/host
	sh -c 'cd ./isom/openssl/src; make distclean; ./Configure linux-armv4 no-asm no-idea no-mdc2 no-rc5 no-ssl3 --prefix=/usr --openssldir=/etc/ssl'
	make -C ./isom/openssl/src -f Makefile depend
	make -C ./isom/openssl/src -f Makefile CC="$(CC)" CFLAG="$(CFLAGS)"
	make -C ./isom/openssl/src -f Makefile DESTDIR=$(LIB_DIR)/isom/openssl/host install_sw
	echo > ./isom/openssl/.make_tls

.PHONY: clean_tls
clean_tls:
	make -C ./isom/openssl/src -f Makefile clean
	rm -f ./isom/openssl/.make_tls

.PHONY: distclean_tls
distclean_tls:
	rm -f ./isom/openssl/.get_tls
	rm -f ./isom/openssl/.make_tls



# OpenSSL engine
# https://acswiki.honeywell.com/display/ILS/Galaxy+PKI+Tailoring

.PHONY: make_openssl
make_openssl: ./openssl/.make_openssl

./openssl/.get_openssl:
	rm -rf ./openssl/src
	mkdir -p ./openssl/src
	git clone https://bitbucket.honeywell.com/scm/ps/cryptoauth-openssl-engine.git ./openssl/src
	cd openssl/src; git checkout c8a3aa04241cbeec099a77161de5be0da7b3e0df
	patch -d ./openssl/src -N -p1 -i ../Makefile.patch
	patch -d ./openssl/src -N -p1 -i ../hal_linux_i2c_c.patch
	echo > ./openssl/.get_openssl

./openssl/.make_openssl: ./openssl/.get_openssl
	rm -rf ./openssl/host
	mkdir -p ./openssl/host
	sh -c 'export CC="$(CC)"; export CXX="$(CXX)"; export LD="$(LD)"; export AR="$(AR)"; export STRIP="$(STRIP)"; export RANLIB="$(RANLIB)"; make -C ./openssl/src -f Makefile'
	cp -r ./openssl/src/install_dir/* ./openssl/host/
	echo > ./openssl/.make_openssl

.PHONY: clean_openssl
clean_openssl:
	make -C ./openssl/src -f Makefile clean
	rm -f ./openssl/.make_openssl

.PHONY: distclean_openssl
distclean_openssl:
	rm -f ./openssl/.get_openssl
	rm -f ./openssl/.make_openssl



# Provisioning
# https://acswiki.honeywell.com/display/ILS/Galaxy+PKI+Tailoring

.PHONY: make_prov
make_prov: ./prov/.make_prov

./prov/.get_prov:
	rm -rf ./prov/src
	mkdir -p ./prov/src
	git clone --recursive https://bitbucket.honeywell.com/scm/ps/ecc508_tools.git ./prov/src
	sh -c 'cd ./prov/src; git checkout ea1adf775a023e35dc9e3e44f404aaf88fa4dd64'
	patch -d ./prov/src -N -p1 -i ../prov_c.patch
	patch -d ./prov/src -N -p1 -i ../atecc508_configurations.patch
	patch -d ./prov/src -N -p1 -i ../makefiles.patch
	echo > ./prov/.get_prov

./prov/.make_prov: ./prov/.get_prov
	sh -c 'export CC="$(CC)"; export CXX="$(CXX)"; export LD="$(LD)"; export AR="$(AR)"; export RL="$(RANLIB)"; make -C ./prov/src -f Makefile'
	echo > ./prov/.make_prov

.PHONY: clean_prov
clean_prov:
	make -C ./prov/src -f Makefile clean
	rm -f ./prov/.make_prov

.PHONY: distclean_prov
distclean_prov:
	rm -f ./prov/.get_prov
	rm -f ./prov/.make_prov



# POCO
# https://acswiki.honeywell.com/display/FRAMEWORK/External+Libraries#externalLibraries-2461355

.PHONY: make_poco
make_poco: ./isom/poco/.make_poco

./isom/poco/.get_poco:
	rm -rf ./isom/poco
	mkdir -p ./isom/poco/src
	git clone https://bitbucket.honeywell.com/scm/isom/exlibs.poco.git ./isom/poco/src
	sh -c 'cd ./isom/poco/src; git checkout tags/poco-1.10.1-isom-2'
	echo > ./isom/poco/.get_poco

./isom/poco/.make_poco: ./isom/openssl/.make_tls ./isom/poco/.get_poco
	rm -rf ./isom/poco/host
	mkdir -p ./isom/poco/host
	#sh -c 'cd ./isom/poco/src; make clean'
	sh -c 'cd ./isom/poco/src; make distclean'
	sh -c 'cd ./isom/poco/src; chmod 777 configure; chmod 777 -R build/script;'
	sh -c 'cd ./isom/poco/src; ./configure --prefix=$(LIB_DIR)/isom/poco/host --include-path=$(LIB_DIR)/isom/openssl/host/usr/include --poquito --no-tests --no-samples --omit=CppUnit,CppUnit/WinTestRunner,XML,JSON,Data,Data/SQLite,Data/ODBC,Data/MySQL,MongoDB,Zip,Encodings,Redis --static --cflags="$(CXXFLAGS)"'
	make -C ./isom/poco/src -f Makefile CC="$(CC)" CXX="$(CXX)" AR="$(AR)" STRIP="$(STRIP)" RANLIB="$(RANLIB)" CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS) -std=c++11"
	make -C ./isom/poco/src -f Makefile install
	echo > ./isom/poco/.make_poco

.PHONY: clean_poco
clean_poco:
	make -C ./isom/poco/src -f Makefile clean
	rm -f ./isom/poco/.make_poco

.PHONY: distclean_poco
distclean_poco:
	rm -f ./isom/poco/.get_poco
	rm -f ./isom/poco/.make_poco



# PROTOBUF
# https://acswiki.honeywell.com/display/FRAMEWORK/External+Libraries#externalLibraries--924520533

.PHONY: make_protobuf
make_protobuf: ./isom/protobuf/.make_protobuf

./isom/protobuf/.get_protobuf:
	rm -rf ./isom/protobuf/src
	mkdir -p ./isom/protobuf/src
	git clone https://bitbucket.honeywell.com/scm/isom/exlibs.protobuf.git ./isom/protobuf/src
	sh -c 'cd ./isom/protobuf/src; git checkout tags/protobuf-3.11.0-isom-1'
	#patch -d ./protobuf/src -N -p2 -i ../configure.patch
	echo > ./isom/protobuf/.get_protobuf

./isom/protobuf/.make_protobuf_protoc: ./isom/protobuf/.get_protobuf
	rm -rf ./isom/protobuf/host
	mkdir -p ./isom/protobuf/host
	sh -c 'cd ./isom/protobuf/src; make clean; make distclean; ./configure --prefix=$(LIB_DIR)/isom/protobuf/host --disable-shared'
	make -C ./isom/protobuf/src -f Makefile $(MFLAGS) install-exec
	make -C ./isom/protobuf/src -f Makefile distclean
	echo > ./isom/protobuf/.make_protobuf_protoc

./isom/protobuf/.make_protobuf: ./isom/protobuf/.get_protobuf ./isom/protobuf/.make_protobuf_protoc
	mv ./isom/protobuf/host/bin/protoc ./isom/protobuf/host/bin/protoc_
	sh -c 'cd ./isom/protobuf/src; make clean; make distclean; ./configure CC="$(CC)" CFLAGS="$(CFLAGS)" CXX="$(CXX)" CXXFLAGS="$(CXXFLAGS) -std=c++11" AR="$(AR)" STRIP="$(STRIP)" RANLIB="$(RANLIB)" --host="$(CROSS_COMPILER)" --without-protoc --prefix=$(LIB_DIR)/isom/protobuf/host --disable-shared'
	make -C ./isom/protobuf/src -f Makefile $(MFLAGS)
	make -C ./isom/protobuf/src -f Makefile install
	mv ./isom/protobuf/host/bin/protoc_ ./isom/protobuf/host/bin/protoc
	echo > ./isom/protobuf/.make_protobuf

.PHONY: clean_protobuf
clean_protobuf:
	make -C ./isom/protobuf/src -f Makefile clean
	rm -f ./isom/protobuf/.make_protobuf

.PHONY: distclean_protobuf
distclean_protobuf:
	rm -f ./isom/protobuf/.get_protobuf
	rm -f ./isom/protobuf/.make_protobuf



# ISOM
# https://acswiki.honeywell.com/display/FRAMEWORK/ISOM+Device+FW+Checkout
# https://acswiki.honeywell.com/display/FRAMEWORK/External+Libraries+-+Include+Path
# https://acswiki.honeywell.com/display/FRAMEWORK/Framework+Build+Instructions#FWbuildinstructions--1522474832

.PHONY: make_device
make_device: ./isom/device/.make_connfw ./isom/device/.make_isomfw

./isom/device/.get_device:
	rm -rf ./isom/device/src
	git clone -b develop https://$(GITUSER)bitbucket.honeywell.com/scm/isom/device.git ./isom/device/src
	sh -c 'cd ./isom/device/src; git checkout c842ac892a7a09e83ba0cd5e9db1be0282496d1d'
	sh -c 'cd ./isom/device/src; git submodule update --init --recursive'
	
	cp ./../src/Schemas/GalaxyCredential*.* ./isom/device/src/Schemas/Proto/Schemas/Proto
	echo > ./isom/device/.get_device

./isom/device/.make_connfw: ./isom/device/.get_device
	rm -rf ./tmp/workspace_cdt_test
	rm -rf ./isom/device/src/Build/Linux/ConnectionFW/Debug
	sh -c 'export CPLUS_INCLUDE_PATH=$(LIB_DIR)/isom/openssl/host/usr/include:$(LIB_DIR)/isom/json/host/Inc/Json:$(LIB_DIR)/isom/json/host/Inc/Json/_internal/Source:$(LIB_DIR)/isom/protobuf/host/include:$(LIB_DIR)/isom/poco/host/include; echo $(CPLUS_INCLUDE_PATH); export CXXFLAGS="$(CXXFLAGS)"; export CFLAGS="$(CFLAGS)"; export MFLAGS="$(MFLAGS)"; $(ECLIPSE_ABSOLUTE)eclipse -configuration ./tmp/ -nosplash --launcher.suppressErrors -application org.eclipse.cdt.managedbuilder.core.headlessbuild -import $(LIB_DIR)/isom/device/src/Build/Linux/ConnectionFW -cleanBuild ConnectionFW/Debug -data ./tmp/workspace_cdt_test -E CXX=$(CXX) -E CC=$(CC) -E AR=$(AR) -E ARFLAGS= -E MAKE=make -E CONFIG_FILE=$(CONFIGFILE)'
	mkdir -p ./isom/device/device_outputs
	cp -rf ./isom/device/src/Output/* ./isom/device/device_outputs/
	echo > ./isom/device/.make_connfw

./isom/device/.make_isomfw: ./isom/device/.make_connfw
	rm -rf ./tmp/workspace_cdt_test
	rm -rf ./isom/device/src/Build/Linux/ISOMFramework/ISOM_FW_LIB_CONFIG
	sh ./isom/device/src/Build/PrebuildScript.sh
	sh -c 'export CPLUS_INCLUDE_PATH="$(LIB_DIR)/isom/openssl/host/usr/include:$(LIB_DIR)/isom/json/host/Inc/Json:$(LIB_DIR)/isom/json/host/Inc/Json/_internal/Source:$(LIB_DIR)/isom/protobuf/host/include:$(LIB_DIR)/isom/poco/host/include"; export CXXFLAGS="$(CXXFLAGS)"; export CFLAGS="$(CFLAGS)"; export MFLAGS="$(MFLAGS)"; $(ECLIPSE_ABSOLUTE)eclipse -configuration ./tmp/ -nosplash --launcher.suppressErrors -application org.eclipse.cdt.managedbuilder.core.headlessbuild -import $(LIB_DIR)/isom/device/src/Build/Linux/ISOMFramework -cleanBuild ISOM_FW_LIB/ISOM_FW_LIB_CONFIG -data ./tmp/workspace_cdt_test -E CXX=$(CXX) -E CC=$(CC) -E ARFLAGS= -E AR=$(AR) -E MAKE=make -E CONFIG_FILE=$(CONFIGFILE)'
	mkdir -p ./isom/device/device_outputs
	cp -rf ./isom/device/src/Output/* ./isom/device/device_outputs/
	echo > ./isom/device/.make_isomfw

.PHONY: clean_device
clean_device:
	rm -f ./isom/device/.make_connfw
	rm -f ./isom/device/.make_isomfw

.PHONY: distclean_device
distclean_device:
	rm -f ./isom/device/.get_device
	rm -f ./isom/device/.make_connfw
	rm -f ./isom/device/.make_isomfw
