#
#                             IPSTB Project
#                   ---------------------------------
#
# Copyright (C) 2008 NXP B.V.,
# All Rights Reserved.
#
# Filename: bootchart/Makefile
#
#
# Rev Date       Author      Comments
#-------------------------------------------------------------------------------
#   1 20080327   laird       Import from STB810

# File: bootchart/Makefile
# Makefile to control building/installing bootchart package (http://www.bootchart.org)

# Always needed at the start:
include $(PRJROOT)/etc/first.mk

$(eval $(PKG_START))

# Define what the package and version is:
PKG:=bootchart
bootchart_VERSION:=
bootchart_SKIPBUILD:=true

$(eval $(PKG_SETUP_VARS))

$(call PKG_DEFINE_TARGETS,help)

# Macro: bootchart_SYSINIT_LINE
# Line to be added to /etc/sysinit to start bootchartd.
bootchart_SYSINIT_LINE=::sysinit:/sbin/bootchartd

# Use perl to update $ROOTFS/etc/inittab, adding a sysinit entry,
# running bootchartd, unless it is already there.
bootchart_UPDATE_INITTAB=perl -i.bak -p -e 'print "$(bootchart_SYSINIT_LINE)\n" if /::sysinit:/ && !$$done++;' $$ROOTFS/etc/inittab; rm -f $$ROOTFS/etc/inittab.bak

# Use perl to remove bootchartd from $ROOTFS/etc/inittab.
bootchart_CLEAN_INITTAB=perl -i.bak -n -e 'print unless m{\Q$(bootchart_SYSINIT_LINE)\E};' $$ROOTFS/etc/inittab; rm -f $$ROOTFS/etc/inittab.bak

# Target: bootchart.install
# Install bootchart into <ROOTFS>:
# - Copies bootchartd into /sbin
# - Copies bootchartd.conf into /etc
# - Compiles and installs accton in /sbin
# - Updates /etc/inittab with a new <bootchart_SYSINIT_LINE> line, if not already present.
bootchart.install: $(BUILDROOT)/apps/accton
	@echo '  INSTALL bootchart'
	$(Q)mkdir -p $$ROOTFS/bootchart_data
	$(Q)cp -f $(bootchart_SRCDIR)/bootchartd $(BUILDROOT)/apps/accton $$ROOTFS/sbin
	$(Q)cp -f $(bootchart_SRCDIR)/bootchartd.conf $$ROOTFS/etc
	$(Q)if ! grep -F '$(bootchart_SYSINIT_LINE)' <$$ROOTFS/etc/inittab >/dev/null; then \
		echo '  Adding to $$ROOTFS/etc/inittab: $(bootchart_SYSINIT_LINE)'; \
		$(bootchart_UPDATE_INITTAB); \
	fi

# Target: bootchart.clean
# See <bootchart.install> for the actions it undoes.
# Also removes $ROOTFS/bootchart.tgz
bootchart.clean:
	@echo '  CLEAN bootchart'
	$(Q)rm -rf $$ROOTFS/bootchart_data
	$(Q)rm -f $(BUILDROOT)/apps/accton $$ROOTFS/sbin/{bootchartd,accton}
	$(Q)rm -f $$ROOTFS/etc/bootchartd.conf $$ROOTFS/bootchart.tgz
	$(Q)if grep -F '$(bootchart_SYSINIT_LINE)' <$$ROOTFS/etc/inittab >/dev/null; then \
		echo '  Removing from $$ROOTFS/etc/inittab: $(bootchart_SYSINIT_LINE)'; \
		$(bootchart_CLEAN_INITTAB); \
	fi

# Target: bootchart.accton
# Compiles accton.c program into <BUILDROOT>/apps directory.
bootchart.accton: $(BUILDROOT)/apps/accton
$(BUILDROOT)/apps/accton: $(bootchart_SRCDIR)/accton.c
	$(CROSS_COMPILE)gcc -o $@ $<

bootchart.png: $(ROOTFS)/bootchart.tgz
	java -jar $(bootchart_SRCDIR)/bootchart.jar $$ROOTFS/bootchart.tgz
.PHONY: bootchart.png

bootchart.help:
	@echo "make bootchart.install:"
	@echo "  installs bootchartd and accton in $$ROOTFS/sbin"
	@echo "  installs bootchartd.conf in $$ROOTFS/etc"
	@echo "  adds /sbin/bootchartd invocation to /etc/sysinit"
	@echo "  creates $$ROOTFS/bootchart_data directory"
	@echo "make bootchart.clean:"
	@echo "  undoes the bootchart.install actions"
	@echo "make bootchart.png:"
	@echo "  converts $$ROOTFS/bootchart.tgz into bootchart.png"
	@echo "See also $(bootchart_SRCDIR)/README.txt for more information"

# Always needed at the end:
$(eval $(PKG_END))
include $(PRJROOT)/etc/last.mk
