#------------------------------------------------------------------------------
# Makefile for EARS
# Beware!  You need a filesystem that allows names longer than 14 chars.
#------------------------------------------------------------------------------
# User defined section
#------------------------------------------------------------------------------

OPT_DEBUG	= -O
#OPT_DEBUG	= -g     	# uncomment this line if you want to debug

GUI	= -DUI_NCURSES
#GUI	= -DUI_RAW       	# uncomment this if ncurses version does not work

#SOUNDDEFS = -DVOXWARE  	# uncomment if you have Linux kernel sound driver

SOUNDDEFS = -DOSS       	# uncomment if you have Open Sound System
SOUND_INCLUDE  = -I/var/lib/oss	# the path where your OSS soundcard.h lives

#SOUNDDEFS = -DSUN        	# uncomment if you have Sun sound 
#SOUND_INCLUDE = -I/usr/demo/SOUND/include
#OTHERLIBS = -L/usr/demo/SOUND/lib -laudio -lm

#SOUNDDEFS = -DAF        	# uncomment if you have AF sound (incomplete)

RECOGS	= -DR_DTW
COMP = gcc
CC = gcc

#------------------------------------------------------------------------------
# You should not need to change anything below this line.
#------------------------------------------------------------------------------
export
VERSION	  = 0.32
dirname	  = ears-$(VERSION)
includedirs =-I. $(SOUND_INCLUDE)
subdirs   = ears modules mrasta ui others
alldirs   = $(subdirs) doc util contrib
mylibs    = $(foreach dir,$(subdirs),$(dir)/lib$(dir).a)
sources   = $(foreach dir,$(subdirs),$(wildcard $(dir)/*.cc $(dir)/*.c))
sources  += train_ears.cc listen.cc
other_files = Makefile CHANGES COPYING HELP.* README* TODO *.words

ifeq ($(GUI),-DUI_NCURSES)
	guilibs = -lpanel -lncurses
endif 
libs      = $(mylibs) -lstdc++ $(guilibs) $(OTHERLIBS)

ifeq (configured,$(wildcard configured))
else
configured:
	#
	# Note:  You need the following things to compile successfully:
	# - gcc-2.7.2 and up
	# - libstdc++.a from libg++-2.7.1.4 and up
	# - libncurses.a / libpanel.a from ncurses-1.9.9e and up
	#
	@sh util/configure
	@touch configured
endif

#-------------------------------------------------------------------------------
all: configured depend allprogs

allprogs: train_ears listen

train_ears: $(mylibs) train_ears.o 
	$(COMP) $(LIBPATH) -o train_ears train_ears.o $(libs)

listen: $(mylibs) listen.o
	$(COMP) $(LIBPATH) -o listen listen.o $(libs) 

# Libraries are named so that libx.a is found in subdirectory x
%.a: 
	make -C $(@D) $(@F)

%.o: %.cc
	@make --no-print-directory -C $(@D) $(@F)

%.o: %.c
	@make --no-print-directory -C $(@D) $(@F)

train_ears.o listen.o: 
	$(COMP) -c $(includedirs) $*.cc
#------------------------------------------------------------------------------
# cleaning up
#------------------------------------------------------------------------------
newrec:                       # delete recorded files and recognizers
	rm -f $(HOME)/.ears/*.NDTW
	rm -f $(HOME)/.ears/*.DTW
	rm -rf $(HOME)/.ears/MRASTA-V
	rm -rf $(HOME)/.ears/raw

uninstall:              # remove all data files
	rm -r $(HOME)/.ears
	rm $(HOME)/.earsrc

deltrash:		# delete all these annoying files
	rm -f \#* *\# *~ *.orig *.rej core t a.out DEADJOE tmp .remind
	rm -f doc/*~ util/*~ contrib/*~ gmon.out bb.out log tt
	for d in $(subdirs); do	make -C $$d deltrash; done

clean: 			# everything except configuration
	rm -f \#* *\# *~ *.orig *.rej core t a.out DEADJOE tmp .remind
	rm -f doc/*~ util/*~ contrib/*~ gmon.out bb.out log tt
	rm -f *.o .depend listen train_ears
	rm -rf $(dirname) 
	for d in $(subdirs); do	make -C $$d clean; done

mostlyclean: clean	

distclean: 		# make it as if you had just unpacked the distribution
	rm -f \#* *\# *~ *.orig *.rej core t a.out DEADJOE tmp .remind
	rm -f doc/*~ util/*~ contrib/*~ gmon.out bb.out log tt
	rm -f *.o .depend listen train_ears configured
	rm -rf $(dirname) 
	rm -f TAGS tags 
	for d in $(subdirs); do	make -C $$d distclean; done

realclean: distclean	# delete everything that can't be build by any means

veryclean: realclean	# this is not GNU, but I'm used to it

#------------------------------------------------------------------------------
# documentation, version control
#------------------------------------------------------------------------------

info:
	@echo "Not yet implemented."

dvi: info

ci:
	@ci -u -q0.20.1 $(ALLSRCS) README* HELP.*
	for d in $(subdirs); do	make -C $$d ci; done

#------------------------------------------------------------------------------
# packing a distribution and backup
#------------------------------------------------------------------------------

dist: # not exactly elegant, but it works for now :-)
	-chmod -R 777 $(dirname)
	rm -f doc/*~ *~
	rm -rf $(dirname)
	mkdir $(dirname)
	for d in $(alldirs); do	mkdir $(dirname)/$$d; done
	ln train_ears.cc listen.cc $(other_files) $(dirname)
	for d in $(alldirs); do	ln $$d/* $(dirname)/$$d; done
	chmod 777 $(dirname)  # some old versions of tar need world writable
	for d in $(alldirs); do	chmod 666 $(dirname)/$$d/*; done
	chmod 666 $(dirname)/* 
	for d in $(alldirs); do	chmod 777 $(dirname)/$$d; done
	tar czf ../ears-$(VERSION).tar.gz $(dirname)
	rm -rf $(dirname)	

bindist: train_ears listen     # assumes that binaries are built already
	strip listen train_ears
	rm -f doc/*~ *~
	-chmod -R 777 $(dirname)
	rm -rf $(dirname)
	mkdir $(dirname)
	mkdir $(dirname)/doc
	mkdir $(dirname)/contrib
# we can't rely on symbolic links
	ln train_ears listen README COPYING *.words HELP.* $(dirname)
	ln doc/* $(dirname)/doc
	ln contrib/* $(dirname)/contrib
	chmod 777 $(dirname)  # some old versions of tar need world writable
	chmod 666 $(dirname)/doc/* $(dirname)/contrib/* $(dirname)/* 
	chmod 777 $(dirname)/listen $(dirname)/train_ears
	chmod 777 $(dirname)/doc $(dirname)/contrib
	tar czf ../ears-$(VERSION)-bin.tar.gz $(dirname)
	rm -rf $(dirname)	

archive := ../EA$(shell date +%y%m%d).TGZ

bkp: deltrash
	find . -maxdepth 2 -type f ! -perm +100 ! -name ".depend"\
	  ! -name "*.o" ! -name "COPYING" ! -name "*.a" ! -name "*,v" >tmp
	tar cfTz $(archive) tmp 
	mcopy -n $(archive) a:
	mcopy -n $(archive) a:bkp
	rm $(archive) tmp
	mdir a:

rcsbkp:
	tar cfz ears-rcs.tgz RCS/* */RCS */RCS/*
	mcopy -n ears-rcs.tgz a:
	rm ears-rcs.tgz
	mdir a:

#------------------------------------------------------------------------------
# more dependencies
#------------------------------------------------------------------------------


ifeq (.depend,$(wildcard .depend))
include .depend
depend:
else
depend: $(sources)
	touch .depend
	for d in $(subdirs) .; do\
		$(CPP) $(includedirs) $(SOUNDDEFS) $(RECOGS) $(GUI) -MM $$d/*.c $$d/*.cc\
		 2>/dev/null | sed "s/^[a-z]/$$d\/&/g" >>.depend;\
	done
	for d in $(subdirs); do\
		echo $$d/lib$$d.a: $$d/*.c $$d/*.cc 2>/dev/null\
		 | sed 's/\b\w*\b\/\*\.cc\?//g' | sed 's/\.cc\?/\.o/g' >>.depend;\
	done
	@for d in $(subdirs); do make -C $$d depend; done
endif
