#
# Copyright (c) 1993-1994 The 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 University of
#	California, Berkeley and the Network Research 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$ (LBL)
#

.SUFFIXES: .cpp $(.SUFFIXES)

ALL = @V_ALL@ histtolut
all:	$(ALL)

# Special case for H261 encoder - no optimisation if using linux gcc 2.96
codec/dct.o:
	rm -f $@; $(C++) -o $@ -c @V_CCOPT_H261@ $(BFLAGS) $*.cpp

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

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

ED_YBITS = 4

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

INCLUDE_TK = @V_INCLUDE_TK@
INCLUDE_TCL = @V_INCLUDE_TCL@
INCLUDE_X11 = @V_INCLUDE_X11@
INCLUDE_MISC = -I. -Icodec/tmndec -Icodec/tmn-x -Icodec/h263 -Irtp -Irender -Inet -Ivideo -Icodec -I../common/src @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 \
		$(LIBRARY_TK)/optMenu.tcl \
		$(LIBRARY_TK)/scrlbar.tcl

LIB_GRABBER = @V_LIB_GRABBER@
INCLUDE_GRABBER = @V_INCLUDE_GRABBER@
OBJ_GRABBER = @V_OBJ_GRABBER@
SRC_GRABBER = $(OBJ_GRABBER:.o=.cpp) 
OBJ_XIL = @V_OBJ_XIL@
OBJ_CRYPT = net/crypt-rijndael.o @V_OBJ_CRYPT@
CODEC_LIB = @V_CODEC_LIB@ codec/tmndec/libh263.a codec/tmn-x/libh263coder.a

LIB = $(LIB_GRABBER) @V_LIB_TK@ @V_LIB_TCL@ @V_LIB_X11@ @V_LIB@ $(CODEC_LIB) $(VIDEO_LIB) \
	../common/src/libuclmmbase.a -lm -lz -lpthread @V_LIB_FFMPEG@ @V_LIB_X264@ 


INCLUDE = $(INCLUDE_MISC) $(INCLUDE_GRABBER) $(INCLUDE_TK) $(INCLUDE_TCL) \
	$(INCLUDE_X11) $(MD_INC) -Icodec/jpeg -Icodec/p64 -I. 
DEFINE = @V_DEFINE@ -DED_YBITS=$(ED_YBITS) -DSIGRET=@V_SIGRET@ -DNLAYER=8 -D_GNU_SOURCE
BFLAGS = $(DEFINE) $(INCLUDE)
CFLAGS = $(CCOPT) $(BFLAGS)

#
# Remember to add a dependency if you add any tcl sources here.
#
TCL_VIC = ui-ctrlmenu.tcl ui-main.tcl ui-resource.tcl ui-relate.tcl \
	ui-titlemaker.tcl ui-srclist.tcl ui-stats.tcl ui-util.tcl ui-windows.tcl \
	ui-switcher.tcl ui-extout.tcl ui-grabber.tcl ui-unix.tcl \
	cf-main.tcl cf-tm.tcl cf-confbus.tcl cf-network.tcl cf-util.tcl \
	tkerror.tcl entry.tcl accessgrid.tcl ag-pixrate.tcl autoplace_ui.tcl

#
# 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@

H263_OBJS = codec/h263/h263rtp.o codec/h263/h263dec.o codec/h263/bitIn.o \
	codec/h263/input.o codec/h263/getgob.o codec/h263/reconh263.o \
	codec/h263/recon.o codec/h263/getvlc.o codec/h263/getblk.o \
	codec/h263/h263enc.o codec/h263/motion.o codec/h263/block.o \
	codec/h263/bitOut.o codec/h263/h263mux.o codec/h263/idctdec.o \
	codec/h263/fdct.o codec/h263/code.o codec/h263/gethdr.o \
	codec/h263/idctenc.o codec/h263/sac.o

# .c objects
OBJ1 =	net/inet.o net/inet6.o codec/cellb_tables.o tkStripchart.o \
	md5c.o random.o @V_CPUDETECT_OBJ@ $(H263_OBJS)

#	session-rtpv1.o session-nv.o session-ivs.o # Not supported anymore
# .cpp objects
OBJ2 =	main.o iohandler.o timer.o idlecallback.o media-timer.o \
	rate-variable.o Tcl.o Tcl2.o module.o \
	net/crypt.o net/crypt-des.o net/pkttbl.o net/pktbuf.o \
	net/net.o net/net-ip.o net/net-addr.o net/net-ipv6.o \
	net/mbus_handler.o net/mbus_engine.o net/group-ipc.o net/confbus.o \
	rtp/session.o rtp/source.o rtp/transmitter.o rtp/pktbuf-rtp.o \
	video/device.o video/grabber.o video/grabber-still.o \
	codec/encoder-h261.o codec/encoder-jpeg.o codec/encoder-raw.o \
	codec/encoder-h263.o codec/encoder-h263v2.o codec/encoder-nv.o \
	codec/encoder-cellb.o codec/encoder-pvh.o codec/compositor.o \
	codec/jpeg/jpeg.o codec/p64/p64.o codec/dct.o codec/transcoder-jpeg.o \
	codec/framer-jpeg.o codec/decoder-pvh.o video/assistor-list.o \
	codec/decoder.o codec/decoder-jpeg.o codec/decoder-nv.o \
	codec/decoder-h263.o codec/decoder-h263v2.o codec/decoder-cellb.o \
	codec/decoder-h261.o codec/decoder-h261v1.o codec/decoder-raw.o \
	render/renderer.o render/renderer-window.o render/color.o \
	render/color-pseudo.o render/color-dither.o \
	render/color-ed.o render/color-quant.o \
	render/color-gray.o render/color-mono.o render/color-hist.o \
	render/rgb-converter.o render/vw.o render/ppm.o @V_OBJ@


OBJ3 =	cm0.o cm1.o huffcode.o version.o bv.o codec/pvh-huff.o \
	$(TCL_VIC:.tcl=.o) tk.o

SRC =	$(OBJ1:.o=.c) $(OBJ2:.o=.cpp) $(BROKEN_OBJ:.o=.c) \
	$(RTIP_OBJ:.o=.c) $(SRC_GRABBER) $(OBJ_XIL:.o=.cpp) $(OBJ_CRYPT:.o=.c)

OBJ =	$(OBJ1) $(OBJ2) $(OBJ3) $(BROKEN_OBJ) $(RTIP_OBJ)

OBJ_VDD = vdd.o p64/p64.o p64/p64dump.o \
	module.o renderer.o renderer-window.o color.o \
	color-dither.o color-gray.o color-pseudo.o color-ed.o \
	color-quant.o color-mono.o color-hist.o \
	color-x11.o \
	Tcl.o Tcl2.o vw.o cm0.o cm1.o \
	dct.o huffcode.o bv.o iohandler.o timer.o \
	ui-vdd.o tk.o random.o
	# color-true.o color-hi.o

OBJ_H261PLAY = h261_play.o p64/p64.o p64/p64dump.o \
	module.o renderer.o renderer-window.o color.o \
	color-dither.o color-gray.o color-pseudo.o color-ed.o \
	color-quant.o color-mono.o color-hist.o \
	color-x11.o \
	Tcl.o Tcl2.o vw.o cm0.o cm1.o \
	dct.o huffcode.o bv.o iohandler.o timer.o \
	ui-h261_play.o tk.o random.o
	# color-true.o color-hi.o

OBJ_JPEGPLAY = jpeg_play.o jpeg/jpeg.o \
	renderer.o renderer-window.o color.o \
	color-dither.o color-gray.o color-pseudo.o color-ed.o \
	color-quant.o color-mono.o \
	color-x11.o \
	Tcl.o Tcl2.o vw.o cm0.o cm1.o \
	dct.o huffcode.o jpeg_play_tcl.o bv.o iohandler.o timer.o \
	ui-jpeg_play.o tk.o random.o
	# color-true.o color-hi.o
	
LIB_CB = @V_LIB_TK@ @V_LIB_TCL@ @V_LIB_X11@ @V_LIB@ -lm
OBJ_CB = net/cbAppInit.o net/cb.o net/confbus.o net/group-ipc.o iohandler.o \
	render/ppm.o \
	net/net.o net/net-ip.o net/net-addr.o net/crypt.o net/crypt-dull.o $(OBJ_CRYPT) net/communicator.o \
	Tcl.o Tcl2.o net/inet.o md5c.o

OBJ_H261DUMP = h261_dump.o p64/p64.o p64/p64dump.o huffcode.o dct.o bv.o

# Don't allow building of the non-DES version...
#vic:	$(OBJ) $(OBJ_GRABBER) $(JV_LIB)
#	rm -f $@
#	$(C++) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJ) $(OBJ_GRABBER) $(LIB) $(STATIC)

vic: $(CODEC_LIB) $(VIDEO_LIB) $(OBJ) $(OBJ_GRABBER) $(JV_LIB) $(OBJ_CRYPT)  $(OBJ_XIL)
	rm -f $@
	$(C++) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJ) $(OBJ_GRABBER) $(OBJ_CRYPT) $(LIB) $(STATIC)

vic.dyn: $(CODEC_LIB) $(VIDEO_LIB) $(OBJ) $(OBJ_GRABBER) $(JV_LIB)
	rm -f $@
	$(C++) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJ) $(OBJ_GRABBER) $(LIB)

# XIL support 
#
vic.xil: $(CODEC_LIB) $(VIDEO_LIB) $(OBJ) $(OBJ_GRABBER) $(JV_LIB) $(OBJ_CRYPT)  $(OBJ_XIL)
	rm -f $@
	$(C++) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJ) $(OBJ_XIL) $(OBJ_GRABBER) $(OBJ_CRYPT) $(LIB) $(STATIC) @V_LIB_XIL@

histtolut: render/histtolut/histtolut.cpp render/histtolut/convex_hull.o
	rm -f $@
	$(C++) -o $@ $(CFLAGS) render/histtolut/histtolut.cpp render/histtolut/convex_hull.o

vdd: $(OBJ_VDD)
	rm -f $@
	$(C++) -o $@ $(CFLAGS) $(LDFLAGS) $(OBJ_VDD) $(LIB) $(STATIC)

cb_wish: $(OBJ_CB)
	rm -f $@
	$(C++) -o $@ $(CFLAGS) $(LDFLAGS) $(OBJ_CB) $(LIB_CB) $(STATIC)

jpeg_play: $(OBJ_JPEGPLAY)
	rm -f $@
	$(C++) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJ_JPEGPLAY) $(LIB) $(STATIC)

h261_play: $(OBJ_H261PLAY)
	rm -f $@
	$(C++) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJ_H261PLAY) $(LIB) $(STATIC)

h261_dump: $(OBJ_H261DUMP)
	rm -f $@
	$(CC) -o $@ $(CFLAGS) $(OBJ_H261DUMP) -lm $(STATIC)

h261tortp: h261tortp.cpp
	rm -f $@
	$(C++) -o $@ $(CFLAGS) h261tortp.cpp

tcl2c++: tcl/tcl2cpp/tcl2cpp.c
	rm -f $@
	$(C++) -o $@ $(CFLAGS) tcl/tcl2cpp/tcl2cpp.c

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

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

#
# pass-2 embedded tcl files
#
TCL_VIC_OBJS=$(TCL_VIC:.tcl=.o) ui-vdd.o ui-h261_play.o
$(TCL_VIC_OBJS): tcl2c++
	base=$(@:.o=); rm -f $@ $$base.cpp ; \
		./tcl2c++ 2 tcl/$$base.tcl > tcl/$$base.cpp ; \
		$(C++) -o $@ -c $(CFLAGS) tcl/$$base.cpp ; rm -f tcl/$$base.cpp

accessgrid.o: tcl/accessgrid.tcl
ag-pixrate.o: tcl/ag-pixrate.tcl 
autoplace_ui.o: tcl/autoplace_ui.tcl 
ui-ctrlmenu.o: tcl/ui-ctrlmenu.tcl
ui-extout.o: tcl/ui-extout.tcl
ui-grabber.o: tcl/ui-grabber.tcl
ui-main.o: tcl/ui-main.tcl
ui-resource.o: tcl/ui-resource.tcl
ui-srclist.o: tcl/ui-srclist.tcl
ui-stats.o: tcl/ui-stats.tcl
ui-switcher.o: tcl/ui-switcher.tcl
ui-unix.o: tcl/ui-unix.tcl
ui-util.o: tcl/ui-util.tcl
ui-windows.o: tcl/ui-windows.tcl
cf-main.o: tcl/cf-main.tcl
cf-util.o: tcl/cf-util.tcl
cf-network.o: tcl/cf-network.tcl
cf-tm.o: tcl/cf-tm.tcl
cf-confbus.o: tcl/cf-confbus.tcl
tkerror.o: tcl/tkerror.tcl
entry.o: tcl/entry.tcl
ui-vdd.o: tcl/ui-vdd.tcl
ui-h261_play.o: tcl/ui-h261_play.tcl
ui-relate.o: tcl/ui-relate.tcl
ui-titlemaker.o: tcl/ui-titlemaker.tcl

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

cm0.o: cm0.c
cm0.c: rgb-cube.ppm jv-cube-128.ppm ppmtolut
	rm -f $@
	./ppmtolut -n cube rgb-cube.ppm >> $@
	./ppmtolut -n jv_cube jv-cube-128.ppm >> $@

cm1.o: cm1.c
cm1.c: yuv-map.ppm cm170.ppm
	rm -f $@
	./ppmtolut -n quant cm170.ppm >> $@
	./ppmtolut -Y $(ED_YBITS) -n ed -e yuv-map.ppm >> $@

huffcode.c: mkhuff
	rm -f $@
	./mkhuff -e > huffcode.c

mkhuff: codec/p64/mkhuff.cpp 
	rm -f $@
	$(C++) -o $@ $(CFLAGS) codec/p64/mkhuff.cpp $(STATIC)

bv.c: mkbv
	rm -f $@
	./mkbv > $@

mkbv: render/mkbv/mkbv.c
	rm -f $@
	$(C++) -o $@ $(CFLAGS) render/mkbv/mkbv.c $(STATIC)

rgb-cube.ppm: mkcube
	rm -f $@
	./mkcube rgb > $@

yuv-map.ppm: mkcube
	rm -f $@
	./mkcube -Y $(ED_YBITS) -U 45 -V 45 yuv > $@

mkcube: render/mkcube/mkcube.cpp
	rm -f $@
	$(C++) -o $@ $(CFLAGS) render/mkcube/mkcube.cpp $(STATIC)

ppmtolut: render/ppmtolut/ppmtolut.c
	rm -f $@
	$(CC) -o $@ $(CFLAGS) render/ppmtolut/ppmtolut.c -lm $(STATIC)

install: force
	 cp vic @prefix@/bin/vic
	 chown bin @prefix@/bin/vic
	 chgrp bin @prefix@/bin/vic
	 chmod 555 @prefix@/bin/vic
	 cp vic.1 @prefix@/man/vic.1
	 cp histtolut @prefix@/bin/histtolut
	 chown bin @prefix@/bin/histtolut
	 chgrp bin @prefix@/bin/histtolut
	 chmod 555 @prefix@/bin/histtolut

clean:
	rm -f *.o codec/jpeg/*.o codec/p64/*.o codec/h263/*.o \
		histtolut render/histtolut/*.o huffcode.c win32/*.o \
		core tcl2c++ mkbv bv.c cpu/*.o \
		codec/*.o render/*.o video/*.o net/*.o rtp/*.o mkhuff \
		tk.tcl vic_tcl.c h261_play_tcl.c tmp.c \
		vic vic.dyn vic.xil h261_play h261_dump jpeg_play cb_wish \
		mkcube rgb-cube.ppm yuv-map.ppm cm0.c cm1.c ppmtolut \
		config.cache config.log domake.* dotar.*
	cd codec/tmn && $(MAKE) clean
	cd codec/tmndec && $(MAKE) realclean
	rm -f codec/tmn-x/*.o codec/tmn-x/*.a

realclean:
	$(MAKE) clean
	rm -f config.log config.cache config.status

tags:	force
	ctags -tdwC $(SRC) *.h $(TKSRC)/*.c $(TKSRC)/*.h \
		$(TCLSRC)/*.c $(TCLSRC)/*.h

force:

depend:
	$(MKDEP) $(INCLUDE) $(DEFINE) $(SRC)

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

srctar: force
	@cwd=`pwd` ; dir=`basename $$cwd` ; \
	    name=vic-`cat VERSION | tr A-Z a-z` ; \
	    tar=vicsrc-`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

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

ffmpeg/lib/libavcodec.a:
	(cd ffmpeg/libavcodec; make; make install )

ffmpeg/lib/libavutil.a:
	(cd ffmpeg/libavutil; make; make install )

ffmpeg/lib/libswscale.a:
	(cd ffmpeg/libswscale; make; make install )

ffmpeg/lib/libpostproc.a:
	(cd ffmpeg/libpostproc; make; make install )

x264/lib/libx264.a:
	(cd x264; make; make install )

codec/tmn-x/libh263coder.a:
	(cd codec/tmn-x; $(MAKE) CC=$(CC) CCOPT="$(CCOPT)" )

codec/tmndec/libh263.a:
	(cd codec/tmndec; $(MAKE) CC=$(CC) CCOPT="$(CCOPT)" -f Makefile.lib)
strip:	vic
	strip vic
