###############################################################
# LR: A virtual ethernet device driver for Linux. 
#
# This device driver is an implementation of Redundancy of
# Link Segment (Yumo's original protocol).
#
# 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=lrcfg.o\
  lrcmd.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 lr.o
	make lrcfg
	make lrcfg.8


lr.o: lr.c lr.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=lr -DKBUILD_MODNAME=lr -c -o lr.o lr.c

lrcfg: lrcfg.o lrcmd.o
	gcc -g -o $@ lrcfg.o lrcmd.o $(EXTRALDFLAGS)

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

lrcmd.o: lrcmd.c lrcfg.h
	gcc -g $(EXTRACFLAGS) -c $<

lrcmd_mod.o: lrcmd_mod.c lrcfg.h
	gcc -g $(EXTRACFLAGS) -c $<

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

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


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

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

-include make.defs
