############################# User Options: ################################

# Char hashing - consumes more memory in favor of speed
HASH_CHARS=-DHASH_CHARS

# Choose one (and only one) of these:
#CC=gcc -Wall -pipe $(OPT)
CC=gcc -Wall -pipe $(DBG)

# XFree86 Extensions Support - Use the following to include DGA and VMODE
#
XFREELIBS=-lXxf86dga -lXxf86vm -lXext
XFREEDEFS=-DUSE_DGA -DUSE_VMODE
#
# Use the following for non XFree86 systems or to not want XFree86 Extensions
#
# XFREELIBS=
# XFREEDEFS=


########################### The rest should be OK: #########################

OPT= -O4
DBG= -g
LD=gcc

MAJOR=  0
MINOR=  0
INSTLIBDIR= /usr/local/lib
SHARE=  libgatos.so
SHAREF= libgatos.so.$(MAJOR).$(MINOR)
SHAREN= libgatos.so.$(MAJOR)
ARC=	libgatos.a

# Warning!  It is not a good idea to use shared libs at this point.  
# They can cause many problems.
#
# Use the following to have a shared lib produced for gatos
# USELIB=	$(SHAREF)
#
# or use this one for just the non-shared version
USELIB=	$(ARC)
#
#Note that you probably need to have /usr/local/lib included in your 
# /etc/ld.so.conf file for the program to run.

# C++ Compiler #
LXX=g++
CXX=g++ -Wall -pipe $(OPT)

XLIBSDIR=-L/usr/X11R6/lib
XINCSDIR=-I/usr/X11R6/include
XLIBS=-lX11
INSTALLDIR=/usr/local/bin

# Uncomment the following to output the raw cc (except 0)
#CCFLAG=-DCCTEST
# Uncomment the following to output the raw eds (except 0)
#CCFLAG=-DCCTEST -DEDSTEST

# Uncomment the following two lines to enable the the curses CC output
# instead of the overlay
#CURS=-DCC_CURSES
#CURF=ccshow.o -lncurses

# Choose one (and only one) of these:
# 1: Use delay.S from Linux kernel sources (2.0.36).
DEFS=-DUSE_DELAY_S $(XFREEDEFS) 
DELAY=delay.o
# 2: Use i2c_delay in i2c.c.
#DEFS=
#DELAY=

INCS=$(XINCSDIR)
OBJS=gatos.o channels.o ati.o board.o bt829.o fi12xx.o sound.o \
     tvout.o saa5281.o pci.o i2c.o cc.o $(DELAY)

all: xatitv atitv yuvsum atitojpg atitogif atitoppm scanpci atisplit

test: aiwtv
	sync
	./aiwtv

tgz: realclean
	(DIR1=`pwd` ; DIR=$${DIR1%/}; cd .. ; \
	tar cvf - "--exclude=*/CVS" $${DIR##*/} | gzip - > $${DIR##*/}.tar.gz )

floppy: tgz
	(DIR1=`pwd` ; DIR=$${DIR1%/}; cd .. ; \
	cp -pf $${DIR##*/}.tar.gz /a/gatos.tgz )

realclean: clean
	rm -f xatitv atitv yuvsum atitojpg atitogif atitoppm atisplit scanpci bin2c cp850_8x16.inc iso01mod_8x16.inc

clean:
	rm -f libgatos.a *.bck *.o core gatos.yuv libgatos.so.*

$(ARC):	$(OBJS)
	ar rcs $(ARC) $(OBJS)

$(SHAREF): $(OBJS) *.h $(ALL) $(ARC)
	ld -share -soname $(SHAREN) -o $(SHAREF) --whole-archive $(ARC)

xatitv:  xatitv.o $(USELIB) xutils.o $(CURF)
	$(LXX) -o xatitv xatitv.o $(CURF) xutils.o $(XLIBSDIR) $(XLIBS) $(XFREELIBS) \
		$(USELIB) -libtk

xatitv.o: xatitv.cpp gatos.h sound.h cc.h
	$(CXX) $(DEFS) $(CURS) $(INCS) -c xatitv.cpp

atitv:  atitv.o $(USELIB)
	$(LXX) -o atitv atitv.o $(XLIBSDIR) $(XLIBS) $(XFREELIBS) $(USELIB)

atitv.o: atitv.c gatos.h sound.h cc.h
	$(CC) $(DEFS) $(INCS) -c atitv.c

xutils.o: xutils.c xutils.h gatos.h cp850_8x16.inc iso01mod_8x16.inc
	$(CC) -O4 $(DEFS) $(HASH_CHARS) $(INCS) -c xutils.c

cc.o: cc.c gatos.h cc.h
	$(CC) $(DEFS) $(CCFLAG) $(INCS) -c cc.c

ccshow.o: ccshow.c cc.h
	$(CC) $(DEFS) $(CCFLAG) $(INCS) -c ccshow.c

cp850_8x16.inc: bin2c cp850-8x16
	./bin2c cp850-8x16 cp850_8x16 > cp850_8x16.inc

iso01mod_8x16.inc: bin2c iso01mod-8x16
	./bin2c iso01mod-8x16 iso01mod_8x16 > iso01mod_8x16.inc

bin2c: bin2c.c
	$(CC) bin2c.c -o bin2c

gatos.o: gatos.c gatos.h channels.h atiregs.h pci.h i2c.h \
	ati.h tvout.h board.h bt829.h fi12xx.h sound.h saa5281.h
	$(CC) $(DEFS) $(INCS) -c gatos.c

channels.o: channels.c gatos.h channels.h
	$(CC) $(DEFS) $(INCS) -c channels.c

ati.o: ati.c gatos.h ati.h atiregs.h
	$(CC) $(DEFS) $(INCS) -c ati.c

board.o: board.c gatos.h board.h atiregs.h i2c.h bt829.h fi12xx.h
	$(CC) $(DEFS) $(INCS) -c board.c

bt829.o: bt829.c gatos.h bt829.h i2c.h
	$(CC) $(DEFS) $(INCS) -c bt829.c

fi12xx.o: fi12xx.c gatos.h fi12xx.h board.h bt829.h i2c.h
	$(CC) $(DEFS) $(INCS) -c fi12xx.c

sound.o: sound.c gatos.h sound.h
	$(CC) $(DEFS) $(INCS) -c sound.c

saa5281.o: saa5281.c gatos.h saa5281.h i2c.h
	$(CC) $(DEFS) $(INCS) -c saa5281.c

pci.o: pci.c gatos.h pci.h atiregs.h
	$(CC) $(DEFS) $(INCS) -c pci.c

i2c.o: i2c.c gatos.h i2c.h ati.h atiregs.h tvout.h
	$(CC) $(DEFS) $(INCS) -c i2c.c

tvout.o: tvout.c gatos.h tvout.h atiregs.h
	$(CC) $(DEFS) $(INCS) -c tvout.c

delay.o: delay.S
	gcc -D__KERNEL__ -D__ASSEMBLY__ -traditional -c delay.S

yuvsum: yuvsum.c
	$(CC) $(DEFS) $(INCS) -o yuvsum yuvsum.c

atitojpg: atitojpg.c
	$(CC) $(DEFS) $(INCS) -o atitojpg atitojpg.c

atitogif: atitogif.c
	$(CC) $(DEFS) $(INCS) -o atitogif atitogif.c

atitoppm: atitoppm.c
	$(CC) $(DEFS) $(INCS) -o atitoppm atitoppm.c

atisplit: atisplit.c
	$(CC) $(DEFS) $(INCS) -o atisplit atisplit.c

scanpci: scanpci.c
	gcc -o scanpci scanpci.c

install: xatitv atitv yuvsum atitoppm atitogif atitojpg atisplit 
	install -d $(INSTALLDIR)
	install -s -o root -m 04755 xatitv $(INSTALLDIR)
	install -s -o root -m 04755 atitv $(INSTALLDIR)
	install -s yuvsum $(INSTALLDIR)
	install -s atitojpg $(INSTALLDIR)
	install -s atitogif $(INSTALLDIR)
	install -s atitoppm $(INSTALLDIR)
	install -s atisplit $(INSTALLDIR)

install_$(SHAREF): $(SHAREF) install_$(ARC)
	install -d $(INSTLIBDIR)
	install $(SHAREF) $(INSTLIBDIR)
	ln -sf $(SHAREN) $(INSTLIBDIR)/$(SHARE)
	ldconfig

install_$(ARC):	$(ARC)
	install -d $(INSTLIBDIR)
	install $(ARC) $(INSTLIBDIR)
