.PHONY: clean check test-check

ARCH ?= x86_64

FINITBIN ?= ../src/finit

BBVER ?= 1.31.0
BBBIN = busybox-$(ARCH)
BBURL ?= https://www.busybox.net/downloads/binaries/$(BBVER)-defconfig-multiarch-musl/$(BBBIN)

testroot = test_root
testroot_bin = $(testroot)/bin
binaries = $(testroot_bin)/cat \
           $(testroot_bin)/date \
           $(testroot_bin)/echo \
           $(testroot_bin)/kill \
           $(testroot_bin)/ls \
           $(testroot_bin)/mkdir \
           $(testroot_bin)/mkfifo \
           $(testroot_bin)/mknod \
           $(testroot_bin)/mount \
           $(testroot_bin)/printf \
           $(testroot_bin)/pgrep \
           $(testroot_bin)/ps \
           $(testroot_bin)/rm \
           $(testroot_bin)/sh \
           $(testroot_bin)/sleep \
           $(testroot_bin)/top \
           $(testroot_bin)/touch
dirs = $(testroot)/bin \
       $(testroot)/dev \
       $(testroot)/etc \
       $(testroot)/lib \
       $(testroot)/lib64 \
       $(testroot)/proc \
       $(testroot)/sbin \
       $(testroot)/sys \
       $(testroot)/tmp \
       $(testroot)/usr/lib

all: check

$(dirs):
	mkdir -p $@

$(BBBIN):
	wget -O $@ $(BBURL)
	md5sum -c $@.md5

$(testroot):
	mkdir $(testroot)

$(testroot_bin)/$(BBBIN): $(BBBIN)
	mkdir -p $(testroot_bin)
	cp $(BBBIN) $(testroot_bin)/
	chmod +x $(testroot_bin)/$(BBBIN)

$(binaries): $(testroot_bin)/$(BBBIN)
	cd $(testroot_bin); \
	  ln -s $(BBBIN) $(notdir $@)

$(testroot_bin)/chrootsetup.sh: chrootsetup.sh
	cp $< $@

libs_src = $(shell ldd $(FINITBIN) | grep -v lib64 | grep '=>' | cut -d' ' -f3)
libs = $(foreach path,$(libs_src),$(testroot)/usr/lib/$(shell basename $(path)))
libs64_src = $(shell ldd $(FINITBIN) | grep lib64 | grep '=>' | cut -d' ' -f3)
libs64 = $(foreach path,$(libs64_src),$(testroot)/lib64/$(shell basename $(path)))

$(testroot)/sbin/finit: $(FINITBIN) $(libs) $(libs64)
	cp $< $@

$(libs): $@
	cp $(shell ldd $(FINITBIN) | cut -d' ' -f3 | grep $(notdir $@)) $@

$(libs64): $@
	cp $(shell ldd $(FINITBIN) | cut -d' ' -f3 | grep $(notdir $@)) $@

check: $(binaries) $(testroot_bin)/chrootsetup.sh $(dirs) $(testroot)/sbin/finit
	./runtest.sh test_start_stop_service/test.sh

test-check:
	shellcheck $(shell find . -name '*.sh')

clean:
	rm -rf $(testroot)
	rm -f $(BBBIN)
