#
# Makefile for native Windows version of Dire Wolf.
#
#
# This is built in the Cygwin environment but with the 
# compiler from http://www.mingw.org/ so there is no
# dependency on extra DLLs.
#
# The MinGW/bin directory must be in the PATH for the 
# compiler.  e.g.   export PATH=/cygdrive/c/MinGW/bin:$PATH
#
# Failure to have the path set correctly will result in the
# obscure message:     Makefile.win:... recipe for target ... failed. 
#
# Type "which gcc" to make sure you are getting the right one!
#


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


# People say we need -mthreads option for threads to work properly.
# They also say it creates a dependency on mingwm10.dll but I'm not seeing that.

CC := gcc
CFLAGS :=    -Wall -Ofast -march=pentium3 -msse -Iregex -Iutm -Igeotranz -mthreads -DUSE_REGEX_STATIC
AR := ar

CFLAGS += -g

#
# Let's see impact of various optimization levels.
# Benchmark results with MinGW gcc version 4.6.2.
#
#	seconds	options, comments
#	------	-----------------
#	119.8	-O2	Used for version 0.8
#	 92.1	-O3
#	 88.7	-Ofast  (should be same as -O3 -ffastmath)
#	 87.5	-Ofast -march=pentium
#	 74.1	-Ofast -msse
#	 72.2	-Ofast -march=pentium -msse
#	 62.0	-Ofast -march=pentium3   (this implies -msse)
#	 61.9	-Ofast -march=pentium3 -msse
#
# A minimum of Windows XP is required due to some of the system
# features being used.  XP requires a Pentium processor or later.
# The DSP filters can be sped up considerably with the SSE instructions.
# The SSE instructions were introduced in 1999 with the 
# Pentium III series.  
# SSE2 instructions, added in 2000, don't seem to offer any advantage.
#
# For version 0.9, a Pentium 3 or equivalent is now the minimum required
# for the prebuilt Windows distribution.
# If you insist on using a computer from the previous century,
# you can compile this yourself with different options.
#




# --------------------------------------  Main application   --------------------------------

demod.o : fsk_demod_state.h
demod_9600.o : fsk_demod_state.h
demod_afsk.o : fsk_demod_state.h


direwolf : direwolf.o config.o recv.o demod.o dsp.o demod_afsk.o demod_9600.o hdlc_rec.o \
		hdlc_rec2.o multi_modem.o redecode.o rdq.o rrbb.o dlq.o \
		fcs_calc.o ax25_pad.o \
		decode_aprs.o symbols.o server.o kiss.o kissnet.o kiss_frame.o hdlc_send.o fcs_calc.o \
		gen_tone.o morse.o audio_win.o audio_stats.o digipeater.o pfilter.o dedupe.o tq.o xmit.o \
		ptt.o beacon.o dwgps.o encode_aprs.o latlong.o textcolor.o \
		dtmf.o aprs_tt.o tt_user.o tt_text.o igate.o nmea.o serial_port.o log.o telemetry.o \
		dwgps.o dwgpsnmea.o dtime_now.o \
		dw-icon.o regex.a misc.a geotranz.a
	$(CC) $(CFLAGS) -o $@ $^ -lwinmm -lws2_32

dw-icon.o : dw-icon.rc dw-icon.ico
	windres dw-icon.rc -o $@


# Optimization for slow processors.

demod.o : fsk_fast_filter.h

demod_afsk.o : fsk_fast_filter.h


fsk_fast_filter.h : demod_afsk.c
	$(CC) $(CFLAGS) -o gen_fff -DGEN_FFF demod_afsk.c dsp.c textcolor.c
	./gen_fff > fsk_fast_filter.h


#
# 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 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 symbols-new.txt~ symbols-new.txt
	cp symbolsX.txt symbolsX.txt~
	wget http://www.aprs.org/symbols/symbolsX.txt -O symbolsX.txt
	-diff symbolsX.txt~ symbolsX.txt



# ----------------------------  Other utilities included with distribution  -------------------------


# Separate application to decode raw data.

decode_aprs : decode_aprs.c dwgpsnmea.o dwgps.o serial_port.o symbols.o ax25_pad.o textcolor.o fcs_calc.o latlong.o log.o telemetry.o tt_text.c regex.a misc.a geotranz.a
	$(CC) $(CFLAGS) -DDECAMAIN -o decode_aprs $^


# Convert between text and touch tone representation.

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

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


# Convert between Latitude/Longitude and UTM coordinates.

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

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


# Convert from log file to GPX.

log2gpx : log2gpx.c textcolor.o misc.a
	$(CC) $(CFLAGS) -o $@ $^


# Test application to generate sound.

gen_packets : gen_packets.o  ax25_pad.o hdlc_send.o fcs_calc.o gen_tone.o morse.o textcolor.o dsp.o misc.a regex.a
	$(CC) $(CFLAGS) -o $@ $^



# -------------------------------------------  Libraries  --------------------------------------------



# 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 $@ $^


#
# When building for Linux, we use regular expression
# functions supplied by the gnu C library.
# For the native WIN32 version, we need to use our own copy.
# These were copied from http://gnuwin32.sourceforge.net/packages/regex.htm
#

regex.a : regex.o
	ar -cr $@ $^	
 
regex.o : regex/regex.c
	$(CC) $(CFLAGS) -Dbool=int -Dtrue=1 -Dfalse=0 -c -o $@ $^


# There are several string functios found in Linux
# but not on Windows.  Need to provide our own copy.

misc.a : strsep.o strtok_r.o strcasestr.o strlcpy.o strlcat.o
	ar -cr $@ $^	
 
strsep.o : misc/strsep.c
	$(CC) $(CFLAGS) -c -o $@ $^

strtok_r.o : misc/strtok_r.c
	$(CC) $(CFLAGS) -c -o $@ $^

strcasestr.o : misc/strcasestr.c
	$(CC) $(CFLAGS) -c -o $@ $^

strlcpy.o : misc/strlcpy.c
	$(CC) $(CFLAGS) -I. -c -o $@ $^

strlcat.o : misc/strlcat.c
	$(CC) $(CFLAGS) -I. -c -o $@ $^


# ---------------------------------  Automated Smoke Test  --------------------------------


# Combine some unit tests into a single regression sanity check.


check : dtest ttest tttexttest pftest tlmtest lltest enctest kisstest check-modem1200 check-modem300 check-modem9600 

# Can we encode and decode at popular data rates?
# Verify that single bit fixup increases the count.

check-modem1200 : gen_packets atest
	gen_packets -n 100 -o test1.wav
	atest -F0 -PE -L70 -G71 test1.wav
	atest -F1 -PE -L73 -G75 test1.wav
	#rm test1.wav

check-modem300 : gen_packets atest
	gen_packets -B300 -n 100 -o test3.wav
	atest -B300 -F0 -L68 -G69 test3.wav
	atest -B300 -F1 -L73 -G75 test3.wav
	rm test3.wav

check-modem9600 : gen_packets atest
	gen_packets -B9600 -n 100 -o test9.wav
	atest -B9600 -F0 -L57 -G59 test9.wav
	atest -B9600 -F1 -L66 -G67 test9.wav
	rm test9.wav

# Unit test for AFSK demodulator

atest : atest.c fsk_fast_filter.h demod.c demod_afsk.c demod_9600.c \
		dsp.o hdlc_rec.o hdlc_rec2.o multi_modem.o \
		rrbb.o fcs_calc.o ax25_pad.o decode_aprs.o \
		dwgpsnmea.o dwgps.o serial_port.o latlong.c \
		symbols.c tt_text.c textcolor.c telemetry.c \
		misc.a regex.a
	echo " " > tune.h
	$(CC) $(CFLAGS) -o $@ $^
	#./atest ..\\direwolf-0.2\\02_Track_2.wav
	#atest -B 9600 z9.wav
	#atest za100.wav

atest9 : atest.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 latlong.c symbols.c textcolor.c telemetry.c misc.a regex.a \
		fsk_fast_filter.h
	echo " " > tune.h
	$(CC) $(CFLAGS) -o $@ $^
	./atest9 -B 9600 ../walkabout9600.wav | grep "packets decoded in" >atest.out
	#./atest9 -B 9600 noise96.wav


# Unit test for inner digipeater algorithm

.PHONY: dtest
dtest : digipeater.c dedupe.c \
		pfilter.o ax25_pad.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 misc.a regex.a
	$(CC) $(CFLAGS) -DDIGITEST -o $@ $^
	./dtest
	rm dtest.exe

# Unit test for APRStt tone seqence parsing.

.PHONTY: ttest
ttest : aprs_tt.c tt_text.c latlong.o textcolor.o geotranz.a misc.a
	$(CC) $(CFLAGS) -Igeotranz -DTT_MAIN  -o $@ $^
	./ttest
	rm ttest.exe

# Unit test for APRStt tone sequence / text conversions.

.PHONY: tttexttest
tttexttest : tt_text.c textcolor.o misc.a
	$(CC) $(CFLAGS) -DTTT_TEST -o $@ $^
	./tttexttest
	rm tttexttest.exe

# Unit test for Packet Filtering.

.PHONY: pftest
pftest : pfilter.c ax25_pad.o textcolor.o fcs_calc.o decode_aprs.o dwgpsnmea.o dwgps.o serial_port.o latlong.o symbols.o telemetry.o tt_text.o misc.a regex.a
	$(CC) $(CFLAGS) -DPFTEST -o $@ $^
	./pftest
	rm pftest.exe



# Unit test for telemetry decoding.

.PHONY: tlmtest
tlmtest : telemetry.c ax25_pad.o fcs_calc.o textcolor.o misc.a regex.a
	$(CC) $(CFLAGS) -DTEST -o $@ $^
	./tlmtest
	rm tlmtest.exe


# Unit test for location coordinate conversion.

.PHONY: lltest
lltest : latlong.c textcolor.o misc.a
	$(CC) $(CFLAGS) -DLLTEST -o $@ $^
	./lltest
	rm lltest.exe

# Unit test for encoding position & object report.

.PHONY: enctest
enctest : encode_aprs.c latlong.c textcolor.c misc.a
	$(CC) $(CFLAGS) -DEN_MAIN -o $@ $^
	./enctest
	rm enctest.exe


# Unit test for KISS encapsulation.

.PHONY: kisstest
kisstest : kiss_frame.c
	$(CC) $(CFLAGS) -DKISSTEST -o $@ $^
	./kisstest
	rm kisstest.exe


# ------------------------------ Other manual testing & experimenting  -------------------------------


# For tweaking the demodulator.

demod.o : tune.h
demod_9600.o : tune.h
demod_afsk.o : tune.h


testagc : atest.c demod.c dsp.c demod_afsk.c demod_9600.o fsk_demod_agc.h \
		hdlc_rec.o hdlc_rec2.o multi_modem.o \
		rrbb.o fcs_calc.o ax25_pad.o decode_aprs.o latlong.o symbols.o textcolor.o telemetry.o \
		dwgpsnmea.o dwgps.o serial_port.o tt_text.o regex.a misc.a
	rm -f atest.exe
	$(CC) $(CFLAGS) -o atest $^
	./atest -P GGG- -F 0 ../02_Track_2.wav | grep "packets decoded in" >atest.out
	echo " " > tune.h


noisy3.wav : gen_packets
	./gen_packets -B 300 -n 100 -o noisy3.wav

testagc3 : atest.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 latlong.c symbols.c textcolor.c telemetry.c regex.a misc.a \
		tune.h 
	rm -f atest.exe
	$(CC) $(CFLAGS) -o atest $^
	./atest -B 300 -P D -D 3 noisy3.wav | grep "packets decoded in" >atest.out
	echo " " > tune.h


noisy96.wav : gen_packets
	./gen_packets -B 9600 -n 100 -o noisy96.wav

testagc9 : atest.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 latlong.c symbols.c textcolor.c telemetry.c regex.a misc.a \
		tune.h 
	rm -f atest.exe
	$(CC) $(CFLAGS) -o atest $^
	./atest -B 9600 ../walkabout9600.wav | grep "packets decoded in" >atest.out
	#./atest -B 9600 noisy96.wav | grep "packets decoded in" >atest.out
	echo " " > tune.h

# Unit test for IGate

itest : igate.c textcolor.c ax25_pad.c fcs_calc.c misc.a regex.a
	$(CC) $(CFLAGS) -DITEST -o $@ $^ -lwinmm -lws2_32





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

aclients : aclients.c ax25_pad.c fcs_calc.c textcolor.c misc.a regex.a
	$(CC) $(CFLAGS) -o $@ $^ -lwinmm -lws2_32


# Touch Tone to Speech sample application.

ttcalc : ttcalc.o ax25_pad.o fcs_calc.o textcolor.o misc.a regex.a
	$(CC) $(CFLAGS) -o $@ $^ -lwinmm -lws2_32


# Send GPS location to KISS TNC each second.

walk96 : walk96.c dwgps.o dwgpsnmea.o kiss_frame.o \
		latlong.o encode_aprs.o serial_port.o textcolor.o \
		ax25_pad.o fcs_calc.o \
		xmit.o hdlc_send.o gen_tone.o ptt.o tq.o \
		hdlc_rec.o hdlc_rec2.o rrbb.o dsp.o audio_win.o \
		multi_modem.o demod.o demod_afsk.o demod_9600.o rdq.o \
		server.o morse.o audio_stats.o dtime_now.o dlq.o \
		regex.a misc.a 
	$(CC) $(CFLAGS) -DWALK96 -o $@ $^ -lwinmm -lws2_32



#--------------------------------------------------------------


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

.PHONY: clean
clean :
	rm -f *.o *.a *.exe fsk_fast_filter.h noisy96.wav
	echo " " > tune.h



# -------------------------------  Packaging for distribution  ----------------------

# Name of zip file for distribution.

z := $(notdir ${CURDIR})


.PHONY: dist-win
dist-win : direwolf.exe decode_aprs.exe text2tt.exe tt2text.exe ll2utm.exe utm2ll.exe \
			aclients.exe log2gpx.exe gen_packets.exe atest.exe ttcalc.exe \
		 	generic.conf dwespeak.bat \
			README.md CHANGES.md \
			doc/User-Guide.pdf \
			doc/Raspberry-Pi-APRS.pdf \
			doc/APRStt-Implementation-Notes.pdf 
	rm -f ../$z-win.zip
	egrep '^C|^W' generic.conf | cut -c2-999 > direwolf.conf
	unix2dos direwolf.conf
	zip --junk-paths ../$z-win.zip \
		README.md \
		CHANGES.md \
		doc/User-Guide.pdf \
		doc/Raspberry-Pi-APRS.pdf \
		doc/A-Better-APRS-Packet-Demodulator-Part-1-1200-baud.pdf \
		doc/A-Better-APRS-Packet-Demodulator-Part-2-9600-baud.pdf \
		doc/APRS-Telemetry-Toolkit.pdf \
		doc/APRStt-Implementation-Notes.pdf \
		doc/APRStt-interface-for-SARTrack.pdf \
		doc/APRStt-Listening-Example.pdf \
		doc/Raspberry-Pi-APRS.pdf \
		doc/Raspberry-Pi-APRS-Tracker.pdf \
		doc/Raspberry-Pi-SDR-IGate.pdf \
		doc/User-Guide.pdf \
		doc/WA8LMF-TNC-Test-CD-Results.pdf \
		LICENSE* \
		direwolf.conf \
		direwolf.exe \
		decode_aprs.exe \
		tocalls.txt symbols-new.txt symbolsX.txt \
		text2tt.exe tt2text.exe \
		ll2utm.exe utm2ll.exe \
		aclients.exe \
		log2gpx.exe \
		gen_packets.exe \
		atest.exe \
		ttcalc.exe \
		dwespeak.bat \
		telemetry-toolkit/*

.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 \
		doc/APRS-Telemetry-Toolkit.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
	dos2unix generic.conf
	dos2unix Makefile
	dos2unix Makefile.linux
	dos2unix Makefile.macosx
	dos2unix telemetry-toolkit/telem-balloon.conf
	dos2unix telemetry-toolkit/telem-balloon.pl
	dos2unix telemetry-toolkit/telem-bits.pl
	dos2unix telemetry-toolkit/telem-data.pl
	dos2unix telemetry-toolkit/telem-data91.pl
	dos2unix telemetry-toolkit/telem-eqns.pl
	dos2unix telemetry-toolkit/telem-m0xer-3.txt
	dos2unix telemetry-toolkit/telem-parm.pl
	dos2unix telemetry-toolkit/telem-unit.pl
	dos2unix telemetry-toolkit/telem-volts.py
	dos2unix telemetry-toolkit/telem-volts.conf
	(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/doc/APRS-Telemetry-Toolkit.pdf \
		$z/Makefile.win $z/Makefile.linux $z/Makefile.macosx $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/* )
	unix2dos Makefile
	unix2dos Makefile.linux
	unix2dos Makefile.macosx
	unix2dos telemetry-toolkit/telem-balloon.conf
	unix2dos telemetry-toolkit/telem-balloon.pl
	dos2unix telemetry-toolkit/telem-bits.pl
	unix2dos telemetry-toolkit/telem-data.pl
	unix2dos telemetry-toolkit/telem-data91.pl
	unix2dos telemetry-toolkit/telem-eqns.pl
	unix2dos telemetry-toolkit/telem-m0xer-3.txt
	unix2dos telemetry-toolkit/telem-parm.pl
	unix2dos telemetry-toolkit/telem-unit.pl
	unix2dos telemetry-toolkit/telem-volts.py
	unix2dos telemetry-toolkit/telem-volts.conf


# Reminders if pdf files are not up to date.




.PHONY: backup
backup :
	mkdir /cygdrive/e/backup-cygwin-home/`date +"%Y-%m-%d"`
	cp -r . /cygdrive/e/backup-cygwin-home/`date +"%Y-%m-%d"`


#
# The following is updated by "make depend"
#
# DO NOT DELETE

