##############################################################################
#
# Teradici TERA12xx PCoIP Host Software for Linux
#
# Copyright 2013 Teradici Corporation
# This program is free software; you can redistribute it and/or modify it
# under the terms and conditions of the GNU General Public License,
# version 2, as published by the Free Software Foundation.
#
##############################################################################

#
# Makefile for the Teradici(R) PCoIP Host Software for Linux driver
#

TARGET = pcoip_host
MINIMUM_VHC_KERNEL_VERSION=2.6.29
SYSTEM_KERNEL_VERSION=$(shell uname -r | cut -f1-3 -d. | cut -f1 -d-)
HIGHEST_VERSION=$(shell echo -e "$(SYSTEM_KERNEL_VERSION)\n$(MINIMUM_VHC_KERNEL_VERSION)" | sort -n -t '.' | tail --lines=1)

#
# The following statement is true if the system kernel version is greater than
# the minimum kernel version that supports the VHC module (2.6.30). When this
# is true the build includes the file tera_vhc_hcd.o.
#
ifneq ($(HIGHEST_VERSION),$(MINIMUM_VHC_KERNEL_VERSION))
	EXTRA_OBJS = tera_vhc_hcd.o
endif

OBJS = $(EXTRA_OBJS) tera_pcoip_driver.o tera_bstream.o tera_gfun.o tera_hcc.o tera_hcc_hmi.o \
       tera_hcc_img.o tera_hcc_kmp.o tera_hdc.o tera_hdc_kmp.o tera_mem.o tera_msg.o \
       tera_pkt.o tera_scp.o tera_tlv.o wacom_wac.o

MDIR = drivers/misc

EXTRA_CFLAGS = -DEXPORT_SYMTAB -I$(src)/../../../common/linux/inc
CURRENT = $(shell uname -r)
KDIR = /lib/modules/$(CURRENT)/build
PWD = $(shell pwd)
DEST = /lib/modules/$(CURRENT)/kernel/$(MDIR)

obj-m := $(TARGET).o
$(TARGET)-objs := $(OBJS)

default:
	make -C $(KDIR) M=$(PWD) modules

$(TARGET).o: $(OBJS)
	$(LD) $(LD_RFLAG) -r -o $@ $(OBJS)

clean:
	-rm -f *.o *.ko *.ko.unsigned modules.order .*.cmd *.bak .*.flags *.mod.c Module.* .tmp_versions/*.mod
	-rm -rf .tmp_versions

install:
	cp ../$(TARGET) /etc/init.d
	/sbin/chkconfig --add $(TARGET)
	mkdir -p /lib/modules/$(CURRENT)/extra/$(TARGET)
	cp $(TARGET).ko /lib/modules/$(CURRENT)/extra/$(TARGET)
	chmod 744 /lib/modules/$(CURRENT)/extra/$(TARGET)/$(TARGET).ko
	cp ../10-pcoip_host_udev.rules /etc/udev/rules.d

remove:
	/sbin/chkconfig --del $(TARGET)
	rm -f /etc/init.d/$(TARGET)
	rm -f /lib/modules/$(CURRENT)/extra/$(TARGET)/$(TARGET).ko
	rmdir /lib/modules/$(CURRENT)/extra/$(TARGET)
	rm -f /etc/udev/rules.d/10-pcoip_host_udev.rules

-include $(KDIR)/Rules.make
