#!/usr/bin/make -f
# ------------------------------------------------------------------------------
# CORAS(LNX)
# for install sharedlibrary and headers (libmtdll and libcoraslib)
# 
# Copyright (c) Since 2018 CONTEC Co.,Ltd. All Rights reserved.
# ------------------------------------------------------------------------------

#get kernel version
KERNEL_VERSION := $(shell uname -r | cut -c-1)
#x86_64 or i686/i386
PLATFORM ?= $(shell uname -m)
#pwd
PWD ?= $(shell pwd)

SOLIB_DIR ?= /usr/lib
INC_DIR ?= /usr/include

DESTDIR ?= 
CORASLIB_NAME := libcoraslib.so
MTDLL_NAME := libmtdll.so
LIBLOC_PATH ?= ./$(PLATFORM)

# ../library/$(PLATFORM)/libcoraslib.so.x.x(.x) ../library/$(PLATFORM)/libmtdll.so.x.x(.x)
CORASLIB_SO ?= $(shell find $(LIBLOC_PATH)/ -type f -name $(CORASLIB_NAME).* -printf '%f\n' | sort -r | head -n1)
MTDLL_SO ?= $(shell find $(LIBLOC_PATH)/ -type f -name $(MTDLL_NAME).* -printf '%f\n' | sort -r | head -n1)

CORASLIB_VER := $(shell echo $(CORASLIB_SO) | sed 's/.*.so.//')
MTDLL_VER := $(shell echo $(MTDLL_SO) | sed 's/.*.so.//')

CORAS_MAJOR := $(shell echo $(CORASLIB_VER) | cut -d'.' -f 1)
# CORAS_MINOR := $(shell echo $(CORASLIB_VER) | cut -d'.' -f 2)
# CORAS_REV   := $(shell echo $(CORASLIB_VER) | cut -d'.' -f 3)

MTDLL_MAJOR := $(shell echo $(MTDLL_VER) | cut -d'.' -f 1)
# MTDLL_MINOR := $(shell echo $(MTDLL_VER) | cut -d'.' -f 2)
# MTDLL_REV   := $(shell echo $(MTDLL_VER) | cut -d'.' -f 3)

install: install_lib install_dev

install_lib:
	# install lib
	install -Ds $(LIBLOC_PATH)/$(CORASLIB_SO) $(DESTDIR)$(SOLIB_DIR)/$(CORASLIB_SO)
	install -Ds $(LIBLOC_PATH)/$(MTDLL_SO) $(DESTDIR)$(SOLIB_DIR)/$(MTDLL_SO)
	ln -sf $(CORASLIB_SO) $(DESTDIR)$(SOLIB_DIR)/$(CORASLIB_NAME).$(CORAS_MAJOR)
	ln -sf $(MTDLL_SO) $(DESTDIR)$(SOLIB_DIR)/$(MTDLL_NAME).$(MTDLL_MAJOR)
	ldconfig

install_dev:
	ln -sf $(CORASLIB_NAME).$(CORAS_MAJOR) $(DESTDIR)$(SOLIB_DIR)/$(CORASLIB_NAME)
	ln -sf $(MTDLL_NAME).$(MTDLL_MAJOR) $(DESTDIR)$(SOLIB_DIR)/$(MTDLL_NAME)
	# install headers
	install -Dm 0644 ./include/mtdll.h $(DESTDIR)$(INC_DIR)/mtdll.h
	install -Dm 0644 ./include/coras_lib.h $(DESTDIR)$(INC_DIR)/coras_lib.h
	install -Dm 0644 ./include/coras_ctl.h $(DESTDIR)$(INC_DIR)/coras_ctl.h

uninstall:
	# uninstall lib
	unlink $(DESTDIR)$(SOLIB_DIR)/$(CORASLIB_NAME)
	unlink $(DESTDIR)$(SOLIB_DIR)/$(CORASLIB_NAME).$(CORAS_MAJOR)
	rm -f $(DESTDIR)$(SOLIB_DIR)/$(CORASLIB_NAME).*
	rm -f $(DESTDIR)$(SOLIB_DIR)/$(CORASLIB_SO)
	unlink $(DESTDIR)$(SOLIB_DIR)/$(MTDLL_NAME)
	unlink $(DESTDIR)$(SOLIB_DIR)/$(MTDLL_NAME).$(MTDLL_MAJOR)
	rm -f $(DESTDIR)$(SOLIB_DIR)/$(MTDLL_NAME).*
	rm -f $(DESTDIR)$(SOLIB_DIR)/$(MTDLL_SO)
	# uninstall headers
	rm -f $(DESTDIR)$(INC_DIR)/mtdll.h
	rm -f $(DESTDIR)$(INC_DIR)/coras_lib.h
	rm -f $(DESTDIR)$(INC_DIR)/coras_ctl.h
