#You need to change DEVICE_PORT paramater,
#  If touch-panel-device is connected to another device-port.

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

## -----
# MANUFACTURE : CONTEC
# MODEL_NAME  : PT-956SLX (PT-956S Series)
#               BX-T210 (PT-M1xSA-210 Series)
## -----
MANUFACTRER_NAME := CONTEC
TARGET_MODELS := PT-956SLX BX-T210

MANUFACTRER ?= $(shell echo -n `cat /sys/devices/virtual/dmi/id/sys_vendor`)
MODEL_NAME ?= $(shell echo -n `cat /sys/devices/virtual/dmi/id/product_name`)

CHK_MANUFACTRER := $(filter $(MANUFACTRER),$(MANUFACTRER_NAME))
CHK_MODEL_NAME := $(filter $(MODEL_NAME),$(TARGET_MODELS))

# ifeq ($(and $(CHK_MANUFACTRER),$(CHK_MODEL_NAME)),)
#   $(shell echo "[WARNING] Untested This Products.")
# endif

#PWD:=$(shell pwd)

distr := $(shell bash ./getdist.sh)

# Tachpanel serial port settings 
# ex.) /dev/ttyS[0-9]
#DEVICE_PORT = /dev/ttyS4
ifeq ($(MODEL_NAME), PT-956SLX)
  ifeq ($(distr), ubuntu)
    DEVICE_PORT ?= /dev/ttyS4
  else
    ifeq ($(distr),$(filter $(distr), RHEL centOS))
      DEVICE_PORT ?= /dev/ttyS2
    else
      # unknown
      #$(shell echo "[WARNING] Untested Linux distributions!")
      DEVICE_PORT ?= /dev/ttyS2
    endif
  endif
else
  DEVICE_PORT ?= /dev/ttyS2
endif

#$(info $(DEVICE_PORT))

#systemctl(systemd) / initctl(upstart) check
SERVICECMDCHK := $(shell bash ./servicechk.sh 2> /dev/null)

CC = gcc
CFLAGS = -Wall
LDLIBS = -lm -lpthread

TPFLAGS = -DDEFAULT_DEVICE=\"$(DEVICE_PORT)\"
TPXINC = -I/usr/X11R6/include -lX11 -lXss -lXaw -lXt  -lXtst -lXrandr -L/usr/X11R6/lib

MOUSE_MODE = -DMOUSE_BTN_INPUT

INSDIR = /opt/contec/tpbkUtility


all: Tpmouse Tpcal
	$(shell sed -i -e "/^Port=/cPort=${DEVICE_PORT}" tpmouse.reg)

Tpmouse : Tpmouse.c
	$(CC) $(CFLAGS) -o Tpmouse Tpmouse.c $(TPFLAGS) $(LDLIBS)
Tpcal : Tpcal.c
	$(CC) $(CFLAGS) -o Tpcal Tpcal.c $(TPFLAGS) $(TPXINC) $(LDLIBS)
Saver : Saver.c
	$(CC) $(CFLAGS) -o Saver Saver.c $(TPFLAGS) $(TPXINC) $(LDLIBS)

#mouse button input
Tpmouse_mouse_mode : Tpmouse.c
	$(CC) $(CFLAGS) -o Tpmouse Tpmouse.c $(TPFLAGS) $(MOUSE_MODE) $(LDLIBS)

install :
	install -D ./Tpmouse $(INSDIR)/Tpmouse
#	install ./Saver $(INSDIR)/Savers
	install ./Tpcal $(INSDIR)/Tpcal
	install -m 0666 ./tpmouse.reg $(INSDIR)/tpmouse.reg
	install -m 0666 ./tpmouse.config $(INSDIR)/tpmouse.config
	cp ./Tpcal.desktop /usr/share/applications/Tpcal.desktop
	install -m 0744 ./tpctl_servicectl.sh $(INSDIR)/tpctl_servicectl.sh
	install -m 0744 ./tpctl.sh $(INSDIR)/tpctl.sh
	install -m 0744 ./tpctl_detect.sh $(INSDIR)/tpctl_detect.sh
	cp ./Tpcal.desktop /usr/share/applications/Tpcal.desktop
	cp -r ./icon $(INSDIR)/icon
ifeq ($(SERVICECMDCHK), systemctl)
	install -m 0644 ./tpctl.service /etc/systemd/system/tpctl.service
	install -m 0644 ./tpctl_restart.service /etc/systemd/system/tpctl_restart.service
	install -m 0644 ./tpctl_detect.service /etc/systemd/system/tpctl_detect.service
	systemctl daemon-reload
else
ifeq ($(SERVICECMDCHK), initctl)
	install -m 0644 ./tpctld.conf /etc/init/tpctld.conf
	install -m 0644 ./tpctl_detect.conf /etc/init/tpctl_detect.conf
	install -m 0744 ./50tpctl /etc/pm/sleep.d/50tpctl
	initctl reload-configuration
else
	$(error "Cannot found systemctl / initctl command!")
endif
endif

uninstall :
	$(INSDIR)/tpctl_servicectl.sh -f
	rm -rf $(INSDIR)
	rm -f /usr/share/applications/Tpcal.desktop
ifeq ($(SERVICECMDCHK), systemctl)
	rm -f /etc/systemd/system/tpctl.service
	rm -f /etc/systemd/system/tpctl_detect.service
	rm -f /etc/systemd/system/tpctl_restart.service
	systemctl daemon-reload
else
ifeq ($(SERVICECMDCHK), initctl)
	rm -f /etc/init/tpctl_detect.conf
	rm -f /etc/init/tpctl_detect.conf.disable
	rm -f /etc/init/tpctld.conf
	rm -f /etc/init/tpctld.conf.disable
	rm -f /etc/pm/sleep.d/50tpctl
	initctl reload-configuration
else
	$(error "Cannot found systemctl / initctl command!")
endif
endif


service_start:
	$(INSDIR)/tpctl_servicectl.sh -s

service_stop:
	$(INSDIR)/tpctl_servicectl.sh -f

clean:
	rm -f *.o Tpmouse Tpcal Saver
