#
# Makefile for Macintosh 10.6+ version of Dire Wolf.
#

# TODO: This is a modified version of Makefile.linux and it
# has fallen a little behind.  For example, it is missing the check target.
# It would be more maintainable if we could use a single file for both.
# The differences are not that great.
# Maybe the most of the differences could go in to platform specific include
# files rather than cluttering it up with too many if blocks.

# Changes:
#
# 16 Dec 2015
# 1. Added condition check for gps/gpsd code. Commented out due to 32/64 bit
#    library issues. Macports gpsd build problem.
# 2. SDK version checks are now performed by a bash script 'search_sdks.sh'.
#    This should resolve the varied locations Apple stored the SDKs on the different
#    Xcode/OS versions. Executing 'make' on the first pass asks the operator
#    which SDK he/she wishes to use. Executing 'make clean' resets the SDK
#    selection and operator intervention is once again required. Selected SDK
#    information resides in a file named './use_this_sdk' in the current working
#    directory.
# 3. Removed fsk_fast_filter.h from atest receipe, clang compiler was having
#    a hissy fit. Not check with GCC.

APPS := direwolf decode_aprs text2tt tt2text ll2utm utm2ll aclients atest log2gpx gen_packets ttcalc

all :  $(APPS) direwolf.desktop direwolf.conf	@echo " "
	@echo "Next step install with: "
	@echo " "
	@echo "      sudo make install"
	@echo " "
	@echo " "

SYS_LIBS :=
SYS_MIN :=
#SDK := $(shell find /Developer -maxdepth 1 -type d -name "SDKs")
#$(info $$SDK = ${SDK})
#ifeq (${SDK},/Developer/SDKs)
#	SDK := $(shell find /Developer/SDKs -maxdepth 1 -type d -name "MacOSX10.8.sdk")
#	ifeq (${SDK},/Developer/SDKs/MacOSX10.8.sdk)
#		SYS_LIBS := -isystem /Developer/SDKs/MacOSX10.8.sdk
#		SYS_MIN := -mmacosx-version-min=10.8
#	else
#		SDK := $(shell find /Developer/SDKs -maxdepth 1 -type d -name "MacOSX10.9.sdk")
#		ifeq (${SDK},/Developer/SDKs/MacOSX10.9.sdk)
#			SYS_LIBS := -isystem /Developer/SDKs/MacOSX10.9.sdk
#			SYS_MIN := -mmacosx-version-min=10.9
#		else
#			SDK := $(shell find /Developer/SDKs -maxdepth 1 -type d -name "MacOSX10.10.sdk")
#			ifeq (${SDK},/Developer/SDKs/MacOSX10.10.sdk)
#				SYS_LIBS := -isystem /Developer/SDKs/MacOSX10.10.sdk
#				SYS_MIN := -mmacosx-version-min=10.10
#			endif
#		endif
#	endif
#endif

SYS_LIBS := $(shell ./search_sdks.sh)
EXTRA_CFLAGS :=
DARWIN_CC := $(shell which clang)
ifeq (${DARWIN_CC},)
DARWIN_CC := $(shell which gcc)
EXTRA_CFLAGS :=
else
EXTRA_CFLAGS := -fvectorize -fslp-vectorize -fslp-vectorize-aggressive -pthread
endif

# Change as required in support of the available libraries

#CC := $(DARWIN_CC) -m64 $(SYS_LIBS) $(SYS_MIN)
CC := $(DARWIN_CC) -m32 $(SYS_LIBS) $(SYS_MIN)

# _XOPEN_SOURCE=600 and _DEFAULT_SOURCE=1 are needed for glibc >= 2.24.
# Explanation here:  https://github.com/wb2osz/direwolf/issues/62

CFLAGS := -Os -pthread -Igeotranz -D_XOPEN_SOURCE=600 -D_DEFAULT_SOURCE=1 $(EXTRA_CFLAGS)

# That was fine for a recent Ubuntu and Raspbian Jessie.
# However, Raspbian wheezy was then missing declaration for strsep and definition of fd_set.

CFLAGS += -D_BSD_SOURCE


# $(info $$CC is [${CC}])

#
# The DSP filters spend a lot of time spinning around in little
# loops multiplying and adding arrays of numbers.  The Intel "SSE"
# instructions, introduced in 1999 with the Pentium III series,
# can speed this up considerably.
#
# SSE2 instructions, added in 2000, don't seem to offer any advantage.
#
#
# Let's take a look at the effect of the compile options.
#
#
# Times are elapsed time to process Track 2 of the TNC test CD.
#
# i.e.   "./atest 02_Track_2.wav"
# Default demodulator type is new "E" added for version 1.2.
#

#
# ---------- x86 (32 bit) ----------
#

#
# gcc 4.6.3 running on Ubuntu 12.04.05.
# Intel(R) Celeron(R) CPU 2.53GHz.  Appears to have only 32 bit instructions.
# Probably from around 2004 or 2005.
#
# When gcc is generating code for a 32 bit x86 target, it assumes the ancient
# i386 processor.  This is good for portability but bad for performance.
#
# The code can run considerably faster by taking advantage of the SSE instructions
# available in the Pentium 3 or later.
#
#       seconds options		comments
#       ------  -------		--------
#         524
#         183	-O2
#         182	-O3
#         183	-O3 -ffast-math  	(should be same as -Ofast)
#         184	-Ofast
#         189	-O3 -ffast-math -march=pentium
#         122	-O3 -ffast-math -msse
#         122	-O3 -ffast-math -march=pentium -msse
#         121	-O3 -ffast-math -march=pentium3   (this implies -msse)
#         120	-O3 -ffast-math -march=native
#
# Note that "-march=native" is essentially the same as "-march=pentium3."
#

# If the compiler is generating code for the i386 target, we can
# get much better results by telling it we have at least a Pentium 3.

CFLAGS += -march=core2 -msse4.1 -std=gnu99
#CFLAGS += -march=pentium3 -sse

#
# gcc 4.8.2 running on Ubuntu 14.04.1.
# Intel Core 2 Duo from around 2007 or 2008.
#
# 64 bit target implies that we have SSE and probably even better vector instructions.
#
#       seconds options		comments
#       ------  -------		--------
#         245
#          75	-01
#          72 	-02
#          71  	-03
#          73  	-O3 -march=native
#          42	-O3 -ffast-math
#          42  	-Ofast			(note below)
#          40	-O3 -ffast-math -march=native
#
#
# Note that "-Ofast" is a newer option roughly equivalent to "-O3 -ffast-math".
# I use the longer form because it is compatible with older compilers.
#
# Why don't I don't have "-march=native?"
# Older compilers don't recognize "native" as one of the valid options.
# One article said it was added with gcc 4.2 but I haven't verified that.
#

# Add -ffastmath in only if compiler version recognizes it.

useffast := $(shell gcc --help -v 2>/dev/null | grep ffast-math)
ifneq ($(useffast),)
CFLAGS += -ffast-math
endif

#
# You would expect "-march=native" to produce the fastest code.
# Why don't I use it here?
#
#	1. In my benchmarks, above, it has a negligible impact if any at all.
#	2. Some older versions of gcc don't recognize "native" as a valid choice.
#	3. Results are less portable.  Not a consideration if you are
#		building only for your own use but very important for anyone
#		redistributing a "binary" version.
#
# If you are planning to distribute the binary version to other
# people (in some ham radio software collection, RPM, or DEB package),
# avoid # fine tuning it for your particular computer.  It could
# cause compatibility issues for those with older computers.
#

#CFLAGS += -D_FORTIFY_SOURCE

# Use PortAudio Library

# Force static linking of portaudio if the static library is available.
PA_LIB_STATIC := $(shell find /opt/local/lib -maxdepth 1 -type f -name "libportaudio.a")
#$(info $$PA_LIB_STATIC is [${PA_LIB_STATIC}])
ifeq (${PA_LIB_STATIC},)
LDLIBS += -L/opt/local/lib -lportaudio
else
LDLIBS += /opt/local/lib/libportaudio.a
endif

# Include libraries portaudio requires.
LDLIBS += -framework CoreAudio -framework AudioUnit -framework AudioToolbox
LDLIBS += -framework Foundation -framework CoreServices

CFLAGS += -DUSE_PORTAUDIO -I/opt/local/include

# Uncomment following lines to enable GPS interface & tracker function.
# Not available for MacOSX (as far as I know).
# Although MacPorts has gpsd, wonder if it's the same thing. Add the check
# just in case it works.
# Well never mind, issue with Macports with 64bit libs ;-( leave the check in
# until (if ever) Macports fixes the issue.

#GPS_HEADER := $(shell find /opt/local/include -maxdepth 1 -type f -name "gps.h")
#ifeq (${GPS_HEADER},)
#GPS_OBJS :=
#else
#CFLAGS += -DENABLE_GPSD
#LDLIBS += -L/opt/local/lib -lgps -lgpsd
#GPS_OBJS := dwgps.o dwgpsnmea.o dwgpsd.o
#endif

# Name of current directory.
# Used to generate zip file name for distribution.

z := $(notdir ${CURDIR})


# Main application.

direwolf : direwolf.o aprs_tt.o audio_portaudio.o audio_stats.o ax25_link.o ax25_pad.o  ax25_pad2.o beacon.o \
		config.o decode_aprs.o dedupe.o demod_9600.o demod_afsk.o demod_psk.o \
		demod.o digipeater.o cdigipeater.o dlq.o dsp.o dtime_now.o dtmf.o dwgps.o \
		encode_aprs.o encode_aprs.o fcs_calc.o fcs_calc.o gen_tone.o \
		geotranz.a hdlc_rec.o hdlc_rec2.o hdlc_send.o igate.o kiss_frame.o \
		kiss.o kissserial.o kissnet.o latlong.o latlong.o log.o morse.o multi_modem.o \
		waypoint.o serial_port.o pfilter.o ptt.o rdq.o recv.o rrbb.o server.o \
		symbols.o telemetry.o textcolor.o tq.o tt_text.o tt_user.o xid.o xmit.o \
		dwgps.o dwgpsnmea.o mheard.o
	$(CC) $(CFLAGS) -o $@ $^ -lpthread $(LDLIBS) -lm


# Optimization for slow processors.

demod.o      : fsk_fast_filter.h

demod_afsk.o : fsk_fast_filter.h


fsk_fast_filter.h : gen_fff
	./gen_fff > fsk_fast_filter.h

gen_fff : demod_afsk.c dsp.c textcolor.c
	echo " " > tune.h
	$(CC) $(CFLAGS) -DGEN_FFF -o $@ $^ $(LDFLAGS)



# UTM, USNG, MGRS conversions.

geotranz.a : error_string.o  mgrs.o  polarst.o  tranmerc.o  ups.o  usng.o  utm.o
	ar -cr $@ $^

error_string.o : geotranz/error_string.c
	$(CC) $(CFLAGS) -c -o $@ $^

mgrs.o : geotranz/mgrs.c
	$(CC) $(CFLAGS) -c -o $@ $^

polarst.o : geotranz/polarst.c
	$(CC) $(CFLAGS) -c -o $@ $^

tranmerc.o : geotranz/tranmerc.c
	$(CC) $(CFLAGS) -c -o $@ $^

ups.o : geotranz/ups.c
	$(CC) $(CFLAGS) -c -o $@ $^

usng.o : geotranz/usng.c
	$(CC) $(CFLAGS) -c -o $@ $^

utm.o : geotranz/utm.c
	$(CC) $(CFLAGS) -c -o $@ $^



# Generate apprpriate sample configuration file for this platform.

direwolf.conf : generic.conf
	egrep '^C|^M' generic.conf | cut -c2-999 > direwolf.conf


# Where should we install it?

# My understanding, of the convention, is that something you compile
# from source, that is not a standard part of the operating system,
# should go in /usr/local/bin.

# This is a step in the right direction but not sufficient to use /usr instead.

INSTALLDIR := /usr/local

# TODO:  Test this better.

# Optional installation into /usr/local/...
# Needs to be run as root or with sudo.
# TODO: Review file locations.

# Command to "install" to system directories.  "install" for Linux.  "ginstall" for Mac.

INSTALL=ginstall

.PHONY: install
install : $(APPS) direwolf.conf tocalls.txt symbols-new.txt symbolsX.txt dw-icon.png direwolf.desktop
#
# Applications, not installed with package manager, normally go in /usr/local/bin.
# /usr/bin is used instead when installing from .DEB or .RPM package.
#
	$(INSTALL) direwolf $(INSTALLDIR)/bin
	$(INSTALL) decode_aprs $(INSTALLDIR)/bin
	$(INSTALL) text2tt $(INSTALLDIR)/bin
	$(INSTALL) tt2text $(INSTALLDIR)/bin
	$(INSTALL) ll2utm $(INSTALLDIR)/bin
	$(INSTALL) utm2ll $(INSTALLDIR)/bin
	$(INSTALL) aclients $(INSTALLDIR)/bin
	$(INSTALL) log2gpx $(INSTALLDIR)/bin
	$(INSTALL) gen_packets $(INSTALLDIR)/bin
	$(INSTALL) atest $(INSTALLDIR)/bin
	$(INSTALL) ttcalc $(INSTALLDIR)/bin
	$(INSTALL) dwespeak.sh $(INSTALLDIR)/bin
#
# Telemetry Toolkit executables.   Other .conf and .txt files will go into doc directory.
#
	$(INSTALL) telemetry-toolkit/telem-balloon.pl $(INSTALLDIR)/bin
	$(INSTALL) telemetry-toolkit/telem-bits.pl $(INSTALLDIR)/bin
	$(INSTALL) telemetry-toolkit/telem-data.pl $(INSTALLDIR)/bin
	$(INSTALL) telemetry-toolkit/telem-data91.pl $(INSTALLDIR)/bin
	$(INSTALL) telemetry-toolkit/telem-eqns.pl $(INSTALLDIR)/bin
	$(INSTALL) telemetry-toolkit/telem-parm.pl $(INSTALLDIR)/bin
	$(INSTALL) telemetry-toolkit/telem-unit.pl $(INSTALLDIR)/bin
	$(INSTALL) telemetry-toolkit/telem-volts.py $(INSTALLDIR)/bin
#
# Misc. data such as "tocall" to system mapping.
#
	$(INSTALL) -D --mode=644 tocalls.txt /usr/share/direwolf/tocalls.txt
	$(INSTALL) -D --mode=644 symbols-new.txt /usr/share/direwolf/symbols-new.txt
	$(INSTALL) -D --mode=644 symbolsX.txt /usr/share/direwolf/symbolsX.txt
	$(INSTALL) -D --mode=644 dw-icon.png /usr/share/direwolf/dw-icon.png
	$(INSTALL) -D --mode=644 direwolf.desktop /usr/share/applications/direwolf.desktop
#
# Documentation.  Various plain text files and PDF.
#
	$(INSTALL) -D --mode=644 README.md $(INSTALLDIR)/share/doc/direwolf/README.md
	$(INSTALL) -D --mode=644 CHANGES.md $(INSTALLDIR)/share/doc/direwolf/CHANGES.md
	$(INSTALL) -D --mode=644 LICENSE-dire-wolf.txt $(INSTALLDIR)/share/doc/direwolf/LICENSE-dire-wolf.txt
	$(INSTALL) -D --mode=644 LICENSE-other.txt $(INSTALLDIR)/share/doc/direwolf/LICENSE-other.txt
#
# ./README.md is an overview for the project main page.
# doc/README.md contains an overview of the PDF file contents and is more useful here.
#
	$(INSTALL) -D --mode=644 doc/README.md $(INSTALLDIR)/share/doc/direwolf/README.md
	$(INSTALL) -D --mode=644 doc/2400-4800-PSK-for-APRS-Packet-Radio.pdf $(INSTALLDIR)/share/doc/direwolf/2400-4800-PSK-for-APRS-Packet-Radio.pdf
	$(INSTALL) -D --mode=644 doc/A-Better-APRS-Packet-Demodulator-Part-1-1200-baud.pdf $(INSTALLDIR)/share/doc/direwolf/A-Better-APRS-Packet-Demodulator-Part-1-1200-baud.pdf
	$(INSTALL) -D --mode=644 doc/A-Better-APRS-Packet-Demodulator-Part-2-9600-baud.pdf $(INSTALLDIR)/share/doc/direwolf/A-Better-APRS-Packet-Demodulator-Part-2-9600-baud.pdf
	$(INSTALL) -D --mode=644 doc/A-Closer-Look-at-the-WA8LMF-TNC-Test-CD.pdf $(INSTALLDIR)/share/doc/direwolf/A-Closer-Look-at-the-WA8LMF-TNC-Test-CD.pdf
	$(INSTALL) -D --mode=644 doc/APRS-Telemetry-Toolkit.pdf $(INSTALLDIR)/share/doc/direwolf/APRS-Telemetry-Toolkit.pdf
	$(INSTALL) -D --mode=644 doc/APRStt-Implementation-Notes.pdf $(INSTALLDIR)/share/doc/direwolf/APRStt-Implementation-Notes.pdf
	$(INSTALL) -D --mode=644 doc/APRStt-interface-for-SARTrack.pdf $(INSTALLDIR)/share/doc/direwolf/APRStt-interface-for-SARTrack.pdf
	$(INSTALL) -D --mode=644 doc/APRStt-Listening-Example.pdf $(INSTALLDIR)/share/doc/direwolf/APRStt-Listening-Example.pdf
	$(INSTALL) -D --mode=644 doc/Bluetooth-KISS-TNC.pdf $(INSTALLDIR)/share/doc/direwolf/Bluetooth-KISS-TNC.pdf
	$(INSTALL) -D --mode=644 doc/Going-beyond-9600-baud.pdf $(INSTALLDIR)/share/doc/direwolf/Going-beyond-9600-baud.pdf
	$(INSTALL) -D --mode=644 doc/Raspberry-Pi-APRS.pdf $(INSTALLDIR)/share/doc/direwolf/Raspberry-Pi-APRS.pdf
	$(INSTALL) -D --mode=644 doc/Raspberry-Pi-APRS-Tracker.pdf $(INSTALLDIR)/share/doc/direwolf/Raspberry-Pi-APRS-Tracker.pdf
	$(INSTALL) -D --mode=644 doc/Raspberry-Pi-SDR-IGate.pdf $(INSTALLDIR)/share/doc/direwolf/Raspberry-Pi-SDR-IGate.pdf
	$(INSTALL) -D --mode=644 doc/Successful-APRS-IGate-Operation.pdf $(INSTALLDIR)/share/doc/direwolf/Successful-APRS-IGate-Operation.pdf
	$(INSTALL) -D --mode=644 doc/User-Guide.pdf $(INSTALLDIR)/share/doc/direwolf/User-Guide.pdf
	$(INSTALL) -D --mode=644 doc/WA8LMF-TNC-Test-CD-Results.pdf $(INSTALLDIR)/share/doc/direwolf/WA8LMF-TNC-Test-CD-Results.pdf
#
# Sample config files also go into the doc directory.
# When building from source, these can be put in home directory with "make install-conf".
# When installed from .DEB or .RPM package, the user will need to copy these to
# the home directory or other desired location.
# Someone suggested that these could go into an "examples" subdirectory under doc.
#
	$(INSTALL) -D --mode=644 direwolf.conf $(INSTALLDIR)/share/doc/direwolf/direwolf.conf
	$(INSTALL) -D --mode=644 telemetry-toolkit/telem-m0xer-3.txt $(INSTALLDIR)/share/doc/direwolf/telem-m0xer-3.txt
	$(INSTALL) -D --mode=644 telemetry-toolkit/telem-balloon.conf $(INSTALLDIR)/share/doc/direwolf/telem-balloon.conf
	$(INSTALL) -D --mode=644 telemetry-toolkit/telem-volts.conf $(INSTALLDIR)/share/doc/direwolf/telem-volts.conf
#
# "man" pages
#
	$(INSTALL) -D --mode=644 man1/aclients.1 $(INSTALLDIR)/man/man1/aclients.1
	$(INSTALL) -D --mode=644 man1/atest.1 $(INSTALLDIR)/man/man1/atest.1
	$(INSTALL) -D --mode=644 man1/decode_aprs.1 $(INSTALLDIR)/man/man1/decode_aprs.1
	$(INSTALL) -D --mode=644 man1/direwolf.1 $(INSTALLDIR)/man/man1/direwolf.1
	$(INSTALL) -D --mode=644 man1/gen_packets.1 $(INSTALLDIR)/man/man1/gen_packets.1
	$(INSTALL) -D --mode=644 man1/ll2utm.1 $(INSTALLDIR)/man/man1/ll2utm.1
	$(INSTALL) -D --mode=644 man1/log2gpx.1 $(INSTALLDIR)/man/man1/log2gpx.1
	$(INSTALL) -D --mode=644 man1/text2tt.1 $(INSTALLDIR)/man/man1/text2tt.1
	$(INSTALL) -D --mode=644 man1/tt2text.1 $(INSTALLDIR)/man/man1/tt2text.1
	$(INSTALL) -D --mode=644 man1/utm2ll.1 $(INSTALLDIR)/man/man1/utm2ll.1
#
	@echo " "
	@echo "If this is your first install, not an upgrade, type this to put a copy"
	@echo "of the sample configuration file (direwolf.conf) in your home directory:"
	@echo " "
	@echo "        make install-conf"
	@echo " "


# TODO:  Should we put the sample direwolf.conf file somewhere like
# /usr/share/doc/direwolf/examples and add that to the
# end of the search path list?
# That would make it easy to see user customizations compared to the
# latest sample.

# These would be done as ordinary user.


.PHONY: install-conf
install-conf : direwolf.conf
	cp direwolf.conf ~
	cp telemetry-toolkit/telem-m0xer-3.txt ~
	cp telemetry-toolkit/telem-*.conf ~


# Separate application to decode raw data.

# First two use .c rather than .o because they depend on DECAMAIN definition.

decode_aprs : decode_aprs.c kiss_frame.c  dwgpsnmea.o dwgps.o dwgpsd.o serial_port.o symbols.o ax25_pad.o textcolor.o fcs_calc.o latlong.o log.o telemetry.o tt_text.o
	$(CC) $(CFLAGS) -DDECAMAIN -o $@ $^ -lm

# Convert between text and touch tone representation.

text2tt : tt_text.c
	$(CC) $(CFLAGS) -DENC_MAIN -o $@ $^

tt2text : tt_text.c
	$(CC) $(CFLAGS) -DDEC_MAIN -o $@ $^


# Convert between Latitude/Longitude and UTM coordinates.

ll2utm : ll2utm.c geotranz.a
	$(CC) $(CFLAGS) -o $@ $^ -lm

utm2ll : utm2ll.c geotranz.a
	$(CC) $(CFLAGS) -o $@ $^ -lm


# Convert from log file to GPX.

log2gpx : log2gpx.c
	$(CC) $(CFLAGS) -o $@ $^ -lm


# Test application to generate sound.

gen_packets : gen_packets.c ax25_pad.c hdlc_send.c fcs_calc.c gen_tone.c morse.c dtmf.c textcolor.c dsp.c
	$(CC) $(CFLAGS) -o $@ $^ $(LDLIBS) -lm

demod.o      : tune.h

demod_afsk.o : tune.h

demod_9600.o : tune.h

demod_psk.o  : tune.h

tune.h :
	echo " " > tune.h


testagc : atest.c demod.c dsp.c demod_afsk.c demod_9600.c hdlc_rec.c hdlc_rec2.o multi_modem.o rrbb.o \
        fcs_calc.c ax25_pad.c decode_aprs.c telemetry.c dtime_now.o latlong.c symbols.c tune.h textcolor.c
	$(CC) $(CFLAGS) -o atest $^ -lm
	./atest 02_Track_2.wav | grep "packets decoded in" > atest.out


# Unit test for demodulators

atest : atest.c demod.c dsp.c demod_afsk.c demod_psk.c demod_9600.c hdlc_rec.c hdlc_rec2.o multi_modem.o rrbb.o \
        fcs_calc.c ax25_pad.c decode_aprs.c dwgpsnmea.o dwgps.o serial_port.o telemetry.c dtest_now.o latlong.c symbols.c textcolor.c tt_text.c
	$(CC) $(CFLAGS) -o $@ $^ -lm
#atest : atest.c fsk_fast_filter.h demod.c dsp.c demod_afsk.c demod_psk.c demod_9600.c hdlc_rec.c hdlc_rec2.o multi_modem.o rrbb.o \
#        fcs_calc.c ax25_pad.c decode_aprs.c dwgpsnmea.o dwgps.o serial_port.o telemetry.c latlong.c symbols.c textcolor.c tt_text.c
#	$(CC) $(CFLAGS) -o $@ $^ -lm

# Unit test for inner digipeater algorithm


dtest : digipeater.c pfilter.o ax25_pad.o dedupe.o fcs_calc.o tq.o textcolor.o \
		decode_aprs.o dwgpsnmea.o dwgps.o serial_port.o latlong.o telemetry.o symbols.o tt_text.o
	$(CC) $(CFLAGS) -DTEST -o $@ $^
	./dtest


# Unit test for APRStt.

ttest : aprs_tt.c tt_text.c  latlong.c geotranz.a
	$(CC) $(CFLAGS) -DTT_MAIN  -o $@ $^


# Unit test for IGate


itest : igate.c textcolor.c ax25_pad.c fcs_calc.c
	$(CC) $(CFLAGS) -DITEST -o $@ $^
	./itest


# Unit test for UDP reception with AFSK demodulator

udptest : udp_test.c demod.c dsp.c demod_afsk.c demod_9600.c hdlc_rec.c hdlc_rec2.c multi_modem.c rrbb.c fcs_calc.c ax25_pad.c decode_aprs.c symbols.c textcolor.c
	$(CC) $(CFLAGS) -o $@ $^ -lm
	./udptest


# Unit test for telemetry decoding.


tlmtest : telemetry.c ax25_pad.c fcs_calc.c textcolor.c
	$(CC) $(CFLAGS) -o $@ $^ -lm
	./tlmtest


# Multiple AGWPE network or serial port clients to test TNCs side by side.

aclients : aclients.c ax25_pad.c fcs_calc.c textcolor.c
	$(CC) $(CFLAGS) -g -o $@ $^


# Touch Tone to Speech sample application.

ttcalc : ttcalc.o ax25_pad.o fcs_calc.o textcolor.o
	$(CC) $(CFLAGS) -g -o $@ $^


depend : $(wildcard *.c)
	makedepend -f $(lastword $(MAKEFILE_LIST)) -- $(CFLAGS) -- $^


.PHONY: clean
clean :
	rm -f $(APPS) gen_fff \
		fsk_fast_filter.h *.o *.a use_this_sdk
	echo " " > tune.h


.PHONY: dist-mac
dist-mac: direwolf decode_aprs text2tt tt2text ll2utm utm2ll aclients log2gpx gen_packets \
		tocalls.txt symbols-new.txt symbolsX.txt dw-icon.png
	rm -f ../direwolf_dist_bin.zip
	(cd .. ; zip direwolf_dist_bin.zip \
	$(INSTALLDIR)/bin/direwolf \
	$(INSTALLDIR)/bin/decode_aprs \
	$(INSTALLDIR)/bin/text2tt \
	$(INSTALLDIR)/bin/tt2text \
	$(INSTALLDIR)/bin/ll2utm \
	$(INSTALLDIR)/bin/utm2ll \
	$(INSTALLDIR)/bin/aclients \
	$(INSTALLDIR)/bin/log2gpx \
	$(INSTALLDIR)/bin/gen_packets \
	$(INSTALLDIR)/bin/atest \
	$(INSTALLDIR)/bin/ttcalc \
	$(INSTALLDIR)/bin/dwespeak.sh \
	$(INSTALLDIR)/share/direwolf/tocalls.txt \
	$(INSTALLDIR)/share/direwolf/config/direwolf.conf \
	$(INSTALLDIR)/share/direwolf/symbols-new.txt \
	$(INSTALLDIR)/share/direwolf/symbolsX.txt \
	$(INSTALLDIR)/share/direwolf/dw-icon.png \
	$(INSTALLDIR)/share/doc/direwolf/README.md \
	$(INSTALLDIR)/share/doc/direwolf/CHANGES.md \
	$(INSTALLDIR)/share/doc/direwolf/LICENSE-dire-wolf.txt \
	$(INSTALLDIR)/share/doc/direwolf/LICENSE-other.txt \
	$(INSTALLDIR)/share/doc/direwolf/User-Guide.pdf \
	$(INSTALLDIR)/share/doc/direwolf/Raspberry-Pi-APRS.pdf \
	$(INSTALLDIR)/share/doc/direwolf/Raspberry-Pi-APRS-Tracker.pdf \
	$(INSTALLDIR)/share/doc/direwolf/APRStt-Implementation-Notes.pdf \
	$(INSTALLDIR)/share/doc/direwolf/APRS-Telemetry-Toolkit.pdf \
	$(INSTALLDIR)/man/man1/aclients.1 \
	$(INSTALLDIR)/man/man1/atest.1 \
	$(INSTALLDIR)/man/man1/decode_aprs.1 \
	$(INSTALLDIR)/man/man1/direwolf.1 \
	$(INSTALLDIR)/man/man1/gen_packets.1 \
	$(INSTALLDIR)/man/man1/ll2utm.1 \
	$(INSTALLDIR)/man/man1/log2gpx.1 \
	$(INSTALLDIR)/man/man1/text2tt.1 \
	$(INSTALLDIR)/man/man1/tt2text.1 \
	$(INSTALLDIR)/man/man1/utm2ll.1 \
	)

# Package it up for distribution.

.PHONY: dist-src
dist-src : README.md CHANGES.md \
		doc/User-Guide.pdf doc/Raspberry-Pi-APRS.pdf \
		doc/Raspberry-Pi-APRS-Tracker.pdf doc/APRStt-Implementation-Notes.pdf \
		dw-start.sh dwespeak.bat dwespeak.sh \
		tocalls.txt symbols-new.txt symbolsX.txt direwolf.spec
	rm -f fsk_fast_filter.h
	echo " " > tune.h
	rm -f ../$z-src.zip
	(cd .. ; zip $z-src.zip \
		$z/README.md \
		$z/CHANGES.md \
		$z/LICENSE* \
		$z/doc/User-Guide.pdf \
		$z/doc/Raspberry-Pi-APRS.pdf \
		$z/doc/Raspberry-Pi-APRS-Tracker.pdf \
		$z/doc/APRStt-Implementation-Notes.pdf \
		$z/Makefile* \
		$z/*.c $z/*.h \
		$z/regex/* $z/misc/* $z/geotranz/* \
		$z/man1/* \
		$z/generic.conf \
		$z/tocalls.txt $z/symbols-new.txt $z/symbolsX.txt \
		$z/dw-icon.png $z/dw-icon.rc $z/dw-icon.ico \
		$z/dw-start.sh $z/direwolf.spec \
		$z/dwespeak.bat $z/dwespeak.sh \
		$z/telemetry-toolkit/* )


#
# The destination field is often used to identify the manufacturer/model.
# These are not hardcoded into Dire Wolf.  Instead they are read from
# a file called tocalls.txt at application start up time.
#
# The original permanent symbols are built in but the "new" symbols,
# using overlays, are often updated.  These are also read from files.
#
# You can obtain an updated copy by typing "make tocalls-symbols".
# This is not part of the normal build process.  You have to do this explicitly.
#
# The locations below appear to be the most recent.
# The copy at http://www.aprs.org/tocalls.txt is out of date.
#

.PHONY: tocalls-symbols
tocalls-symbols :
	cp tocalls.txt tocalls.txt~
	wget http://www.aprs.org/aprs11/tocalls.txt -O tocalls.txt
	-diff -Z tocalls.txt~ tocalls.txt
	cp symbols-new.txt symbols-new.txt~
	wget http://www.aprs.org/symbols/symbols-new.txt -O symbols-new.txt
	-diff -Z symbols-new.txt~ symbols-new.txt
	cp symbolsX.txt symbolsX.txt~
	wget http://www.aprs.org/symbols/symbolsX.txt -O symbolsX.txt
	-diff -Z symbolsX.txt~ symbolsX.txt
