###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
# Copyright (C) 2007-2018  IPFire Team  <info@ipfire.org>                     #
#                                                                             #
# This program is free software: you can redistribute it and/or modify        #
# it under the terms of the GNU General Public License as published by        #
# the Free Software Foundation, either version 3 of the License, or           #
# (at your option) any later version.                                         #
#                                                                             #
# This program is distributed in the hope that it will be useful,             #
# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
# GNU General Public License for more details.                                #
#                                                                             #
# You should have received a copy of the GNU General Public License           #
# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
#                                                                             #
###############################################################################

###############################################################################
# Definitions
###############################################################################

include Config

VER        = 2018.03

THISAPP    = u-boot-$(VER)
DL_FILE    = $(THISAPP).tar.bz2
DL_FROM    = $(URL_IPFIRE)
DIR_APP    = $(DIR_SRC)/$(THISAPP)
TARGET     = $(DIR_INFO)/$(THISAPP)-$(MKIMAGE)
SUP_ARCH   = armv5tel aarch64

###############################################################################
# Top-level Rules
###############################################################################

objects = $(DL_FILE)

$(DL_FILE) = $(DL_FROM)/$(DL_FILE)

$(DL_FILE)_MD5 = 02922bdf0ee003fe25bfc32749ffdeab

install : $(TARGET)

check : $(patsubst %,$(DIR_CHK)/%,$(objects))

download :$(patsubst %,$(DIR_DL)/%,$(objects))

md5 : $(subst %,%_MD5,$(objects))

###############################################################################
# Downloading, checking, md5sum
###############################################################################

$(patsubst %,$(DIR_CHK)/%,$(objects)) :
	@$(CHECK)

$(patsubst %,$(DIR_DL)/%,$(objects)) :
	@$(LOAD)

$(subst %,%_MD5,$(objects)) :
	@$(MD5)

dist: 
	@$(PAK)

###############################################################################
# Installation Details
###############################################################################

$(TARGET) : $(patsubst %,$(DIR_DL)/%,$(objects))
	@$(PREBUILD)
	@rm -rf $(DIR_APP) && cd $(DIR_SRC) && tar axf $(DIR_DL)/$(DL_FILE)

ifneq "$(MKIMAGE)" "1"
ifeq "${BUILD_ARCH}" "armv5tel"
	# Pandaboard
	-mkdir -pv /usr/share/u-boot/pandaboard
	cd $(DIR_APP) && make CROSS_COMPILE="" omap4_panda_config
	cd $(DIR_APP) && sed -i -e 's!^CONFIG_IDENT_STRING=.*!CONFIG_IDENT_STRING=" Pandaboard - IPFire.org"!' .config
	cd $(DIR_APP) && make CROSS_COMPILE="" HOSTCC="gcc $(CFLAGS)"
	cd $(DIR_APP) && install -v -m 644 MLO u-boot.img \
		/usr/share/u-boot/pandaboard
	# Install pandaboard uboot as default
	cd $(DIR_APP) && install MLO /boot/
	cd $(DIR_APP) && install u-boot.img /boot/
	cd $(DIR_APP) && make distclean
	

	# Wandboard
	-mkdir -pv /usr/share/u-boot/wandboard
	cd $(DIR_APP) && make CROSS_COMPILE="" wandboard_config
	cd $(DIR_APP) && sed -i -e 's!^CONFIG_IDENT_STRING=.*!CONFIG_IDENT_STRING=" Wandboard - IPFire.org"!' .config
	cd $(DIR_APP) && make CROSS_COMPILE="" HOSTCC="gcc $(CFLAGS)"
	cd $(DIR_APP) && make CROSS_COMPILE="" HOSTCC="gcc $(CFLAGS)" u-boot.imx
	cd $(DIR_APP) && install -v -m 644 u-boot.imx \
		/usr/share/u-boot/wandboard
	cd $(DIR_APP) && make distclean

	# Raspberry Pi
	-mkdir -pv /usr/share/u-boot/rpi
	cd $(DIR_APP) && make CROSS_COMPILE="" rpi_config
	cd $(DIR_APP) && sed -i -e 's!^CONFIG_IDENT_STRING=.*!CONFIG_IDENT_STRING=" RPi1 - IPFire.org"!' .config
	cd $(DIR_APP) && make CROSS_COMPILE="" HOSTCC="gcc $(CFLAGS)"
	cd $(DIR_APP) && install -v -m 644 u-boot.bin \
		/usr/share/u-boot/rpi/kernel.img
	# Install rpi u-boot as default rpi kernel
	cd $(DIR_APP) && install u-boot.bin /boot/kernel.img
	cd $(DIR_APP) && make distclean

	# Raspberry Pi 2
	-mkdir -pv /usr/share/u-boot/rpi2
	cd $(DIR_APP) && make CROSS_COMPILE="" rpi_2_config
	cd $(DIR_APP) && sed -i -e 's!^CONFIG_IDENT_STRING=.*!CONFIG_IDENT_STRING=" RPi2 - IPFire.org"!' .config
	cd $(DIR_APP) && make CROSS_COMPILE="" HOSTCC="gcc $(CFLAGS)"
	cd $(DIR_APP) && install -v -m 644 u-boot.bin \
		/usr/share/u-boot/rpi2/kernel7.img
	# Install rpi2 u-boot as default rpi kernel7
	cd $(DIR_APP) && install u-boot.bin /boot/kernel7.img
	cd $(DIR_APP) && make distclean

	# Raspberry Pi 3 32bit
	-mkdir -pv /usr/share/u-boot/rpi3
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/u-boot/rpi/u-boot-2018.03_add_rpi3-b-plus.patch
	cd $(DIR_APP) && make CROSS_COMPILE="" rpi_3_32b_config
	cd $(DIR_APP) && sed -i -e 's!^CONFIG_IDENT_STRING=.*!CONFIG_IDENT_STRING=" RPi3 - IPFire.org"!' .config
	cd $(DIR_APP) && make CROSS_COMPILE="" HOSTCC="gcc $(CFLAGS)"
	cd $(DIR_APP) && install -v -m 644 u-boot.bin \
		/usr/share/u-boot/rpi3/kernel8-32.img
	# Install rpi3 32bit u-boot as default rpi kernel8-32
	cd $(DIR_APP) && install u-boot.bin /boot/kernel8-32.img
	cd $(DIR_APP) && make distclean

	# Orange Pi Zero
	-mkdir -pv /usr/share/u-boot/orangepi_zero
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/u-boot/sunxi/orangepi-zero-add-macs.diff
	cd $(DIR_APP) && make CROSS_COMPILE="" orangepi_zero_config
	cd $(DIR_APP) && sed -i -e 's!^CONFIG_IDENT_STRING=.*!CONFIG_IDENT_STRING=" OrangePi Zero/IPFire Nano - IPFire.org"!' .config
	cd $(DIR_APP) && make CROSS_COMPILE="" HOSTCC="gcc $(CFLAGS)"
	cd $(DIR_APP) && install -v -m 644 u-boot-sunxi-with-spl.bin \
		/usr/share/u-boot/orangepi_zero
	cd $(DIR_APP) && make distclean

	# Orange Pi PC
	-mkdir -pv /usr/share/u-boot/orangepi_pc
	cd $(DIR_APP) && make CROSS_COMPILE="" orangepi_pc_config
	cd $(DIR_APP) && sed -i -e 's!^CONFIG_IDENT_STRING=.*!CONFIG_IDENT_STRING=" OrangePi PC - IPFire.org"!' .config
	cd $(DIR_APP) && make CROSS_COMPILE="" HOSTCC="gcc $(CFLAGS)"
	cd $(DIR_APP) && install -v -m 644 u-boot-sunxi-with-spl.bin \
		/usr/share/u-boot/orangepi_pc
	cd $(DIR_APP) && make distclean

	# LeMaker Banana Pi
	-mkdir -pv /usr/share/u-boot/banana_pi
	cd $(DIR_APP) && make CROSS_COMPILE="" Bananapi_config
	cd $(DIR_APP) && sed -i -e 's!^CONFIG_IDENT_STRING=.*!CONFIG_IDENT_STRING=" BananaPi - IPFire.org"!' .config
	cd $(DIR_APP) && make CROSS_COMPILE="" HOSTCC="gcc $(CFLAGS)"
	cd $(DIR_APP) && install -v -m 644 u-boot-sunxi-with-spl.bin \
		/usr/share/u-boot/banana_pi
	cd $(DIR_APP) && make distclean

else
	# Raspberry Pi 3
	-mkdir -pv /usr/share/u-boot/rpi3
	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/u-boot/rpi/u-boot-2018.03_add_rpi3-b-plus.patch
	cd $(DIR_APP) && make CROSS_COMPILE="" rpi_3_config
	cd $(DIR_APP) && sed -i -e 's!^CONFIG_IDENT_STRING=.*!CONFIG_IDENT_STRING=" RPi3 - IPFire.org"!' .config
	cd $(DIR_APP) && make CROSS_COMPILE="" HOSTCC="gcc $(CFLAGS)"
	cd $(DIR_APP) && install -v -m 644 u-boot.bin \
		/usr/share/u-boot/rpi3/kernel8.img
	# Install rpi3 u-boot as default rpi kernel8
	cd $(DIR_APP) && install u-boot.bin /boot/kernel8.img
	cd $(DIR_APP) && make distclean

endif
	# create an empty 128 KB File for the u-boot env
	dd if=/dev/zero of=/boot/uboot.env bs=1K count=128
	
	# config (uEnv.txt and boot.*)
	cp -vf $(DIR_SRC)/config/u-boot/* /boot/
	# patch real Kernel version to uEnv.txt
	sed -e "s/xxxKVERxxx/$(KVER)/g" -i /boot/uEnv.txt

else
	# mkimage
ifeq "${BUILD_ARCH}" "armv5tel"
	cd $(DIR_APP) && make CROSS_COMPILE="" omap4_panda_config
else
	cd $(DIR_APP) && make CROSS_COMPILE="" rpi_3_config
endif
	cd $(DIR_APP) && make CROSS_COMPILE="" HOSTCC="gcc $(CFLAGS)" tools
	cd $(DIR_APP) && install -v -m 755 tools/mkimage /usr/bin
endif

	@rm -rf $(DIR_APP)
	@$(POSTBUILD)
