include .config
include Makefile.check
include $(ENV_PATH)/platform/linux/env.mk

export ENV_PATH:= $(ENV_PATH)
export PLATFORM := $(PLATFORM)
export FLASH_SIZE := $(FLASH_SIZE)
export WL_PLATFORM := $(WL_PLATFORM)
export PRJ_PATH := $(ENV_PATH)
export FS_PATH := $(ENV_PATH)/rootfs/target
export KERNEL_PATH := $(ENV_PATH)/platform/linux/kernels/mips-linux-2.6.31
export LOADER_PATH := $(ENV_PATH)/platform/linux/u-boot
export APPS_PATH := $(ENV_PATH)/apps
export IMG_PATH := $(ENV_PATH)/image
export TOOL_PATH := $(ENV_PATH)/tools
export WWW_PATH := $(ENV_PATH)/www/$(CONFIG_MODEL_NAME)
export PLATFORM_PATH := $(ENV_PATH)/platform/linux
export PATH:=$(TOOL_PATH):${PATH}
export CONFIG_MODEL_NAME := $(CONFIG_MODEL_NAME)
export CONFIG_MODEL_NAME_NO_DASH := $(shell perl -w $(PRJ_PATH)/tools/split.pl $(CONFIG_MODEL_NAME))
export WL_PATH := $(ENV_PATH)/wireless/driver
export APPS_CROSS := $(APPS_CROSS)
export KRL_CROSS := $(KRL_CROSS)
export APPS_CFLAG := $(APPS_CFLAG)
export KRL_CFLAG := $(KRL_CFLAG)
export WL_CROSS := $(WL_CROSS)
export WL_CFLAG := $(WL_CFLAG)
include Makefile_$(WL_PLATFORM).inc

ifeq ($(WL_PLATFORM), AP136)
        __PLATFORM__=linux
else
        __PLATFORM__=$(PLATFORM)
endif

# in ./.config
ifeq ($(FLASH_SIZE),)
export FLASH_SIZE := 8
else
export FLASH_SIZE := $(FLASH_SIZE)
endif

export HARDWARE_ID := $(HARDWARE_ID)

all: mytools build install

kernel: kernel_build install

kernel_build: mytools
	$(MAKE) -C platform

kernel_clean:
	cd $(KERNELPATH) &&  $(MAKEARCH) mrproper

normal: normal_build install

normal_build:	
	$(MAKE) -C apps || exit 1 ;

ifeq ($(__PLATFORM__), linux)
SUBDIRS:=rootfs platform www

$(SUBDIRS):
	$(MAKE) -C $@

APPS:=$(wildcard apps/*)

$(APPS) apps/%:
	$(MAKE) -C apps $(notdir $@)
 
$(addsuffix -install, $(APPS)):
	$(MAKE) -C apps $(notdir $@)
$(addsuffix -clean, $(APPS)):
	$(MAKE) -C apps $(notdir $@)
endif

normal_clean:
	$(MAKE) -C apps clean;


ifeq ($(WL_PLATFORM), AP136)
loader: mytools
	$(MAKE) -C platform redboot_build
else
loader:
	$(MAKE) -C platform redboot_build AP_TYPE=$(AP_TYPE) ETH_SWITCH=$(ETH_SWITCH)
endif

loader_clean:
	$(MAKE) -C platform redboot_clean AP_TYPE=$(AP_TYPE)

build:
	echo $(PATH)
	rm -fr $(IMG_PATH)
	mkdir -p $(IMG_PATH)
	$(MAKE) -C rootfs
	$(MAKE) -C platform 
	$(MAKE) -C apps	
	
mytools:
	@echo =================== tools ===================
	@if [ ! -e ./tools/cp_html/cp_html ] ; then \
		$(MAKE) -C ./tools/cp_html ; \
	else \
		echo " *** cp_html already build. (skip)" ; \
	fi
ifeq ($(__PLATFORM__), linux)
	@if [ ! -e ./tools/mksquashfs_db12x ] ; then \
		$(MAKE) -C ./tools/squashfs-4.0/squashfs-tools/; \
		cp ./tools/squashfs-4.0/squashfs-tools/mksquashfs ./tools/mksquashfs_db12x ;\
		chmod 755 ./tools/mksquashfs_db12x ; \
	else \
		echo " *** mksquashfs (4.0) already build. (skip)" ; \
	fi
	@if [ ! -f ./tools/lzma_4.57/lzma ] ; then \
		$(MAKE) -C ./tools/lzma_4.57/CPP/7zip/Compress/LZMA_Alone/ -f makefile.gcc || exit 1 ; \
		cp ./tools/lzma_4.57/CPP/7zip/Compress/LZMA_Alone/lzma ./tools/lzma_4.57/ ; \
	else \
		echo " *** lzma (4.57) already build. (skip)"; \
	fi
else
	$(MAKE) -C ./tools/squashfs-tools
	$(MAKE) -C ./tools/squashfs-tools4 install
	$(MAKE) -C ./tools/lzma/sdk4.17/SRC/7zip/Compress/LZMA_Alone
endif

install:
	$(MAKE) -C platform install
	$(MAKE) -C apps install || exit 1
	$(MAKE) -C rootfs install
	@echo =================== Finish ===================

rootfs-install:
	$(MAKE) -C rootfs install
platform-install:
	$(MAKE) -C platform install

clean: 
	$(MAKE) -C apps clean
	$(MAKE) -C platform clean
	$(MAKE) -C rootfs clean
	$(MAKE) -C ./tools/cp_html clean
	$(MAKE) -C ./tools/squashfs-tools clean
	$(MAKE) -C ./tools/squashfs-tools4 clean
	$(MAKE) -C ./tools/squashfs-4.0/squashfs-tools clean
	$(MAKE) -f makefile.gcc -C ./tools/lzma_4.57/CPP/7zip/Compress/LZMA_Alone clean 
	@if [ -e ./tools/mksquashfs_db12x ] ; then \
		rm ./tools/mksquashfs_db12x ; \
	fi
	@if [ -e ./tools/lzma_4.57/lzma ] ; then \
		rm ./tools/lzma_4.57/lzma ; \
	fi

conf mconf:
ifeq ($(AP_TYPE), AP91)
	cp -f ./config/Config_ap91 ./config/Config
endif
ifeq ($(AP_TYPE), AP93)
	cp -f ./config/Config_ap93 ./config/Config
endif
ifeq ($(AP_TYPE), AP99)
	cp -f ./config/Config_ap99 ./config/Config
endif
	$(MAKE) -C config
	@./config/$@ ./config/Config
	# kernel configure
	$(MAKE) apps$@

appsconf:
	$(MAKE) -C $(APPS_PATH) config

appsmconf:
	$(MAKE) -C $(APPS_PATH) menuconfig
		

menuconfig: mconf

# Default configurations
.config:
	cp config/defconfig $@
	$(MAKE) mconf
    
apps:
	$(MAKE) -C apps clean
	$(MAKE) -C apps 

platform_%:
	$(MAKE) -C platform $(subst platform_,,$@ )

help:
	@echo "platform: perform make -C platform"
	@echo "platform-install: perform make -C platform-install"
	@echo "platform_kernel: build platform/<plat>/kernels"
	@echo "platform_driver: build platform/<plat>/drivers"
	@echo "rootfs-install: final step, create image from rootfs/target"
#
### Generic rules
#
%:
	[ ! -d $* ] || $(MAKE) -C $*

%-clean:
	[ ! -d $* ] || $(MAKE) -C $* clean

%-install:
	[ ! -d $* ] || $(MAKE) -C $* install || exit 1
		
.PHONY: all clean install mytools
.PHONY: apps 
.PHONY: dummy $(APPS) $(SUBDIRS)
