#
## Makefile for UNICORN ADSL Modem
#

# Version
VERSION=USB-APL-7-2.0-0.2.0.20
#Linux Version supported
#e.g. SUPPORT=FC2 for fedora and SUPPORT=MDK for mandrake
SUPPORT=Linux
#Support Type (Alpha, Beta and Release)
TYPE=Alpha

#find out RPM toplevel directory
RPMDIR=$(shell rpm --eval %{_topdir})


PKG_VERSION=\"$(VERSION)\"

# path to kernel sources and headers (must correspond to running kernel)
KERNEL_SOURCES=/lib/modules/$(shell uname -r)/build
DRIVER_DESTINATION=/lib/modules/$(shell uname -r)/extra
HPATH=$(KERNEL_SOURCES)/include
MAKE=make  

.EXPORT_ALL_VARIABLES:

DRVDIRS = unicorn_usb 

all: applis modules
 
applis: 
	for i in $(DIRS) ; do make -C $$i all ; done

modules: 
	for i in $(DRVDIRS) ; do make -C $$i modules MODDIR=$(PWD)/$$i ; done

clean: 
	for i in $(DIRS) $(DRVDIRS); do (if [ -d $$i ]; then make -C $$i clean; fi); done

install: applis_install modules_install

applis_install: 
	for i in $(DIRS) ; do make -C $$i install prefix=$(prefix); done

modules_install: 
	for i in $(DRVDIRS) ; do  make -C $$i modules_install MODDIR=$(PWD)/$$i ; done
	/sbin/depmod -ae

only_modules_install: 
	for i in $(DRVDIRS) ; do make -C $$i modules_install ; done

rpm:
	@echo "Making RPM package for Unicorn II driver : $(PKG_VERSION)"
	#Remove old data if any"
	rm -rf $(VERSION)-$(SUPPORT).tar.gz
	rm -rf $(VERSION)-$(SUPPORT)
	#make directory structor
	mkdir -p $(VERSION)-$(SUPPORT)
	mkdir -p $(VERSION)-$(SUPPORT)/pkg
	#copy require files
	cp unicorn_usb/unicorn_usb_eth.ko $(VERSION)-$(SUPPORT)/pkg
	cp README $(VERSION)-$(SUPPORT)/pkg
	#Create installer's Makefile
	@echo "install:" > $(VERSION)-$(SUPPORT)/Makefile
	@echo -e "\t/bin/mkdir -p /lib/modules/`uname -r`/extra" >> $(VERSION)-$(SUPPORT)/Makefile
	@echo -e "\t/bin/mkdir -p /usr/local/share/pixmaps/" >> $(VERSION)-$(SUPPORT)/Makefile
	@echo -e "\t/bin/mkdir -p /usr/doc/unicorn2" >> $(VERSION)-$(SUPPORT)/Makefile
	@echo -e "\t/bin/cp ./pkg/unicorn_usb_eth.ko /lib/modules/`uname -r`/extra/" >> $(VERSION)-$(SUPPORT)/Makefile
	@echo -e "\t/bin/cp ./pkg/README /usr/doc/unicorn2" >> $(VERSION)-$(SUPPORT)/Makefile
	@echo "uninstall:" >> $(VERSION)-$(SUPPORT)/Makefile
	@echo -e "\t/bin/rm -rf /lib/modules/`uname -r`/extra/unicorn_usb_eth.ko" >> $(VERSION)-$(SUPPORT)/Makefile
	@echo -e "\t/bin/rm -rf /usr/doc/unicorn2" >> $(VERSION)-$(SUPPORT)/Makefile
	#Create tar.gz file of the package
	tar -zcvf $(VERSION)-$(SUPPORT).tar.gz $(VERSION)-$(SUPPORT)
	#copy the file to rpm tree
	cp $(VERSION)-$(SUPPORT).tar.gz $(RPMDIR)/SOURCES
	#Create rpmbuild spec file
	@echo "Name: $(VERSION)" > unicorn2.spec
	@echo "Summary: ADSL USB Unicorn II Driver" >> unicorn2.spec
	@echo "Version: $(SUPPORT)" >> unicorn2.spec
	@echo "Release: $(TYPE)" >> unicorn2.spec
	@echo "License: GPL" >> unicorn2.spec
	@echo "Group: System Environment/Kernel" >> unicorn2.spec
	@echo "Prefix: %{_prefix}" >> unicorn2.spec
	@echo "Vendor: STMicroelectronics" >> unicorn2.spec
	@echo "URL: http://www.st.com" >> unicorn2.spec
	@echo "Source: %{name}-%{version}.tar.gz" >> unicorn2.spec
	@echo "Provides:  unicorn_usb_eth.ko" >> unicorn2.spec
	@echo "%define debug_package %{nil}" >> unicorn2.spec
	@echo "%description" >> unicorn2.spec
	@echo "This driver supports the Unicorn II USB ADSL chipset from STMicroelectronics." >> unicorn2.spec
	@echo "The chipset consists of the ADSL DMT transceiver ST70138 and the ST70174" >> unicorn2.spec
	@echo "Analog Front End (AFE)." >> unicorn2.spec
	@echo "%prep" >> unicorn2.spec
	@echo "%setup -q" >> unicorn2.spec
	@echo "%build" >> unicorn2.spec
	@echo "#make clean && make %{_smp_mflags}" >> unicorn2.spec
	@echo "%install" >> unicorn2.spec
	@echo "rm -rf $RPM_BUILD_ROOT" >> unicorn2.spec
	@echo "make install" >> unicorn2.spec
	@echo "%clean" >> unicorn2.spec
	@echo "rm -rf $RPM_BUILD_ROOT" >> unicorn2.spec
	@echo "%post" >> unicorn2.spec
	@echo "/sbin/depmod -ae" >> unicorn2.spec
	@echo "%postun" >> unicorn2.spec
	@echo "/sbin/depmod -ae" >> unicorn2.spec
	@echo "%files" >> unicorn2.spec
	@echo "%defattr (-, root, root)" >> unicorn2.spec
	@echo "%dir $(DRIVER_DESTINATION)" >> unicorn2.spec
	@echo "%dir /usr/doc/unicorn2" >> unicorn2.spec
	@echo "%dir /usr/local/share/pixmaps" >> unicorn2.spec
	@echo "$(DRIVER_DESTINATION)/unicorn_usb_eth.ko" >> unicorn2.spec
	#run rpmbuild to make final package
	rpmbuild -bb --target=i586 unicorn2.spec
	mv $(RPMDIR)/RPMS/i586/$(VERSION)-$(SUPPORT)-$(TYPE).i586.rpm .
	rm -rf $(VERSION)-$(SUPPORT).tar.gz
	rm -rf $(VERSION)-$(SUPPORT)
	rm -rf unicorn2.spec
