#
# sample util
# cothermald
# 
# 

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

INCLUDE_DIR = /usr/src/linux/include

#pwd
PWD := $(shell pwd)

#systemctl(systemd) / initctl(upstart) check
SERVICECMDCHK ?= $(shell bash ./servicechk.sh)

# MANUFACTURER ?= $(shell dmidecode --type system | grep "Manufacturer" | cut -f 2 -d ' ')
# PRODUCT_NAME ?= $(shell dmidecode --type system | grep "Product" | cut -f 3 -d ' '| sed 's/-//g')
MANUFACTURER ?= $(shell echo -n `cat /sys/devices/virtual/dmi/id/sys_vendor`)
MODEL_NAME ?= $(shell echo -n `cat /sys/devices/virtual/dmi/id/product_name`)
PRODUCT_NAME := $(subst -,,$(MODEL_NAME))

LDFLAGS = 

CFLAGS = -Wall -D$(PRODUCT_NAME) -Wl,--no-as-needed

CC = gcc

OBJS = cothermald.o

LDLIBS = -lmtdll

all:$(OBJS)
	$(CC) $(CFLAGS) $(LDFLAGS) $^ -o cothermald $(LDLIBS)

install:
ifeq ($(SERVICECMDCHK), systemctl)
	install -D ./cothermald /opt/contec/cothermald/cothermald
	install -m 0644 ./cothermald.service /etc/systemd/system/cothermald.service
	install -m 0644 ./cothermald.config /opt/contec/cothermald/cothermald.config
	install -m 0644 ./cothermald.config.origin /opt/contec/cothermald/cothermald.config.origin
	install -m 0744 ./cothermald_servicectl.sh /opt/contec/cothermald/cothermald_servicectl.sh
	systemctl daemon-reload
	install -m 0744 ./popup_mesg.sh /opt/contec/cothermald/popup_mesg.sh
else
ifeq ($(SERVICECMDCHK), initctl)
	install -D ./cothermald /opt/contec/cothermald/cothermald
	install -m 0644 ./cothermald.conf /etc/init/cothermald.conf
	install -m 0644 ./cothermald.config /opt/contec/cothermald/cothermald.config
	install -m 0644 ./cothermald.config.origin /opt/contec/cothermald/cothermald.config.origin
	install -m 0744 ./cothermald_servicectl.sh /opt/contec/cothermald/cothermald_servicectl.sh
	initctl reload-configuration
	install -m 0744 ./popup_mesg.sh /opt/contec/cothermald/popup_mesg.sh
else
	$(error "Cannot found systemctl / initctl command!")
endif
endif

uninstall:
ifeq ($(SERVICECMDCHK), systemctl)
	$(shell test -f /opt/contec/cothermald/cothermald_servicectl.sh && /opt/contec/cothermald/cothermald_servicectl.sh -f)
#	/opt/contec/cothermald/cothermald_servicectl.sh -f
	rm -rf /opt/contec/cothermald
	rm -rf /etc/systemd/system/cothermald.service
	systemctl daemon-reload
else
ifeq ($(SERVICECMDCHK), initctl)
	$(shell test -f /opt/contec/cothermald/cothermald_servicectl.sh && /opt/contec/cothermald/cothermald_servicectl.sh -f)
#	/opt/contec/cothermald/cothermald_servicectl.sh -f
	rm -rf /opt/contec/cothermald
	rm -rf /etc/init/cothermald.conf
	rm -rf /etc/init/cothermald.conf.disable
	initctl reload-configuration
else
	$(error "Cannot found systemctl / initctl command!")
endif
endif

service_start:
ifeq ($(SERVICECMDCHK), systemctl)
	/opt/contec/cothermald/cothermald_servicectl.sh -s
else
ifeq ($(SERVICECMDCHK), initctl)
	/opt/contec/cothermald/cothermald_servicectl.sh -s
else
	$(error "Cannot found systemctl / initctl command!")
endif
endif

service_stop:
ifeq ($(SERVICECMDCHK), systemctl)
	/opt/contec/cothermald/cothermald_servicectl.sh -f
else
ifeq ($(SERVICECMDCHK), initctl)
	/opt/contec/cothermald/cothermald_servicectl.sh -f
else
	$(error "Cannot found systemctl / initctl command!")
endif
endif

clean:
	rm -f *.o cothermald
