#!/usr/bin/make
# Makefile for building Linux Broadcom Gigabit ethernet driver as a module.
# $id$

# PREFIX may be set by the RPM build to set the effective root.
PREFIX=
ifeq ($(shell ls /lib/modules/$(shell uname -r)/build > /dev/null 2>&1 && echo build),)
  ifeq ($(shell ls /usr/src/linux > /dev/null 2>&1 && echo linux),)
    LINUX=
  else
    LINUX=/usr/src/linux
  endif
else
  LINUX=/lib/modules/$(shell uname -r)/build
endif

ifeq ($(LINUX),)
  $(error Linux kernel source tree not found)
endif

# check if 2.4 kernel or 2.5+ kernel
BCM_KVER:=$(shell uname -r | cut -c1-3 | sed 's/2\.[56]/2\.6/')

ifeq ($(BCM_KVER), 2.6)
# Makefile for 2.5+ kernel

BCM_DRV = bcm4400.ko

ifneq ($(KERNELRELEASE),)

obj-m += bcm4400.o

bcm4400-objs    := b44um.o b44proc.o b44lm.o 

else

default:
	make -C $(LINUX) SUBDIRS=$(shell pwd) modules

endif

else # ifeq ($(BCM_KVER),2.6)
# Makefile for 2.4 kernel

BCM_DRV = bcm4400.o

ifeq ($(shell which kgcc > /dev/null 2>&1 && echo kgcc),)
CC = gcc
else
  ifneq ($(findstring gcc version egcs-2.91.66, $(shell cat /proc/version 2>&1)),)
    CC = kgcc
  else
    CC = gcc
  endif
endif
LD = ld
CFLAGS=-DMODULE -D__KERNEL__ -DDBG=0 -Wall -Wstrict-prototypes -O6 -I$(LINUX)/include

ifeq ($(wildcard ~/rpmdir),)
  rpmdir = /usr/src/redhat
else
  rpmdir = $(wildcard ~/rpmdir)
endif

ifdef DBG
  CFLAGS += -DB44_DEBUG=1
endif

ifdef SMALL
  CFLAGS += -DBCM_SMALL_DRV=1
endif

bcm4400.o: b44um.o b44proc.o b44lm.o
	$(LD) -i $^ -o $@

debug:
	@make DBG=1

endif # ifeq ($(BCM_KVER),2.6)

bcm4400.4.gz:
	gzip -c bcm4400.4 > bcm4400.4.gz

ifeq ($(BCM_KVER), 2.6)
install: default bcm4400.4.gz
else
install: $(BCM_DRV) bcm4400.4.gz
endif
	@if [ -d $(PREFIX)/lib/modules/$(shell uname -r)/kernel/3rdparty/bcm4400 ];\
	then rm -f $(PREFIX)/lib/modules/$(shell uname -r)/kernel/3rdparty/bcm4400/$(BCM_DRV).gz;\
	install -m 444 $(BCM_DRV) $(PREFIX)/lib/modules/$(shell uname -r)/kernel/3rdparty/bcm4400;\
	elif [ -d $(PREFIX)/lib/modules/$(shell uname -r)/kernel/drivers/net/bcm ];\
	then install -m 444 $(BCM_DRV) $(PREFIX)/lib/modules/$(shell uname -r)/kernel/drivers/net/bcm;\
	elif [ -d $(PREFIX)/lib/modules/$(shell uname -r)/kernel ];\
	then mkdir -p $(PREFIX)/lib/modules/$(shell uname -r)/kernel/drivers/net;\
	install -m 444 $(BCM_DRV) $(PREFIX)/lib/modules/$(shell uname -r)/kernel/drivers/net;\
	else mkdir -p $(PREFIX)/lib/modules/$(shell uname -r)/net;\
	install -m 444 $(BCM_DRV) $(PREFIX)/lib/modules/$(shell uname -r)/net/;\
	fi
	@if [ "$(PREFIX)" = "" ]; then /sbin/depmod -a ;\
	else echo " *** Run '/sbin/depmod -a' to update the module database.";\
	fi
	@if [ -d $(PREFIX)/usr/share/man ];\
	then install -m 444 bcm4400.4.gz $(PREFIX)/usr/share/man/man4;\
	elif [ -d $(PREFIX)/usr/man ];\
	then install -m 444 bcm4400.4.gz $(PREFIX)/usr/man/man4;\
	fi

bcm4400.tgz: Makefile *.[cho] LICENSE *.TXT
	tar cfvz $@ $^

rpm: bcm4400.tgz bcm4400.spec
	cp -p $< ${rpmdir}/SOURCES/
	rpm -ba bcm4400.spec

all: bcm4400.o
tar: bcm4400.tgz

.PHONEY: all clean install tar

clean:
	-rm -f bcm4400.o bcm4400.ko b44um.o b44proc.o b44lm.o bcm4400.mod.c bcm4400.mod.o
