
# these drivers are for 2.2.x and 2.4.x

# KERNSRC is always needed to get kernel version
KERNSRC=/usr/src/linux
# CONFIG is needed to specify non default config file
CONFIG=/usr/src/linux/configs/unknown.config

ifeq ($(CONFIG),$(wildcard $(CONFIG)))
include $(CONFIG)
else
ifeq ($(KERNSRC)/.config,$(wildcard $(KERNSRC)/.config))
include $(KERNSRC)/.config
else
warn_config:
	@echo ======================================================= ; 
	@echo Cannot find kernel configuration file ;
	@echo Use KERNSRC to give kernel source directory or ;
	@echo CONFIG to specify kernel config file; 
	@echo ======================================================= ; 
	@exit 1; 
endif
endif

ifneq ($(KERNSRC)/Makefile,$(wildcard $(KERNSRC)/Makefile))
warn_makefile:
	@echo ======================================================= ; 
	@echo Cannot find kernel Makefile ;
	@echo Use KERNSRC to give kernel source directory ;
	@echo ======================================================= ; 
	@exit 1; 
endif


CCFLAGS= -O2 -I.
MCFLAGS= -Ilinux -I. -I$(KERNSRC)/include -O2  -D__KERNEL__ -DMODULE -fomit-frame-pointer  -DEXPORT_SYMTAB -Wall -Wstrict-prototypes -fno-strict-aliasing -pipe -mno-fp-regs -ffixed-8 -mcpu=ev6 -Wa,-mev6

ifeq ($(CONFIG_SMP),y)
MCFLAGS += -D__SMP__
endif

ifdef CONFIG_MODULES
ifdef CONFIG_MODVERSIONS
MCFLAGS += -DMODVERSIONS -include $(KERNSRC)/include/linux/modversions.h
endif
endif

VERSION       := $(shell grep VERSION $(KERNSRC)/Makefile | head -1 | awk '{print $$NF}' )
PATCHLEVEL    := $(shell grep PATCHLEVEL $(KERNSRC)/Makefile | head -1 | awk '{print $$NF}' )
SUBLEVEL      := $(shell grep SUBLEVEL $(KERNSRC)/Makefile | head -1 | awk '{print $$NF}' )
EXTRAVERSION  := $(shell grep EXTRAVERSION $(KERNSRC)/Makefile | head -1 | awk '$$NF != "=" {print $$NF}' )

VERSTR=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)

SYSTYPE       = $(shell chmod 755 ./getsystype; ./getsystype )

MTARGET=i2c-cchip.o i2c-smb-ali.o i2c-algo-pcf.o i2c-dev.o i2c-algo-bit.o i2c-core.o \
	i2c-up2000.o i2c-temp-mon.o sensors.o lm75.o adm9240.o sysactled.o

CTARGET=hwmon asset nodeid_up2k+

all: $(MTARGET) $(CTARGET)

redled.h: redled b2c.c
	cc -O b2c.c -o b2c
	./b2c redled redled.h sysactled_redled

sysactled.o:: redled.h

%.yy.c: %.lex %.tab.c
	flex -o$@ $<

%.tab.c: %.y
	bison -d $<

%: %.c
	$(CC) $(CCFLAGS) -o $@ $<

hwmon.o: hwmon.c hwmon.h hwmon_cfg.h
	$(CC) -O2 -I. -c -o $@ $<

hwmon: hwmon_cfg.tab.c hwmon_cfg.yy.c listmgr.o hwmon.o
	$(CC) -O2 -I. -o $@ hwmon_cfg.tab.c hwmon_cfg.yy.c listmgr.o hwmon.o -lfl


clean:
	rm -f $(MTARGET) $(CTARGET)
	rm -f *.yy.c *.tab.c *.tab.h

%.o: %.c
	$(CC) $(MCFLAGS) -c -o $@ $<

install: all
	if [ $(PATCHLEVEL) = 2 ] ; then \
		[ -d  /lib/modules/$(VERSTR)/misc ] || mkdir -p /lib/modules/$(VERSTR)/misc ; \
		cp -f $(MTARGET) /lib/modules/$(VERSTR)/misc ; \
	elif [ $(PATCHLEVEL) = 4 ] ; then \
		[ -d  /lib/modules/$(VERSTR)/kernel/drivers/misc ] || mkdir -p /lib/modules/$(VERSTR)/kernel/drivers/misc ; \
		cp -f $(MTARGET) /lib/modules/$(VERSTR)/kernel/drivers/misc ; \
	fi
	cp -f nodeid_cs20 nodeid_up2k+ hwmon /usr/sbin
	cp -f hwmon.d /etc/rc.d/init.d/hwmon
	chmod 755 /etc/rc.d/init.d/hwmon
	ln -sf /etc/rc.d/init.d/hwmon /etc/rc.d/rc3.d/S99hwmon
	cp -f sysactled.d /etc/rc.d/init.d/sysactled
	chmod 755 /etc/rc.d/init.d/sysactled
	ln -sf /etc/rc.d/init.d/sysactled /etc/rc.d/rc3.d/S99sysactled
	cp -f hwmon-$(SYSTYPE).conf /etc/hwmon.conf



