#
# Makefile for glue logic
#

export TOPDIR ?= $(shell cd ../..; pwd)
include ../makefile.envars

#

.PHONY: all main

all: main

ifneq ($(wildcard makefile.corner),)
include makefile.corner
endif

#

IMAGE_DIR  := $(TOPDIR)/image
FINAL_ROOT := $(TOPDIR)/image/root
UI_DIR	   := $(TOPDIR)/rootfs/src/amit_priv/_WebUI/www

MKSQUASHFS := $(TOPDIR)/tools/mksquashfs
SQUASHFS := $(TOPDIR)/image/squashfs.o

#

.PHONY: show_msg cp_glue mk_root mk_image mk_pre4


ifeq ("$(RD_LEVEL)","1")
main: gen_bms show_msg cp_glue mk_root ui mk_image 
else
main: gen_bms show_msg cp_glue mk_root mk_image
endif

gen_bms:
ifdef FULL_COPY
	@./genbms
endif

show_msg:
	@echo "*** Making glue logic..."

cp_glue: mk_pre4 INSTALL.copy_glue
#cp_glue: INSTALL.copy_glue

mk_pre4:
ifdef FULL_COPY
	@CORNER_DE_PRJROOT=$(TOPDIR) FW_REV=`$(TOPDIR)/rootfs/glue/getfwrev` \
	FW_VER=${shell cat $(TOPDIR)/VERSION} \
	$(PRIV_TOOLS)/bin/mkcsxf -d $(TOPDIR)/rootfs/src/var/csidbase -o pre4.dat -zcb pre4.txt -X c 
	echo "$(PRIV_TOOLS)/bin/mkcsxf -d $(TOPDIR)/rootfs/src/var/csidbase -o pre4.dat -zcb pre4.txt -X c"
endif

mk_root:
	@rm -fr $(FINAL_ROOT)
	@mkdir -p $(FINAL_ROOT)
	if test -d $(TOPDIR)/linux/root; then \
		cp -ar $(TOPDIR)/linux/root/*   $(FINAL_ROOT); \
	fi
	cp -ar $(TOPDIR)/busybox/root/* $(FINAL_ROOT)
	cp -ar $(TOPDIR)/rootfs/root/*  $(FINAL_ROOT)
	if test -d $(FINAL_ROOT)/modules; then \
		tar c -C $(FINAL_ROOT)/modules . | tar x -C $(FINAL_ROOT)/lib/modules; \
		rm -fr $(FINAL_ROOT)/modules; \
	fi
	./grablibs $(FINAL_ROOT) $(KERNEL_VERSION)
ifeq ("$(KERNEL_VERSION)","2.6.x")
	./MAKEDEV.2.6  $(FINAL_ROOT)
else
	./MAKEDEV  $(FINAL_ROOT)
endif

.PHONY: mk_image_squashfs

mk_image: mk_image_squashfs

mk_image_squashfs:
	rm -f $(SQUASHFS)
	$(MKSQUASHFS) $(FINAL_ROOT) $(SQUASHFS) -be
#	ROOTSIZE=`du -s $(FINAL_ROOT) | cut -f1`
#	IMGSIZE=`du -s $(SQUASHFS) |  cut -f1`
#	echo "=============================================="
#	echo "Summary:"
#	echo "==>Squashfs disk size  = $(ROOTSIZE)KBytes"
#	echo "==>Squashfs image size = $(IMGSIZE) KBytes"
ifdef FULL_COPY
ifeq ("$(RD_LEVEL)","1")
	$(PRIV_TOOLS)/bin/binman --s d_rd_1.bms --d $(IMAGE_DIR)
else
	$(PRIV_TOOLS)/bin/binman --s d.bms --d $(IMAGE_DIR)
endif
	$(PRIV_TOOLS)/bin/binman --s p.bms --d $(IMAGE_DIR)
	#-rm -f $(IMAGE_DIR)/squashfs.o
	ls -l $(IMAGE_DIR)/*.bin
else
	cd ../../image; $(PRIV_TOOLS)/bin/binboy @linux -r "$(ROM_ID)"
	cd ../../image; $(PRIV_TOOLS)/bin/binboy @rootfs -r "$(ROM_ID)"
	cd ../../image; $(PRIV_TOOLS)/bin/binboy @all
endif
	#$(TOPDIR)/makebin_src/make_wir294_bin root $(SQUASHFS) $(IMAGE_DIR)/root.bin

mk_image_ext2fs:
ifdef FULL_COPY
	cp -a $(FINAL_ROOT)/usr/etc/rcS $(IMAGE_DIR)
	cat $(IMAGE_DIR)/rcS | sed "s/^commander/#commander/g" > $(FINAL_ROOT)/usr/etc/rcS
	$(TOPDIR)/toolchain/buildroot/build_i386/genext2fs-1.3/genext2fs -U -e 0 -r 0 -b 5120 -i 2048 -d $(FINAL_ROOT) -D devtab.txt $(IMAGE_DIR)/initrd.dat
	mv $(IMAGE_DIR)/rcS $(FINAL_ROOT)/usr/etc/rcS
	gzip -9 < $(IMAGE_DIR)/initrd.dat > $(IMAGE_DIR)/initrd.img
	-rm -fr $(IMAGE_DIR)/initrd.dat
	ls -l $(IMAGE_DIR)/initrd.img
endif

mk_image_cramfs:
	$(TOPDIR)/tools/open/bin/mkcramfs -q -l -D devtab.txt $(FINAL_ROOT) $(IMAGE_DIR)/Cramfs.img
ifdef FULL_COPY
	$(PRIV_TOOLS)/bin/binman --s d.bms --d $(IMAGE_DIR)
	-rm -f $(IMAGE_DIR)/Cramfs.img
	ls -l $(IMAGE_DIR)/*.bin
else
	cd ../../image; $(PRIV_TOOLS)/bin/binboy @linux -r "$(ROM_ID)"
	cd ../../image; $(PRIV_TOOLS)/bin/binboy @rootfs -r "$(ROM_ID)"
	cd ../../image; $(PRIV_TOOLS)/bin/binboy @all
endif

mk_ui_squashfs:
	$(MKSQUASHFS) $(UI_DIR) $(IMAGE_DIR)/UI.sqfs -be
	$(PRIV_TOOLS)/bin/binman --s ui.bms --d $(IMAGE_DIR)	
	@rm -f $(IMAGE_DIR)/UI.sqfs
#

.PHONY: rootfs

rootfs:
	make -C .. all

#

.PHONY: boot

boot: gen_bms
	$(PRIV_TOOLS)/bin/binman --s b.bms --d $(IMAGE_DIR)

#

.PHONY: ui

ui:
	make mk_ui_squashfs

#
.PHONY: tja

tja:
	$(PRIV_TOOLS)/bin/binman --s tja.bms --d $(IMAGE_DIR)

#
include ../makefile.install
