SONICS_SHAREWARE=/Users/robertguenzel/apps
OCPIP_TLM2=/Users/robertguenzel/mydev/cpp_systemc/GreenSocsSVN

### Change the variable below, so they match your system ###

#Path where to install ocpip package
INSTALLPATH=$(SONICS_SHAREWARE)/ocp_install

#Path to your systemc installation
SYSTEMC=$(SONICS_SHAREWARE)/systemc-2.2

#Path to your TLM2 installation
TLM2=$(OCPIP_TLM2)/../tlm2_svn/svn

#the target architecture of your system
#  the default value tries to guess it from your systemc installation
#  if that fails, set it manually
TARGET_ARCH = $(subst $(SYSTEMC)/lib-,,$(wildcard $(SYSTEMC)/lib-*))

#### DO NOT MODIFY BELOW THIS LINE !#####

VERSION=$(shell cat recent_version.txt)

DIR=$(shell pwd)

DOC=$(INSTALLPATH)/doc/ocp_manual_$(VERSION).pdf
TPPS=$(subst ocpip_dev,ocpip_$(VERSION),$(foreach file, $(wildcard src/ocpip_dev/*.tpp), $(INSTALLPATH)/$(file)))
G_HEADERS=$(foreach file, $(wildcard include/generic/*.h), $(INSTALLPATH)/$(file)) 
V_HEADERS=$(subst ocpip_dev,ocpip_$(VERSION),$(foreach file, $(wildcard include/ocpip_dev/*.h), $(INSTALLPATH)/$(file)))

BASE_SOCKET_HEADERS=$(subst ocpip_dev,ocpip_$(VERSION),$(foreach file, $(wildcard include/ocpip_dev/base_socket/*.h), $(INSTALLPATH)/$(file)))
BASE_SOCKET_TPPS=$(subst ocpip_dev,ocpip_$(VERSION),$(foreach file, $(wildcard src/ocpip_dev/base_socket/*.tpp), $(INSTALLPATH)/$(file)))

MON_INST=$(wildcard $(INSTALLPATH)/mon)
LEGACY_SOURCE=$(foreach file, $(wildcard include/legacy_code_base/*.legacy.h), $(INSTALLPATH)/$(file))

all: install

install: make_dirs $(DOC) $(G_HEADERS) $(V_HEADERS) $(TPPS) $(LEGACY_SOURCE) $(BASE_SOCKET_HEADERS) $(BASE_SOCKET_TPPS) Makefile $(INSTALLPATH)/include/ocpip_$(VERSION).h
	@echo $(shell cd $(INSTALLPATH)/include/; sed s/ocpip_dev/`ls ocpip_*.h | sort | tail -n1 | sed s/.h//`/ $(DIR)/include/ocpip.h >ocpip.hh;);
	mv $(INSTALLPATH)/include/ocpip.hh $(INSTALLPATH)/include/ocpip.h; 
	@echo Installation done

install_examples: install
	make SYSTEMC=$(SYSTEMC) TLM2=$(TLM2) GREENSOCKET=$(GREENSOCKET) TARGET_ARCH=$(TARGET_ARCH) INSTALLPATH=$(INSTALLPATH)  -C ./examples install_examples;
ifeq ($(MON_INST), $(INSTALLPATH)/mon)
	@echo ATTENTION: Monitor package was installed prior to examples, so please re-run the monitor installation so that the example makefiles get updated.
endif


uninstall_examples:
	make SYSTEMC=$(SYSTEMC) TLM2=$(TLM2) GREENSOCKET=$(GREENSOCKET) TARGET_ARCH=$(TARGET_ARCH) INSTALLPATH=$(INSTALLPATH)  -C ./examples uninstall_examples

rm_recent_ver:
	rm -f recent_ver.h
make_dirs:
	rm -f $(INSTALLPATH)/include/ocpip.h;
	mkdir -p $(INSTALLPATH)/include/ocpip_$(VERSION);
	mkdir -p $(INSTALLPATH)/doc;
	mkdir -p $(INSTALLPATH)/include/ocpip_$(VERSION)/base_socket;
	mkdir -p $(INSTALLPATH)/include/generic;
	mkdir -p $(INSTALLPATH)/src/ocpip_$(VERSION);
	mkdir -p $(INSTALLPATH)/src/ocpip_$(VERSION)/base_socket;
	mkdir -p $(INSTALLPATH)/include/legacy_code_base;
	mkdir -p $(INSTALLPATH)/include/legacy_support;


$(INSTALLPATH)/src/ocpip_$(VERSION)/%.tpp : ./src/ocpip_dev/%.tpp
	cp $< $@

$(INSTALLPATH)/include/ocpip_$(VERSION)/%.h : ./include/ocpip_dev/%.h
	cp $< $@

$(INSTALLPATH)/doc/%.pdf : ./doc/recent_build/ocp_manual.pdf
	cp $< $@

$(INSTALLPATH)/include/legacy_code_base/%.legacy.h : ./include/legacy_code_base/%.legacy.h
	./cp_legacy $< $@ $(INSTALLPATH) $(@F) ./recent_version.txt

$(INSTALLPATH)/include/generic/%.h : ./include/generic/%.h
	cp $< $@

$(INSTALLPATH)/include/ocpip_$(VERSION).h : ./include/ocpip_dev.h
	sed s/__OCPIP_DEV_H__/__OCP_IP_$(VERSION)_H__/ $< | sed s/ocpip_dev/ocpip_$(VERSION)/ > $@

