#
#	Makefile for Fastwel CPC108 card flash image
#

# The network node number that the CPC108 board is assigned now
TARGET = $(t)

EKIT_BIN_DIR= ../../../bin
BONE_DIR = ./root_bone
ROOT_DIR = ./root_flash

# Extract file and directory list from local 'root_bone' directory
DIRS = $(shell (cd $(BONE_DIR) ; find ./ -name '*' -type d -print -follow))
FILES = $(shell (cd $(BONE_DIR) ; find ./ -name '*' -type f -print -follow))

default:
	@echo "\nThe CPC108 Makefile supports the following targets:\n\n" \
	"make all           Make all targets, but don't reprogram the CPC108 flash.\n" \
	"make os_romqnx     Make the OS to be built into the rom image.\n" \
	"make efs           Make the FFS image to be built into the rom image.\n" \
	"make image         Make a new rom image. Includes OS and FFS.\n" \
	"make reflash       Write a rom image into the CPC108 flash memory. The\n" \
	"                   CPC108 must be connected via Ethernet and booted into\n" \
	"                   QNX 4.2x.\n"
	"                   Node number must be set with "t=NODE" parameter to make\n" \
	"make clean         Delete all files built by this Makefile except\n" \
	"                   the rom image.\n" \
	"make spotless      Delete totally all files built by this Makefile.\n" \

all : image
	@echo "All done"

os_romqnx : os.build
	buildqnx -c -V os.build os_buildqnx | tee build.lst
	$(EKIT_BIN_DIR)/romqnx -v -d0 os_buildqnx os_romqnx | tee romqnx.lst
	
efs :
	# Make all the directories needed in the runtime filesystem
	(for i in $(DIRS); do (echo $$i ; mkdir $(ROOT_DIR)/$$i) ; done)
	# Compress the files into the runtime filesystem
	(for i in $(FILES); do (echo $$i ; cp -v $(BONE_DIR)/$$i $(ROOT_DIR)/$$i) ; done)
	$(EKIT_BIN_DIR)/mkffs -v -b16k -s63M -o efs $(ROOT_DIR) | tee ffs.lst
	
image : os_romqnx efs flash.info
	$(EKIT_BIN_DIR)/mkcis -f image flash.info | tee flash.lst
	
reflash : image
	on -n$(TARGET) ticksize 1
	slay -f -n$(TARGET) Efsys.cpc108 Efsys.cpc &
	sleep 1
	on -n$(TARGET) ../efs/Efsys.cpc108 &
	sleep 2
	$(EKIT_BIN_DIR)/efsinit -s 10 //$(TARGET)/dev/skt1
	cp -V image //$(TARGET)/dev/skt1
	
clean :
	rm -rf efs os_buildqnx os_romqnx ./*.map ./*.lst ./*~ $(ROOT_DIR)/*
	
spotless : clean
	rm -f image
