#
# Makefile for glue logic
#

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

UCLIBCDIR = $(subst trendchip/mips-linux-uclibc/bin,trendchip/mips-linux-uclibc/lib,$(TOOLPATH))
STRIP=$(TARGET)-strip
#STRIP=$(CROSS_COMPILE)strip
#

.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
PRIV_TOOLS := $(TOPDIR)/tools/priv
OPEN_TOOLS := $(TOPDIR)/tools/open
#MKSQUASHFS := mksquashfs_lzma-3.2
MKSQUASHFS := $(TOPDIR)/tools/mksquashfs-lzma
SQUASHFS   := $(TOPDIR)/image/squashfs.o
ifeq ($(MYDLINK),y)
MYDLINK_DIR	   := $(TOPDIR)/rootfs/src/amit_priv/OMyDlink/MyDlink
endif
WIFI_BIN_PATH := $(FINAL_ROOT)/usr/mv2ram/etc/Wireless/RT2860AP

.PHONY: show_msg cp_glue mk_root cp_wifi_bin strip_proj mk_image_squashfs
#ifdef XXX
ifdef FULL_COPY

ifeq ($(MYDLINK),y)
main: show_msg cp_glue mk_root cp_wifi_bin strip_proj mk_ui_squashfs mk_image_squashfs mk_mydlink_squashfs  show_image image_add_time
else
main: show_msg cp_glue mk_root cp_wifi_bin strip_proj mk_ui_squashfs mk_image_squashfs show_image image_add_time
endif

else
main: show_msg cp_glue mk_root cp_wifi_bin strip_proj mk_image_squashfs show_image_gpl
endif
#endif
#main: show_msg mk_image_squashfs show_image

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

cp_glue: mk_pre4 inst_glue #INSTALL.copy_glue

inst_glue:
ifdef FULL_COPY
	@cp -a etc.inittab				$(INSTALL_ROOT)/usr/etc/inittab
	@cp -a etc.rcS					$(INSTALL_ROOT)/usr/etc/rcS
	@cp -a etc.fstab				$(INSTALL_ROOT)/usr/etc/fstab
	@cp -a etc.profile				$(INSTALL_ROOT)/usr/etc/profile
	@cp -a led.conf					$(INSTALL_ROOT)/usr/mv2ram/etc/led.conf
	@cp -a etc.services				$(INSTALL_ROOT)/usr/mv2ram/etc/services
	@cp -a etc.passwd				$(INSTALL_ROOT)/usr/mv2ram/etc/passwd
	@cp -a etc.group	 	   		$(INSTALL_ROOT)/usr/mv2ram/etc/group
	@cp -a etc.issue.net			$(INSTALL_ROOT)/usr/mv2ram/etc/issue.net
	@cp -a usr.bin.telnetd-action	$(INSTALL_ROOT)/usr/bin/telnetd-action
	@chmod 777						$(INSTALL_ROOT)/usr/etc/rcS
	@mkdir -p						$(INSTALL_ROOT)/root
	@MASCOT=`svn info | grep "Repository Root"`; MASCOT=$${MASCOT##*/}; \
			echo "$$MASCOT" > $(INSTALL_ROOT)/usr/mv2ram/etc/hostname
	@mkdir -p						$(INSTALL_ROOT)/usr/mv2ram/tmp/csman
	@mv pre4.dat					$(INSTALL_ROOT)/usr/mv2ram/tmp/csman
	@ln -fs /usr/www/pre7.dat		$(INSTALL_ROOT)/usr/mv2ram/tmp/csman/pre7.dat
	@ln -fs /usr/www/reset.dat		$(INSTALL_ROOT)/usr/mv2ram/tmp/csman/reset.dat
	@if [ -e $(TOPDIR)/image/svn.list ]; then cp -a $(TOPDIR)/image/svn.list $(INSTALL_ROOT)/usr/etc/svn.list ; fi
ifeq ($(ENABLE_MULTI_WAN),1)
	@cp -a etc.rt_tables			$(INSTALL_ROOT)/usr/mv2ram/etc/iproute2/rt_tables
endif

ifeq ($(FLASHOFDM_FOR_FINLAND),y)
	@echo "Copy FlashOFDM image for Finland "
	@mkdir -p						$(INSTALL_ROOT)/lib/modules
	@cp -a finland_ft3000.img		$(INSTALL_ROOT)/lib/modules/ft3000.img
endif
ifeq ($(FLASHOFDM_FOR_GERMANY),y)
	@echo "Copy FlashOFDM image for Germany "
	@mkdir -p						$(INSTALL_ROOT)/lib/modules
	@cp -a germany_ft3000.img		$(INSTALL_ROOT)/lib/modules/ft3000.img
endif

ifeq ($(MYDLINK),y)
#Add by aaron , for temporary bin file for MYDLINK
#	@cp -r -f -a mydlink		$(INSTALL_ROOT)/usr/mv2ram/
#	@cp -a libssl.so.0.9.8 $(INSTALL_ROOT)/lib/
#	@cp -a libcrypto.so.0.9.8 $(INSTALL_ROOT)/lib/
	@ln -fs /bin/busybox $(INSTALL_ROOT)/usr/bin/killall
	@ln -fs /ram/mydlink/mdb $(INSTALL_ROOT)/sbin/mdb
#	@ln -fs /usr/mydlink/mdb $(INSTALL_ROOT)/sbin/mdb
endif
	
endif
        
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 -cb 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) $(PROJECT_NAME)
	@./MAKEDEV  $(FINAL_ROOT)

cp_wifi_bin:
	@echo "*** Copy Wifi Bin ***"
ifeq (${CP_RT5350_BIN}, 1)
	@cp -f $(TOPDIR)/rootfs/glue/RT5350_AP_1T1R_V1_0.bin $(WIFI_BIN_PATH)
endif
ifeq (${CP_RT3352_BIN}, 1)
	@cp -f $(TOPDIR)/rootfs/glue/RT3352_AP_2T2R_V11.bin $(WIFI_BIN_PATH)
endif
ifeq (${CP_RT3883_BIN}, 1)
ifeq (${PROJECT_NAME}, CDE665NC_001)
	@cp -f $(TOPDIR)/rootfs/glue/RT3662_AP_2T3R_V0_0.bin $(WIFI_BIN_PATH)
else
	@cp -f $(TOPDIR)/rootfs/glue/RT3883_AP_3T3R_V0_1.bin $(WIFI_BIN_PATH)
endif
	@cp -f $(TOPDIR)/rootfs/glue/RT5392_PCIe_2T2R_ALC_V1_3.bin $(WIFI_BIN_PATH)
endif

ifeq (${CP_RT3052_BIN}, 1)
	@cp -f $(TOPDIR)/rootfs/glue/RT3050_AP_1T1R_V1_0.bin  $(WIFI_BIN_PATH)
endif
ifeq (${CP_RT3050_BIN}, 1)
	@cp -f $(TOPDIR)/rootfs/glue/RT3050_AP_1T1R_V1_0.bin  $(WIFI_BIN_PATH)
endif

strip_proj:
ifeq (${STRIP_FINAL_PROJ}, y)
	@echo "*** STRIP ALL Binary and Library ***"
	@-for t in `ls ${FINAL_ROOT}/usr/bin` ; do \
		if [ "`head -n 1 ${FINAL_ROOT}/usr/bin/$$t | grep '#!/bin'`" = "" ]; then \
			${STRIP} --remove-section=.note --remove-section=.comment ${FINAL_ROOT}/usr/bin/$$t; \
		fi \
	done;
	@-for t in `ls ${FINAL_ROOT}/usr/sbin` ; do \
		if [ "`head -n 1 ${FINAL_ROOT}/usr/sbin/$$t | grep '#!/bin'`" = "" ]; then \
			${STRIP} --remove-section=.note --remove-section=.comment ${FINAL_ROOT}/usr/sbin/$$t; \
		fi \
	done;
	@-for t in `ls ${FINAL_ROOT}/lib/iptables` ; do \
		if [ -f ${FINAL_ROOT}/lib/iptables/$$t ]; then \
			${STRIP} --remove-section=.note --remove-section=.comment ${FINAL_ROOT}/lib/iptables/$$t; \
		fi \
	done;
	@-for t in `ls ${FINAL_ROOT}/lib` ; do \
		if [ -f ${FINAL_ROOT}/lib/$$t ]; then \
			${STRIP} --strip-unneeded  ${FINAL_ROOT}/lib/$$t; \
		fi \
	done; 
	@-for t in `ls ${FINAL_ROOT}/lib/modules/kernel/*/*` ; do \
		if [ -f $$t ]; then \
			${STRIP} --strip-unneeded $$t; \
		fi \
	done;
	@-for t in `ls ${FINAL_ROOT}/lib/modules/kernel/*/*/*` ; do \
		if [ -f $$t ]; then \
			${STRIP}  --strip-unneeded $$t; \
		fi \
	done;
	@-for t in `ls ${FINAL_ROOT}/lib/modules/kernel/*/*/*/*` ; do \
		if [ -f $$t ]; then \
			${STRIP} --strip-unneeded $$t; \
		fi \
	done;
	@-for t in `ls ${FINAL_ROOT}/lib/modules/kernel/*/*/*/*/*` ; do \
		if [ -f $$t ]; then \
			${STRIP} --strip-unneeded  $$t; \
		fi \
	done;
	@echo ""
endif

mk_image_squashfs:
	@rm -f $(SQUASHFS)
	@echo "*** Compressing ROOTFS ***"
	@$(MKSQUASHFS) $(FINAL_ROOT) $(SQUASHFS) -be -all-root > /dev/null
ifdef FULL_COPY
ifeq ($(PROJECT_VOICE), DISABLE_VOICE)
	@$(PRIV_TOOLS)/bin/binman --s d_cdg.bms --d $(IMAGE_DIR)
	#@ls -l $(IMAGE_DIR)/*.bin
else
	@$(PRIV_TOOLS)/bin/binman --s d.bms --d $(IMAGE_DIR)
	#@ls -l $(IMAGE_DIR)/*.bin
endif
else
	@$(PRIV_TOOLS)/bin/binboy-static > /dev/null 2>&1 ;\
		if [ "$$?" = "0" ]; then \
			cd ../../image; \
			$(PRIV_TOOLS)/bin/binboy-static @linux ; \
			$(PRIV_TOOLS)/bin/binboy-static @rootfs ; \
			$(PRIV_TOOLS)/bin/binboy-static @all ; \
			cd - ;\
		    exit 0; \
		else \
			$(PRIV_TOOLS)/bin/binboy > /dev/null 2>&1 ;\
    		if [ "$$?" = "0" ]; then \
				cd ../../image; \
				$(PRIV_TOOLS)/bin/binboy @linux ; \
				$(PRIV_TOOLS)/bin/binboy @rootfs ; \
				$(PRIV_TOOLS)/bin/binboy @all ; \
				cd - ;\
		        exit 0; \
		    else \
		        echo "Error !! Can't apply header on firmware, please check your compile environment !"; \
		        echo ""; \
		        exit 1; \
		    fi; \
		fi
endif

show_image:
ifeq ($(MYDLINK),y)
	@$(PRIV_TOOLS)/bin/check_image BOOT.bin kernel.bin rootfs.bin ui.bin mydlink.bin
else
	@$(PRIV_TOOLS)/bin/check_image BOOT.bin kernel.bin rootfs.bin ui.bin
endif

show_image_gpl:
	@echo ""
	@echo "======= Show image size ======"
	@echo "kernel.bin  : `ls -l ../../image/kernel.bin   |awk '{print $$5}'` (`du -h ../../image/kernel.bin  | awk '{print $$1}'`)"
	@echo "rootfs.bin  : `ls -l ../../image/rootfs.bin   |awk '{print $$5}'` (`du -h ../../image/rootfs.bin  | awk '{print $$1}'`)"
	@echo "firmware.bin: `ls -l ../../image/firmware.bin |awk '{print $$5}'` (`du -h ../../image/firmware.bin| awk '{print $$1}'`)"
	@echo ""
	@echo "======= Show ROM/FM ID info.  ======"
	@if [ -e ../../../tools/src/BinMan/binman -a -e ../../../tools/src/BinMan/binman.bms ]; then \
		cp ../../../tools/src/BinMan/binman .  ;\
		cp ../../../tools/src/BinMan/binman.bms . ;\
		./binman ../../image/firmware.bin  | grep "rom_id\|family" ;\
		rm -f binman binman.bms ;\
	fi
	@echo ""
	@echo ""


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 -be -all-root > /dev/null
	@$(PRIV_TOOLS)/bin/binman --s ui.bms --d $(IMAGE_DIR) > /dev/null
	@rm -f $(IMAGE_DIR)/UI.sqfs

ifeq ($(MYDLINK),y)
mk_mydlink_squashfs:
	@$(MKSQUASHFS) $(MYDLINK_DIR) $(IMAGE_DIR)/mydlink.sqfs -be -all-root > /dev/null
	@$(PRIV_TOOLS)/bin/binman --s mydlink.bms --d $(IMAGE_DIR) > /dev/null
	@rm -f $(IMAGE_DIR)/mydlink.sqfs
endif

image_add_time:
	@cd $(IMAGE_DIR) ; \
	find -name "*.*-*.bin" -delete ; \
	for f in * ; do \
		[ "$$f" = "$${f%%.bin}" ] && continue ; \
		f_="$${f%%.bin}" ; \
		t="`stat -c %y \"$$f\"`" ; \
		t1="`expr substr \"$$t\" 6 5`" ; \
		t1="`echo $$t1 | sed 's/-//g'`" ; \
		t2="`expr substr \"$$t\" 12 5`" ; \
		t2="`echo $$t2 | sed 's/://g'`" ; \
		mv "$$f" "$$f_.$$t1-$$t2.bin" ; \
	done ; \
	cd -

.PHONY: rootfs
rootfs:
	make -C .. all

#include ../makefile.install

