#----------------------------------------------------------------------
# Makefile (requires GNU make)
#
# $Id: Makefile,v 1.88 2003/12/10 02:35:49 purnhage Exp $
#
# Authors:
# HP    Heiko Purnhagen, Uni Hannover <purnhage@tnt.uni-hannover.de>
# ...   and some authors of the MPEG-4 Audio VM makefile
#
# Changes:
# 16-sep-98   HP    based on the MPEG-4 Audio VM makefile
# 17-sep-98   HP    libs for SGI
# 25-sep-98   HP    added randhead and other awk-scripts
# 13-oct-98   HP    fixed OSTYPE platform detection
# 20-dec-98   HP    included other autools, better "make tgz" etc ...
# 29-dec-98   HP    CVS, enhasp
# 30-dec-98   HP    USE_AFSP
# 29-jan-99   HP    genam, genfm
# 23-apr-99   HP    AFSP_XXX_PATH
# 17-may-99   HP    Linux6 platform directory (same as Linux)
# 04-nov-99   HP    fixed bug in SED
# 14-nov-99   HP    added DEBUG
# 31-mai-00   HP    added auduplex
# 06-jun-00   HP    added fifo
# 06-sep-00   HP    added targets install, install-xxx
# 11-sep-00   HP    renamed Linux6 to Linux to simplify root's "make install"
# 20-Jul-2001 HP    added playseaq
# 20-Aug-2001 HP    added toc2plax-trk, toc2plax-idx, wav2au
# 01-Nov-2001 HP    added toc2toc toc2plax-cut
# 22-Nov-2001 HP    added dao2toc cd-cover.fig autools.html gen-html etc.
# 23-Nov-2001 HP/CF added MSVC stuff
# 06-Feb-2002 HP    updated auquery, autools.html generator, ...
# 08-Mar-2002 HP    added wav2toc
# 20-Mar-2002 HP    updated to 1.1.7
# 10-Apr-2002 HP    added ac3spdif
# 17-Apr-2002 HP    added noicod-0.6
# 07-Nov-2002 HP    added wav2toc to tgz
# 2003-01-19  HP    updated to 1.2.0, added rateconv-dll
# 2003-03-15  HP    changed from cp to install
# 2003-05-12  HP    added toc2toc-offset, toc2enc-trk
# 2003-10-11  HP    updated to 1.2.3
# 2003-12-10  HP    updated to 1.2.4
#----------------------------------------------------------------------

# usage:
#   make all
#   make tgz


# Supported platforms (set SYSTEM_NAME accordingly):
#   Linux
#   Solaris
#   IRIX
#   cdk    (no audio device support)
# All platform dependent files (binaries, objects, ...) are kept in
# distinct directories (linux, sol, sgi, cdk).

# After modifying CFLAGS or LDFLAGS, do "make clean" first!


ifeq "$(SYSTEM_NAME)" ""
ifeq "$(OSTYPE)" ""
SYSTEM_NAME = $(shell uname -s)
else
ifeq "$(OSTYPE)" "linux"
SYSTEM_NAME = Linux
endif
ifeq "$(OSTYPE)" "linux-gnu"
SYSTEM_NAME = Linux
endif
ifeq "$(OSTYPE)" "solaris"
SYSTEM_NAME = Solaris
endif
ifeq "$(OSTYPE)" "iris4d"
SYSTEM_NAME = IRIX
endif
ifeq "$(OSTYPE)" "cygwin"
SYSTEM_NAME = Cygwin
endif
endif
endif
ifeq "$(SYSTEM_NAME)" "Linux6"
override SYSTEM_NAME = Linux
endif


# directory for binaries used by "make install"
INSTALL_DIR = /usr/local/bin


ifeq "$(MAKELEVEL)" "0"


NAME = autools
VER = 1.2.4
NAMEVER = $(NAME)-$(VER)


# paths for AFsp's libtsp can be set by the environment variables
# AFSP_INCLUDE_PATH and AFSP_LIBRARY_PATH

# disable AFsp by "make USE_AFSP=0"
ifndef USE_AFSP
USE_AFSP = 1
endif

# use "make DEBUG=1 clean all" for debugging
ifndef DEBUG
DEBUG = 0
endif

ifeq "$(DEBUG)" "1"
CFLAGS = -g -Wall
LDFLAGS = -g
else
ifeq "$(BOUNDS)" "1"
CFLAGS = -g -fbounds-checking -Wall
LDFLAGS = -g -fbounds-checking
else
CFLAGS = -O3 -Wall -Wno-uninitialized -Wno-unused
endif
endif

ifeq "$(USE_AFSP)" "1"
override CFLAGS += -DUSE_AFSP
endif

override CFLAGS += -DDISTVER=\\\"$(NAMEVER)\\\"

ifeq "$(HOME)" "/home/purnhage"
PLAYSEAQ = 1
CODEUTIL = 1
endif

# CACHE1: Size of first level data cache: 2**13 = 8k for Pentium and Alpha
# CACHE1: Size of first level data cache: 2**14 = 16k for SuperSPARC
# CACHE1: Size of first level data cache: 2**13 = 8k for MIPS R4000
ifeq "$(SYSTEM_NAME)" "Linux"
override CFLAGS += -D"CACHE1=13"
else
ifeq "$(SYSTEM_NAME)" "Solaris"
override CFLAGS += -D"CACHE1=14"
else
ifeq "$(SYSTEM_NAME)" "IRIX"
override CFLAGS += -D"CACHE1=13"
else
ifeq "$(SYSTEM_NAME)" "Cygwin"
override CFLAGS += -D"CACHE1=13"
override CFLAGS += -DWIN32
else
# unknown: use 2**13 = 8k as default
override CFLAGS += -D"CACHE1=13"
endif
endif
endif
endif

# define for platform dependent code
ifneq "$(SYSTEM_NAME)" ""
override CFLAGS += -D$(SYSTEM_NAME)
endif
# path for libtsp.h
ifneq "$(AFSP_INCLUDE_PATH)" ""
override CFLAGS += -I$(AFSP_INCLUDE_PATH)
ifneq "$(WIN32API_INCLUDE_PATH)" ""
override CFLAGS += -I$(WIN32API_INCLUDE_PATH)
endif
#override CFLAGS += -I ../AFsp-v5r2/include/
#override CFLAGS += -I ./w32api/include/
endif
# path for libtsp.a
ifneq "$(AFSP_LIBRARY_PATH)" ""
override LDFLAGS += -L$(AFSP_LIBRARY_PATH)
ifneq "$(WIN32API_LIBRARY_PATH)" ""
override LDFLAGS += -L$(WIN32API_LIBRARY_PATH)
endif
#override LDFLAGS += -L  ../AFsp-v5r2/lib/
#override LDFLAGS += -L ./w32api/lib/
endif
# libraries
LIBS = -lm
ifeq "$(USE_AFSP)" "1"
LIBS += -ltsp
endif
ifeq "$(SYSTEM_NAME)" "IRIX"
LIBS += -laudio
endif
ifeq "$(SYSTEM_NAME)" "Cygwin"
LIBS += -lwinmm
endif

# rules (level 0)

default : all

all : plax randhead augen auquant auquery ac3spdif
ifeq "$(USE_AFSP)" "1"
all : playlist
endif
ifeq "$(SYSTEM_NAME)" "Linux"
all : auduplex
endif
ifeq "$(SYSTEM_NAME)" "Cygwin"
all : auinfo
endif
all : fifo wav2au

ifeq "$(PLAYSEAQ)" "1"
all : playseaq
endif

ifeq "$(CODEUTIL)" "1"
all : noicod
endif

plax : 
	$(MAKE) $(MFLAGS)\
	OBJS="plax.o common.o cmdline.o austream.o playrec.o rateconv.o"\
	LIBS="$(LIBS)"\
	CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"\
	INCLDEP=1 TARGET=$@ binary

playlist : 
	$(MAKE) $(MFLAGS)\
	OBJS="playlist.o common.o cmdline.o austream.o playrec.o rateconv.o"\
	LIBS="$(LIBS)"\
	CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"\
	INCLDEP=1 TARGET=$@ binary

randhead : 
	$(MAKE) $(MFLAGS)\
	OBJS="randhead.o"\
	LIBS=""\
	CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"\
	INCLDEP=1 TARGET=$@ binary

augen : 
	$(MAKE) $(MFLAGS)\
	OBJS="augen.o common.o cmdline.o audio.o austream.o"\
	LIBS="$(LIBS)"\
	CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"\
	INCLDEP=1 TARGET=$@ binary

auquant : 
	$(MAKE) $(MFLAGS)\
	OBJS="auquant.o common.o cmdline.o audio.o austream.o"\
	LIBS="$(LIBS)"\
	CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"\
	INCLDEP=1 TARGET=$@ binary

auduplex : 
	$(MAKE) $(MFLAGS)\
	OBJS="auduplex.o common.o cmdline.o austream.o"\
	LIBS="$(LIBS)"\
	CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"\
	INCLDEP=1 TARGET=$@ binary

fifo : 
	$(MAKE) $(MFLAGS)\
	OBJS="fifo.o"\
	LIBS="$(LIBS)"\
	CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"\
	INCLDEP=1 TARGET=$@ binary

auquery : 
	$(MAKE) $(MFLAGS)\
	OBJS="auquery.o common.o cmdline.o playrec.o"\
	LIBS="$(LIBS)"\
	CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"\
	INCLDEP=1 TARGET=$@ binary

ac3spdif : 
	$(MAKE) $(MFLAGS)\
	OBJS="ac3spdif.o common.o cmdline.o audio.o austream.o"\
	LIBS="$(LIBS)"\
	CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"\
	INCLDEP=1 TARGET=$@ binary

auinfo : 	
	$(MAKE) $(MFLAGS)\
	OBJS="auinfo.o common.o cmdline.o"\
	LIBS="$(LIBS)"\
	CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"\
	INCLDEP=1 TARGET=$@ binary	

wav2au : 
	$(MAKE) $(MFLAGS)\
	OBJS="wav2au.o"\
	LIBS="$(LIBS)"\
	CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"\
	INCLDEP=1 TARGET=$@ binary

playseaq : 
	$(MAKE) $(MFLAGS)\
	OBJS="playseaq.o common.o audio.o austream.o playrec.o"\
	LIBS="$(LIBS) -lcurses"\
	CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"\
	INCLDEP=1 TARGET=$@ binary

noicod:
	$(MAKE) $(MFLAGS)\
	OBJS="noicod.o common.o cmdline.o audio.o austream.o m4m_fft.o enhancer.o pmtv.o"\
	LIBS="$(LIBS)"\
	CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"\
	INCLDEP=1 TARGET=$@ binary

clean :
	$(MAKE) $(MFLAGS) clean

FILES_INSTALL_PLT = plax playlist randhead augen auquant auduplex fifo \
	auquery ac3spdif wav2au
ifeq "$(PLAYSEAQ)" "1"
FILES_INSTALL_PLT += playseaq
endif
FILES_INSTALL = catcol genam genfm \
	toc2plax-trk toc2plax-idx wav2toc toc2toc toc2plax-cut dao2toc \
	toc2toc-offset toc2enc-trk

install : all
	$(MAKE) $(MFLAGS) install\
	FILES_INSTALL_PLT="$(FILES_INSTALL_PLT)"\
	INSTALL_DIR_PLT="$(INSTALL_DIR)"
	-install -v -p $(FILES_INSTALL) $(INSTALL_DIR)

# TNT-specific installs
install-linux6 :
	$(MAKE) $(MFLAGS) install\
	FILES_INSTALL_PLT="$(FILES_INSTALL_PLT)"\
	PLTDIR="linux" INSTALL_DIR_PLT="/appl/ton/bin/linux6"
	-install -v -p $(FILES_INSTALL) /appl/ton/bin

install-sol :
	$(MAKE) $(MFLAGS) install\
	FILES_INSTALL_PLT="$(FILES_INSTALL_PLT)"\
	PLTDIR="sol" INSTALL_DIR_PLT="/appl/ton/bin/sol"
	-install -v -p $(FILES_INSTALL) /appl/ton/bin

install-sgi :
	$(MAKE) $(MFLAGS) install\
	FILES_INSTALL_PLT="$(FILES_INSTALL_PLT)"\
	PLTDIR="sgi" INSTALL_DIR_PLT="/appl/ton/bin/sgi"
	-install -v -p $(FILES_INSTALL) /appl/ton/bin

FILES = README COPYING Makefile CVS \
	common.c common.h cmdline.c cmdline.h \
	austream.c austream.h playrec.c playrec.h rateconv.c rateconv.h \
	plax.c playlist.c \
	audio.c audio.h augen.c auquant.c \
	randhead.c catcol \
	auduplex.c fifo.c auquery.c wav2au.c auinfo.c ac3spdif.c \
	genam genfm \
	noise-side-mid.gen scale-c1-c2.gen sweep.gen demo.lis \
	toc2plax-trk toc2plax-idx wav2toc toc2toc toc2plax-cut \
	toc2toc-offset toc2enc-trk \
	dao2toc cd-cover.fig \
	autools.html gen-html txt2html html2txt \
	MSVC/CVS \
	MSVC/README MSVC/plax-comand-line.txt \
	MSVC/Msvc.dsp MSVC/Msvc.dsw \
	MSVC/augen MSVC/auinfo MSVC/auquant MSVC/plax \
	MSVC/playlist MSVC/randhead MSVC/wav2au \
	rateconv-dll/CVS \
	rateconv-dll/Makefile.msvc rateconv-dll/README \
	rateconv-dll/rateconv.c rateconv-dll/rateconv.h \
	rateconv-dll/rateconv.def rateconv-dll/rateconv.dll \
	rateconv-dll/rateconv.lib

FILESSEAQ = playseaq.c CreateSessions.m DoAnalysis.m LTexample.m \
	Subjective_testing.doc

ifeq "$(SYSTEM_NAME)" "Linux"
autools.html : gen-html all
	./gen-html $(NAMEVER) > autools.html.tmp
	./gen-html $(NAMEVER) autools.html.tmp > autools.html
	rm autools.html.tmp
else
autools.html : gen-html all
	@echo "WARNING: can not update autools.html (requires Linux ;-)"
endif

README : autools.html
	./html2txt < autools.html > README

tgz : README
	@echo "*****" "tar'n'zip'ing" ...
	ln -sn $(NAME) ../$(NAMEVER)
	tar czh -C .. --exclude=Root -f $(NAMEVER).tgz \
	$(addprefix $(NAMEVER)/,$(FILES))
	rm ../$(NAMEVER)
	mv -i $(NAMEVER).tgz ..

EXES = augen.exe auinfo.exe auquant.exe plax.exe playlist.exe randhead.exe wav2au.exe

zip :
	@echo "*****" "zip'ing bin/*.exe" ...
	ln -sn $(NAME) ../$(NAMEVER)
	cd ..; zip $(NAME)/$(NAMEVER)exe.zip $(addprefix $(NAMEVER)/MSVC/bin/,$(EXES))
	rm ../$(NAMEVER)
	mv -i $(NAMEVER)exe.zip ..

tgzseaq : README
	@echo "*****" "tar'n'zip'ing" ...
	ln -sn $(NAME) ../$(NAMEVER)
	tar czh -C .. -f $(NAMEVER)seaq.tgz $(addprefix $(NAMEVER)/,$(FILES)) \
	$(addprefix $(NAMEVER)/,$(FILESSEAQ))
	rm ../$(NAMEVER)
	mv -i $(NAMEVER)seaq.tgz ..


endif    #MAKELEVEL == 0


ifeq "$(MAKELEVEL)" "1"


DEPFLAGS = -M
SED = sed 's@$*.o@$(PLTDIR)/$*.o $(PLTDIR)/$*.d@g'

# automatic platform detection

ifeq "$(SYSTEM_NAME)" "Linux"
override CFLAGS += -ansi -pedantic
PLTDIR = linux
CC = g++
LINK = g++
else

ifeq "$(SYSTEM_NAME)" "Solaris"
override CFLAGS += -ansi -pedantic
PLTDIR = sol
CC = g++
LINK = g++
else

ifeq "$(SYSTEM_NAME)" "IRIX"
PLTDIR = sgi
override CFLAGS +=
CC = gcc
LINK = gcc
else

ifeq "$(SYSTEM_NAME)" "Cygwin"
override CFLAGS += -ansi -pedantic
PLTDIR = Cygwin
CC = g++
LINK = g++
else

PLTDIR = unknown
CC = cc
LINK = cc
endif
endif
endif
endif


vpath %.c

DIRTARGET = $(PLTDIR)/$(TARGET)
DIROBJS = $(addprefix $(PLTDIR)/,$(OBJS))
DIRDEPS = $(DIROBJS:%.o=%.d)
PLTDIRTAG = $(PLTDIR)/_$(SYSTEM_NAME)_


# rules (level 1)

binary : $(PLTDIRTAG)
binary : $(DIRTARGET)
	@echo "*****" made binary $<

$(DIRTARGET) : $(PLTDIRTAG)
$(DIRTARGET) : $(DIROBJS)
	@echo "*****" linking $@ ...
	$(LINK) $(LDFLAGS) $(DIROBJS)\
	$(LIBS) $(COMLIBS) $(SYSLIBS) -o $@

$(DIROBJS) : $(PLTDIRTAG)
$(DIROBJS) : $(PLTDIR)/%.o : %.c
	@echo "*****" compiling $< ...
	$(CC) -c $(CFLAGS) $< -o $@

$(DIRDEPS) : $(PLTDIRTAG)
$(DIRDEPS) : $(PLTDIR)/%.d : %.c
	@echo "*****" making dependencies for $< ...
	$(CC) $(DEPFLAGS) $(CFLAGS) $< | $(SED) > $@

# include dependency files *.d
# NOTE: If an included *.d file does not exist, this file becomes a target
#       that is to be made if a corresponding rule exists!
#       (here: "generate dependencies" rule)
ifeq "$(INCLDEP)" "1"
-include $(DIRDEPS)
endif

$(PLTDIRTAG) :
	@echo "*****" making directory
	-mkdir $(PLTDIR)
	touch $(PLTDIRTAG)

install : 
	@echo "*****" installing ...
	-install -v -p $(addprefix $(PLTDIR)/,$(FILES_INSTALL_PLT)) $(INSTALL_DIR_PLT)

clean : 
	@echo "*****" cleaning ...
	-rm $(PLTDIR)/*


endif	#(MAKELEVEL) == 1


# end of makefile
