#!/usr/bin/make -f
# ------------------------------------------------------------------------------
# COHWRAID(LNX) sample utility
# cohwraidmond : contec hwraid monitor daemon sample
# 
# Copyright (c) Since 2021 CONTEC Co.,Ltd. All Rights reserved.
# ------------------------------------------------------------------------------

# ifneq ($(shell whoami), root)
# $(error "Require root privilege!")
# endif

#systemctl(systemd) check
#ifeq (, $(shell which systemctl))
#$(error "Can not find systemctl(systemd)!")
#endif

LDFLAGS += 
CFLAGS += -Wall -Wl,--no-as-needed
CC = gcc
OBJS = cohwraidmond.o
LDLIBS += -lCoHwRaid

INCFLAGS ?= #-I../../library/include
#../../library/x86_64/libCoHwRaid.so.x.x
ADDITIONAL_LIBS ?= #$(shell find ../../library/x86_64 -type f -name libCoHwRaid.so.* -printf '%p\n' | sort -r | head -n1)
SYSTEMDDIR ?= /lib/systemd/system
#SYSTEMDDIR ?= /usr/lib/systemd/system
SBIN_INST_DIR ?= /opt/contec/cohwraidmond
#SBIN_INST_DIR ?= /usr/local/sbin
CONF_INST_DIR ?= /opt/contec/cohwraidmond
#CONF_INST_DIR ?= /usr/local/etc/cohwraidmond
DESTDIR ?= 

all : cohwraidmond

cohwraidmond :
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $@.c $(ADDITIONAL_LIBS) $(INCFLAGS) $(LDLIBS)
	sed -e 's:%SBIN_DIR%:$(SBIN_INST_DIR):g' -e 's:%SYSCONF_DIR%:$(CONF_INST_DIR):g' < ./cohwraidmond.service.in > ./cohwraidmond.service

install :
	install -Dm 0755 ./cohwraidmond $(DESTDIR)$(SBIN_INST_DIR)/cohwraidmond
	install -Dm 0644 ./cohwraidmond.service $(DESTDIR)$(SYSTEMDDIR)/cohwraidmond.service
	install -Dm 0644 ./cohwraidmond.conf $(DESTDIR)$(CONF_INST_DIR)/cohwraidmond.conf
	systemctl daemon-reload

uninstall :
	rm -f $(DESTDIR)$(SBIN_INST_DIR)/cohwraidmond
	rm -f $(DESTDIR)$(CONF_INST_DIR)/cohwraidmond.conf
	if [ -d "$(DESTDIR)$(SBIN_INST_DIR)" ]; then \
	  rmdir $(DESTDIR)$(SBIN_INST_DIR);          \
	fi
	if [ -d "$(DESTDIR)$(CONF_INST_DIR)" ]; then \
	  rmdir $(DESTDIR)$(CONF_INST_DIR);          \
	fi
	rm -f $(DESTDIR)$(SYSTEMDDIR)/cohwraidmond.service
	systemctl daemon-reload

service_start :
	systemctl enable --now cohwraidmond.service

service_stop :
	systemctl disable --now cohwraidmond.service

clean :
	rm -f *.o cohwraidmond
	rm -f ./cohwraidmond.service
