PREFIX = /usr
BIN = $(PREFIX)/bin
SBIN = $(PREFIX)/sbin
SHARE = $(PREFIX)/share

# if PREFIX is the default /usr then install to /sbin
# otherwise install inside prefix anyway
ifeq ($(PREFIX),/usr)
NONUSR_SBIN = /sbin
else
NONUSR_SBIN = $(SBIN)
endif

ETC = /etc
INITD = $(ETC)/init.d
CONFD = $(ETC)/conf.d
UDEVRULESD = $(shell pkgconf --variable=udevdir udev 2>/dev/null)/rules.d
ifeq ($(UDEVRULESD),/rules.d)
UDEVRULESD = /usr/lib/udev/rules.d
endif
LIBEXEC = $(PREFIX)/libexec
ARCH = $(shell uname -m)
TOOLS = tools/abos-tools tools/sig_forwarder
MTOOLS_LINKS = tools/fsfreeze tools/mkbootscr tools/renameat2 tools/schedule_ts
MTOOLS_OBJ = tools/abos-tools.o \
	     tools/fsfreeze.o tools/mkbootscr.o \
	     tools/renameat2.o tools/schedule_ts.o \
	     tools/mmc/emmc-sref.o tools/mmc/helpers.o \
	     tools/mmc/selfrefresh.o tools/mmc/setup.o
deps := $(MTOOLS_OBJ:.o=.d)

.PHONY: tools install clean check check_podman

CFLAGS += -Wall -Wextra -MMD -MP -g -D_FORTIFY_SOURCE=3

tools: $(TOOLS) $(MTOOLS_LINKS)

$(MTOOLS_LINKS): tools/abos-tools
	ln -fs abos-tools $@

tools/abos-tools: $(MTOOLS_OBJ)
	$(CC) $(LDFLAGS) $(TARGET_ARCH) -o $@ $^ -lz

tools/sig_forwarder: tools/sig_forwarder.c
	$(CC) $(LDFLAGS) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -static -o $@ $<

clean:
	rm -f $(TOOLS) $(MTOOLS_LINKS) $(MTOOLS_OBJ) $(deps)

check: check_podman check_tools

check_podman: tools/schedule_ts
	./tests/podman_start.sh
	./tests/podman_rw.sh
	./tests/abos-ctrl.sh

check_tools: tools
	./tests/mkbootscr.sh
	./tests/schedule_ts/run.sh

install: tools
	install -D -t $(DESTDIR)$(INITD) init.d/overlayfs init.d/reset_bootcount
	install -D -t $(DESTDIR)$(INITD) init.d/fsck_atlog init.d/net-online-atmark
	install -D -t $(DESTDIR)$(INITD) init.d/indicator_signals init.d/cryptsetup
	install -D -t $(DESTDIR)$(INITD) init.d/zramswap init.d/micron-emmc-reten
	install -D -t $(DESTDIR)$(INITD) init.d/rtos-logger
	install -D -t $(DESTDIR)$(CONFD) -m 0644 conf.d/micron-emmc-reten
	install -D -t $(DESTDIR)$(BIN) tools/persist_file tools/sig_forwarder
	install -D -t $(DESTDIR)$(BIN) tools/abos-tools
	install -D -t $(DESTDIR)$(SBIN) tools/abos-ctrl
	# getty_console was historically in /sbin, keep it there for compat...
	install -D -t $(DESTDIR)$(NONUSR_SBIN) tools/getty_console
	# note we don't install links on purpose by default:
	# for the alpine package triggers will install them.
	install -D -t $(DESTDIR)$(LIBEXEC)/abos-ctrl tools/abos-ctrl.d/*
	install -D -t $(DESTDIR)$(SHARE)/abos-ctrl tools/usb_device_class
	install -D -t $(DESTDIR)$(UDEVRULESD) -m 0644 tools/90-abos-ctrl-usb-filter.rules
	install -D -t $(DESTDIR)$(ETC)/profile.d -m 0644 profile.d/atmark.sh
	install -D -t $(DESTDIR)$(ETC)/sysctl.d -m 0644 sysctl.d/10-atmark.conf
	# create and own /live for overlayfs
	install -d $(DESTDIR)/live
	# likewise, we manage /var/at-log for... atlog
	install -d $(DESTDIR)/var/at-log
	# podman
	install -D -t $(DESTDIR)$(BIN) podman/podman_start
	install -D -t $(DESTDIR)$(BIN) podman/podman_switch_storage
	install -D -t $(DESTDIR)$(BIN) podman/podman_partial_image
	install -D -t $(DESTDIR)$(INITD) podman/init.d/podman-atmark
	install -D -t $(DESTDIR)$(INITD) podman/init.d/netavark-dhcp-proxy
	install -D -t $(DESTDIR)$(CONFD) -m 0644 podman/conf.d/podman-atmark
	install -D -t $(DESTDIR)$(ETC)/profile.d -m 0644 podman/profile.d/podman.sh
	install -D -t $(DESTDIR)$(ETC)/atmark/containers -m 0644 podman/conf/README
	install -D -t $(DESTDIR)$(ETC)/atmark/containers -m 0644 podman/conf/alpine.conf.example
	if [ "$(ARCH)" = "aarch64" ]; then \
		install -D -t $(DESTDIR)$(ETC)/atmark/containers -m 0644 podman/conf/at-debian-image.conf.example; \
	fi
	if [ "$(ARCH)" = "armv7l" ]; then \
		install -D -t $(DESTDIR)$(ETC)/atmark/containers -m 0644 podman/conf/at-debian-image-armv7.conf.example; \
	fi
	# swupdate
	install -D -t $(DESTDIR)$(INITD) swupdate/init.d/swupdate-hawkbit
	install -D -t $(DESTDIR)$(INITD) swupdate/init.d/swupdate-url
	install -D -t $(DESTDIR)$(LIBEXEC) swupdate/libexec/swupdate-url
	install -D -t $(DESTDIR)$(LIBEXEC) swupdate/libexec/swupdate-usb
	install -D -t $(DESTDIR)$(UDEVRULESD) -m 0644 swupdate/udev/swupdate-usb.rules
	install -d $(DESTDIR)/target
	# reset-default
	install -D -t $(DESTDIR)$(ETC)/atmark -m 0644 reset-default/reset_default_custom.sh.example

-include $(deps)
