RESOURCE_DIR=src/main/resources
NATIVE_DIR=$(RESOURCE_DIR)/native
RSYNC=rsync -av -e ssh --exclude=".svn"

WIN_SERVER=192.168.131.10
WIN_WORK=work/eclipse/xerial/sqlite-jdbc

MAC_SERVER=192.168.131.9
MAC_WORK=work/eclipse/workspace/xerial/sqlite-jdbc

LINUX_SERVER=sprite
LINUX_WORK=work/eclipse/xerial/sqlite-jdbc

AMD_SERVER=utgb01
AMD_WORK=work/eclipse/xerial/sqlite-jdbc

BUILD_CMD = svn update && make clean-native && make native
TEST_CMD = mvn test

all: compile test

include VERSION
sqlite-version := sqlite-$(sqlite_version)

LIB_DIR=target/dll/$(sqlite-version)
DLL_DIR=$(LIB_DIR)/native

DLL_WIN=$(DLL_DIR)/Windows/x86/sqlitejdbc.dll
DLL_MAC=$(DLL_DIR)/Mac/i386/libsqlitejdbc.jnilib
DLL_LINUX=$(DLL_DIR)/Linux/i386/libsqlitejdbc.so
DLL_AMD64=$(DLL_DIR)/Linux/amd64/libsqlitejdbc.so
LIB_PUREJAVA=$(LIB_DIR)/purejava/SQLite.class

$(LIB_PUREJAVA):
	ssh $(MAC_SERVER) "source .zprofile && cd $(MAC_WORK)/sqlitejdbc && make -f Makefile.nested"
	mkdir -p $(LIB_DIR)/purejava
	scp $(MAC_SERVER):./$(MAC_WORK)/sqlitejdbc/build/org/sqlite/SQLite.class $(LIB_DIR)/purejava/


$(DLL_WIN): 
	ssh $(WIN_SERVER) "source .zprofile && cd $(WIN_WORK) && $(BUILD_CMD)"
	mkdir -p $(DLL_DIR)/Windows
	$(RSYNC)  $(WIN_SERVER):./$(WIN_WORK)/$(DLL_DIR)/Windows/ $(DLL_DIR)/Windows/

$(DLL_MAC): 
	ssh $(MAC_SERVER) "source .zprofile && cd $(MAC_WORK) && $(BUILD_CMD)"
	mkdir -p $(DLL_DIR)/Mac
	$(RSYNC)  $(MAC_SERVER):./$(MAC_WORK)/$(DLL_DIR)/Mac/ $(DLL_DIR)/Mac/

$(DLL_LINUX):
	ssh $(LINUX_SERVER) "source .zprofile && source /etc/profile.d/java.sh && cd $(LINUX_WORK) && $(BUILD_CMD)"
	mkdir -p $(DLL_DIR)/Linux/i386
	$(RSYNC)  $(LINUX_SERVER):./$(LINUX_WORK)/$(DLL_DIR)/Linux/i386/ $(DLL_DIR)/Linux/i386/

$(DLL_AMD64):
	ssh $(AMD_SERVER) "source .zprofile && cd $(AMD_WORK) && $(BUILD_CMD)"
	mkdir -p $(DLL_DIR)/Linux/amd64
	$(RSYNC)  $(AMD_SERVER):./$(AMD_WORK)/$(DLL_DIR)/Linux/amd64/ $(DLL_DIR)/Linux/amd64/

$(DLL_DIR):
	mkdir -p $@

compile: $(DLL_DIR) $(LIB_PUREJAVA) $(DLL_WIN) $(DLL_MAC) $(DLL_LINUX) $(DLL_AMD64)
	cp $(LIB_PUREJAVA) $(RESOURCE_DIR)/org/sqlite
	cp -r $(DLL_DIR)/ $(RESOURCE_DIR)/native/

test:
	ssh $(WIN_SERVER) "source .zprofile && cd $(WIN_WORK) && $(TEST_CMD)"
	ssh $(MAC_SERVER) "source .zprofile && cd $(MAC_WORK) && $(TEST_CMD)"
	ssh $(LINUX_SERVER) "source .zprofile && source /etc/profile.d/java.sh && cd $(LINUX_WORK) && $(TEST_CMD)"
	ssh $(AMD_SERVER) "source .zprofile && cd $(AMD_WORK) && $(TEST_CMD)"
	ssh $(MAC_SERVER) "source .zprofile && cd $(MAC_WORK) && $(TEST_CMD) -DargLine='-Dsqlite.purejava=true'"

	
release:
	mvn release:prepare
	mvn release:perform

release-xerial:
	mvn release:perform -DconnectionUrl=scm:svn:https://www.xerial.org/svn/project/XerialJ/tags/sqlite-jdbc/sqlite-jdbc-$(sqlite_version)

release-sourceforge:
	mvn release:perform -Darguments="-P sourceforge" -DconnectionUrl=scm:svn:https://www.xerial.org/svn/project/XerialJ/tags/sqlite-jdbc/sqlite-jdbc-$(sqlite_version)
	