#
# Minimal dmidecode-derived helper: chassis type string only (GPL-2.0+, upstream dmidecode).
#
# Capture this directory before including buildconf.mk (which shifts MAKEFILE_LIST).
_DMIDECODE_BUILTIN_MK_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
include ../../buildconf.mk

# Vendored dmidecode util/types (GPL-2+); no external dmidecode tree required.
VENDOR  := $(_DMIDECODE_BUILTIN_MK_DIR)vendor
PROG    := $(BUILD_VDEVD_HELPERS)/dmidecode-chassis-type-builtin
OBJDIR  := $(BUILD)/third-party/dmidecode-builtin
UTIL_O  := $(OBJDIR)/util.o
MAIN_O  := $(OBJDIR)/dmidecode_chassis_type_builtin.o

CFLAGS += -D_FILE_OFFSET_BITS=64 -W -Wall -Wstrict-prototypes -Wmissing-prototypes \
	-Wshadow -Wpointer-arith -I$(VENDOR)

.PHONY: all clean install

all: $(PROG)

$(OBJDIR):
	@mkdir -p $@

$(UTIL_O): $(VENDOR)/util.c $(VENDOR)/util.h $(VENDOR)/types.h $(VENDOR)/config.h | $(OBJDIR)
	@test -f "$(VENDOR)/util.c" || (echo "Missing $(VENDOR)/util.c (vendored dmidecode snapshot)." >&2; exit 1)
	$(CC) $(CFLAGS) -I$(VENDOR) -c "$(VENDOR)/util.c" -o $@

$(MAIN_O): dmidecode_chassis_type_builtin.c | $(OBJDIR)
	$(CC) $(CFLAGS) -I$(VENDOR) -c $< -o $@

$(PROG): $(MAIN_O) $(UTIL_O)
	@mkdir -p "$(dir $@)"
	$(CC) $(LDFLAGS) $^ -o $@

install: $(PROG)
	@mkdir -p "$(INSTALL_VDEVD_HELPERS)"
	cp -a "$(PROG)" "$(INSTALL_VDEVD_HELPERS)/"

clean:
	rm -f "$(PROG)" "$(MAIN_O)" "$(UTIL_O)"
	@rmdir "$(OBJDIR)" 2>/dev/null || true
