include ../buildconf.mk

INITRAMFS_FILES := $(wildcard initramfs/hooks/*) \
        $(wildcard initramfs/scripts/init-bottom/*) \
        $(wildcard initramfs/scripts/init-top/*) \
		$(wildcard initramfs/modules.d/vdev) \
		$(wildcard initramfs/init)

INITRAMFS_BUILD := \
    $(patsubst initramfs/%,$(BUILD_VDEV_INITRAMFS)/%,$(INITRAMFS_FILES))

INITRAMFS_INSTALL := \
    $(patsubst initramfs/%,$(INSTALL_VDEV_INITRAMFS)/%,$(INITRAMFS_FILES))

# Staged under $(BUILD)/etc/init.d (same names as example/conf.mk / package install)
INITSCRIPT_BUILD := $(BUILD_VDEV_INITSCRIPT)/vdev
INITSCRIPT_COLDPLUG_BUILD := $(BUILD_VDEV_INITSCRIPT)/vdev-coldplug.sh
SYSV_INITSCRIPTS_BUILD := $(INITSCRIPT_BUILD) $(INITSCRIPT_COLDPLUG_BUILD)

VDEV_BUILD := $(INITRAMFS_BUILD)
VDEV_INSTALL := $(INITRAMFS_INSTALL)

TOOLS := ../tools/
VDEVD := ../vdevd/

GEN_IFNAMES := $(BUILD_VDEVD_HELPERS)/gen-ifnames.sh
MKINITRAMFS := make-initramfs.sh

# debugging
print-%: ; @echo $*=$($*)

all: $(VDEV_BUILD) $(SYSV_INITSCRIPTS_BUILD)

$(INITSCRIPT_BUILD): sysv-initscript.sh
	@mkdir -p $(shell dirname "$@")
	@cp -a "$<" "$@"

$(INITSCRIPT_COLDPLUG_BUILD): sysv-coldplug.sh
	@mkdir -p $(shell dirname "$@")
	@cp -a "$<" "$@"
	@chmod 755 "$@"

$(BUILD_VDEV_CONFIG)/actions/%: actions/%
	@mkdir -p $(shell dirname "$@")
	@rm -f "$@"
	@cat "$<" > "$@"

$(BUILD_VDEV_INITRAMFS)/%: initramfs/%
	@mkdir -p $(shell dirname "$@")
	@cp -a "$<" "$@"

$(INSTALL_VDEV_INITRAMFS)/%: $(BUILD_VDEV_INITRAMFS)/%
	@mkdir -p $(shell dirname "$@")
	@cp -a "$<" "$@"

.PHONY: initramfs
# Requires a full vdev /etc tree on the host (see make-initramfs.sh)
initramfs: $(VDEV_BUILD)
	$(SHELL) ./$(MKINITRAMFS)

.PHONY: install
install: $(VDEV_INSTALL)

.PHONY: clean
clean:
	rm -f $(VDEV_BUILD) $(SYSV_INITSCRIPTS_BUILD)
