######################################################################
# Take over variables from configure
######################################################################

VERSION = $(CFG_VERSION)

SRCDIR = $(CFG_SRCDIR)
PREFIX = $(CFG_PREFIX)
BINDIR = $(CFG_BINDIR)

EFENCE_LFLAGS = $(CFG_EFENCE_LFLAGS)
EFENCE_LIBS   = $(CFG_EFENCE_LIBS)

BZ2_INCL   = $(CFG_BZ2_INCL)
BZ2_LFLAGS = $(CFG_BZ2_LFLAGS)
BZ2_LIBS   = $(CFG_BZ2_LIBS)

PNG_INCL   = $(CFG_PNG_INCL)
PNG_LFLAGS = $(CFG_PNG_LFLAGS)
PNG_LIBS   = $(CFG_PNG_LIBS)

SYS_OPTIONS  = $(CFG_SYS_OPTIONS)
HAVE_OPTIONS = $(CFG_HAVE_OPTIONS)
WITH_OPTIONS = $(CFG_WITH_OPTIONS)
EXE_SUFFIX   = $(CFG_EXE_SUFFIX)

LOCATIONS = -DSRCDIR=\"$(SRCDIR)\" -DBINDIR=\"$(BINDIR)\" -DDOCDIR=\"$(DOCDIR)\" -DLOCALEDIR=\"$(LOCALEDIR)\"
COPTS = $(CFLAGS) $(LOCATIONS) $(SYS_OPTIONS) $(HAVE_OPTIONS) $(WITH_OPTIONS) $(BZ2_INCL) $(PNG_INCL)
LOPTS = $(LDFLAGS) $(BZ2_LFLAGS) $(PNG_LFLAGS)

LIBS = $(EFENCE_LIBS) $(BZ2_LIBS) $(PNG_LIBS) -lm

CFILES = $(CFG_CFILES)
OFILES = $(CFG_OFILES)

######################################################################
# Compilation related
######################################################################

.PHONY : show

.c.o:
	@echo "Compiling:" $*.c
	@$(CC) $(COPTS) -DPNGPACK  -c $*.c

# Note that we build a self-contained (statically linked) pngpack.exe
# under Windows so that we do not have to ship the .dll files for
# bzip2, libpng, and zlib.

pngpack: $(OFILES)
	@echo "Linking  : pngpack";
	@if test $(CFG_SYS_OPTIONS) == -DSYS_MINGW; \
	then $(CC) $(LOPTS) $(OFILES) $(LIBS) -lz --static -o pngpack; \
	else $(CC) $(LOPTS) $(OFILES) $(LIBS) -o pngpack; \
	fi

show: 
	@echo -e "Current build configuration in ./Makefile:\n" 
	@echo "VERSION      = " $(VERSION)
	@echo "CC           = " $(CC)
	@echo "MAKE         = " $(MAKE)
	@echo "SHELL        = " $(SHELL)
	@echo "SRCDIR       = " $(SRCDIR)
	@echo "PREFIX       = " $(PREFIX)
	@echo "BINDIR       = " $(BINDIR)
	@echo
	@echo "EFENCE_LFLAGS= " $(EFENCE_LFLAGS)
	@echo "EFENCE_LIBS  = " $(EFENCE_LIBS)
	@echo
	@echo "PNG_LFLAGS   = " $(PNG_LFLAGS)
	@echo "PNG_LIBS     = " $(PNG_LIBS)
	@echo
	@echo "SYS_OPTIONS  = " $(SYS_OPTIONS)
	@echo "EXE_SUFFIX   = " $(EXE_SUFFIX)
	@echo "HAVE_OPTIONS = " $(HAVE_OPTIONS)
	@echo "WITH_OPTIONS = " $(WITH_OPTIONS)
	@echo
	@echo "CFLAGS       = " $(CFLAGS)
	@echo "COPTS        = " $(COPTS)
	@echo "LDFLAGS      = " $(LDFLAGS)
	@echo "LOPTS        = " $(LOPTS)
	@echo "LIBS         = " $(LIBS)


######################################################################
# Distribution management
######################################################################

.PHONY : clean distclean

distclean:
	@echo "Removing local .png and .pngpack files in ./tools"
	@rm -f *.png
	@echo "Removing symbolic links in ./tools"
	@rm -f md5.h md5.c
	@echo "Removing configuration files in ./tools"
	@rm -f configure.log Makefile.config Makefile
	@echo "all:" >>Makefile
	@echo -e "\t@echo" >>Makefile
	@echo -e "\t@echo \"Please create a Makefile by entering \\\"./configure\\\" first\"" >>Makefile
	@echo -e "\t@echo" >>Makefile

clean:
	@echo "Removing rebuildable files in ./tools"
	@rm -rf *.o pngpack *.exe *.dll *.dll.a core *.stackdump
	@find . -name \*\~ -print | xargs rm -f;
