#
#  Copyright (c) 1991-1993 Regents of the University of California.
#  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#	This product includes software developed by the Computer Systems
#	Engineering Group at Lawrence Berkeley Laboratory.
# 4. Neither the name of the University nor of the Laboratory may be used
#    to endorse or promote products derived from this software without
#    specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# @(#) $Header: Makefile.in,v 1.35 96/04/04 03:46:05 van Exp $ (LBL)
#

.SUFFIXES: .cc $(.SUFFIXES)

ALL = @V_ALL@
all:	$(ALL)

.cc.o:
	rm -f $@; $(C++) -c $(CFLAGS) $*.cc

.c.o:
	rm -f $@; $(CC) -o $@ -c $(CFLAGS) $*.c

CC = @CC@
C++ = @CXX@
TABCC = @V_TABCC@
CCOPT = @V_CCOPT@

INCLUDE_TK = @V_INCLUDE_TK@
INCLUDE_TCL = @V_INCLUDE_TCL@
INCLUDE_GSM = @V_INCLUDE_GSM@
INCLUDE_X11 = @V_INCLUDE_X11@
INCLUDE_MISC = @V_INCLUDE@

STATIC = @V_STATIC@
MKDEP = ./mkdep

@V_SHELL@

LIBRARY_TK = @V_LIBRARY_TK@
LIBRARY_TCL = @V_LIBRARY_TCL@
TK_LIBRARY_FILES = \
		$(LIBRARY_TCL)/init.tcl \
		$(LIBRARY_TK)/tk.tcl \
		$(LIBRARY_TK)/button.tcl \
		$(LIBRARY_TK)/dialog.tcl \
		$(LIBRARY_TK)/entry.tcl \
		$(LIBRARY_TK)/focus.tcl \
		$(LIBRARY_TK)/listbox.tcl \
		$(LIBRARY_TK)/menu.tcl \
		$(LIBRARY_TK)/palette.tcl \
		$(LIBRARY_TK)/scale.tcl \
		$(LIBRARY_TK)/tearoff.tcl \
		$(LIBRARY_TK)/text.tcl \
		@V_TKDOSNAMES@

LIB_AUDIO = @V_LIB_AUDIO@
INCLUDE_AUDIO = @V_INCLUDE_AUDIO@
OBJ_AUDIO = @V_OBJ_AUDIO@
OBJ_CRYPT = @V_OBJ_CRYPT@
SRC_AUDIO = $(OBJ_AUDIO:.o=.cc) 
LIB = $(LIB_AUDIO) @V_LIB_TK@ @V_LIB_TCL@ @V_LIB_GSM@ @V_LIB_X11@ @V_LIB@ -lm
INCLUDE = $(INCLUDE_MISC) $(INCLUDE_AUDIO) $(INCLUDE_TK) $(INCLUDE_TCL) \
	$(INCLUDE_X11) $(MD_INC)
DEFINE = @V_DEFINE@ -DSIGRET=@V_SIGRET@ -DFRAMESIZE=160
BFLAGS = $(DEFINE) $(INCLUDE)
CFLAGS = $(CCOPT) $(BFLAGS)

#
# Remember to add a dependency if you add any tcl sources here.
#
TCL_VAT = tkerror.tcl entry.tcl ui-main.tcl ui-resource.tcl ui-util.tcl \
	ui-stats.tcl ui-audio.tcl ui-unix.tcl \
	cf-main.tcl cf-network.tcl cf-util.tcl cf-confbus.tcl 
TCL_VAT_C = $(TCL_VAT:.tcl=.cc)

#
# These modules are broken in libg++.  Rather than fix libg++
# every time I install it on a given system, just make them
# here (these sources came from tcl's compat directory)
#
BROKEN_OBJ = @V_BROKEN_OBJ@

OBJ1 =	inet.o tonetab.o adpcm.o lpc.o tkStripchart.o md5c.o random.o vat-gsm.o
OBJ2 =	session.o session-vat.o source.o audio.o controller.o \
	crypt.o crypt-dull.o filter.o group-ipc.o iohandler.o main.o \
	net.o net-ip.o observe.o sitebox.o confbus.o \
	audio-sock.o ss.o timer.o idlecallback.o Tcl.o Tcl2.o tkwidget.o \
	vu.o wiener.o media-timer.o transmitter.o rate-variable.o \
	encoder.o encoder-lpc.o encoder-adpcm.o encoder-gsm.o \
	decoder.o decoder-lpc.o decoder-adpcm.o decoder-gsm.o
OBJ3 =	tabmulaw.o tabmix.o tabsmix.o tabmulawx.o
OBJ4 =	$(TCL_VAT:.tcl=.o) tk.o version.o
SRC =	$(OBJ1:.o=.c) $(OBJ2:.o=.cc) $(OBJ_ARCH:.o=.cc) $(OBJ_AUDIO:.o=.cc) \
	$(OBJ_CRYPT:.o=.cc)
OBJ =	$(OBJ1) $(OBJ2) $(OBJ3) $(OBJ4) $(OBJ_ARCH) $(BROKEN_OBJ) \
	$(OBJ_AUDIO) $(OBJ_CRYPT)
SRC3 =	$(OBJ3:.o=.c)

vat:	$(OBJ)
	$(C++) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJ) $(LIB) $(STATIC)

#
# Compile without optimzation to work around an OSF compiler bug.
#
Tcl2.o: Tcl.h
	rm -f $@
	$(C++) -c $(BFLAGS) Tcl2.cc

#
# Compile without optimzation to work around an SCO compiler bug.
#
main.o: main.cc
	rm -f $@
	$(C++) -c $(BFLAGS) main.cc

#
# pass-1 embedded tcl files
#
tk.o: $(TK_LIBRARY_FILES) tcl2c++
	base=tk; rm -f $@ $$base.cc ; \
		./tcl2c++ 1 $(TK_LIBRARY_FILES) > $$base.cc ; \
		$(C++) -o $@ -c $(CFLAGS) $$base.cc ; rm -f $$base.cc

#
# pass-2 embedded tcl files
#
TCL_VAT_OBJS=$(TCL_VAT:.tcl=.o)
$(TCL_VAT_OBJS): tcl2c++
	base=$(@:.o=); rm -f $@ $$base.cc ; \
		./tcl2c++ 2 $$base.tcl > $$base.cc ; \
		$(C++) -o $@ -c $(CFLAGS) $$base.cc ; rm -f $$base.cc

tkerror.o: tkerror.tcl
entry.o: entry.tcl
ui-main.o: ui-main.tcl
ui-audio.o: ui-audio.tcl
ui-util.o: ui-util.tcl
ui-stats.o: ui-stats.tcl
ui-resource.o: ui-resource.tcl
ui-unix.o: ui-unix.tcl
cf-main.o: cf-main.tcl
cf-network.o: cf-network.tcl
cf-util.o: cf-util.tcl
cf-confbus.o: cf-confbus.tcl

version.c: VERSION
	rm -f version.c
	cat VERSION | sed 's/.*/char version[] = "&";/' > version.c

$(OBJ3): $(SRC3)
	$(TABCC) $(RFLAG) -c $*.c

$(SRC3): mktab.c
	$(CC) $(CFLAGS) -o mktab mktab.c $(LIB) $(STATIC)
	rm -f $(SRC3)
	./mktab -mulaw > tabmulaw.c
	./mktab -sum > tabmix.c
	./mktab -scale > tabsmix.c
	./mktab -muX > tabmulawx.c
	rm -f mktab

vat-gsm.o: vat-gsm.c
	rm -f $@; $(CC) -o $@ -c $(INCLUDE_GSM) $(CFLAGS) $*.c

tar: force
	tar=vatbin-`cat VERSION`-@V_TAR_TARGET@.tar.gz ; \
	rm -f $$tar ; \
	@V_TARCMD@ - README CHANGES.html vat.1 @V_TAR_EXTRA@ $(ALL) | \
		gzip -c > $$tar

srctar: force
	@cwd=`pwd` ; dir=`basename $$cwd` ; \
	    name=vat-`cat VERSION | tr A-Z a-z` ; \
	    tar=vatsrc-`cat VERSION`.tar.gz ; \
	    list="" ; \
	    for i in `cat FILES` ; do list="$$list $$name/$$i" ; done; \
	    echo \
	    "(rm -f $$tar; cd .. ; ln -s $$dir $$name)" ; \
	     (rm -f $$tar; cd .. ; ln -s $$dir $$name) ; \
	    echo \
	    "(cd .. ; tar cfh $$tar [lots of files])" ; \
	     (cd .. ; tar cfh - $$list) | gzip -c > $$tar ; \
	    echo \
	    "rm ../$$name; chmod 444 $$tar" ;  \
	     rm ../$$name; chmod 444 $$tar

install: force
	 cp vat @prefix@/bin/vat; \
	 chown bin @prefix@/bin/vat; \
	 chgrp bin @prefix@/bin/vat; \
	 chmod 555 @prefix@/bin/vat

install-man: force
	cp vat.1 @prefix@/man/man1/vat.1
	chown bin @prefix@/man/man1/vat.1
	chgrp bin @prefix@/man/man1/vat.1
	chmod 444 @prefix@/man/man1/vat.1

CLEAN = vat core core.vat tcl2c++ config.log config.cache
clean:
	rm -f $(CLEAN) $(OBJ1) $(OBJ2) $(OBJ4) $(OBJ_AUDIO) $(OBJ_CRYPT) \
	      $(TCL_VAT_C)
		
c++-clean:
	rm -f $(OBJ2) $(OBJ_AUDIO)

realclean:
	rm -f $(CLEAN) $(OBJ) tags \
		mktab tabmix.c tabmulaw.c tabsmix.c
		
force:

tags:	$(SRC)
	./systags.sh

depend:
	./mkdep $(INCLUDE) $(DEFINE) $(INCLUDE_GSM) $(SRC)
