#
# Makefile for tormenta/carrier driver and utilities
#
BASEADDR=0xd0000
DEFAULTZONE=0

KINCLUDES=$(KSRC)/include
CFLAGS+=-I. -O2 -g -Wall -DBUILDING_TONEZONE #-DTONEZONE_DRIVER
LCFLAGS=-fPIC $(CFLAGS) -DBUILDING_TONEZONE
KFLAGS=-I$(KINCLUDES) -O2
KFLAGS+=-DMODULE -D__KERNEL__ -DEXPORT_SYMTAB \
       -Wall -I. -Wstrict-prototypes -fomit-frame-pointer
KFLAGS+=$(shell [ -f $(KINCLUDES)/linux/modversions.h ] && echo "-DMODVERSIONS -include $(KINCLUDES)/linux/modversions.h")
KFLAGS+=$(shell if uname -m | grep -q ppc; then echo "-msoft-float -fsigned-char"; fi)

#
# Features
#
#
# Define CONFIG_CALC_XLAW if you have a small number of channels and/or
# a small level 2 cache, to optimize for few channels
#
#KFLAGS+=-DCONFIG_CALC_XLAW
#
# Define if you want MMX optimizations in zaptel
#
#KFLAGS+=-DCONFIG_ZAPTEL_MMX
#
# Pick your echo canceller: MARK2, MARK3, STEVE, or STEVE2 :)
# 
#KFLAGS+=-DECHO_CAN_STEVE
#KFLAGS+=-DECHO_CAN_STEVE2
#KFLAGS+=-DECHO_CAN_MARK
KFLAGS+=-DECHO_CAN_MARK2
#KFLAGS+=-DECHO_CAN_MARK3
#
# Uncomment for aggressive residual echo supression under 
# MARK2 echo canceller
#
#KFLAGS+=-DAGGRESSIVE_SUPPRESSOR
#
# Uncomment -DCONFIG_ZAPATA_NET to enable SyncPPP, CiscoHDLC, and Frame Relay
# support.
#
#KFLAGS+=-DCONFIG_ZAPATA_NET
#
# Uncomment for Generic PPP support (i.e. ZapRAS)
#
KFLAGS+=-DCONFIG_ZAPATA_PPP
#
# ISA Defaults can be set here.
#
KFLAGS+=-DTORMENTA_BASE=$(BASEADDR)

KFLAGS+=-DDEFAULT_TONE_ZONE=$(DEFAULTZONE)
#
# Uncomment if you have an SMP kernel
#
#KFLAGS+=-D__SMP__

KFLAGS+=-DSTANDALONE_ZAPATA
CFLAGS+=-DSTANDALONE_ZAPATA

INSTALL_PREFIX=

MODCONF=$(shell if [ -d $(INSTALL_PREFIX)/etc/modutils ]; then echo "$(INSTALL_PREFIX)/etc/modutils/zaptel"; elif [ -f $(INSTALL_PREFIX)/etc/modules.conf ]; then echo $(INSTALL_PREFIX)/etc/modules.conf; else echo $(INSTALL_PREFIX)/etc/conf.modules ; fi)

DEVFS=$(shell ps ax | grep -v grep | grep devfsd)

TZOBJS=zonedata.lo tonezone.lo
LIBTONEZONE=libtonezone.so.1.0
MODULES=zaptel.o tor2.o torisa.o wcusb.o wcfxo.o wcfxs.o \
	ztdynamic.o ztd-eth.o wct1xxp.o # ztdummy.o
#MODULES+=wcfxsusb.o
ZTTOOL=$(shell if [ -f /usr/include/newt.h ]; then echo zttool; fi)
#PRIMARY=wcfxsusb
PRIMARY=torisa
#PRIMARY=wcfxo

all:	modules programs

modules: $(MODULES)

programs: $(LIBTONEZONE) ztcfg torisatool makefw ztmonitor ztspeed $(ZTTOOL)

devel: tor2ee 

tests: patgen pattest patlooptest hdlcstress hdlctest hdlcgen hdlcverify timertest

tor2.o: tor2.c tor2-hw.h tor.h tor2fw.h zaptel.h
	gcc $(KFLAGS) -c tor2.c

zaptel.o: zaptel.c zaptel.h digits.h arith.h sec.h mec.h sec-2.h mec2.h mec3.h
	gcc $(KFLAGS) -c zaptel.c

torisa.o: torisa.c zaptel.h torisa.h
	gcc $(KFLAGS) -c torisa.c

wcusb.o: wcusb.c wcusb.h zaptel.h
	gcc $(KFLAGS) -c wcusb.c

wcfxsusb.o: wcfxsusb.c wcfxsusb.h zaptel.h
	gcc $(KFLAGS) -c wcfxsusb.c

wcfxo.o: wcfxo.c zaptel.h
	gcc $(KFLAGS) -c wcfxo.c

wct1xxp.o:wct1xxp.c zaptel.h
	gcc $(KFLAGS) -c wct1xxp.c

wcfxs.o:wcfxs.c zaptel.h
	gcc $(KFLAGS) -c wcfxs.c

ztdummy.o:ztdummy.c ztdummy.h
	gcc $(KFLAGS) -c ztdummy.c

ztdynamic.o: ztdynamic.c zaptel.h
	gcc $(KFLAGS)  -c ztdynamic.c

ztd-eth.o: ztd-eth.c zaptel.h
	gcc $(KFLAGS)  -c ztd-eth.c

tor2ee.o: tor2-hw.h

tor2ee: tor2ee.o
	gcc $(CFLAGS) -o tor2ee tor2ee.o -lpci

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

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

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

tones.h: gendigits
	./gendigits

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

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

zaptel.c: tones.h 

zttool.o: zttool.c zaptel.h

ztprovision.o: ztprovision.c zaptel.h

ztmonitor.o: ztmonitor.c zaptel.h

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

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

ztprovision: ztprovision.o
	$(CC) -o ztprovision ztprovision.o -lnewt

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

ztcat: ztcat.o
	$(CC) -o ztcat ztcat.o -ltonezone

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

$(LIBTONEZONE): $(TZOBJS)
	$(CC) -shared -Wl,-soname,libtonezone.so.1 -lm -o $@ $(TZOBJS)
	/sbin/ldconfig -n .
	ln -sf libtonezone.so.1 libtonezone.so                                  

ztcfg.c: ztcfg.h

ztcfg: ztcfg.o  $(LIBTONEZONE)
	$(CC) -o ztcfg ztcfg.o -lm -L. -ltonezone

ztcfg-dude: ztcfg-dude.o mknotch.o complex.o $(LIBTONEZONE)
	$(CC) -o ztcfg ztcfg-dude.o mknotch.o complex.o -lm -L. -ltonezone

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

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

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

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

fxstest: fxstest.o 
	$(CC) -o fxstest fxstest.o -ltonezone

devices:
ifeq ($(DEVFS),)
	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
	@echo "**** devfs detected -- not making device nodes"
endif

install: install-programs install-modules install-modconf devices

install-programs: programs
	install -d $(DESTDIR)$(INSTALL_PREFIX)/sbin
	install -m 755 ztcfg $(DESTDIR)$(INSTALL_PREFIX)/sbin
	if [ -f sethdlc ]; then install -m 755 sethdlc $(DESTDIR)$(INSTALL_PREFIX)/sbin ; fi
	if [ -f zttool ]; then install -m 755 zttool $(DESTDIR)$(INSTALL_PREFIX)/sbin; fi
	if [ -f ztmonitor ]; then install -m 755 ztmonitor $(DESTDIR)$(INSTALL_PREFIX)/sbin; fi

	install -d $(DESTDIR)$(INSTALL_PREFIX)/include/linux
#	install -m 644 zaptel.h $(DESTDIR)$(INSTALL_PREFIX)/include/linux
#	install -m 644 torisa.h $(DESTDIR)$(INSTALL_PREFIX)/include/linux
	install -m 644 tonezone.h $(DESTDIR)$(INSTALL_PREFIX)/include

	install -d $(DESTDIR)$(INSTALL_PREFIX)/lib
	install -m 755 $(LIBTONEZONE) $(DESTDIR)$(INSTALL_PREFIX)/lib
	ln -sf $(LIBTONEZONE) $(DESTDIR)$(INSTALL_PREFIX)/lib/libtonezone.so
	/sbin/ldconfig -n $(DESTDIR)$(INSTALL_PREFIX)/lib

install-modules:
	mkdir -p $(DESTDIR)/lib/modules/$(KVERS)/zaptel
	for x in $(MODULES); do \
		install -m 644 $$x $(DESTDIR)/lib/modules/$(KVERS)/zaptel ; \
	done

install-modconf:
	if [ -f $(MODCONF) ]; then mv -f $(MODCONF) $(MODCONF).bak ; fi
	cat $(MODCONF).bak | grep -v "alias char-major-250" | \
	grep -v "post-install torisa /sbin/ztcfg" | \
	grep -v "post-install wcfxsusb /sbin/ztcfg" | \
	grep -v "post-install wcfxs /sbin/ztcfg" > $(MODCONF) || true
	if ! grep "options torisa" $(MODCONF); then \
		echo "options torisa base=$(BASEADDR)" >> $(MODCONF); \
	fi
	if ! grep "alias char-major-196" $(MODCONF); then \
		echo "alias char-major-196 $(PRIMARY)" >> $(MODCONF); \
	fi
	if ! grep "post-install wcfxs" $(MODCONF); then \
		echo "post-install wcfxs /sbin/ztcfg" >> $(MODCONF); \
	fi
	if ! grep "post-install wcfxsusb" $(MODCONF); then \
		echo "post-install wcfxsusb /sbin/ztcfg" >> $(MODCONF); \
	fi
	if ! grep "post-install torisa" $(MODCONF); then \
		echo "post-install torisa /sbin/ztcfg" >> $(MODCONF); \
	fi
	if ! grep "post-install tor2" $(MODCONF); then \
		echo "post-install tor2 /sbin/ztcfg" >> $(MODCONF); \
	fi
	if ! grep "post-install wcfxo" $(MODCONF); then \
		echo "post-install wcfxo /sbin/ztcfg" >> $(MODCONF); \
	fi
	if ! grep "post-install wct1xxp" $(MODCONF); then \
		echo "post-install wct1xxp /sbin/ztcfg" >> $(MODCONF); \
	fi
	if [ -d /etc/modutils ]; then \
		/sbin/update-modules ; \
	fi
	-/sbin/depmod -a
	[ -f $(INSTALL_PREFIX)/etc/zaptel.conf ] || install -m 644 zaptel.conf.sample $(INSTALL_PREFIX)/etc/zaptel.conf

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

clean:
	rm -f torisatool makefw tor2fw.h fxstest ztmonitor ztspeed
	rm -f zttool
	rm -f *.o ztcfg tzdriver sethdlc
	rm -f $(TZOBJS) $(LIBTONEZONE) *.lo
	rm -f gendigits tones.h
	rm -f libtonezone*
	rm -f tor2ee
	rm -f core
