#
# Makefile for full system
#

export TARGET   ?= mips-linux-uclibc
export TOPDIR   := $(shell pwd)
export TOOLPATH ?= $(shell cat TOOLPATH)
export PATH := $(TOOLPATH):$(PATH)

export LC_ALL := C

#
ifneq ($(wildcard ROM_ID),)
export ROM_ID := `cat $(TOPDIR)/ROM_ID`
endif

TOOLS_LIST  := tools
KERNEL_LIST := kernel_build kernel_install
ROOTFS_LIST := busybox rootfs
PART_LIST   := bootloader $(TOOLS_LIST) $(KERNEL_LIST) $(ROOTFS_LIST) show_o3g_version
CLEAN_LIST  := $(TOOLS_LIST) linux $(ROOTFS_LIST)
	
.PHONY: all toolchain $(PART_LIST) 

all:  check_project $(PART_LIST) 

tools rootfs busybox:
	@make -s -C $@ all

bootloader:
	@make -s -C bootloader

kernel_install:
	@make -s -C linux install

kernel_build:
	@TARGET=mips-linux make -C linux all

#

#make a whole image
linuxall: 
	@make clean 
	@make -s $(PART_LIST)

#make a kernel.bin
linux: kernel_build kernel_install glue

#quickly make a filesystem
glue:
	@make -s -C rootfs glue

#make filesystem
rootfs: $(ROOTFS_LIST)

help:
	@echo "Make Command List"
	@echo "make: make all of the project without cleaning"
	@echo "make linuxall: make a whole new world"
	@echo "make linux: make kernel"
	@echo "make rootfs: make filesystem (include busybox, rootfs)"
	@echo "make busybox: make busybox and install to busybox/root"
	@echo "make glue: quickly make filesystem"
	@echo "make clean: clean linux ,filesystem"
	@echo "make svnst: check the all Modified/Add component in project "
	@echo "make svnlist: export the svn path/version of the used component  "
	@echo "make list: show the svn path/version of the used component and the trunk*"
	@echo "make GPL: release GPL code of the current project"
	@echo "make GPL_tools: re-package tools code for GPL"
	@echo "make GPL_linux: re-package linux code for GPL"
	@echo "make GPL_rootfs: re-package rootfs code for GPL"
	@echo "make GPL_pack: package GPL code"
	@echo "make chkGPL: try to make GPL firmware, and compare with normal firmware"
	
show_o3g_version:
	@if [ -f rootfs/glue/get_o3g_version ]; then \
		echo "----------------------------"; \
		rootfs/glue/get_o3g_version; \
		echo "----------------------------"; \
	fi

check_project:
	@if [ ! -f PROJECT ] ; then \
		make -C project_config menuconfig; \
	else \
		make -C project_config check; \
	fi

menuconfig:
	@make -C project_config clean
	@make -C project_config menuconfig
	@. project_config/project.config ; \
		[ "$$CLEAN_PROJ" = "y" ] && \
			echo "Clean UP Project" && make -s clean || \
			echo "No Clean UP Project"

svnlist:
	@rm -f image/svn.list
	@url="`svn info | grep URL: | cut -d " " -f 2`" ;\
	ver="`svn info | grep Rev: | cut -d " " -f 4`" ;\
	echo "$$url -r $$ver" >> image/svn.list ; 
	@url="`svn info bootloader/src | grep URL: | cut -d " " -f 2`" ;\
	ver="`svn info bootloader/src | grep Rev: | cut -d " " -f 4`" ;\
	echo "$$url -r $$ver" >> image/svn.list ;
	@url="`svn info linux/src | grep URL: | cut -d " " -f 2`" ;\
	ver="`svn info linux/src | grep Rev: | cut -d " " -f 4`" ;\
	echo "$$url -r $$ver" >> image/svn.list ;
	@for tmp_comp in `grep "PRJ_INSTALL_[_0-9a-zA-Z\-]*=[1|y]" ${TOPDIR}/project_config/project.instcomp`; \
	do  \
		tmp_comp=$${tmp_comp%%=1} ; \
		inst_comp=$${tmp_comp##PRJ_INSTALL_} ; \
		if [ -d rootfs/src/amit_priv/$${inst_comp} ]; then \
			url="`svn info rootfs/src/amit_priv/$${inst_comp} | grep URL: | cut -d " " -f 2`" ;\
			ver="`svn info rootfs/src/amit_priv/$${inst_comp} | grep Rev: | cut -d " " -f 4`" ;\
			echo "$$url -r $$ver" >> image/svn.list ; \
		elif [ -d rootfs/src/open_src/$${inst_comp} ]; then \
			url="`svn info rootfs/src/open_src/$${inst_comp} | grep URL: | cut -d " " -f 2`" ;\
			ver="`svn info rootfs/src/open_src/$${inst_comp} | grep Rev: | cut -d " " -f 4`" ;\
			echo "$$url -r $$ver" >> image/svn.list ; \
		elif [ -d rootfs/src/amit_open/$${inst_comp} ]; then \
			url="`svn info rootfs/src/amit_open/$${inst_comp} | grep URL: | cut -d " " -f 2`" ;\
			ver="`svn info rootfs/src/amit_open/$${inst_comp} | grep Rev: | cut -d " " -f 4`" ;\
			echo "$$url -r $$ver" >> image/svn.list ; \
		fi;\
	done 

CLEAN_LIST := $(patsubst %,clean-%,$(CLEAN_LIST))

.PHONY: clean $(CLEAN_LIST)

clean: $(CLEAN_LIST)
	rm -f image/*.bin
	rm -f image/zImage.lzma
	rm -f image/squashfs.o

$(CLEAN_LIST):
	@echo "*** Cleaning $(patsubst clean-%,%,$@)..."
	@make -s -C $(patsubst clean-%,%,$@) clean

#

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