# COPYRIGHT
#
# This file is part of Mustajuuri.
#
# Author: Tommi Ilmonen, 1999-2001.
#
# Mustajuuri is licensed under GNU Lesser General Public License
# (LGPL). The LGPL text can be founs in file "LGPL" that is
# distributed with this source package. The Mustajuuri way to
# interpret LGPL can be found in file "MUSTAJUURI_LICENSE" that is
# distributed with the mustajuuri source code.  
#
#
# Load compile time parameters
#

# Guess a path if it is undefined
ifeq ($(MUSTAJUURI_PATH),)
	MUSTAJUURI_PATH=$(shell pwd)/..
endif

include $(MUSTAJUURI_PATH)/src/Makeconf

DATE=$(shell date '+%d-%m-%Y')

#
# Local variables
#

# Directories that contain headers that need to be available to all
MAINDIRS      = divabase \
		divaio \
		qawl \
		mjcore \
		ac \
		mjdsp \
		mjwidgets \
		mj \
		mjmodules \
		mjeditor \
		mjutils \
		stk

# DIRS: Libraries and applications in compilation order
DIRS	      =	$(MAINDIRS) \
		plugins \
		ladspaexport \
		utils

default: all
		@echo 
		@echo "  Now you hopefully have a nice Mustajuuri "
		@echo "  in ./mj/$(PLATFORM)/dmain"
		@echo "  This is also linked to ../bin/$(PLATFORM)/mustajuuri"
		@echo 

all \
dbin \
dmain \
sbin \
smain \
dlib \
slib \
dep \
dependoff \
dependon:;	@for DIR in $(DIRS); \
		 do \
		   $(MAKE) MUSTAJUURI_PATH=$(MUSTAJUURI_PATH) -C $$DIR $@; \
		 done

don: dependon

nothing:
		@echo "Nothing to be done for nothing"

doff: dependoff

clean \
cleaner \
cleanlibs \
cleanest:;	@for DIR in $(DIRS); \
		 do \
		   $(MAKE) MUSTAJUURI_PATH=$(MUSTAJUURI_PATH) -C $$DIR $@; \
		 done
		@echo "Removing object files and core ..."
		@-rm -f *.o core core*
		@echo "Removing emacs backup files ..."
		@-rm -f *~ #*#

yetcleaner: ;
		rm -rf $(MUSTAJUURI_PATH)/src/doc/html/rtf 
		rm -rf $(MUSTAJUURI_PATH)/bin/*
		find $(MUSTAJUURI_PATH)/src/doc/html/ -name "doxygen-*" -exec rm -rf {} \; ;
		find $(MUSTAJUURI_PATH)/* \( -name "foo" -o -name "foo2" -o -name "*.so" -o -name "*.[ao]" -o -name "[sdk]main" -o -name "Depends-*" -o -name "*~"  -o -name "*.wav" -o -name "core" -o -name ".nfs*" -o -name "*_moc.C" -o -name "config.log" -o -name "config.cache"  -o -name "*.ii"  -o -name "ii_files" -o -name "*-previewwav" \) -exec rm -rf {} \; ;

remcr: ;
		find . \( -name "*.[hHcC]" -o -name "*.tex" -o -name "*.html" -o -name "*.css" -o -name "*.supp"  -o -name "*.sh"  -o -name "*.po" -o -name "doxygen-*" -o -name "*.vbap" -o -name "*.txt" \) -exec remcr {} \; ;

dirs:;		@echo "Creating global include and library directories ..."
		-mkdir ../include/$(PLATFORM)
		-mkdir ../lib
		-mkdir ../lib/$(PLATFORM)
		@echo "Creating local library directories ..."
		@for DIR in $(DIRS); \
		 do \
		   $(MAKE) MUSTAJUURI_PATH=$(MUSTAJUURI_PATH) -C $$DIR $@; \
		 done

# Generate Mustajuuri core API documentation
doc-mjcore:;	echo "Removing old html files ..."; \
		rm -rf $(MUSTAJUURI_PATH)/src/doc/html/doxygen-mjcore; \
		echo "Running doxygen ..."; \
		$(DOXYGEN) doc/doxygen-mjcore

# Generate Mustajuuri core API documentation
doc-mjdsp:;	echo "Removing old html files ..."; \
		rm -rf $(MUSTAJUURI_PATH)/src/doc/html/doxygen-mjdsp; \
		echo "Running doxygen ..."; \
		$(DOXYGEN) doc/doxygen-mjdsp

# Generate Mustajuuri code documentation
doc-mj: doc-mjcore doc-mjdsp
		echo "Removing old html files ..."; \
		rm -rf $(MUSTAJUURI_PATH)/src/doc/html/doxygen-mj; \
		echo "Running doxygen ..."; \
		$(DOXYGEN) doc/doxygen-mj

# Generate DIVA code documentation
doc-di:;		echo "Removing old html files ..."; \
		rm -rf $(MUSTAJUURI_PATH)/src/doc/html/doxygen-di; \
		echo "Running doxygen ..."; \
		$(DOXYGEN) doc/doxygen-di

doc-ac:;		echo "Removing old html files ..."; \
		rm -rf $(MUSTAJUURI_PATH)/src/doc/html/doxygen-ac; \
		echo "Running doxygen ..."; \
		$(DOXYGEN) doc/doxygen-ac

doc-qawl:;	echo "Removing old html files ..."; \
		rm -rf $(MUSTAJUURI_PATH)/src/doc/html/doxygen-qawl; \
		echo "Running doxygen ..."; \
		$(DOXYGEN) doc/doxygen-qawl

doc-remove:
		rm -rf $(MUSTAJUURI_PATH)/src/doc/html/doxygen-mj \
		$(MUSTAJUURI_PATH)/src/doc/html/doxygen-di

faqs:
		cd doc/; zsh FAQ-create.sh

doc-all: doc-mj doc-di doc-ac doc-qawl faqs

../include:
		mkdir ../include

../include/$(PLATFORM):
		mkdir ../include/$(PLATFORM)

incdirs: ../include ../include/$(PLATFORM)


links-os: incdirs
		@echo "Creating include links ..."
		rm -rf ../include/$(PLATFORM)/*; \
		for DIR in $(MAINDIRS); \
		 do \
		  echo $$DIR; \
		  cd ../include/$(PLATFORM); \
		  ln -s -f ../../src/$$DIR/$(PLATFORM)/*.h .; \
		  cd ..; \
		 done

links: incdirs
		@echo "Creating include links ..."
		 cd ../include; \
		  rm *.h *tmpl.C; \
		ls -al; \
		ln -s -f ../src/plugins/*/*.h .; \
		for DIR in $(MAINDIRS); \
		 do \
		  echo $$DIR; \
		  ln -s -f ../src/$$DIR/*.h .; \
		  ln -s -f ../src/$$DIR/*tmpl.C .; \
		  cd ../include/$(PLATFORM); \
		  ln -s -f ../../src/$$DIR/$(PLATFORM)/*.h .; \
		  cd ..; \
		 done

unlinks-os:
		@echo "Turning library links to files"
		./scripts/dirlinks2files.sh ../lib/$(PLATFORM)
		./scripts/dirlinks2files.sh ../plugins/$(PLATFORM)
		@echo "Turning files to proper links"
		./scripts/dirfiles2links.sh ../plugins/$(PLATFORM) ../../lib/$(PLATFORM)

install:
		@echo "Installing Mustajuuri and all plugins under $(INSTALLDIR)/ ..."
		$(INSTALL) -d $(INSTALLDIR)/lib/mustajuuri/etc
		$(INSTALL) -d $(INSTALLDIR)/lib/mustajuuri/plugins
		$(INSTALL) -d $(INSTALLDIR)/lib/mustajuuri/etc
		$(INSTALL) -d $(INSTALLDIR)/lib/mustajuuri/icons
		$(INSTALL) -d $(INSTALLDIR)/bin
		$(INSTALL) -d $(INSTALLDIR)/lib
		$(INSTALL) -d $(INSTALLDIR)/lib/ladspa
		$(INSTALL) ../bin/$(PLATFORM)/* $(INSTALLDIR)/bin/
		$(INSTALL) ../lib/$(PLATFORM)/*.so $(INSTALLDIR)/lib/
		$(INSTALL) ../plugins/$(PLATFORM)/*.so $(INSTALLDIR)/lib/mustajuuri/plugins/
		$(INSTALL) ../etc/*.qm $(INSTALLDIR)/lib/mustajuuri/etc/
		$(INSTALL) ../icons/*.xpm $(INSTALLDIR)/lib/mustajuuri/icons/
		./scripts/dirfiles2links.sh $(INSTALLDIR)/lib/mustajuuri/plugins ../../../lib/
		$(INSTALL) ladspaexport/$(PLATFORM)/libmjladspaexport.so $(INSTALLDIR)/lib/ladspa/

#		cd utils/givecap; make install

MJ_INC_DIR = $(INSTALLDIR)/lib/mustajuuri/include
MJ_CONFS_DIR = $(INSTALLDIR)/lib/mustajuuri/src

dev-install:
		$(INSTALL) -d $(MJ_INC_DIR)
		$(INSTALL) -d $(MJ_CONFS_DIR)/$(PLATFORM)
		$(INSTALL) ../include/*.h  $(MJ_INC_DIR)
		$(INSTALL) ../include/$(PLATFORM)/*_*.h $(MJ_INC_DIR)
		$(INSTALL) Make* $(MJ_CONFS_DIR)/
		$(INSTALL) $(PLATFORM)/* $(MJ_CONFS_DIR)/$(PLATFORM)

doc-install: doc-all
		$(INSTALL) -d $(INSTALLDIR)/lib/mustajuuri/doc/html
		cp -rf doc/html/doxygen-* $(INSTALLDIR)/lib/mustajuuri/doc/html

uninstall:
		./scripts/dircleanmatches.sh ../bin/$(PLATFORM)/ $(INSTALLDIR)/bin
		./scripts/dircleanmatches.sh ../lib/$(PLATFORM)/ $(INSTALLDIR)/lib
		rm -rf $(INSTALLDIR)/mustajuuri


# Put the tar ball to the web.
# We assume the target "tgz" has been just made
wwwpub:;
		mv $(MUSTAJUURI_PATH)/../mustajuuri-$(DATE).tgz \
		$(HOME)/public_html/mustajuuri/download/src/;
		cd $(HOME)/public_html/mustajuuri/download/src/; \
		rm mustajuuri-latest.tgz; \
		ln -sf mustajuuri-$(DATE).tgz mustajuuri-latest.tgz

MJWWW := $(HOME)/public_html/mustajuuri/
MJCODEWWW := $(MJWWW)/doc/code

DICODEWWW := $(HOME)/public_html/diva/doc/code


# Copy part of the Mustajuuri codumentation to web pages.
wwwdocmjsmall:;
		cp -r plugins/example0/*.[hC]  $(MJCODEWWW)/example0/ 
		cp -r plugins/example0/Makefile  $(MJCODEWWW)/example0/ 
		cd plugins/example0/; rm -rf example-html; doxygen doxygen-ex0
		cp -r plugins/example0/example-html $(MJCODEWWW)/example0/
		cp -r plugins/example1/*.[hC]  $(MJCODEWWW)/example1/ 
		cp plugins/example1/mj_example1_fi.po $(MJCODEWWW)/example1/ 
		cp plugins/example1/Makefile $(MJCODEWWW)/example1/ 
		cp -r plugins/example2/*.[hC]  $(MJCODEWWW)/example2/ 
		cd doc; cp FAQ-devel/FAQ-devel.html  FAQ-devel/FAQ-devel.css \
                  FAQ-user/FAQ-user.html FAQ-user/FAQ-user.css \
                  $(MJWWW)/doc/; \
		cp zshrc-sample $(MJWWW)/doc/
		cp ../INSTALL.html $(MJWWW)/install_text.php

# Copy all of the Mustajuuri documentation to the web pages.
wwwdocmj: wwwdocmjsmall
		rm -rf $(MJCODEWWW)/doxygen-mj;
		cp -r $(MUSTAJUURI_PATH)/src/doc/html/doxygen-mj \
			$(MJCODEWWW);
		rm -rf $(MJCODEWWW)/doxygen-mjcore;
		cp -r $(MUSTAJUURI_PATH)/src/doc/html/doxygen-mjcore \
			$(MJCODEWWW);
		rm -rf $(MJCODEWWW)/doxygen-mjdsp;
		cp -r $(MUSTAJUURI_PATH)/src/doc/html/doxygen-mjdsp \
			$(MJCODEWWW);

# Copy DIVA documentation to the web
wwwdocdi:;
		rm -rf $(DICODEWWW);
		mkdir  $(DICODEWWW);
		cp -r $(MUSTAJUURI_PATH)/src/doc/html/doxygenn-di \
			$(DICODEWWW);

# Create a gzipped tar package (for distribution/backup)
tgz:;
		@echo "Creating tgz package ..."
		cd ../../; ln -sf $(MUSTAJUURI_PATH) mustajuuri-$(DATE); \
		tar cvfz mustajuuri-$(DATE).tgz mustajuuri-$(DATE)/* ;\
		rm mustajuuri-$(DATE); \
		ls -l mustajuuri-$(DATE).tgz
