#
# Copyright (C) 1998 The Information-technology Promotion Agency, Japan(IPA), LGPL
# $Id: GNUmakefile,v 2.3 2001/04/17 12:19:15 john Exp $
#

SHELL = /bin/sh

export UNIX_TYP:=$(shell uname -s)
ifeq ($(UNIX_TYP), CYGWIN_NT-5.0)
LIBEXT:=dll
else
LIBEXT:=so
endif


#### ######################################################################
####
#### Set values of the following variables to match your system ###########

## GRASS-related variables ####

# GRASS home directory
GRASS_HOME = $(GISBASE)

# Root of the GRASS source tree
GRASS_SRC = ../../src

# GRASS compilation name ('architecture' in terminology of GRASS
# installation documentation)
GRASS_ARCH = `echo $(HEADER) | cut -d'.' -f2`

# directory in the GRASS source tree that contains GIS library header files
# if previous variables were set this should be ok
GISH = $(GRASS_SRC)/include 

# directories in the GRASS source tree that contain Vector library header files
# if previous variables were set this should be ok
VECTH1 = $(GRASS_SRC)/libes/vect32/Vlib
VECTH2 = $(GRASS_SRC)/libes/vect32/diglib

# directory that contains a libgis.a for your grass compilation 
# if previous variables were set this should be ok
#GISLIB = $(GRASS_SRC)/libes/LIB.$(ARCH)
#GISLIB = $(GRASS_SRC)/libes/$(LIBARCH)
GISLIB = /usr/local/grass5/lib

## Settings to run the test programs

# Location of GRASS programs 
#GISBASE = $(GRASS_HOME) # not required when using gmake5 to build

# GRASS data base
GISDBASE = $(GRASS_HOME)/data

# Location name in database
# by default uses test data set provided along with GRASS 
LOCATION_NAME = spearfish

# Mapset name under location. To run test programs successfully user must
# have read/write access to the specified mapset
MAPSET = $(USER)

## Java-related variables ####

# this specifies directory where to put HTML files generated by javadoc
DOCDEST = htmls

# sets flags for javadoc, comment it out if do not need this information
#JAVADOCFLAGS = -private -version -author

# location of JDK, uncomment this line and set variable to an appropriate 
# value if it is not set in your environment
JDK_HOME = /JBuilder4/jdk1.3

## OS dependent settings (default to Solaris) ####

# instructs gcc to produce shared library instead of executable module
#SO_FLAGS = -G         # Solaris 2.5.1
SO_FLAGS = -shared   # Linux 2.x

# location of OS dependent JNI headers for C programs
#JNI_H = $(JDK_HOME)/include/solaris   # Solaris 2.5.1
#JNI_H = $(JDK_HOME)/include/linux   # Linux 2.x
JNI_H = $(JDK_HOME)/include

## C-related settings ####
CC	= gcc $(DEBUG)

## other variables ####
DEST  = .
DEBUG = -g

#### This is the end of configuration section ##############################
####
#### #######################################################################

JAVAC   = javac -deprecation $(DEBUG) -d $(DEST)
JAVA    = $(JDK_HOME)/bin/java
JAVAH   = $(JDK_HOME)/bin/javah -jni
JAVADOC = $(JDK_HOME)/bin/javadoc $(JAVADOCFLAGS) 
JAR     = $(JDK_HOME)/bin/jar

FIND  = find
STRIP = strip

EXTRA_CFLAGS = -I$(JDK_HOME)/include/win32 -I$(JNI_H) -I$(GISH) \
	       -I$(VECTH1) -I$(VECTH2)

SRC_GIS = GRASS_libgis.c GRASS_CELL.c GRASS_Cellhead.c GRASS_Colors.c \
	  GRASS_FD.c GRASS_Range.c GRASS_Cellstats.c GRASS_Categories.c \
          GRASS_FILE.c
H_GIS   = $(addsuffix .h,$(basename $(SRC_GIS)))
OBJ_GIS = $(addsuffix .o,$(basename $(SRC_GIS)))

SRC_VECT= GRASS_libvect.c GRASS_Mapinfo.c GRASS_linepnts.c
H_VECT  = $(addsuffix .h,$(basename $(SRC_VECT)))
OBJ_VECT= $(addsuffix .o,$(basename $(SRC_VECT)))

SRC_ETC = jmalloc.c throw.c checknull.c
H_ETC   = etc.h
OBJ_ETC = $(addsuffix .o,$(basename $(SRC_ETC)))

CLASS = $(addsuffix .class,$(basename $(wildcard GRASS/*.java)))
SO    = libgis.$(LIBEXT) libvect.$(LIBEXT)

# Standard Targets 

# makes all libraries and executables with debug option if not unset
# does not make any of documentation
all:	$(H_GIS) $(H_VECT) $(CLASS) $(SO)

alldocs: all docs # build all with documentation

check:	check1 check2

clean:
	rm -f $(H_GIS) $(H_VECT) *.class *.o core* GRASS/*.class

distclean: clean cleandocs
	rm -f *.$(LIBEXT) *.jar 
	rm -rf OBJ.*

# these are tags common for GNU programs but I have not yet implemented them
install uninstall install-strip:
	echo Target not implemented

# Project Specific Targets

libgis.$(LIBEXT): $(OBJ_GIS) $(H_GIS) $(OBJ_ETC) $(H_ETC) $(CLASS) $(GISLIB)
	$(CC) $(SO_FLAGS) $(EXTRA_CFLAGS) $(LDFLAGS) -o $@ \
        $(OBJ_GIS) $(OBJ_ETC) -L$(GISLIB) -lgis -ldatetime -lrpclib -lz
	rm -f $(subst .$(LIBEXT),_g.$(LIBEXT),$@)
ifdef DEBUG
	cp $@ $(subst .$(LIBEXT),_g.$(LIBEXT),$@)
endif
	$(STRIP) $@

libvect.$(LIBEXT): $(OBJ_VECT) $(H_VECT) $(OBJ_ETC) $(H_ETC) $(CLASS) $(GISLIB)
	$(CC) $(SO_FLAGS) $(EXTRA_CFLAGS) $(LDFLAGS) -o $@ \
	$(OBJ_VECT) $(OBJ_ETC) -L$(GISLIB) -lvect -ldig2 -L. -lgis -ldatetime -lrpclib -lz
	rm -f $(subst .$(LIBEXT),_g.$(LIBEXT),$@)
ifdef DEBUG
	cp $@ $(subst .$(LIBEXT),_g.$(LIBEXT),$@)
endif
	$(STRIP) $@

%.o:	%.c 
	$(CC) -c $(EXTRA_CFLAGS) $(LDFLAGS) -o $@ $< 

%.class: %.java
	$(JAVAC) $<

jar:	all
	$(JAR) -c0f GRASS.jar `$(FIND) GRASS -name "*.class" -print`

GRASS_%.h:	GRASS/%.class
	$(JAVAH) $(subst /,.,$(basename $<))

docs:	
	-test -d $(DOCDEST) || mkdir $(DOCDEST)
	$(JAVADOC) -d $(DOCDEST) GRASS

_docs2:	
	-test -d $(DOCDEST) || mkdir $(DOCDEST)
	make JDK_HOME=/usr/java1.2 docs2

BOTTOM = '<font size="-1">\
	<a TARGET="_top" href="http://www.vtt.co.jp/staff/sorokin/jni">\
	GRASS-JNI Home Page</a></font>'

docs2:	
	-test -d $(DOCDEST) || mkdir $(DOCDEST)
	$(JAVADOC) -d $(DOCDEST) -windowtitle 'GRASS-JNI r.'`cat RELEASE` \
	  -bottom $(BOTTOM) GRASS/*.java

cleandocs:
	rm -fr $(DOCDEST)

LD_LIBRARY_PATH=.

check1:	all check1.class
	$(JAVA) check1 $(GISBASE) $(GISDBASE) $(LOCATION_NAME) $(MAPSET)

check2:	all check2.class
	$(JAVA) check2 $(GISBASE) $(GISDBASE) $(LOCATION_NAME) $(MAPSET)

# these are tags to be used only by developers

vers:
	uname -a
	$(JAVA) -version
	$(JAVA) -fullversion
	$(CC) --version
	$(MAKE) --version

wc:
	wc *.c *.java GRASS/*.java

DISTNAME = grass-jni
DISTSRC = $(SRC_GIS) $(SRC_VECT) $(SRC_ETC) $(H_ETC) $(wildcard check?.java)
DISTSUB = $(wildcard GRASS/*.java) 
DISTPRE = README INSTALL 
DISTAUX = COPYING CONTRIBUTORS RELEASE ChangeLog Makefile GNUmakefile TODO

dist: $(DISTSRC) $(DISTAUX) $(DISTJAV)
	echo $(DISTNAME)-`cat RELEASE` > .fname
	-rm -rf `cat .fname`

	mkdir `cat .fname`
	ln $(DISTAUX) `cat .fname`
	for f in $(DISTSRC) ; do \
	  cat Preamble $$f | \
	  perl -pe '( s/^   \*.*$$// || s/ \*S.*$$// ) && s/^\s*$$//' > \
          `cat .fname`/$$f;\
        done

	for f in $(DISTPRE) ; do \
	  cat $$f | perl -pe 's/X\.X\.X/'`cat RELEASE`'/g' > `cat .fname`/$$f;\
        done

	mkdir `cat .fname`/GRASS
	for f in $(DISTSUB); do\
	  cat Preamble $$f > `cat .fname`/$$f;\
	done

	tar -chf - `cat .fname` | gzip > `cat .fname`.src.tar.gz
	cd `cat .fname`; make jar; make CLASSPATH=./GRASS.jar:. check
	tar -chf - `cat .fname` | gzip > `cat .fname`.sol251.tar.gz
	make JAVADOCFLAGS=-public _docs2
	tar -chf - htmls | gzip > `cat .fname`.doc.tar.gz
	#-rm -rf `cat .fname`

WWWPRE   = index.html README INSTALL 
WWWFILES = check2scr01.gif ChangeLog
 
www: 
	echo www-`cat RELEASE` > .wname
	echo $(DISTNAME)-`cat RELEASE` > .fname
	-rm -rf `cat .wname`

	mkdir `cat .wname`
	ln $(WWWFILES) `cat .wname`

	for f in $(WWWPRE) ; do \
	  cat $$f | perl -pe 's/X\.X\.X/'`cat RELEASE`'/g' > `cat .wname`/$$f;\
        done

	cp -rp htmls `cat .wname`
	chmod -R a+rw `cat .wname`

	tar -chf - `cat .wname` | gzip > `cat .wname`.tar.gz

	#-rm -rf `cat .wname`

print:	.printm .prints

.printm: $(filter %akefile, $(DISTSRC) $(DISTSUB) $(DISTAUX) )
	a2ps -g --pretty-print=make $?
	touch $@

.prints: $(filter-out %akefile, $(DISTSRC) $(DISTSUB) $(DISTAUX) )
	a2ps -g $?
	touch $@

