###############################################################################
#                                                                             #
# 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)-kirkwood
SUP_ARCH   = armv5tel

###############################################################################
# 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)

	# Dreamplug
	-mkdir -pv /usr/share/u-boot/dreamplug
	cd $(DIR_APP) && make CROSS_COMPILE="" dreamplug_config
	cd $(DIR_APP) && sed -i -e 's!^CONFIG_IDENT_STRING=.*!CONFIG_IDENT_STRING=" Globalscale Dreamplug - IPFire.org"!' .config
	cd $(DIR_APP) && sed -i -e 's!^# CONFIG_CMD_BOOTZ is not set!CONFIG_CMD_BOOTZ=y!' .config
	cd $(DIR_APP) && make CROSS_COMPILE="" HOSTCC="gcc $(CFLAGS)"
	cd $(DIR_APP) && install -v -m 644 u-boot.kwb \
		/usr/share/u-boot/dreamplug
	cd $(DIR_APP) && make distclean

	# ICY-Box 6x20
	-mkdir -pv /usr/share/u-boot/icybox
	cd $(DIR_APP) && make CROSS_COMPILE="" ib62x0_config
	cd $(DIR_APP) && sed -i -e 's!^CONFIG_IDENT_STRING=.*!CONFIG_IDENT_STRING=" Raidsonic ICY-Box 62x0 - IPFire.org"!' .config
	cd $(DIR_APP) && sed -i -e 's!^# CONFIG_CMD_BOOTZ is not set!CONFIG_CMD_BOOTZ=y!' .config
	cd $(DIR_APP) && make CROSS_COMPILE="" HOSTCC="gcc $(CFLAGS)"
	cd $(DIR_APP) && install -v -m 644 u-boot.kwb \
		/usr/share/u-boot/icybox
	cd $(DIR_APP) && make distclean

	# Iomega Iconnect
	-mkdir -pv /usr/share/u-boot/iconnect
	# Fix crash at env save because this uboot is larger than 512KB
	cd $(DIR_APP) && echo "#undef CONFIG_ENV_OFFSET" >> include/configs/iconnect.h
	cd $(DIR_APP) && echo "#define CONFIG_ENV_OFFSET 0xC0000" >> include/configs/iconnect.h
	cd $(DIR_APP) && make CROSS_COMPILE="" iconnect_config
	cd $(DIR_APP) && sed -i -e 's!^CONFIG_IDENT_STRING=.*!CONFIG_IDENT_STRING=" Iomega iConnect - IPFire.org"!' .config
	cd $(DIR_APP) && sed -i -e 's!^# CONFIG_CMD_BOOTZ is not set!CONFIG_CMD_BOOTZ=y!' .config
	cd $(DIR_APP) && sed -i -e 's!^# CONFIG_HUSH_PARSER is not set!CONFIG_HUSH_PARSER=y!' .config
	cd $(DIR_APP) && sed -i -e 's!^CONFIG_MTDPARTS_DEFAULT=.*!CONFIG_MTDPARTS_DEFAULT="mtdparts=orion_nand:0xC0000@0x0(uboot),0x40000@0xC0000(uboot_env),-@0x100000(rootfs)"!' .config
	cd $(DIR_APP) && make CROSS_COMPILE="" HOSTCC="gcc $(CFLAGS)"
	cd $(DIR_APP) && make CROSS_COMPILE="" HOSTCC="gcc $(CFLAGS)" u-boot.kwb
	cd $(DIR_APP) && install -v -m 644 u-boot.kwb \
		/usr/share/u-boot/iconnect
	cd $(DIR_APP) && make distclean

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