MACH = $(shell ascmach)

PROGNAME = mpeg_encode
EXENAME  = $(BINDIR)/$(PROGNAME)
JPEGDIR  = /Projects/Common/jpeg-6b
JPEGLIB  = $(JPEGDIR)/$(MACH)/libjpeg.a
INCLUDES = -Iheaders -I$(JPEGDIR)
OBJS     = mfwddct.o postdct.o huff.o bitio.o mheaders.o mpeg.o \
           subsample.o param.o rgbtoycc.o readframe.o combine.o \
           jrevdct.o frame.o fsize.o frametype.o libpnmrw.o \
           specifics.o rate.o opts.o iframe.o pframe.o bframe.o \
           psearch.o bsearch.o block.o noparallel.o jpeg.o main.o

#
# Directories
#
BINDIR = ../bin/$(MACH)
OBJDIR = $(MACH)

#
# Tools
#
CC    = gcc-2.8.1
LD    = $(CC)
MKDIR = mkdir -p
RM    = rm -f

#
# Flags
#
CFLAGS    = -O
CPPFLAGS  = $(INCLUDES)
LDFLAGS   =
LIBS      = $(JPEGLIB)
SYSLIBS   = -lm

###############################################################################
CFLAGS_AIX_4.3        = -O -DAIX

CFLAGS_HP-UX_B.10.20  = -O -DHPUX

CC_IRIX_6.5           = cc
CFLAGS_IRIX_6.5       = -O -o32 -DIRIX
LDFLAGS_IRIX_6.5      = -o32

CC_Linux_2.2.5-15smp      = cc
LDFLAGS_Linux_2.2.5-15smp = -L/usr/X11R6/lib -static
###############################################################################

#
# Override
#
ifneq ($(CC_$(MACH)),)
  CC = $(CC_$(MACH))
endif
ifneq ($(CFLAGS_$(MACH)),)
  CFLAGS = $(CFLAGS_$(MACH))
endif
ifneq ($(LDFLAGS_$(MACH)),)
  LDFLAGS = $(LDFLAGS_$(MACH))
endif
ifneq ($(SYSLIBS_$(MACH)),)
  SYSLIBS = $(SYSLIBS_$(MACH))
endif

###############################################################################
# how to create objects from source files
$(OBJDIR)/%.o: %.c
	$(CC) -c $(CPPFLAGS) $(CFLAGS) -o $@ $<
###############################################################################

.PHONY: all title mkdir clean
.SILENT:

all: title mkdir $(EXENAME)

title:
	echo `uname -n` $(MACH)

mkdir:
	if [ ! -d $(BINDIR) ]; then $(MKDIR) $(BINDIR); fi
	if [ ! -d $(OBJDIR) ]; then $(MKDIR) $(OBJDIR); fi

clean:
	$(RM) $(addprefix $(OBJDIR)/,$(OBJS))
	$(RM) $(EXENAME)

$(EXENAME): $(addprefix $(OBJDIR)/,$(OBJS))
	$(LD) $(LDFLAGS) -o $@ $^ $(LIBS) $(SYSLIBS)
	echo "    Made $(EXENAME)"

test:
	$(RM) /tmp/ts.stat
	$(EXENAME) -stat /tmp/ts.stat ./tst/ts.param >/dev/null
	./tst/diffscript /tmp/ts.stat ./tst/ts.stat /tmp/ts.mpg ./tst/ts.mpg
	$(RM) /tmp/ts.stat /tmp/ts.mpg

testd:
	$(RM) /tmp/tsd.stat
	$(EXENAME) -stat /tmp/tsd.stat ./tst/tsd.param >/dev/null
	./tst/diffscript /tmp/tsd.stat ./tst/tsd.stat /tmp/tsd.mpg ./tst/tsd.mpg
	$(RM) /tmp/tsd.stat /tmp/tsd.mpg
