#
# Copyright (C) 2008-Present      Sigma Designs
#
# Sigma Designs
# 1778 McCarthy BLVD
# Milpitas CA 95035, USA
#
##############################################################
#############################################################
#
# genext2fs to build to target ext2 filesystems
#
#############################################################
TARGETS+=ext2
ifeq ($(strip $(BR2_TARGET_ROOTFS_EXT2)),y)
SUBDIR:=target/$(TARGETS)
BUILD_DIR:=$(BASE_DIR)/$(SUBDIR)
VERSION:=$(strip $(subst ",, $(BR2_EXT2_VERSION)))
#"))
SOURCE_DIR=$(BUILD_DIR)/gen$(TARGETS)fs-$(VERSION)
SOURCE=gen$(TARGETS)fs_$(VERSION).orig.tar.gz
UNZIP:=$(TAR)
UNZIP_OPTIONS:=$(GZ_EXT)
ifeq ($(strip $(DOWNLOAD_SITE_PUBLIC)),y)
SITE=http://ftp.debian.org/debian/pool/main/g/gen$(TARGETS)fs
else
SITE := $(DOWNLOAD_SITE)
endif
SOURCE_DIR=$(BUILD_DIR)/gen$(TARGETS)fs-$(VERSION)
SOURCE_BINS:= $(SOURCE_DIR)/gen$(TARGETS)fs
STAGING_BINS:= $(STAGING_DIR)/bin/gen$(TARGETS)fs
-include $(BASE_DIR)/makefile.newrules
CONFIGURE:= chmod a+x $(SOURCE_DIR)/configure; \
	(cd $(SOURCE_DIR); rm -rf config.cache; \
		./configure \
		--prefix=$(STAGING_DIR); \
	)
ifneq '$(shell find $(TARGET_DIR)/bin/ -name busybox -type f)' ''
BUILD:= $(MAKE) CFLAGS="-Wall -O2 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE \
		-D_FILE_OFFSET_BITS=64" -C $(SOURCE_DIR)
else
BUILD:=
$(warning Please build busybox first)
endif
#############################################################
#
# Build the ext2 root filesystem image
#
#############################################################
EXT2_OPTS :=
ifeq ($(strip $(BR2_TARGET_ROOTFS_EXT2_SQUASH)),y)
EXT2_OPTS += -U
endif
ifneq ($(strip $(BR2_TARGET_ROOTFS_EXT2_BLOCKS)),0)
EXT2_OPTS += -b $(strip $(BR2_TARGET_ROOTFS_EXT2_BLOCKS))
endif
ifneq ($(strip $(BR2_TARGET_ROOTFS_EXT2_INODES)),0)
EXT2_OPTS += -i $(strip $(BR2_TARGET_ROOTFS_EXT2_INODES))
endif
ifneq ($(strip $(BR2_TARGET_ROOTFS_EXT2_RESBLKS)),0)
EXT2_OPTS += -r $(strip $(BR2_TARGET_ROOTFS_EXT2_RESBLKS))
endif
EXT2_BASE:=$(subst ",,$(BR2_TARGET_ROOTFS_EXT2_OUTPUT))
#")
ifeq ($(strip $(BR2_TARGET_ROOTFS_EXT2_GZ)),y)
EXT2_TARGET := $(EXT2_BASE).gz
else
EXT2_TARGET := $(EXT2_BASE)
endif
$(EXT2_BASE):
	@find $(TARGET_DIR) -type f -perm +111 | xargs $(STRIP) 2>/dev/null || true;
	@rm -rf $(TARGET_DIR)/usr/man
	@rm -rf $(TARGET_DIR)/usr/share/man
	@rm -rf $(TARGET_DIR)/usr/info
ifeq ($(strip $(BR2_TARGET_ROOTFS_EXT2_BLOCKS)),0)
	@GENEXT2_REALSIZE=`LANG=C du -l -s -c -k $(TARGET_DIR) | grep total | sed -e "s/total//"`; \
	GENEXT2_ADDTOROOTSIZE=`if [ $$GENEXT2_REALSIZE -ge 2400 ] ; then echo 16384; else echo 2400; fi`; \
	GENEXT2_SIZE=`expr $$GENEXT2_REALSIZE + $$GENEXT2_ADDTOROOTSIZE`; \
	GENEXT2_ADDTOINODESIZE=`find $(TARGET_DIR) | wc -l`; \
	GENEXT2_INODES=`expr $$GENEXT2_ADDTOINODESIZE + 400`; \
	set -x; \
	$(SOURCE_DIR)/gen$(TARGETS)fs \
		-b $$GENEXT2_SIZE \
		-i $$GENEXT2_INODES \
		-d $(TARGET_DIR) \
		-D $(TARGET_DEVICE_TABLE) \
		$(EXT2_OPTS) $(EXT2_BASE)
else
	@$(SOURCE_DIR)/gen$(TARGETS)fs \
		-d $(TARGET_DIR) \
		-D $(TARGET_DEVICE_TABLE) \
		$(EXT2_OPTS) \
		$(EXT2_BASE)
endif
$(EXT2_BASE).gz: $(EXT2_BASE); 	@gzip --best -fv $(EXT2_BASE)
EXT2_COPYTO := $(strip $(subst ",,$(BR2_TARGET_ROOTFS_EXT2_COPYTO)))
#")
extra-operations: $(EXT2_TARGET)
ifneq ($(EXT2_COPYTO),)
	@cp -f $(EXT2_TARGET) $(EXT2_COPYTO)
endif
extra-remove:;	@-rm -f $(EXT2_TARGET)
else
$(error Please use "make menuconfig" to enable $(TARGETS))
endif
