INSTALL_DIR=please_set_the_INSTALL_DIR_variable_in_the_top_level_makefile_and_run_make_config

LIB=$(INSTALL_DIR)/lib
MYLIB=$(LIB)/usb_portmux

all:	install

SOFTDIR=software/firmware
BSPDIR=software/firmware_bsp

PROJ=usb_control
QSYS=$(PROJ).qsys
SOPCINFO=$(PROJ).sopcinfo
MEMINIT=$(SOFTDIR)/mem_init/firmware.hex
BSPLIB=$(BSPDIR)/libhal_bsp.a

$(SOPCINFO):	$(QSYS)
	nios2_command_shell.sh ip-generate --report-file=qip:$(PROJ).qip \
		--report-file=sopcinfo:$(SOPCINFO) \
		--component-file=$(QSYS) \
		--file-set=QUARTUS_SYNTH \
		--output-directory=.

$(BSPLIB):	$(SOPCINFO)
	cd $(BSPDIR); nios2_command_shell.sh sh ./create-this-bsp

$(MEMINIT):	$(BSPLIB)
	cd $(SOFTDIR); nios2_command_shell.sh sh ./create-this-app	
	cd $(SOFTDIR); nios2_command_shell.sh make mem_init_generate

install:	$(MEMINIT) usbportmux_gen.tcl usb_portmux.v.template
	cp usbportmux_gen.tcl $(LIB)
	@if test ! -d $(MYLIB); then mkdir $(MYLIB); fi
	cp usb_portmux.v.template $(MYLIB)
	cp usb_control.v $(MYLIB)
	cp submodules/* $(MYLIB)
	cp $(MEMINIT) $(MYLIB)

clean:
	rm -rf usb_control.qip

reallyclean:	clean
	rm -rf usb_control.v
	rm -rf $(SOPCINFO) make.out
	rm -f $(BSPLIB)
	rm -f $(MEMINIT)
	cd $(SOFTDIR); make clean
	rm -f $(SOFTDIR)/Makefile
	cd $(BSPDIR); make clean
	rm -f $(BSPDIR)/Makefile
