##############################################
# OpenWrt Makefile for bootloader
#
##############################################


include $(TOPDIR)/rules.mk

# Build in kernel build dir
include $(INCLUDE_DIR)/kernel.mk

ifeq ($(PR_NAME),)
$(error PR_NAME should not be empty)
endif


# Name and release number of this package
PKG_NAME:=bootloader
PKG_RELEASE:=1

# this package type is bin not ipkg, no ipk needed
PKG_TARGETS:=bin

# This specifies the directory where we're going to build the program.
PKG_BUILD_DIR:=$(KERNEL_BUILD_DIR)/$(PKG_NAME)

FACTORYBOOT_BUILD_DIR:=$(PKG_BUILD_DIR)/factory_boot

PKG_SRC_DIR:=$(subst ",,$(CONFIG_BOOT_SRC_DIR))

#used in factory boot
COMMON_SRC_DIR:=shared_libs

include $(INCLUDE_DIR)/package.mk

define Package/bootloader
  SECTION:=boot
  CATEGORY:=Boot Loader
  TITLE:=bootloader for NVMP
  MENU:=1
endef

FACTORY_BOOT_IMAGE:=$(if $(IMAGE),$(IMAGE),slp-$(BOARD)-$(PROFILE)-factory_boot.bin)


ifdef PKG_SRC_DIR


#FACTORYBOOT_MAKEOPTS used to compile factoryboot
include $(PKG_SRC_DIR)/configs/factoryboot.mk

endif


# if COMPRESSED_UBOOT is 1, then tuboot.bin is the final binary file.
# if COMPRESSED_UBOOT is 0, then u-boot.bin is the final binary file.
# the value of the parameter "COMPRESSED_UBOOT" is got from parsing the
COMPRESSED_FACTORY_BOOT=$(patsubst COMPRESSED_UBOOT=%,%,$(filter COMPRESSED_UBOOT=%, $(FACTORYBOOT_MAKEOPTS)))
ifeq ($(COMPRESSED_FACTORY_BOOT),y)
	COMPRESSED_FACTORY_BOOT=1
endif
ifeq ($(COMPRESSED_FACTORY_BOOT),1)
	FACTORY_BOOT_BINARY:=factory_tuboot.bin
else
	FACTORY_BOOT_BINARY:=factory_u-boot.bin
endif

ifdef PKG_SRC_DIR
	include $(PKG_SRC_DIR)/configs/compile.mk
endif

# Specify what needs to be done to prepare for building the package.
# In our case, we need to copy the source files to the build directory.
# This is NOT the default.  The default uses the PKG_SOURCE_URL and the
# PKG_SOURCE which is not defined here to download the source from the web.
define Build/Prepare
	$(Build/Prepare/factory_boot)
endef

define Build/Configure
	$(Build/Configure/factory_boot)
endef

define Build/Compile
	$(Build/Compile/factory_boot)
endef

define Package/bootloader/install
	$(Package/bootloader/install/factory_boot)
endef

$(eval $(call BuildPackage,bootloader))
