# ################################################################################# #
# Copyright (c) Omnissa, LLC. All rights reserved.
# This product is protected by copyright and intellectual property laws in the
# United States and other countries as well as by international treaties.
# -- Omnissa Restricted
# ################################################################################# #

OS_NAME := $(shell uname -s)

ifneq ($(findstring Linux, $(OS_NAME)), )
	OS := Linux
	DIRNAME := dirname -z

else ifneq ($(findstring Darwin, $(OS_NAME)), )
	OS := Mac
	DIRNAME := dirname

else
	DIRNAME := true
endif


PWD  := $(shell pwd)
PWD1 := $(shell $(DIRNAME) $(PWD))
PWD2 := $(shell $(DIRNAME) $(PWD1))
PWD3 := $(shell $(DIRNAME) $(PWD2))
PWD4 := $(shell $(DIRNAME) $(PWD3))
SAMPLES_DIR := $(PWD3)
SDK_DIR := $(PWD4)

SRCS = LocalOverlayClient.cpp
SRCS += $(SAMPLES_DIR)/common/helpers.cpp
SRCS += $(SAMPLES_DIR)/common/LogUtils.cpp
SRCS += $(SAMPLES_DIR)/common/RPCManager.cpp
SRCS += $(SAMPLES_DIR)/common/RPCManagerPosix.cpp

INC = stdafx.h
INC += $(SAMPLES_DIR)/common/wintypes.h
INC += $(SAMPLES_DIR)/common/helpers.h
INC += $(SAMPLES_DIR)/common/LogUtils.h
INC += $(SAMPLES_DIR)/common/RPCManager.h
INC += $(SDK_DIR)/include/horizon_sdk.h

OBJS = $(SRCS:.cpp=.o)
LIB = libLocalOverlay

INCLUDE = -I$(PWD) -I$(SAMPLES_DIR)/common -I$(SDK_DIR)/include
LIBS =

CC = g++
CFLAGS = -c  -DHORIZON_SDK_CLIENT  $(INCLUDE) $(LIBS)  -fPIC  -fvisibility=hidden

ifeq ($(OS), Linux)
	LIB := $(addsuffix .so, $(LIB))
	LIBS += -lstdc++ -lm -ldl -lrt
	HORIZON_PATH := /usr/lib/omnissa/horizon
	PLUGIN_PATH := ~/.omnissa/vdpService
	HORIZON_LEGACY_PATH := /usr/lib/vmware/view
	PLUGIN_LEGACY_PATH := ~/.vmware/vdpService
endif

ifeq ($(OS), Mac)
	LIB := $(addsuffix .dylib, $(LIB))
	LIBS +=
	HORIZON_PATH := "/Applications/Omnissa Horizon Client.app/Contents"
	PLUGIN_PATH := ~/.omnissa/vdpService
	HORIZON_LEGACY_PATH := "/Applications/VMware Horizon Client.app/Contents"
	PLUGIN_LEGACY_PATH := ~/.vmware/vdpService
endif

HORIZON_INSTALLED := $(shell test -d $(HORIZON_PATH) ; echo $$? )
HORIZON_LEGACY_INSTALLED := $(shell test -d $(HORIZON_LEGACY_PATH) ; echo $$? )


.PHONY: all rebuild build clean checkOS copyLib
all: build

rebuild: clean build

build: checkOS copyLib copyLegacyLib

clean:
	rm -f *.o *~ $(OBJS) $(LIB)


checkOS:
ifneq ($(OS), )
	@echo $(OS) detected
else
	@echo unrecognized OS - \"$(OS_NAME)\"
	@false  # force an exit from makefile
endif

ifeq ($(HORIZON_INSTALLED), 0)
	@echo Horizon installed
endif

ifeq ($(HORIZON_LEGACY_INSTALLED), 0)
	@echo Horizon legacy installed
endif


copyLib: $(LIB)
ifeq ($(HORIZON_INSTALLED), 0)
	@mkdir -p  $(PLUGIN_PATH)
	@cp -p $< $(PLUGIN_PATH)/$(<F)
	@ls -l $(PLUGIN_PATH)/$(<F)
else
	@ls -l $(<F)
endif
	@echo


copyLegacyLib: $(LIB)
ifeq ($(HORIZON_LEGACY_INSTALLED), 0)
	@mkdir -p  $(PLUGIN_LEGACY_PATH)
	@cp -p $< $(PLUGIN_LEGACY_PATH)/$(<F)
	@ls -l $(PLUGIN_LEGACY_PATH)/$(<F)
	@echo
endif


$(LIB): $(OBJS) $(INC)
	$(CC) -shared -rdynamic -o $@ $(OBJS)
ifeq ($(OS), Mac)
	@echo --------------------------------------------------------------------------------------
	@echo $(@F) must be signed with the developer\'s Apple ID certificate unless SIP is disabled
	@echo --------------------------------------------------------------------------------------
endif
	@echo $(@F) \[OK\]


%.o: %.cpp $(INC)
	$(CC) $(CFLAGS) $< -o $@
