#!/usr/bin/make -f
# ------------------------------------------------------------------------------
# CORAS(LNX) for VPC/Solution-ePC/EPC/LPC
#
# Copyright (c) Since 2022 CONTEC Co.,Ltd. All Rights reserved.
# ------------------------------------------------------------------------------

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

INCLUDE_DIR = /usr/src/linux/include

MANUFACTURER_NAME = CONTEC
TARGET_MODELS = GMB-MQ37000 VPC-5000 GMB-AW48000 GMB-IW48000 LPC-400 \
                GMB-MQ67000

### -----
# set to
#   MANUFACTURER_NAME : CONTEC
#   PRODUCT_NAME      : GMB-MQ37000 (VPC-1700, VPC-3100)
#                       VPC-5000 (VPC-5000-J)
#                       GMB-AW48000 (MR4100)
#                       GMB-IW48000 (EPC-C5000)
#                       LPC-400 (LPC-400)
#                       GMB-MQ67000 (VPC-1800, VPC-3200)
### -----
MANUFACTURER  ?= $(shell echo -n `cat /sys/devices/virtual/dmi/id/sys_vendor`)
PRODUCT_NAME ?= $(shell echo -n `cat /sys/devices/virtual/dmi/id/product_name`)
# PRODUCT_NAME "-" removed
PRODUCT_NAME_OPT ?= $(shell echo -n ${PRODUCT_NAME} | sed 's/-//g')

#Check Manufacturer
ifeq ($(filter $(shell echo -n ${MANUFACTURER}), $(MANUFACTURER_NAME)),)
$(error "This Product is not supported!")
endif
#Check Product name
ifeq ($(filter $(shell echo -n ${PRODUCT_NAME}), $(TARGET_MODELS)),)
$(error "This Product is not supported!")
endif

# mod lib
install : module_install lib_install
uninstall : lib_uninstall module_uninstall
# mod lib samples
install_all : module_install lib_install install_cohwmond console_sample corasctl
uninstall_all : uninstall_cohwmond console_sample_clean lib_uninstall module_uninstall
# samples
install_util : install_cohwmond
uninstall_util : uninstall_cohwmond

sample : console_sample

install_sample_utils : install_cohwmond console_sample corasctl
uninstall_sample_utils : uninstall_cohwmond console_sample_clean

# remove made files (Object files, Executable files, etc.)
clean : console_sample_clean corasctl_clean
	# CORAS module
	make clean -C ./module
	# cohwmond sample
	make clean -C ./samples/cohwmond

# module install
module_install :
	make -C ./module
	make install -C ./module
	make service_start -C ./module

# library/header install (libcoraslib, libmtdll)
lib_install :
	make install -C ./library

# library/header uninstall (libcoraslib, libmtdll)
lib_uninstall :
	make uninstall -C ./library

# module uninstall
module_uninstall :
	make uninstall -C ./module
	make clean -C ./module

# cohwmond install
install_cohwmond :
	make -C ./samples/cohwmond
	make install -C ./samples/cohwmond
	make service_start -C ./samples/cohwmond

# cohwmond uninstall
uninstall_cohwmond :
	make uninstall -C ./samples/cohwmond
	make clean -C ./samples/cohwmond

corasctl :
	make -C ./samples/corasctl

corasctl_clean :
	make clean -C ./samples/corasctl

# make console_sample
console_sample :
	mkdir -p ./samples/console_samples/bin
ifeq ($(PRODUCT_NAME_OPT),$(filter $(PRODUCT_NAME_OPT), GMBMQ37000 VPC5000 GMBMQ67000))
	make -C ./samples/console_samples/dio
	cp -f ./samples/console_samples/dio/dio ./samples/console_samples/bin/
endif
ifeq ($(PRODUCT_NAME_OPT),$(filter $(PRODUCT_NAME_OPT), GMBMQ37000 VPC5000 GMBAW48000 GMBIW48000 LPC400 GMBMQ67000))
	make -C ./samples/console_samples/hwmon
	cp -f ./samples/console_samples/hwmon/hwmon ./samples/console_samples/bin/
	make -C ./samples/console_samples/PMonitor
	cp -f ./samples/console_samples/PMonitor/Pmonitor ./samples/console_samples/bin/
	make -C ./samples/console_samples/wdt
	cp -f ./samples/console_samples/wdt/wdt ./samples/console_samples/bin/
endif
ifeq ($(PRODUCT_NAME_OPT),$(filter $(PRODUCT_NAME_OPT), GMBMQ37000 VPC5000 GMBAW48000 GMBIW48000 GMBMQ67000))
	make -C ./samples/console_samples/Serial
	cp -f ./samples/console_samples/Serial/serial ./samples/console_samples/bin/
endif
ifeq ($(PRODUCT_NAME_OPT),$(filter $(PRODUCT_NAME_OPT), GMBMQ37000 GMBMQ67000))
	make -C ./samples/console_samples/sled
	cp -f ./samples/console_samples/sled/sled ./samples/console_samples/bin/
endif

# clean console_sample
console_sample_clean :
ifeq ($(PRODUCT_NAME_OPT),$(filter $(PRODUCT_NAME_OPT), GMBMQ37000 VPC5000 GMBMQ67000))
	make clean -C ./samples/console_samples/dio
endif
ifeq ($(PRODUCT_NAME_OPT),$(filter $(PRODUCT_NAME_OPT), GMBMQ37000 VPC5000 GMBAW48000 GMBIW48000 LPC400 GMBMQ67000))
	make clean -C ./samples/console_samples/hwmon
	make clean -C ./samples/console_samples/PMonitor
	make clean -C ./samples/console_samples/wdt
endif
ifeq ($(PRODUCT_NAME_OPT),$(filter $(PRODUCT_NAME_OPT), GMBMQ37000 VPC5000 GMBAW48000 GMBIW48000 GMBMQ67000))
	make clean -C ./samples/console_samples/Serial
endif
ifeq ($(PRODUCT_NAME_OPT),$(filter $(PRODUCT_NAME_OPT), GMBMQ37000 GMBMQ67000))
	make clean -C ./samples/console_samples/sled
endif
	rm -f ./samples/console_samples/bin/*
