#
# Makefile for Zaptel driver modules and utilities
#
# Copyright (C) 2001-2006 Digium, Inc.
#
#


HOSTCC=gcc
PWD:=$(shell pwd)

INSTALL_PREFIX:=$(DESTDIR)

# If you want to build for a kernel other than the current kernel, set KVERS
ifndef KVERS
KVERS:=$(shell uname -r)
endif
ifndef KSRC
  ifneq (,$(wildcard /lib/modules/$(KVERS)/build))
    KSRC:=/lib/modules/$(KVERS)/build
  else
    KSRC_SEARCH_PATH:=/usr/src/linux-2.4 /usr/src/linux
    KSRC:=$(shell for dir in $(KSRC_SEARCH_PATH); do if [ -d $$dir ]; then echo $$dir; break; fi; done)
  endif
endif
KINCLUDES:=$(KSRC)/include

CFLAGS+=-I. -O4 -g -Wall -DBUILDING_TONEZONE #-DTONEZONE_DRIVER
CFLAGS_PPC:=$(shell if uname -m | grep -q ppc; then echo "-fsigned-char"; fi)
CFLAGS_X86-64:=$(shell if uname -m | grep -q x86_64; then echo "-m64"; fi)
CFLAGS+=$(CFLAGS_PPC) $(CFLAGS_X86-64)
LCFLAGS:=-fPIC $(CFLAGS) -DBUILDING_TONEZONE
KFLAGS:=-I$(KINCLUDES) -O6
KFLAGS+=-DMODULE -D__KERNEL__ -DEXPORT_SYMTAB -I$(KSRC)/drivers/net \
	-Wall -I. -Wstrict-prototypes -fomit-frame-pointer -I$(KSRC)/drivers/net/wan -I$(KINCLUDES)/net
ifneq (,$(wildcard $(KINCLUDES)/linux/modversions.h))
  KFLAGS+=-DMODVERSIONS -include $(KINCLUDES)/linux/modversions.h
endif
KFLAGS_PPC:=$(shell if uname -m | grep -q ppc; then echo "-msoft-float -fsigned-char"; fi)
KFLAGS+=$(KFLAGS_PPC)
KFLAGS+=$(shell if uname -r | grep -q 2.4; then if uname -m | grep -q x86_64; then echo "-mcmodel=kernel"; fi; fi)

#
# Features are now configured in zconfig.h
#

KFLAGS+=-DSTANDALONE_ZAPATA
CFLAGS+=-DSTANDALONE_ZAPATA
KMAKE:= $(MAKE) -C $(KSRC) SUBDIRS=$(PWD)
KMAKE_INST:= $(KMAKE) INSTALL_MOD_PATH=$(INSTALL_PREFIX) INSTALL_MOD_DIR=misc modules_install

CONFIG_FILE:=$(INSTALL_PREFIX)/etc/zaptel.conf
CFLAGS+=-DZAPTEL_CONFIG=\"$(CONFIG_FILE)\"

ifeq (2.6,$(shell echo $(KVERS) | cut -d. -f1-2))
  BUILDVER:=linux26
else
  BUILDVER:=linux24
endif

ifeq ($(BUILDVER),linux24)
#We only support DEVFS in linux 2.4 kernels, since its considered obsolete post 2.4
DYNFS:=$(shell ps ax | grep -v grep | grep -q devfsd && echo "yes")
endif

ifeq ($(BUILDVER),linux26)
  ifneq (,$(wildcard $(INSTALL_PREFIX)/etc/udev/rules.d))
    DYNFS=yes
    UDEVRULES=yes
  endif
  HOTPLUG_FIRMWARE:=$(shell if grep CONFIG_FW_LOADER $(KINCLUDES)/linux/autoconf.h | grep -q undef; then echo "no"; else echo "yes"; fi)
endif

CHKCONFIG:=$(shell sh -c 'type -p chkconfig' 2> /dev/null)
ifndef CHKCONFIG
CHKCONFIG:=:
endif

ifneq ($(wildcard .version),)
  ZAPTELVERSION:=$(shell cat .version)
endif

# Set this to override hotplug firmware loading and revert to classic header based
#HOTPLUG_FIRMWARE=no

ifeq ($(HOTPLUG_FIRMWARE),yes)
  CFLAGS+=-DHOTPLUG_FIRMWARE
endif

# CVS mirrors of SVN have .svnrevision files showing
# which SVN revision they are based on, and .svnbranch
# showing the branch they are made from
ifneq ($(wildcard .svnrevision),)
  ZAPTELVERSION:=SVN-$(shell cat .svnbranch)-r$(shell cat .svnrevision)
else
  ifneq ($(wildcard .svn),)
    ZAPTELVERSION=SVN-$(shell build_tools/make_svn_branch_name)
  endif
endif

TZOBJS:=zonedata.lo tonezone.lo
LIBTONEZONE_SO:=libtonezone.so
LIBTONEZONE_SO_MAJOR_VER:=1
LIBTONEZONE_SO_MINOR_VER:=0

MODULES:=zaptel tor2 torisa wcusb wcfxo wctdm wctdm24xxp \
	 ztdynamic ztd-eth wct1xxp wcte11xp pciradio \
         ztd-loc # ztdummy
#MODULES+=wcfxsusb
# build ztdummy by default for 2.6 kernels
ifeq ($(BUILDVER),linux26)
MODULES+=ztdummy
endif

MODULESO:=$(MODULES:%=%.o)
MODULESKO:=$(MODULES:%=%.ko)

MOD_DESTDIR:=zaptel

obj-m:=$(MODULESO)
obj-m+=wct4xxp/

# Also build xpp in the subdirectory xpp/ . But only for >=2.6.10 and only 
# for i386. On other archs the module will probably build but panic.
# This line is only meaningful when this Makefile is used as kconfig for 
# 2.6 build

ifneq (,$(shell [ 0$(SUBLEVEL) -ge 10 ] && [ "$(ARCH)" = 'i386' ] && echo 1))
obj-m+=xpp/
endif

ifneq (,$(wildcard /usr/include/newt.h))
ZTTOOL:=zttool
endif
BINS=ztcfg torisatool makefw ztmonitor ztspeed $(ZTTOOL) zttest fxotune

all: $(BUILDVER) $(LIBTONEZONE_SO)

linux24: prereq $(MODULESO) wct4xxp/wct4xxp.o $(BINS)

linux26: prereq $(BINS)
	@echo $(KSRC)
	@if [ -z "$(KSRC)" -o ! -d "$(KSRC)" ]; then echo "You do not appear to have the sources for the $(KVERS) kernel installed."; exit 1 ; fi
	$(MAKE) -C $(KSRC) SUBDIRS=$(PWD) modules

version.h: FORCE
	ZAPTELVERSION="${ZAPTELVERSION}" build_tools/make_version_h > $@.tmp
	if cmp -s $@.tmp $@ ; then echo; else \
		mv $@.tmp $@ ; \
	fi
	rm -f $@.tmp

wct4xxp/wct4xxp.o:
	$(MAKE) -C wct4xxp KFLAGS="$(KFLAGS) -I.." CFLAGS="$(CFLAGS) -I.."

devel: tor2ee 

tests: patgen pattest patlooptest hdlcstress hdlctest hdlcgen hdlcverify timertest

tor2.o: tor2-hw.h tor2fw.h

zaptel.o: digits.h arith.h sec.h mec.h sec-2.h mec2.h mec3.h zconfig.h version.h

torisa.o: torisa.h

wcusb.o: wcusb.h

wcfxsusb.o: wcfxsusb.h

wctdm.o: wctdm.h

wctdm24xxp.o: wctdm.h

pciradio.o: radfw.h

ztdummy.o: ztdummy.h

$(MODULESO): %.o: %.c zaptel.h
	$(CC) $(KFLAGS) -o $@ -c $<

tor2ee.o: tor2-hw.h

tor2ee: tor2ee.o
	$(CC) $(CFLAGS) -o $@ $^ -lpci

zonedata.lo: zonedata.c
	$(CC) -c $(LCFLAGS) -o $@ $^

tonezone.lo: tonezone.c
	$(CC) -c $(LCFLAGS) -o $@ $^

torisatool: torisatool.o
	$(CC) -o $@ $^

tones.h: gendigits
	./gendigits > $@

tor2fw.h: makefw tormenta2.rbt
	./makefw tormenta2.rbt tor2fw > tor2fw.h

radfw.h: makefw pciradio.rbt
	./makefw pciradio.rbt radfw > radfw.h

gendigits: gendigits.o
	$(CC) -o $@ $^ -lm

zaptel.c: tones.h 

prereq: tones.h tor2fw.h radfw.h version.h

zttool.o: zttool.c zaptel.h

ztprovision.o: ztprovision.c zaptel.h

ztmonitor.o: ztmonitor.c zaptel.h

ztspeed.o: ztspeed.c
	$(CC) -o $@ -c $^

zttool: zttool.o
	$(CC) -o $@ $^ -lnewt

ztmonitor: ztmonitor.o
	$(CC) -o $@ $^

ztspeed: ztspeed.o
	$(CC) -o $@ $^

sethdlc-new: sethdlc-new.o
	$(CC) -o $@ $^

sethdlc-new.o: sethdlc-new.c
	$(CC) -o $@ -c $(CFLAGS) -I$(KINCLUDES) $^

libtonezone.a: $(TZOBJS)
	ar rcs libtonezone.a $^

$(LIBTONEZONE_SO): $(TZOBJS)
	$(CC) -shared -Wl,-soname,$(LIBTONEZONE_SO).$(LIBTONEZONE_SO_MAJOR_VER).$(LIBTONEZONE_SO_MINOR_VER) -lm -o $@ $^

ztcfg.c: ztcfg.h

ztcfg-shared: ztcfg.o $(LIBTONEZONE_SO)
	$(CC) -o $@ $^ -lm

ztcfg: ztcfg.o libtonezone.a
	$(CC) -o $@ $^ -lm

ztcfg-dude: ztcfg-dude.o mknotch.o complex.o $(LIBTONEZONE_SO)
	$(CC) -o $@ $^ -lm

mknotch.o: mknotch.cc
	$(CC) -o $@ -c $^

complex.o: complex.cc
	$(CC) -o $@ -c $^

usbfxstest.o: usbfxstest.c
	$(CC) -o $@ -g -c $^

usbfxstest: usbfxstest.o 
	$(CC) -o $@ $^ -lzap

fxstest: fxstest.o $(LIBTONEZONE_SO)
	$(CC) -o $@ $^ -lm

fxotune: fxotune.o
	$(CC) -o $@ $^ -lm

fxsdump: fxsdump.o
	$(CC) -o $@ $^ -lm

stackcheck: checkstack $(BUILDVER)
	./checkstack *.o

ztdiag: ztdiag.o 
	$(CC) -o $@ $^

devices:
ifndef DYNFS
	mkdir -p $(INSTALL_PREFIX)/dev/zap
	rm -f $(INSTALL_PREFIX)/dev/zap/ctl
	rm -f $(INSTALL_PREFIX)/dev/zap/channel
	rm -f $(INSTALL_PREFIX)/dev/zap/pseudo
	rm -f $(INSTALL_PREFIX)/dev/zap/timer
	rm -f $(INSTALL_PREFIX)/dev/zap/253
	rm -f $(INSTALL_PREFIX)/dev/zap/252
	rm -f $(INSTALL_PREFIX)/dev/zap/251
	rm -f $(INSTALL_PREFIX)/dev/zap/250
	mknod $(INSTALL_PREFIX)/dev/zap/ctl c 196 0
	mknod $(INSTALL_PREFIX)/dev/zap/timer c 196 253
	mknod $(INSTALL_PREFIX)/dev/zap/channel c 196 254
	mknod $(INSTALL_PREFIX)/dev/zap/pseudo c 196 255
	N=1; \
	while [ $$N -lt 250 ]; do \
		rm -f $(INSTALL_PREFIX)/dev/zap/$$N; \
		mknod $(INSTALL_PREFIX)/dev/zap/$$N c 196 $$N; \
		N=$$[$$N+1]; \
	done
else # DYNFS
  ifdef UDEVRULES
	build_tools/genudevrules > $(INSTALL_PREFIX)/etc/udev/rules.d/zaptel.rules
  else # !UDEVRULES
	@echo "**** Dynamic filesystem detected -- not creating device nodes"
  endif
endif

b410p:
	@wget -V >/dev/null || (echo "You need wget in order to install the drivers" ; false)
	[ -f misdn-b410p.tar.bz ] || wget ftp://ftp.digium.com/pub/zaptel/b410p/misdn-b410p.tar.gz
	tar -zxf misdn-b410p.tar.gz
	$(MAKE) -C misdn install
	[ -f mISDNuser.tar.gz ] || wget ftp://ftp.digium.com/pub/zaptel/b410p/mISDNuser.tar.gz
	tar -zxf mISDNuser.tar.gz
	$(MAKE) -C mISDNuser install

install: all devices firmware
	install -D -m 755 ztcfg $(INSTALL_PREFIX)/sbin/ztcfg
	if [ -f sethdlc-new ]; then \
		install -D -m 755 sethdlc-new $(INSTALL_PREFIX)/sbin/sethdlc; \
	elif [ -f sethdlc ]; then \
		install -D -m 755 sethdlc $(INSTALL_PREFIX)/sbin/sethdlc ; \
	fi
	if [ -f zttool ]; then install -D -m 755 zttool $(INSTALL_PREFIX)/sbin/zttool; fi
ifeq ($(BUILDVER),linux26)
	for x in $(MODULESKO); do \
		rm -f $(INSTALL_PREFIX)/lib/modules/$(KVERS)/extra/$$x ; \
	done; \
	$(KMAKE_INST);
else
	for x in $(MODULESO) wct4xxp/wct4xxp.o; do \
		install -D -m 644 $$x $(INSTALL_PREFIX)/lib/modules/$(KVERS)/misc/$$x ; \
	done;
endif
	if ! [ -f wcfxsusb.o ]; then \
		rm -f $(INSTALL_PREFIX)/lib/modules/$(KVERS)/misc/wcfxsusb.o; \
	fi; \
	rm -f $(INSTALL_PREFIX)/lib/modules/$(KVERS)/misc/wcfxs.o
	install -D -m 755 $(LIBTONEZONE_SO) $(INSTALL_PREFIX)/usr/lib/$(LIBTONEZONE_SO).$(LIBTONEZONE_SO_MAJOR_VER).$(LIBTONEZONE_SO_MINOR_VER)
	[ `id -u` = 0 ] && /sbin/ldconfig || :
	rm -f $(INSTALL_PREFIX)/usr/lib/$(LIBTONEZONE_SO)
	ln -sf $(LIBTONEZONE_SO).$(LIBTONEZONE_SO_MAJOR_VER).$(LIBTONEZONE_SO_MINOR_VER) \
		$(INSTALL_PREFIX)/usr/lib/$(LIBTONEZONE_SO).$(LIBTONEZONE_SO_MAJOR_VER)
	ln -sf $(LIBTONEZONE_SO).$(LIBTONEZONE_SO_MAJOR_VER).$(LIBTONEZONE_SO_MINOR_VER) \
		$(INSTALL_PREFIX)/usr/lib/$(LIBTONEZONE_SO)
	if [ -x /usr/sbin/sestatus ] && (/usr/sbin/sestatus | grep "SELinux status:" | grep -q "enabled") ; then restorecon -v $(INSTALL_PREFIX)/usr/lib/$(LIBTONEZONE_SO); fi
	install -D -m 644 zaptel.h $(INSTALL_PREFIX)/usr/include/linux/zaptel.h
	install -D -m 644 torisa.h $(INSTALL_PREFIX)/usr/include/linux/torisa.h
	install -D -m 644 tonezone.h $(INSTALL_PREFIX)/usr/include/tonezone.h
	install -m 644 doc/ztcfg.8 $(INSTALL_PREFIX)/usr/share/man/man8
	install -m 644 doc/zttool.8 $(INSTALL_PREFIX)/usr/share/man/man8
	[ `id -u` = 0 ] && /sbin/depmod -a $(KVERS) || :
	[ -f $(CONFIG_FILE) ] || install -D -m 644 zaptel.conf.sample $(CONFIG_FILE)
	build_tools/genmodconf $(BUILDVER) "$(INSTALL_PREFIX)" "$(filter-out zaptel,$(MODULES))"
	@if [ -d /etc/modutils ]; then \
		/sbin/update-modules ; \
	fi

install-udev: devices

firmware:
ifeq ($(HOTPLUG_FIRMWARE),yes)
	if [ -d $(INSTALL_PREFIX)/usr/lib/hotplug/firmware ]; then \
		install -m 644 wct4xxp/*.ima $(INSTALL_PREFIX)/usr/lib/hotplug/firmware; \
	fi
	if [ -d $(INSTALL_PREFIX)/lib/firmware ]; then \
		install -m 644 wct4xxp/*.ima $(INSTALL_PREFIX)/lib/firmware; \
	fi
	@echo "Installed firmware"
else
	@echo "Not installing firmware because we are not using hotplug firmware"
endif

config:
	if [ -d $(INSTALL_PREFIX)/etc/rc.d/init.d ]; then \
		install -D -m 755 zaptel.init $(INSTALL_PREFIX)/etc/rc.d/init.d/zaptel; \
		$(CHKCONFIG) --add zaptel; \
	elif [ -d $(INSTALL_PREFIX)/etc/init.d ]; then \
		install -D -m 755 zaptel.init $(INSTALL_PREFIX)/etc/init.d/zaptel; \
		$(CHKCONFIG) --add zaptel; \
	fi 
	if [ -d /etc/default ] && [ ! -f /etc/default/zaptel ]; then \
			install -D -m 644 zaptel.sysconfig $(INSTALL_PREFIX)/etc/default/zaptel; \
	fi
	if [ -d /etc/sysconfig ] && [ ! -f /etc/sysconfig/zaptel ]; then \
		install -D -m 644 zaptel.sysconfig $(INSTALL_PREFIX)/etc/sysconfig/zaptel; \
	fi
	if [ -d /etc/sysconfig/network-scripts ]; then \
		install -D -m 755 ifup-hdlc $(INSTALL_PREFIX)/etc/sysconfig/network-scripts/ifup-hdlc; \
	fi

update:
	@if [ -d .svn ]; then \
		echo "Updating from Subversion..." ; \
		svn update | tee update.out; \
		rm -f .version; \
		if [ `grep -c ^C update.out` -gt 0 ]; then \
			echo ; echo "The following files have conflicts:" ; \
			grep ^C update.out | cut -b4- ; \
		fi ; \
		rm -f update.out; \
	else \
		echo "Not under version control";  \
	fi

clean:
	rm -f torisatool makefw tor2fw.h radfw.h
	rm -f $(BINS)
	rm -f *.o ztcfg tzdriver sethdlc sethdlc-new
	rm -f $(TZOBJS) $(LIBTONEZONE_SO) *.lo
ifeq ($(BUILDVER),linux26)
	$(MAKE) -C $(KSRC) SUBDIRS=$(PWD) clean
else
	$(MAKE) -C wct4xxp clean
endif
	rm -rf .tmp_versions
	rm -f gendigits tones.h
	rm -f libtonezone*
	rm -f tor2ee
	rm -f fxotune
	rm -f core
	rm -f ztcfg-shared fxstest
	rm -rf misdn*
	rm -rf mISDNuser*

FORCE:
