.SUFFIXES: .java .class

CLASSPATH=classes:./*:/usr/share/java/*
CLASSFILES=\
	Altos.class \
	AltosChannelMenu.class \
	AltosConfigFC.class \
	AltosConfigFCUI.class \
	AltosConvert.class \
	AltosCRCException.class \
	AltosCSV.class \
	AltosCSVUI.class \
	AltosDebug.class \
	AltosEepromDownload.class \
	AltosEepromMonitor.class \
	AltosEepromReader.class \
	AltosEepromRecord.class \
	AltosFile.class \
	AltosFlash.class \
	AltosFlashUI.class \
	AltosFlightInfoTableModel.class \
	AltosFlightStatusTableModel.class \
	AltosGPS.class \
	AltosGreatCircle.class \
	AltosHexfile.class \
	AltosLine.class \
	AltosInfoTable.class \
	AltosLog.class \
	AltosLogfileChooser.class \
	AltosParse.class \
	AltosPreferences.class \
	AltosReader.class \
	AltosRecord.class \
	AltosSerialMonitor.class \
	AltosSerial.class \
	AltosState.class \
	AltosStatusTable.class \
	AltosTelemetry.class \
	AltosTelemetryReader.class \
	AltosUI.class \
	AltosDevice.class \
	AltosDeviceDialog.class \
	AltosRomconfig.class \
	AltosRomconfigUI.class \
	AltosVoice.class

JAVA_ICONS=\
	../icon/altus-metrum-16.png \
	../icon/altus-metrum-32.png \
	../icon/altus-metrum-48.png \
	../icon/altus-metrum-64.png \
	../icon/altus-metrum-128.png \
	../icon/altus-metrum-256.png

WINDOWS_ICON=../icon/altus-metrum.ico

# where altosui.jar gets installed
ALTOSLIB=/usr/share/java

# where freetts.jar is to be found
FREETTSLIB=/usr/share/java

# all of the freetts files
FREETTSJAR= \
	$(FREETTSLIB)/cmudict04.jar \
	$(FREETTSLIB)/cmulex.jar \
	$(FREETTSLIB)/cmu_time_awb.jar \
	$(FREETTSLIB)/cmutimelex.jar \
	$(FREETTSLIB)/cmu_us_kal.jar \
	$(FREETTSLIB)/en_us.jar \
	$(FREETTSLIB)/freetts.jar

# The current hex files
HEXLIB=../src
HEXFILES = \
	$(HEXLIB)/telemetrum-v1.0.ihx \
	$(HEXLIB)/teledongle-v0.2.ihx

JAVAFLAGS=-Xlint:unchecked -Xlint:deprecation

ALTOSUIJAR = altosui.jar
FATJAR = fat/altosui.jar

OS:=$(shell uname)

LINUX_APP=altosui

DARWIN_ZIP=Altos-Mac.zip

WINDOWS_EXE=Altos-Windows.exe

LINUX_TGZ=Altos-Linux.tgz

all: altosui.jar $(LINUX_APP)
fat: altosui.jar $(LINUX_APP) $(DARWIN_ZIP) $(WINDOWS_EXE) $(LINUX_TGZ)

$(CLASSFILES):

.java.class:
	javac -encoding UTF8 -classpath "$(CLASSPATH)" $(JAVAFLAGS) $*.java

altosui.jar: classes/images classes/altosui classes/libaltosJNI $(CLASSFILES) Manifest.txt
	cd ./classes && jar cfm ../$@ altosui/Manifest.txt images/* altosui/*.class libaltosJNI/*.class

Manifest.txt: Makefile $(CLASSFILES)
	echo 'Main-Class: altosui.AltosUI' > $@
	echo "Class-Path: $(FREETTSLIB)/freetts.jar" >> $@

classes/altosui:
	mkdir -p classes
	ln -sf .. classes/altosui

classes/libaltosJNI:
	mkdir -p classes
	ln -sf ../libaltos/libaltosJNI classes/libaltosJNI

classes/images:
	mkdir -p classes/images
	ln -sf ../$(JAVA_ICONS) classes/images

altosui:
	echo "#!/bin/sh" > $@
	echo "exec java -Djava.library.path=/usr/lib/altos -jar /usr/share/java/altosui.jar" >> $@
	chmod +x ./altosui

fat/altosui:
	echo "#!/bin/sh" > $@
	echo 'ME=`which "$0"`' >> $@
	echo 'DIR=`dirname "$ME"`' >> $@
	echo 'exec java -Djava.library.path="$$DIR" -jar "$$DIR"/altosui.jar' >> $@
	chmod +x $@

fat/altosui.jar: $(CLASSFILES) $(JAVA_ICONS) fat/classes/Manifest.txt
	mkdir -p fat/classes
	test -L fat/classes/altosui || ln -sf ../.. fat/classes/altosui
	mkdir -p fat/classes/images
	cp $(JAVA_ICONS) fat/classes/images
	test -L fat/classes/libaltosJNI || ln -sf ../../libaltos/libaltosJNI fat/classes/libaltosJNI
	cd ./fat/classes && jar cfm ../../$@ Manifest.txt images/* altosui/*.class libaltosJNI/*.class

fat/classes/Manifest.txt: $(CLASSFILES) Makefile
	mkdir -p fat/classes
	echo 'Main-Class: altosui.AltosUI' > $@
	echo "Class-Path: freetts.jar" >> $@

install: altosui.jar altosui
	install -m 0644 altosui.jar $(DESTDIR)/usr/share/java/altosui.jar
	install -m 0644 altosui.1 $(DESTDIR)/usr/share/man/man1/altosui.1
	install altosui $(DESTDIR)/usr/bin/altosui

clean:
	rm -f *.class altosui.jar
	rm -f AltosUI.app/Contents/Resources/Java/*
	rm -rf classes
	rm -rf windows linux

distclean:	clean
	rm -f $(DARWIN_ZIP) $(WINDOWS_EXE) $(LINUX_TGZ)
	rm -rf darwin fat

FAT_FILES=$(FATJAR) $(FREETTSJAR) $(HEXFILES)

LINUX_FILES=$(FAT_FILES) libaltos/libaltos.so fat/altosui
$(LINUX_TGZ): $(LINUX_FILES)
	rm -f $@
	mkdir -p linux/AltOS
	rm -f linux/AltOS/*
	cp $(LINUX_FILES) linux/AltOS
	cd linux && tar czf ../$@ AltOS

DARWIN_RESOURCES=$(FATJAR) $(FREETTSJAR) libaltos/libaltos.dylib
DARWIN_EXTRA=$(HEXFILES)
DARWIN_FILES=$(DARWIN_RESOURCES) $(DARWIN_EXTRA)

$(DARWIN_ZIP): $(DARWIN_FILES)
	rm -f $@
	cp -a AltosUI.app darwin/
	mkdir -p darwin/AltosUI.app/Contents/Resources/Java
	cp $(DARWIN_RESOURCES) darwin/AltosUI.app/Contents/Resources/Java
	mkdir -p darwin/AltOS
	cp $(DARWIN_EXTRA) darwin/AltOS
	cd darwin && zip -r ../$@ AltosUI.app AltOS

WINDOWS_FILES = $(FAT_FILES) libaltos/altos.dll ../altusmetrum.inf $(WINDOWS_ICON)

$(WINDOWS_EXE): $(WINDOWS_FILES) altos-windows.nsi
	rm -f $@
	mkdir -p windows/AltOS
	rm -f windows/AltOS/*
	cp $(WINDOWS_FILES) windows/AltOS
	makensis altos-windows.nsi
