###############################################################
# VETH: A virtual ethernet device driver for Linux. 
#
# This device driver is an implementation of Aggregation of Multiple
# Link Segments (IEEE 802.3ad).
#
# Author: Yumo (Katsuyuki Yumoto) 2000-2004 yumo@st.rim.or.jp
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#################################################################

#-----------------------------------------------------
# If ethmap is already installed, enable following lines.
#-----------------------------------------------------
#EXTRACFLAGS=-DETHMAP
#EXTRALDFLAGS=-lethmap

prefix=/usr/local
LINUX_SRC=/usr/src/linux

LINUX_VER := $(shell grep UTS_RELEASE $(LINUX_SRC)/include/linux/version.h|awk -F ' ' '{print $$3}'|sed -e "s/\"//g" )
LINUX_VER_SUB := $(shell grep UTS_RELEASE $(LINUX_SRC)/include/linux/version.h|awk -F ' ' '{print $$3}' |sed -e "s/\"//g" |awk -F '.' '{print $$1"."$$2}' )
ARCH := $(shell uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ \
                                  -e s/arm.*/arm/ -e s/sa110/arm/ \
                                  -e s/s390x/s390/ -e s/parisc64/parisc/ )

VETHCFG_OBJS=vethcfg.o\
  vethcmd.o


all: prep


prep:
	rm -f include
	ln -s $(LINUX_SRC)/include include
	sed -e "/^all:/d" -e "/^install/d" \
		$(LINUX_SRC)/arch/$(ARCH)/Makefile > make.defs
#	make veth.o
	make vethcfg
	make vethcfg.8


veth.o: veth.c veth.h 
	gcc -nostdinc -iwithprefix include -D__KERNEL__ -Iinclude -Wall -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -pipe -mpreferred-stack-boundary=2 -O2 -fomit-frame-pointer $(CFLAGS) -DMODULE -DKBUILD_BASENAME=veth -DKBUILD_MODNAME=veth -c -o veth.o veth.c

vethcfg: $(VETHCFG_OBJS)
	gcc -g -o $@ $(VETHCFG_OBJS) $(EXTRALDFLAGS)

vethcfg.o: vethcfg.c veth.h vethcfg.h
	gcc -g $(EXTRACFLAGS) -c $<

vethcmd.o: vethcmd.c veth.h vethcfg.h
	gcc -g $(EXTRACFLAGS) -c $<

vethcfg.8: vethcfg.8.in vethcfg.h
	cpp -P $< | sed -e "/^$$/d" > $@

clean:
	rm -f *.o *.ko vethcfg vethcfg.8 make.defs include

install:
	ginstall -o root -g bin vethcfg $(prefix)/sbin
	ginstall -o root -g root vethcfg.8 $(prefix)/man/man8
	if [ $(LINUX_VER_SUB) = 2.2 ]; then \
		ginstall -o root -g root -m 644 veth.o /lib/modules/$(LINUX_VER)/net; \
	elif [ $(LINUX_VER_SUB) = 2.4 ]; then \
		mkdir -p /lib/modules/$(LINUX_VER)/veth; \
		ginstall -o root -g root -m 644 veth.o /lib/modules/$(LINUX_VER)/veth; \
	else \
		mv veth.o veth.ko; \
		mkdir -p /lib/modules/$(LINUX_VER)/veth; \
		ginstall -o root -g root -m 644 veth.ko /lib/modules/$(LINUX_VER)/veth; \
	fi
	depmod -a

uninstall:
	rm -f $(prefix)/sbin/vethcfg
	rm -f $(prefix)/man/man8/vethcfg.8
	if [ $(LINUX_VER_SUB) = 2.2 ]; then \
		rm -f /lib/modules/$(LINUX_VER)/net/veth.o; \
	elif [ $(LINUX_VER_SUB) = 2.4 ]; then \
		rm -f /lib/modules/$(LINUX_VER)/veth/veth.o; \
		rmdir /lib/modules/$(LINUX_VER)/veth; \
	else \
		rm -f /lib/modules/$(LINUX_VER)/veth/veth.ko; \
		rmdir /lib/modules/$(LINUX_VER)/veth; \
	fi
	depmod -a

-include make.defs
