EXECUTABLE=javadjvu.jar

# All files that are not automatically generated (these are used to create the archive)
PROJECT_FILES=$(shell cat dependencies/project_files)
# All files that are not automatically generated excluding symlinks (used to check dependencies)
PROJECT_DEPFILES=$(shell cat dependencies/project_depfiles)
# All files in the package directory excluding symlinks (used for dependency checking for the package)
PACKAGE_DEPFILES=$(shell cat dependencies/package_depfiles)
# All files in the installer directory excluding symlinks (used for dependency checking for the installer)
INSTALLER_DEPFILES=$(shell cat dependencies/installer_depfiles)
# All the java source files:
SOURCES=$(shell cat dependencies/source_files)

# Name of the package and the name of the directory containing the installation files:
PACKAGE_NAME=javadjvu
PACKAGE_VERSION=0.2
PACKAGE_ARCH=arm
PACKAGE=$(PACKAGE_NAME)_$(PACKAGE_VERSION)_$(PACKAGE_ARCH).ipk
SHORTNAME=$(PACKAGE_NAME)-$(PACKAGE_VERSION)
ARCHIVE=$(SHORTNAME).tar.bz2
INSTALLER=$(SHORTNAME).zip

INSTALL=/usr/bin/install
INSTALL_STRIP=$(INSTALL) -s
IPKGBUILD=ipkg-build -o root -g root
TAR=tar
ZIP=zip

prefix=/home
exec_prefix=${prefix}
bindir=${exec_prefix}/intent

all: $(INSTALLER)

# Make the executable
executable: $(EXECUTABLE)
$(EXECUTABLE): $(SOURCES)
	cd src; ant
	cd src; ant -f jar.xml

# Make an ipkg of the project
package: $(PACKAGE)
$(PACKAGE): $(EXECUTABLE) $(PACKAGE_DEPFILES) 
	-rm -Rf $(SHORTNAME)
	svn export package $(SHORTNAME)
	$(INSTALL) $(EXECUTABLE) $(SHORTNAME)/$(bindir)
	$(IPKGBUILD) $(SHORTNAME)
	rm -Rf $(SHORTNAME)

# Make a tar.bz2 archive of the project files
archive: $(ARCHIVE)
$(ARCHIVE): $(PROJECT_DEPFILES)
	-rm -Rf $(SHORTNAME)
	svn export . $(SHORTNAME)
	$(TAR) -cjf $(ARCHIVE) $(SHORTNAME)
	rm -Rf $(SHORTNAME)

installer: $(INSTALLER)
$(INSTALLER): $(PACKAGE) $(ARCHIVE) $(INSTALLER_DEPFILES)
	-rm -Rf $(SHORTNAME)
	-rm $(INSTALLER)
	svn export installer $(SHORTNAME)
	$(INSTALL) $(PACKAGE) $(SHORTNAME)/install/$(PACKAGE)
	$(INSTALL) $(ARCHIVE) $(SHORTNAME)/source/$(ARCHIVE)
	$(ZIP) -r $(INSTALLER) $(SHORTNAME)
	rm -Rf $(SHORTNAME)

# Clean all the generated files
clean:
	-rm -Rf javadjvu/build
	-rm $(EXECUTABLE)
	-rm $(PACKAGE)
	-rm package/$(bindir)/$(EXECUTABLE)
	-rm $(ARCHIVE)
	-rm $(INSTALLER)
	-rm installer/install/$(PACKAGE)
	-rm installer/source/$(ARCHIVE)
	-rm -Rf $(SHORTNAME)

# Generate dependencies
# Note that the subversion repository is used to determine which files are project files
depfiles:
	-rm -Rf $(SHORTNAME)
	svn export . $(SHORTNAME)
	cd $(SHORTNAME) ; find . -printf "%P\n" > ../dependencies/subversion_files
	rm -Rf $(SHORTNAME)
	find . -empty -or -not -type d -printf "%P\n" | sort dependencies/subversion_files - | uniq -d > dependencies/project_files
	find . -type f -printf "%P\n" | sort dependencies/subversion_files - | uniq -d > dependencies/project_depfiles
	find package -type f | sort dependencies/subversion_files - | uniq -d > dependencies/package_depfiles
	find installer -type f | sort dependencies/subversion_files - | uniq -d > dependencies/installer_depfiles
	find src -type f | sort dependencies/subversion_files - | uniq -d > dependencies/source_files
