TARGET=dwelling-upload

SYSCTL=${shell which systemctl}
SYSDDIR_=${shell pkg-config systemd --variable=systemdsystemunitdir}
SYSDDIR=${SYSDDIR_:/%=%}
DESTDIR=/

LDFLAGS=-ldflags "-s -w"

SOURCES := ${wildcard *.go}

all: ${TARGET}

.PHONY: ${TARGET}

${TARGET}:
	go build -o bin/$@ ${LDFLAGS} cmd/$@/main.go
	go build -o bin/$@-clear ${LDFLAGS} cmd/$@-clear/main.go

run:
	bin/${TARGET} -conf configs/config.yaml

run-clear:
	bin/${TARGET}-clear -conf configs/config.yaml

install:
	install -Dm 0755 ${TARGET} ${DESTDIR}usr/bin/${TARGET}
	install -Dm 0755 ${TARGET}-clean ${DESTDIR}usr/bin/${TARGET}-clean
	install -Dm 0644 LICENSE ${DESTDIR}usr/share/licenses/${TARGET}/LICENSE

	install -Dm 0644 init/systemd/${TARGET}.service ${DESTDIR}${SYSDDIR}/${TARGET}.service
	install -Dm 0644 init/systemd/${TARGET}-clear.timer ${DESTDIR}${SYSDDIR}/${TARGET}-clear.timer
	install -Dm 0644 init/systemd/${TARGET}-clear.service ${DESTDIR}${SYSDDIR}/${TARGET}-clear.service

uninstall:
	rm ${DESTDIR}usr/bin/${TARGET}
	rm ${DESTDIR}usr/bin/${TARGET}-clear
	rm ${DESTDIR}usr/share/licenses/${TARGET}/LICENSE

	${SYSCTL} stop ${TARGET}.service
	${SYSCTL} stop ${TARGET}-clear.timer
	${SYSCTL} disable ${TARGET}.service
	${SYSCTL} disable ${TARGET}-clear.timer
	rm ${DESTDIR}${SYSDDIR}/${TARGET}.service
	rm ${DESTDIR}${SYSDDIR}/${TARGET}-clear.timer
	rm ${DESTDIR}${SYSDDIR}/${TARGET}-clear.service

clean:
	go clean