#
## Makefile for linking and installing 
##   The SpeedTouch USB Linux mgmt module
#

CC = gcc
WARNING=
LIBCWARNING=


# RedHat kgcc support
CCVERSION=$(shell $(CC) --version | cut -d '.' -f 1-2)
ifeq ($(CCVERSION), 2.96)
	# We don;t want this experimental compiler!
	CC=kgcc
	CCVERSION=$(shell $(CC) --version)
	ifneq ($(shell echo $(CCVERSION)| cut -d'.' -f 1-2), $(shell echo egcs-2.91.66 | cut -d'.' -f 1-2))
		CC=gcc
	else
		WARNING="Switching to kgcc ($(CCVERSION))"
	endif
endif
# end RH kgcc support

# verify compiler version
CCVERSION=$(shell $(CC) --version)
ifneq ($(CCVERSION),egcs-2.91.66)
	WARNING="Warning: Object was compiled with egcs-2.91.66, not $(CCVERSION)."
endif

LIBC_VERSION=$(shell strings /lib/libc.so.* |grep -a "GNU C Library" | tail -n 1 | cut -d ' ' -f 7 |cut -d ',' -f 1)
ifneq ($(LIBC_VERSION),2.1.92)
	LIBCWARNING="Warning: Object was compiled with libc-2.1.92, you APPEAR to be using libc-$(LIBC_VERSION)"
endif

speedmgmt : mgmt.o
ifdef WARNING
	@echo $(WARNING)
endif
ifdef LIBCWARNING
	@echo $(LIBCWARNING)
endif
	$(CC) mgmt.o -o $@
  
clean:
	/bin/rm speedmgmt

install: speedmgmt speedtouch speedtch.usermap
	cp speedmgmt /usr/sbin/
	mkdir -p /etc/hotplug/usb
	cp speedtouch /etc/hotplug/usb/speedtouch
	chmod a-wrx /etc/hotplug/usb/speedtouch
	chmod u+rx /etc/hotplug/usb/speedtouch
	mv /etc/hotplug/usb.usermap /etc/hotplug/usb.usermap.bak
	cat /etc/hotplug/usb.usermap.bak | grep -v speedtouch > /etc/hotplug/usb.usermap
	cat speedtch.usermap >> /etc/hotplug/usb.usermap

uninstall:
	/bin/rm -f /usr/sbin/speedmgmt
	/bin/rm -f /etc/hotplug/usb/speedtouch
	cat /etc/hotplug/usb.usermap.bak | grep -v speedtouch > /etc/hotplug/usb.usermap

help:
	@echo "Welcome to the Alcatel SpeedTouch Management Application"
	@echo "Available targets are:"
	@echo "  speedmgmt : build management application (default)"
	@echo "  clean     : remove management application"
	@echo "  install   : install management application"
	@echo "  uninstall : uninstall management application and restore environment"
