TOP:=$(abspath ../..)
-include $(TOP)/common.mk

BRDIR=buildroot-git
FTP_BR=$(FTP_BASE)/linux/linux-2.6/distributions/buildroot
BASENAME:=leon-buildroot

# Build Directory Config
ifeq ($(LB_BUILDROOT_BUILDDIR),)
BUILDDIR=build-br
else
BUILDDIR=$(subst ",,$(LB_BUILDROOT_BUILDDIR))
#"
endif

.PHONY: download unpack upgrade preconfig postconfig build clean mkbuilddir

# Find Latest Buildroot dist from Gaisler FTP server
download:
	@$(SCRIPTS)/download.sh $(FTP_BR) $(PWD)/reference $(BASENAME)

# Install latest Buildroot distribution in reference directory, make sure
# to do download target first...
install:
# Stop if package already installed.
	@if [ -f reference/CURRENT.$(BASENAME) ]; then \
	 if diff -q reference/CURRENT.$(BASENAME) reference/LATEST.$(BASENAME) > /dev/null; then \
	  echo ; \
	  echo "!!! Latest LEON Buildroot package already installed" ; \
	  echo ; \
	  exit 0 ; \
	  fi; \
	 fi; \
	 $(SCRIPTS)/unpack.sh $(PWD)/reference $(PWD) $(BASENAME) || exit 1; \
	 echo INSTALLING...; \
	 newname=`sed -n -e 's/^NAME=//gp' reference/LATEST.$(BASENAME)`; \
	 echo NEWNAME=$$newname ; \
	 rm -f leon-br; \
	 ln -s $(BASENAME)-$$newname leon-br; \
	 mv reference/LATEST.$(BASENAME) reference/CURRENT.$(BASENAME); \
	 if [ -x leon-br/pkg/install.sh ]; then \
	  leon-br/pkg/install.sh; \
	  fi; \
	 cd leon-br; make install;

upgrade: download install

# Create build directory and initialize a default LEON config if no
# configuration is present
mkbuilddir:
	-@if [ ! -d $(BUILDDIR) ]; then \
	 mkdir -p $(BUILDDIR); \
	 cp leon-br/config/defconfig $(BUILDDIR)/.config ; \
	 fi;

# Generate menu/xconfig configuration file
preconfig:

# Fixup before configuration
pre_postconfig: mkbuilddir
	-@rm -f ../.rootfs.cpio ../.toolchain_path
	-@ln -s buildroot/$(BUILDDIR)/images/rootfs.cpio ../.rootfs.cpio
	echo $(PWD)/$(BUILDDIR)/output/staging/usr/bin > ../.toolchain_path
	-@rm -f ../../output/images/fs
	-@ln -s ../../dist/buildroot/$(BUILDDIR)/images ../../output/images/fs
# Update Buildroot Configuration to create CPIO Image if user enabled it
ifeq ($(LB_LINUX_SET_CPIO),y)
	sed 's/^# BR2_TARGET_ROOTFS_CPIO is not set/BR2_TARGET_ROOTFS_CPIO=y/g' \
	 $(BUILDDIR)/.config > $(BUILDDIR)/.config.tmp
	if diff -q $(BUILDDIR)/.config $(BUILDDIR)/.config.tmp > /dev/null ; then \
	 rm -f $(BUILDDIR)/.config.tmp ; \
	 else \
	 mv $(BUILDDIR)/.config.tmp $(BUILDDIR)/.config ; \
	 fi;
endif

# Fixup after configuration
postconfig: mkbuilddir

build:
	-@rm -f ../.rootfs.cpio
	-@ln -s buildroot/$(BUILDDIR)/images/rootfs.cpio ../.rootfs.cpio
	@if [ ! -d $(BUILDDIR) -o ! -f $(BUILDDIR)/.config ] ; then \
	 echo ; \
	 echo "!!! Build directory missing or Buildroot configuration not created." ; \
	 echo ; \
	 exit 1 ; \
	 fi;
	cd $(BUILDDIR); \
	 make -C $(PWD)/$(BRDIR) BUILDROOT_DL_DIR=$(PWD)/downloads O=`pwd` 2>&1 | tee $(CURDIR)/$(BUILDDIR)/build.log;

clean:
	cd $(BUILDDIR); \
	 make -C $(PWD)/$(BRDIR) BUILDROOT_DL_DIR=$(PWD)/downloads O=`pwd` clean;

xconfig: mkbuilddir
	cd $(BUILDDIR); make -C $(PWD)/$(BRDIR) BUILDROOT_DL_DIR=$(PWD)/downloads O=`pwd` xconfig

gconfig: mkbuilddir
	cd $(BUILDDIR); make -C $(PWD)/$(BRDIR) BUILDROOT_DL_DIR=$(PWD)/downloads O=`pwd` gconfig

menuconfig: mkbuilddir
	cd $(BUILDDIR); make -C $(PWD)/$(BRDIR) BUILDROOT_DL_DIR=$(PWD)/downloads O=`pwd` menuconfig

cfg_save:
	@if [ ! -d $(BUILDDIR) ] ; then \
	  echo ; \
	  echo "!!! No Buildroot Configuration created in project" ; \
	  echo ; \
	  exit 1; \
	  fi;
	mkdir -p $(CFG_BASE)/dist/buildroot/$(BUILDDIR)
	cp -a $(BUILDDIR)/.config $(CFG_BASE)/dist/buildroot/$(BUILDDIR)/.config

cfg_load:
	cp -fR $(CFG_BASE)/dist/buildroot/* .
