##########################################################
#  This Makefile is to build the ASN1 library to be used
#  for external codec in Titan
#  This Makefile is expected that asn1c compiler is 
#  installed in the PATHs
##########################################################

# The name of the library
ASN_LIBRARY = libItsAsn.so 
# ASN.1 files to be included in the library
ASN_FILES = LibIts/CAM/CAM.asn \
            LibIts/DENM/DENM.asn \
            LibIts/IS/ETSI_TS_103301/IVIM_PDU_Descriptions.asn \
            LibIts/IS/ETSI_TS_103301/MAPEM_PDU_Descriptions.asn \
            LibIts/IS/ETSI_TS_103301/SPATEM_PDU_Descriptions.asn \
            LibIts/IS/ETSI_TS_103301/SREM_PDU_Descriptions.asn \
            LibIts/IS/ETSI_TS_103301/SSEM_PDU_Descriptions.asn \
            LibIts/IS/ISO_TS_14816/AVIAEINumberingAndDataStructures.asn \
            LibIts/IS/ISO_TS_14906/EfcDsrcApplication.asn \
            LibIts/IS/ISO_TS_14906/EfcDsrcGeneric.asn \
            LibIts/IS/ISO_TS_17419/CITSapplMgmtIDs.asn \
            LibIts/IS/ISO_TS_19091/original/AddGrpC.asn \
            LibIts/IS/ISO_TS_19091/original/DSRC.asn \
            LibIts/IS/ISO_TS_19091/original/REGION.asn \
            LibIts/IS/ISO_TS_19321/IVI.asn \
            LibIts/IS/ISO_TS_24534-3/ElectronicRegistrationIdentificationVehicleDataModule.asn \
            LibIts/ITS-Container/ITS_Container.asn \
            LibIts/Security/1609Dot2/IEEE1609dot2.asn \
            LibIts/Security/1609Dot2/IEEE1609dot2BaseTypes.asn \
            LibIts/Security/EtsiTs103097Module.asn \
            LibIts/Security/EtsiTs102941OfflineDataStructures.asn \
            LibIts/Security/EtsiTs102941BaseTypes.asn \
            LibIts/Security/EtsiTs102941TrustLists.asn \
            LibIts/Security/EtsiTs102941Its_sMessages.asn

# PDUs
PDU = CAM DENM MAPEM SPATEM SREM SSEM IVIM EtsiTs103097Data EtsiTs103097Certificate

ASN1C:=asn1c
#Override ASN1C instalation path
#ASN1C_PATH = 

ifneq (, $(ASN1C_PATH))
OS=$(shell uname)
ifneq ("CYGWIN_NT-6.1", $(OS))
ASN1C:=$(ASN1C_PATH)/asn1c/asn1c -S $(ASN1C_PATH)/skeletons
else
ASN1C:=$(ASN1C_PATH)/asn1c/asn1c.exe -S $(ASN1C_PATH)/skeletons
endif
endif

ifeq (,$(ASN_DIR))
##########################################################
# Build path for the library
BIN_DIR := ../bin/asn1
# Relative path from the build path to the directory with 
# ASN.1 files
ASN_DIR := ../../asn1

.PHONY: FORCE

all: $(BIN_DIR)/../$(ASN_LIBRARY)
clean:
	rm -rf $(BIN_DIR)

$(BIN_DIR)/../$(ASN_LIBRARY): $(BIN_DIR)/Makefile
	make -C $(BIN_DIR) all

$(BIN_DIR):
	mkdir -p $@

$(BIN_DIR)/Makefile: $(BIN_DIR) Makefile
	echo HOME=$(HOME)
	echo ASN_DIR=$(ASN_DIR) > $@
ifneq (, $(findstring yann,$(HOME)))
	echo ASN1C_PATH = $(HOME)/frameworks/asn1c >> $@
endif 
ifneq (, $(findstring filatov,$(HOME)))
	echo ASN1C_PATH = C:/Users/filatov/Work/asn1c >> $@
endif
	cat Makefile           >>$@

##########################################################
else
##########################################################
# This part is executed within the BIN_DIR directory to
# override variables from the Makefile.am.libasncodec 
# generated by asn1c
LIB_MAKEFILE=Makefile.am.libasncodec
include $(LIB_MAKEFILE)
OBJS = ${ASN_MODULE_SRCS:.c=.o}
CONVERTER=converter
ASN_CONVERTER_SOURCES := \
	converter-example.c\
	pdu_collection.c
CONVERTER_OBJS=${ASN_CONVERTER_SOURCES:.c=.o}
CFLAGS += $(ASN_MODULE_CFLAGS) -DPDU=CAM -DASN_PDU_COLLECTION -I.

all: $(CONVERTER)

$(LIB_MAKEFILE): $(addprefix $(ASN_DIR)/, $(ASN_FILES))
	$(ASN1C) -gen-PER -gen-OER $(addprefix -pdu=,$(PDU)) -fcompound-names $^

$(CONVERTER): $(ASN_LIBRARY) $(CONVERTER_OBJS) 
	$(CC) $(CFLAGS) -o $@ $(CONVERTER_OBJS) $(ASN_LIBRARY) $(LIBS)

$(ASN_LIBRARY): ${ASN_MODULE_SRCS:.c=.o}
	$(CC) $(CFLAGS) -o $@ $^ -shared $(LDFLAGS) $(LIBS)

.SUFFIXES:
.SUFFIXES: .c .o

.c.o:
	$(CC) $(CFLAGS) -o $@ -c $<

clean:
	rm -f $(CONVERTER) $(ASN_LIBRARY)
	rm -f $(OBJS) $(CONVERTER_OBJS)

regen: clear-asn1c $(LIB_MAKEFILE)
clear-asn1c:
	rm -f $(LIB_MAKEFILE)

##########################################################
endif
