#
# Copyright (C) 2009-2010 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/image.mk

img_head:=TL-ER7520Gv1_en_1.0.0
partition_table:="partition.json"
build_date:=$(shell date +%Y%m%d)
#build_time:=$(shell date +%H%M%S)
build_time:=$(shell expr $(shell date +%s) - $(shell date -d $(shell date +%x) +%s))
img_version:="$(img_head)_[$(build_date)-re$(build_time)]"
soft_version:="Build.$(build_date).Rel.$(build_time)"
boot_loader_file="$(TOPDIR)/tplink/u-boot/bootloader.bin"
#product_name:=$(shell cat $(TOPDIR)/.model)

define Image/BuildKernel
	$(STAGING_DIR_HOST)/bin/lzma e $(KDIR)/vmlinux $(KDIR)/$(IMG_PREFIX)-vmlinux.lzma
	$(CP) $(LINUX_DIR)/arch/$(LINUX_KARCH)/boot/dts/$(M_DTB_DIR)/$(M_DTB_NAME).dtb $(KDIR)/$(M_DTB_NAME).dtb
	dd if=$(KDIR)/$(M_DTB_NAME).dtb of=$(KDIR)/$(IMG_PREFIX)-vmlinux.lzma.bin bs=131072 count=1 conv=sync
	dd if=$(KDIR)/$(IMG_PREFIX)-vmlinux.lzma of=$(KDIR)/$(IMG_PREFIX)-vmlinux.lzma.bin bs=131072 seek=1
	$(CP) $(KDIR)/$(IMG_PREFIX)-vmlinux.lzma.bin $(BIN_DIR)/$(IMG_PREFIX)-vmlinux.lzma.bin
endef

define Image/Build/squashfs
    $(call prepare_generic_squashfs,$(KDIR)/root.squashfs)
endef

define Image/Build/ext4/erlite
	mkdir -p $(KDIR)/sysupgrade-erlite/
	echo "BOARD=erlite" > $(KDIR)/sysupgrade-erlite/CONTROL
	$(CP) $(KDIR)/vmlinux.elf $(KDIR)/sysupgrade-erlite/kernel
	$(CP) $(KDIR)/root.ext4 $(KDIR)/sysupgrade-erlite/root
	(cd $(KDIR); $(TAR) cvf \
		$(BIN_DIR)/$(IMG_PREFIX)-erlite-sysupgrade.tar sysupgrade-erlite)
endef

define Image/Build/ext4
    $(call Image/Build/ext4/erlite)
endef

define Image/Build/cleanup
	rm -rf $(BIN_DIR)/$(IMG_PREFIX)-upgrade*.bin
	rm -rf $(BIN_DIR)/$(img_head)*up.bin
endef

define Image/Build/TPLINK
	$(call Image/Build/cleanup)
	#cp -f $(TOPDIR)/tplink/configs/$(PRODUCT)/resources/* $(BIN_DIR)/ ; 
	cd $(BIN_DIR) && rm -f *.json *_*flash*.bin *_up.bin *_up.debug.bin ; \
	rm -f $(TOPDIR)/tplink/configs/$(PRODUCT)/database/database.tar.gz ; \
	if [ -d $(TOPDIR)/tplink/configs/$(PRODUCT)/database ]; then \
		cd $(BIN_DIR) ; \
		tar -C $(TOPDIR)/tplink/configs/$(PRODUCT)/ -czf $(BIN_DIR)/database.tar.gz database/; \
		cp database.tar.gz $(TOPDIR)/tplink/configs/$(PRODUCT)/database/; \
		cd -; \
	fi; \
	rm -f $(TOPDIR)/tplink/configs/$(PRODUCT)/database2/database2.tar.gz ; \
	if [ -d $(TOPDIR)/tplink/configs/$(PRODUCT)/database2 ]; then \
		cd $(BIN_DIR) ; \
		tar -C $(TOPDIR)/tplink/configs/$(PRODUCT)/ -czf $(BIN_DIR)/database2.tar.gz database2/; \
		cp database2.tar.gz $(TOPDIR)/tplink/configs/$(PRODUCT)/database2/; \
		cd -; \
	fi; \

	cd $(TOPDIR)/tplink/configs/$(PRODUCT)/resources/ ; \
	allfile=`find . -maxdepth 1 -type f | sed 's#.*/##'` ; \
	for temp_file in $$$${allfile} ; \
	do \
		cp -f $$$${temp_file} $(BIN_DIR)/ ; \
	done; \
	alldirs=`ls -d */` ; \
	for temp_dir in $$$${alldirs} ; \
	do \
		cd $$$${temp_dir} ; \
		allfile=`find . -maxdepth 1 -type f | sed 's#.*/##'` ; \
		for temp_file in $$$${allfile} ; \
		do \
			cp -f $$$${temp_file} $(BIN_DIR)/ ; \
		done; \
		cd ../ ; \
	done ; \
	cd $(BIN_DIR) ; \
	image_type_dir=$(TOPDIR)/tplink/configs/$(PRODUCT)/image_type ; \
	echo "%%%%%%%%%%%" ; \
	echo $$$${image_type_dir} ; \
	if [ -d $$$${image_type_dir} ]; then \
		image_type_list=`ls $$$${image_type_dir} | sed 's/\r//'` ; \
		for image_type in $$$${image_type_list} ; \
		do \
			rootfs_image_type_dir=$(TARGET_DIR)-$$$${image_type} ; \
			if [ -d $$$${rootfs_image_type_dir} ]; then \
				rm -rf $$$${rootfs_image_type_dir}/ ; \
			fi ; \
			mkdir -p $$$${rootfs_image_type_dir} ; \
			cp -rf $(TARGET_DIR)/* $$$${rootfs_image_type_dir}/ ; \
			cp -rf $(TOPDIR)/tplink/configs/$(PRODUCT)/image_type/$$$${image_type}/* $$$${rootfs_image_type_dir}/ ; \
			cp -rf $$$${rootfs_image_type_dir}/../cli_server/ipkg-cn913x/cli_server/usr/sbin/cli_server_dbg $$$${rootfs_image_type_dir}/usr/sbin/ ; \
			mv -f $$$${rootfs_image_type_dir}/usr/sbin/cli_server_dbg $$$${rootfs_image_type_dir}/usr/sbin/cli_server ; \
			$(STAGING_DIR_HOST)/bin/mksquashfs4 $$$${rootfs_image_type_dir}/ $(BIN_DIR)/root-$$$${image_type}.squashfs  -nopad \
			-noappend -root-owned -comp xz -Xpreset 9 -Xe -Xlc 0 -Xlp 2 -Xpb 2  -b 512k -p '/dev d 755 0 0' -p '/dev/console c 600 0 0 5 1' -processors 1; \
			$(STAGING_DIR_HOST)/bin/padjffs2 $(BIN_DIR)/root-$$$${image_type}.squashfs 4 8 16 64 128 256 ; \
			dd if=$(BIN_DIR)/root-$$$${image_type}.squashfs of=$(BIN_DIR)/openwrt-cn913x-root-$$$${image_type}.squashfs bs=128k conv=sync ; \
		done ; \
	fi;\
	date_now=`date +%Y%m%d%H%M%S` ; \
	all_model_json=`find . -name 'model*.json' -type f | sed 's#.*/##'` ; \
	echo "######################################" ; \
	echo $$$${all_model_json} ; \
	for temp_model_json in $$$${all_model_json} ; \
	do \
		echo $$$${temp_model_json} ; \
		$(STAGING_DIR_HOST)/bin/dkmgt_firmware_make -b -m $$$${temp_model_json} -T $$$${date_now} ; \
	done; \
	cd $(BIN_DIR) ; \
	all_up_bin=`find . -name '*_common_up.bin' -type f | sed 's#.*/##'` ; \
	for temp_up_bin in $$$${all_up_bin} ; \
	do \
		common_up_image_prefix=`echo -n $$$${temp_up_bin} | sed 's/_common_up.bin//g'` ; \
		rm -f $$$${common_up_image_prefix}*flash.bin ; \
	done ; \
	all_flash_bin=`find . -name '*nandflash.bin' -type f | sed 's#.*/##'` ; \
	for temp_flash_bin in $$$${all_flash_bin} ; \
	do \
		echo $$$${temp_flash_bin} ; \
		flash_image_prefix=`echo -n $$$${temp_flash_bin} | sed 's/nandflash.bin//g'` ; \
		ecc_tool -b 4 -p 2048 -s 128 -i $$$${flash_image_prefix}nandflash.bin -o $$$${flash_image_prefix}nandflash_ecc.bin ; \
		mv $$$${flash_image_prefix}nandflash.bin $$$${flash_image_prefix}nandflash_noecc.bin ; \
		soft_ver=`echo $$$${flash_image_prefix} | grep -o -P '[0-9]\.[0-9]\.[0-9]'`	; \
		soft_date=`echo $$$${flash_image_prefix} | grep -o -P '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'` ; \
		soft_time=`echo $$$${flash_image_prefix} | grep -o -P 'rel[0-9][0-9][0-9][0-9][0-9]' | grep -o -P '[0-9][0-9][0-9][0-9][0-9]'`	; \
		sub_dir="$$$${soft_ver} Build $$$${soft_date} Rel.$$$${soft_time}" ; \
		dirname=$(TOPDIR)/image/$(PRODUCT)/"$$$${sub_dir}" ; \
		if [ ! -d "$$$${dirname}" ];then \
			mkdir -p "$$$${dirname}" ; \
		fi ; \
		if [ -e makeParitionTable.sh ]; then \
			. makeParitionTable.sh ; \
			cp ParitionTable.bin $$$${flash_image_prefix}_ParitionTable.bin ; \
		fi; \
		cp -f $(BIN_DIR)/"$$$${flash_image_prefix}"*.bin "$$$${dirname}" ; \
		cp -f $(BIN_DIR)/*up.bin "$$$${dirname}" ; \
		cp -f $(BIN_DIR)/*norflash.bin "$$$${dirname}" ; \
	done ; \
	cd $(BIN_DIR) ; \
	cp -f $(BIN_DIR)/openwrt-cn913x-root.squashfs $(TOPDIR)/image/$(PRODUCT)/"$$$${sub_dir}" ; \
	cp -f $(BIN_DIR)/openwrt-cn913x-vmlinux.lzma.bin $(TOPDIR)/image/$(PRODUCT)/"$$$${sub_dir}" ;
endef

# Marvell cn913x Board 
define Image/BuildKernel/Profile/cn913x
	cp $(KDIR)/Image $(BIN_DIR)/openwrt-cn913x-Image
	cp $(KDIR)/cn9131-db-B.dtb $(BIN_DIR)/openwrt-cn913x.dtb
endef

define Image/Build
	$(call Image/Build/$(1))
	dd if=$(KDIR)/root.$(1) of=$(BIN_DIR)/$(IMG_PREFIX)-root.$(1) bs=128k conv=sync
	$(call Image/BuildKernel/Profile/cn913x)
ifdef CONFIG_PACKAGE_dkmgt
	$(call Image/Build/TPLINK)
endif
endef

$(eval $(call BuildImage))
