#!/usr/bin/make -f
#
# \brief  Build rumpkernel based tools for use within the Genode OS Framework
#         tool chain
# \author Josef Soentgen
# \date   2014-04-24

help:
	$(ECHO)
	$(ECHO) "Build rump tools for the Genode OS Framework tool chain"
	$(ECHO)
	$(ECHO) "--- available commands ---"
	$(ECHO) "build         - build rump tools"
	$(ECHO) "clean         - clean everything except contrib sources"
	$(ECHO) "cleanall      - clean everything including contrib sources"
	$(ECHO) "install       - copy rump tools to '$(INSTALL_LOCATION)'"
	$(ECHO)

.PHONY: build help install

.NOTPARALLEL: install

#
# Rump tool list
#

TOOLS = cat cgdconfig cp dd df disklabel fsck_ext2fs fsck_ffs \
        fsck_msdos halt ln ls mkdir mount_ext2fs mount_ffs mv \
        newfs newfs_ext2fs newfs_msdos rm rump_server umount  \
        vnconfig

#
# Source and install location
#

RUMPRUN_URL      = https://github.com/cnuke/rumprun.git
INSTALL_LOCATION = /usr/local/genode-rump
CONTRIB_DIR      = contrib/rumprun
BUILD_DIR        = build/rumprun
TOOLS_LIST       = $(addprefix $(BUILD_DIR)/bin/, $(TOOLS))

#
# Utilities
#

SHELL       = bash
BRIGHT_COL  = \033[01;33m
DEFAULT_COL = \033[0m
ECHO        = @echo -e
VERBOSE     = @

#
# Target rules
#

$(BUILD_DIR):
	$(VERBOSE)mkdir -p $@

$(CONTRIB_DIR):
	$(VERBOSE)mkdir -p $@

$(CONTRIB_DIR)/.checkout_sources.tag:
	$(ECHO) "$(BRIGHT_COL)downloading rump sources...$(DEFAULT_COL)"
	$(VERBOSE)git clone $(RUMPRUN_URL) $(CONTRIB_DIR) && \
	cd $(CONTRIB_DIR) && git submodule update --init --recursive && \
	./buildrump.sh/buildrump.sh -s rumpsrc checkout
	@touch $@

$(CONTRIB_DIR)/.patch_sources.tag: $(CONTRIB_DIR)/.checkout_sources.tag
	$(ECHO) "$(BRIGHT_COL)patching rump sources...$(DEFAULT_COL)"
	$(VERBOSE)cd $(CONTRIB_DIR) && \
	patch -N -d nbusersrc -p1 < updatesrc.sh.patch && \
	patch -N -d buildrump.sh -p1 < buildrump.sh.patch && \
	patch -N -d rumpsrc/usr.bin/rump_allserver -p0 < rump_allserver.c.patch
	@touch $@

$(CONTRIB_DIR)/.prepare_sources.tag: $(CONTRIB_DIR)/.patch_sources.tag
	$(ECHO) "$(BRIGHT_COL)preparing rump sources...$(DEFAULT_COL)"
	$(VERBOSE)cd $(CONTRIB_DIR)/nbusersrc && \
	sh updatesrc.sh && cd .. && mv nbusersrc nbusersrc_old && \
	mv nbusersrc_old/newsrc nbusersrc && \
	cp -Rp nbusersrc/* rumpsrc/
	@touch $@

$(CONTRIB_DIR)/.compile.tag: $(CONTRIB_DIR)/.prepare_sources.tag
	$(ECHO) "$(BRIGHT_COL)building rump tools...$(DEFAULT_COL)"
	$(VERBOSE)cd $(CONTRIB_DIR) && \
	./buildnb.sh && \
	make
	@touch $@

$(BUILD_DIR)/bin/rump_server: $(CONTRIB_DIR)/.compile.tag $(BUILD_DIR)
	$(ECHO) "$(BRIGHT_COL)finished building rump tools.$(DEFAULT_COL)"
	$(VERBOSE)cp -a $(CONTRIB_DIR)/rumpdyn/* $(BUILD_DIR) && \
		cp -a $(CONTRIB_DIR)/bin $(BUILD_DIR)
	@touch $@

build: $(CONTRIB_DIR) $(BUILD_DIR)/bin/rump_server

copy: $(CONTRIB_DIR) $(BUILD_DIR)/bin/rump_server
	$(ECHO) "$(BRIGHT_COL)installing rump tools to '$(INSTALL_LOCATION)'...$(DEFAULT_COL)"
	$(VERBOSE)sudo mkdir -p $(INSTALL_LOCATION)/bin >/dev/null 2>&1 || true
	$(VERBOSE)sudo mkdir -p $(INSTALL_LOCATION)/share/man >/dev/null 2>&1 || true
	$(VERBOSE)sudo cp -a $(TOOLS_LIST) $(INSTALL_LOCATION)/bin
	$(VERBOSE)sudo cp -a $(BUILD_DIR)/lib/ $(INSTALL_LOCATION)
	$(VERBOSE)sudo cp -a $(BUILD_DIR)/share/man/man* $(INSTALL_LOCATION)/share/man

#
# Since at least the libraries are installed with 444 permission by the
# NetBSD build framework and it requires some effort we strip the binaries
# manually by calling strip(1) with root permission prior to copying the
# binaries.
#

strip-binaries: $(CONTRIB_DIR) $(BUILD_DIR)/bin/rump_server
	$(ECHO) "$(BRIGHT_COL)stripping binaries prior to installation...$(DEFAULT_COL)"
	$(VERBOSE)sudo find $(BUILD_DIR)/bin -type f \
	                -exec strip --strip-all {} \; > /dev/null 2>&1
	$(VERBOSE)sudo find $(BUILD_DIR)/lib -type f \
	                -exec strip --strip-all {} \; > /dev/null 2>&1

install: strip-binaries copy

#
# Clean rules
#

clean:
	$(VERBOSE)rm -rf $(BUILD_DIR)
	$(VERBOSE)cd $(CONTRIB_DIR) > /dev/null 2>&1 && \
	          rm .compile.tag > /dev/null 2>&1 && \
	          make cleanrump > /dev/null 2>&1 || true

cleanall: clean
	$(VERBOSE)rm -rf $(CONTRIB_DIR)