# JEtherPix - Java implementation of EtherPix output library
#
# Copyright 2010-2017 Stefan Schuermans <stefan schuermans info>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

VERSION_MAJOR=1
VERSION_MINOR=3
VERSION_REVISION=0

JAVAC=javac
JAR=jar
KEYTOOL=keytool
JARSIGNER=jarsigner
KEYPASS=JEtherPix
JAVA=java
PKG_PATH=org/blinkenarea/JEtherPix
PKG_PATH_EX=$(PKG_PATH)/examples

CLASSES=AddrParser Config Constants Display Distri \
        Mapping MessageIf MsgType Pixel PixelParser Version
CLASSES_EX=Blink Fade Msg
CLASS_FILES=$(addprefix $(PKG_PATH)/,$(addsuffix .class,$(CLASSES)))
CLASS_FILES_EX=$(addprefix $(PKG_PATH_EX)/,$(addsuffix .class,$(CLASSES_EX)))
CONFIG_EX=$(PKG_PATH_EX)/config/single8x8.flp

CLASSPATH=.

.PHONY: all clean jar blink fade
.SUFFIXES:
.SECONDARY:

all: jar $(CLASS_FILES_EX)

clean:
	rm -f $(PKG_PATH)/Version.java
	rm -f $(CLASS_FILES)
	rm -f JEtherPix.jar
	rm -f $(CLASS_FILES_EX)

jar: JEtherPix.jar

blink: $(PKG_PATH_EX)/Blink.class
	$(JAVA) -classpath . $(subst /,.,$(basename $<)) $(CONFIG_EX)

fade: $(PKG_PATH_EX)/Fade.class
	$(JAVA) -classpath . $(subst /,.,$(basename $<)) $(CONFIG_EX)

$(CLASS_FILES_EX): $(CLASS_FILES)

$(PKG_PATH)/Version.java: Makefile
	echo "package org.blinkenarea.JEtherPix;" >$@
	echo "public class Version {" >>$@
	echo "  public static int Major = $(VERSION_MAJOR);" >>$@
	echo "  public static int Minor = $(VERSION_MINOR);" >>$@
	echo "  public static int Revision = $(VERSION_REVISION);" >>$@
	echo "}" >>$@

%.class: %.java
	$(JAVAC) -classpath $(CLASSPATH) $<

JEtherPix.keystore:
	$(KEYTOOL) -genkey -alias JEtherPix -dname CN=JEtherPix,O=blinkenarea,C=org -keypass $(KEYPASS) -keystore $@ -storepass $(KEYPASS) -validity 3652

JEtherPix.jar: JEtherPix.keystore $(CLASS_FILES)
	$(JAR) cf $@ $(CLASS_FILES)
	$(JARSIGNER) -keystore $< -storepass $(KEYPASS) $@ JEtherPix