	include ./Makefile.common

name = ossim
#bindir = $(prefix)/bin
#datadir = $(prefix)
#docdir = $(datadir)/doc
version = $(OSSIM_VERSION)
OSSIM_ROOT	=	.

default: 
	(cd src; $(MAKE))
	(cd scripts; $(MAKE))
	($(MAKE) framework)

framework:
	$(RMDIR) $(TOP)/lib/ossim.framework/
	$(INSTALL) -d lib/ossim.framework \
		lib/ossim.framework/Versions \
		lib/ossim.framework/Versions/A \
		lib/ossim.framework/Versions/A/Headers 
	$(INSTALL) include/ossim/ossimConfig.h lib/ossim.framework/Versions/A/Headers
	$(INSTALL) lib/ossim/libossim.$(SO_REALNAME_EXT) lib/ossim.framework/Versions/A/libossim.dylib
	(cd src/ossim; $(MAKE) framework)
	(cd lib/ossim.framework/Versions; \
	  $(RM_NOPROMPT) current ; \
	  ln -s A current; \
	  cd ..; \
	  $(RM_NOPROMPT) ossim; \
	  $(RM_NOPROMPT) Headers; \
	  ln -s Versions/current/libossim.dylib ossim ; \
	  ln -s Versions/current/Headers Headers; )

install: 
	($(RM) -f $(TOP)/ossim-uninstall)
	($(INSTALL) -d \
		$(INST_BIN) \
		$(INST_SHARE)\
		$(INST_DOCS) \
		$(INST_TEMPLATES) \
		/Library/frameworks/ossim.framework \
		/Library/frameworks/ossim.framework/Versions )
	($(INSTALL) COPYING.GPL COPYING.LGPL README.txt $(INST_DOCS))
	(cd src; $(MAKE) install)
	($(RMDIR) /Library/frameworks/ossim.framework/Versions/${OSSIM_VERSION})
	($(RM_NOPROMPT) /Library/frameworks/ossim.framework/Headers)
	($(RM_NOPROMPT) /Library/frameworks/ossim.framework/ossim)
	(cd lib/ossim.framework/Versions; cp -r ${OSSIM_VERSION} /Library/Frameworks/ossim.framework/Versions; \
	 cd /Library/Frameworks/ossim.framework/Versions; \
	  $(RM_NOPROMPT) current ; \
	  ln -s ${OSSIM_VERSION} current; \
	  cd ..; \
	  $(RM_NOPROMPT) ossim; \
	  $(RM_NOPROMPT) Headers; \
	  ln -s Versions/current/libossim.dylib ossim ; \
	  ln -s Versions/current/Headers Headers; )
	( for bin in bin/* ; do \
		if test -f $$bin -a -x $$bin ; then \
			echo installing $$bin ... ; \
			$(INSTALL_PROGRAM) $$bin $(INST_BIN) ;\
			(echo "$(RM) -f $(prefix)/$$bin"  >> $(TOP)/ossim-uninstall) ; \
		fi ; \
	  done)
	( for t in etc/templates/* ; do \
		if test x$$t != "etc/templates/CVS" ; then \
			echo installing $$t ... ; \
			install $$t $(INST_TEMPLATES) ; \
		fi ; \
	  done)
	($(MAKE) ossim-config-inst)
	($(INSTALL_PROGRAM) src/apps/ossim-config-inst $(INST_BIN)/ossim-config)
	(echo "$(RM) -rf $(prefix)/include/ossim"  >> $(TOP)/ossim-uninstall ; \
	echo "$(RM) -rf $(prefix)/etc/ossim"  >> $(TOP)/ossim-uninstall ; \
	echo "$(RM) -rf $(prefix)/share/ossim*"  >> $(TOP)/ossim-uninstall ; \
	echo "$(RM) -rf $(prefix)/share/doc/ossim*"  >> $(TOP)/ossim-uninstall ; \
	echo "$(RM) -f $(INST_BIN)/ossim-config"  >> $(TOP)/ossim-uninstall ; \
	echo "$(RM) -f $(INST_BIN)/ossim-uninstall"  >> $(TOP)/ossim-uninstall ; \
	$(INSTALL_PROGRAM) $(TOP)/ossim-uninstall $(INST_BIN) )

shared:
	(cd src; $(MAKE) shared) ;

ossim-config-inst:
	@rm -f src/apps/ossim-config-inst
	@echo '#!/bin/sh' > src/apps/ossim-config-inst
	@echo 'OSSIM_LIBS="$(OSSIM_CONFIG_APP_LINK_INST)"' >> src/apps/ossim-config-inst
	@echo 'OSSIM_CFLAGS="$(CFLAGS) $(BASE_DEFINES)"' >> src/apps/ossim-config-inst
	@echo 'OSSIM_CPPFLAGS="$(CXXFLAGS) $(BASE_DEFINES)"' >> src/apps/ossim-config-inst
	@echo 'OSSIM_PREFIX="$(prefix)"' >> src/apps/ossim-config-inst
	@echo 'OSSIM_VERSION="$(OSSIM_VERSION)"' >> src/apps/ossim-config-inst
	@echo 'OSSIM_INCLUDES="$(BASE_INCLUDES_INST)"' >> src/apps/ossim-config-inst
	@cat src/apps/ossim-config.in >> src/apps/ossim-config-inst
	@chmod a+x src/apps/ossim-config-inst

clean:	
	if test "${RM}" != "" ; then \
		for d in `find . -name "*.d"` ; do \
			if ! test -d $$d ; then \
				${RM} -f $$d ; \
			fi ; \
		done ; \
	fi ;
	(cd src; $(MAKE) clean) ;
	(cd lib; $(RMDIR) ossim.framework; $(RM_CMD) *ossim* ; )
	(for d in bin/* ; do \
	    if test -f $$d; then \
		$(RM_CMD)  $$d ; \
	    fi ; \
	done ; \
	) ; 

depends: clean_depends
	(cd src; $(MAKE) depends) ;

clean_depends:
	(cd src; $(MAKE) clean_depends) ;

configure:	configure.in aclocal.m4
	aclocal
	autoconf

all:	default

RPMTOP=${TOP}/../rpmbuild
CYGWINDIST=${TOP}/../cygwin_ossim
DISTTOP=${TOP}/..

gzip-dist:
	cd ${DISTTOP} ; \
	${RM} -rf ${DISTTOP}/${OSSIM_ARCHIVE} ; \
	${RM} -rf ${DISTTOP}/${OSSIM_ARCHIVE_TGZ} ; \
	cp -r ossim ${OSSIM_ARCHIVE} ; \
	${RM} -f ${OSSIM_ARCHIVE}/lib/* ; \
	${RM} -f ${OSSIM_ARCHIVE}/bin/* ; \
	${RM} -rf `find ${DISTTOP}/${OSSIM_ARCHIVE} -name "*.o" -o -name "CVS" -o -name "cvs"` ; \
	cd ${DISTTOP} ; \
	tar cvfz $(OSSIM_ARCHIVE_TGZ) ${OSSIM_ARCHIVE} ;

cygwin-dist: gzip-dist
	@echo "*** Building cygwin-dist ***"
	${RM} -rf ${CYGWINDIST} ; \
	${INSTALL} -d ${CYGWINDIST} ; \
	${INSTALL} -d ${CYGWINDIST}/ossim_install ; \
	cp -f ${DISTTOP}/${OSSIM_ARCHIVE_TGZ} ${CYGWINDIST} ; \
	${RM} -f ${DISTTOP}/${OSSIM_ARCHIVE} ; \
	${RM} -rf ${DISTTOP}/${OSSIM_ARCHIVE}/ossim_install/* ; \
	cd ${CYGWINDIST} ; \
	tar -xvzf $(OSSIM_ARCHIVE_TGZ) ; \
	cd ${CYGWINDIST}/${OSSIM_ARCHIVE} ; \
	./configure --prefix=${CYGWINDIST}/ossim_install --enable-optimization --disable-debug --with-gdalconf="--with-hdf4=/usr/local"; \
	make; make install; \
	if test "${SED}" != "" ; then \
		${SED}  -e s@$prefix=.*@prefix=/usr/local@g ${CYGWINDIST}/${OSSIM_ARCHIVE}/src/apps/ossim-config > ${INSTALL_BIN}/ossim-config; \
	fi ; \
	cd ${CYGWINDIST}/ossim_install ; \
	tar cvfz ossim-cygwin-${OSSIM_VERSION}.tgz include bin share etc lib;

rpm: gzip-dist
	@echo "*** Building RPMs ***"
	${INSTALL} -d  $(RPMTOP) \
			$(RPMTOP)/SOURCES \
			$(RPMTOP)/SPECS \
			$(RPMTOP)/BUILD \
			$(RPMTOP)/RPMS \
			$(RPMTOP)/SRPMS ;
	cp -f ${DISTTOP}/$(OSSIM_ARCHIVE_TGZ) $(RPMTOP)/SOURCES
	cp -f ${TOP}/ossim.spec $(RPMTOP)/SPECS
	rpmbuild -ba --define "_topdir ${RPMTOP}" $(RPMTOP)/SPECS/ossim.spec
