#
# Makefile for glue logic
#

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

UCLIBCDIR = $(subst buildroot-gcc342/bin/,buildroot-gcc342/mipsel-linux-uclibc/lib,$(TOOLPATH))

#

.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 := mksquashfs_lzma-3.2
SQUASHFS := $(TOPDIR)/image/squashfs.o

#

.PHONY: show_msg cp_glue mk_root mk_image_squashfs

ifdef FULL_COPY
main: show_msg cp_glue mk_root mk_ui_squashfs mk_image_squashfs show_image
else
main: show_msg cp_glue mk_root mk_image_squashfs
endif

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

cp_glue: mk_pre4 INSTALL.copy_glue

mk_pre4:
ifdef FULL_COPY
	@export CORNER_DE_PRJROOT=$(TOPDIR); FW_REV=`$(TOPDIR)/rootfs/glue/getfwrev`; \
	FW_VER=${shell cat $(TOPDIR)/VERSION}; \
	MASCOT=`svn info | grep "Repository Root"`; MASCOT=$${MASCOT##*/}; \
	echo ""; \
	echo "-------------------------------------------------"; \
	echo "$$MASCOT,$$FW_VER,$$FW_REV"; \
	echo "-------------------------------------------------"; \
	echo ""
	@export 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 -zc pre4.txt -X c > /dev/null
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) $(UCLIBCDIR)
	@./MAKEDEV  $(FINAL_ROOT)
	@cp -f $(TOPDIR)/rootfs/glue/RT3050_AP_1T1R_V1_0.bin $(FINAL_ROOT)/usr/mv2ram/etc/Wireless/RT2860AP

mk_image_squashfs:
	@rm -f $(SQUASHFS)
	@echo "Compressing ROOTFS ..."
	@$(MKSQUASHFS) $(FINAL_ROOT) $(SQUASHFS) -le -all-root > /dev/null
ifdef FULL_COPY
	@$(PRIV_TOOLS)/bin/binman --s d.bms --d $(IMAGE_DIR)
	#@ls -l $(IMAGE_DIR)/*.bin
else
	@cd ../../image; $(PRIV_TOOLS)/bin/binboy @linux
	@cd ../../image; $(PRIV_TOOLS)/bin/binboy @rootfs
	@cd ../../image; $(PRIV_TOOLS)/bin/binboy @all
endif

show_image:
	@./check_image

show_image_old:
	@ROOTSIZE=$(shell du -s $(FINAL_ROOT) | cut -f1); \
	IMGSIZE=$(shell du -s $(SQUASHFS) |  cut -f1); \
	echo "=============================================="; \
	echo "Summary:"; \
	echo "==>Squashfs disk size  = $$ROOTSIZE KBytes"; \
	echo "==>Squashfs image size = $$IMGSIZE KBytes"; \
	echo "=============================================="

mk_ui_squashfs:
	@$(MKSQUASHFS) $(UI_DIR) $(IMAGE_DIR)/UI.sqfs -le -all-root -no-progress > /dev/null
	@$(PRIV_TOOLS)/bin/binman --s ui.bms --d $(IMAGE_DIR) > /dev/null
	@rm -f $(IMAGE_DIR)/UI.sqfs

#

.PHONY: rootfs

rootfs:
	make -C .. all

#

include ../makefile.install

