#
# Copyright (c) 2001, Sasha Vasko <sasha at aftercode.net>
# Copyright (c) 1998, Guylhem AZNAR <guylhem@oeil.qc.ca>
#

subdirs = libjpeg libpng libungif zlib

LIBJPEG_OBJS = 	libjpeg/jcapimin.o libjpeg/jcapistd.o libjpeg/jccoefct.o \
		libjpeg/jccolor.o libjpeg/jcdctmgr.o libjpeg/jchuff.o \
		libjpeg/jcinit.o libjpeg/jcmainct.o libjpeg/jcmarker.o \
		libjpeg/jcmaster.o libjpeg/jcomapi.o libjpeg/jcparam.o \
		libjpeg/jcphuff.o libjpeg/jcprepct.o libjpeg/jcsample.o \
		libjpeg/jctrans.o libjpeg/jdapimin.o libjpeg/jdapistd.o \
		libjpeg/jdatadst.o libjpeg/jdatasrc.o libjpeg/jdcoefct.o \
		libjpeg/jdcolor.o libjpeg/jddctmgr.o libjpeg/jdhuff.o \
		libjpeg/jdinput.o libjpeg/jdmainct.o libjpeg/jdmarker.o \
		libjpeg/jdmaster.o libjpeg/jdmerge.o libjpeg/jdphuff.o \
		libjpeg/jdpostct.o libjpeg/jdsample.o libjpeg/jdtrans.o \
		libjpeg/jerror.o libjpeg/jfdctflt.o libjpeg/jfdctfst.o \
		libjpeg/jfdctint.o libjpeg/jidctflt.o libjpeg/jidctfst.o \
		libjpeg/jidctint.o libjpeg/jidctred.o libjpeg/jquant1.o \
		libjpeg/jquant2.o libjpeg/jutils.o libjpeg/jmemmgr.o libjpeg/jmemnobs.o

LIBPNG_OBJS = 	libpng/png.o libpng/pngset.o libpng/pngget.o libpng/pngrutil.o \
		libpng/pngtrans.o libpng/pngwutil.o libpng/pngread.o libpng/pngrio.o \
		libpng/pngwio.o libpng/pngwrite.o libpng/pngrtran.o \
		libpng/pngwtran.o libpng/pngmem.o libpng/pngerror.o \
		libpng/pngpread.o libpng/pnggccrd.o

ZLIB_OBJS = 	zlib/adler32.o 	zlib/compress.o zlib/crc32.o \
		zlib/gzio.o 	zlib/uncompr.o 	zlib/deflate.o \
		zlib/trees.o 	zlib/zutil.o 	zlib/inflate.o \
		zlib/infback.o 	zlib/inftrees.o zlib/inffast.o

LIBUNGIF_OBJS = libungif/dgif_lib.o libungif/egif_lib.o libungif/gifalloc.o libungif/gif_err.o

AFTERIMAGE_OBJS= @AFTERBASE_C@ asimage.o ascmap.o asfont.o asimagexml.o asstorage.o \
		asvisual.o blender.o bmp.o char2uni.o export.o imencdec.o import.o \
		pixmap.o transform.o ungif.o xcf.o ximage.o xpm.o

################################################################
# library specifics :

LIB_INCS= afterimage.h config.h afterbase.h ascmap.h asfont.h \
		asimage.h asimagexml.h asvisual.h blender.h bmp.h char2uni.h \
		export.h imencdec.h import.h pixmap.h transform.h ungif.h \
		xcf.h ximage.h xpm.h

LIB_OBJS=       @JPEG_OBJS@ @ZLIB_OBJS@ @PNG_OBJS@ @UNGIF_OBJS@ $(AFTERIMAGE_OBJS)

APPS_SRCS=apps/common.c apps/ascompose.c apps/asview.c \
		  apps/asscale.c apps/astile.c apps/asmerge.c \
		  apps/asgrad.c apps/asflip.c apps/astext.c

APPS_INCS=apps/common.h

# end specifics
################################################################
# generic makefile stuff :

LIB_DIR_NAME    = libAfterImage
LIB_NAME        = LIBAFTERIMAGE
LIB_STATIC      = libAfterImage.a
LIB_SHARED      = libAfterImage.so
LIBVER          = 0.99
LIBVERMAJOR     = 0

CC		= @CC@
CCFLAGS         = @CFLAGS@
# -march=pentiumpro -mcpu=pentiumpro
EXTRA_DEFINES	= @DEFINE_XLOCALE@

RANLIB		= @RANLIB@
AR		= ar clq
CP		= @CP@
MV		= @MV@
RM		= @RM@
RMF		= @RM@ -f
MKDIR		= @MKDIR@
FIND		= @FIND@
XARGS		= @XARGS@
LDCONFIG	= @LDCONFIG@
ROBODOC     = robodoc

YACC		= @YACC@
LEX		= @LEX@
YACCFLAGS	= -d
LEXFLAGS	=

INSTALL		= @INSTALL@
INSTALL_PROGRAM	= @INSTALL@ -s -m 755
INSTALL_DATA	= @INSTALL@ -m 644
INSTALL_HEADER	= @INSTALL@ -m 644
INSTALL_LIB	= @INSTALL@ -m 755
INSTALL_SCRIPT	= @INSTALL@ -m 755

INCS_EXTRA	= @XPM_CFLAGS@ @JPEG_CFLAGS@ @PNG_CFLAGS@ @GIF_CFLAGS@ @TIFF_CFLAGS@ @TTF_INCLUDES@
INCS_X		= @X_CFLAGS@
INCS_PRIVATE    = 
INCLUDES	= $(INCS_EXTRA) $(INCS_PRIVATE) $(INCS_X)

USER_LD_FLAGS	= @user_ldflags@
LIBS_X		= @x_libs@
LIBS_XEXTS	=
LIBS_AFTERIMAGE = @AFTERIMAGE_APPS_LIBS@
LIBRARIES	= $(USER_LD_FLAGS) $(LIBS_AFTERIMAGE) $(LIBS_X) $(LIBS_XEXTS)

prefix          = @prefix@
exec_prefix     = @exec_prefix@
LIBDIR          = $(DESTDIR)@libdir@
INCLUDEDIR      = $(DESTDIR)@includedir@
AFTER_INC_DIR	= $(DESTDIR)@includedir@/$(LIB_DIR_NAME)
AFTER_BIN_DIR	= $(DESTDIR)@bindir@
AFTER_MAN_DIR	= $(DESTDIR)@mandir@/man3
AFTER_APP_MAN_DIR	= $(DESTDIR)@mandir@/man1
AFTER_SHAREDIR	= $(DESTDIR)@datadir@/$(LIB_DIR_NAME)
AFTER_DOC_DIR	= $(AFTER_SHAREDIR)/doc

all:            @LIBPROG@ $(INSTALLONBUILD) apps

install.bin:

install.lib:	@LIBPROG@ @LIBINSTALL@ install.inc

install.static:
		$(INSTALL_SCRIPT) afterimage-libs afterimage-config $(AFTER_BIN_DIR)
		@( \
		if [ -d $(LIBDIR) ] && [ -w $(LIBDIR) ]; then \
		    echo "$(INSTALL_LIB) $(LIB_STATIC) $(LIBDIR)"; \
		    $(INSTALL_LIB) $(LIB_STATIC) $(LIBDIR); \
		    if [ `uname` = "Linux" ]; then \
			if test $(LIBDIR) = "/lib" || test $(LIBDIR) = "/usr/lib"; then \
		    	    echo "" > /dev/null; \
			elif grep -q $(LIBDIR) /etc/ld.so.conf > /dev/null 2>&1; then \
		    	    echo "" > /dev/null; \
			else \
		    	    echo "Unable to find $(LIBDIR) in ld.so.conf. In order to use "; \
		    	    echo "$(LIB_STATIC), you may need to add it or set LD_LIBRARY_PATH."; \
			fi; \
			if test -w /etc; then \
			    echo "$(LDCONFIG)"; \
			    $(LDCONFIG); \
			fi; \
		    fi ;\
		fi \
		)

install.script:

install.dyn:
		$(INSTALL_LIB) $(LIB_SHARED).$(LIBVER) $(LIBDIR)
		$(INSTALL_SCRIPT) afterimage-libs afterimage-config $(AFTER_BIN_DIR)
		-@/bin/rm -f $(LIBDIR)/$(LIB_SHARED).$(LIBVERMAJOR) $(LIBDIR)/$(LIB_SHARED)
		ln -sf $(LIB_SHARED).$(LIBVER) $(LIBDIR)/$(LIB_SHARED).$(LIBVERMAJOR)
		ln -sf $(LIB_SHARED).$(LIBVERMAJOR) $(LIBDIR)/$(LIB_SHARED)
		@( \
		 if test `uname` = "Linux"; then \
		   if test $(LIBDIR) = "/lib" || test $(LIBDIR) = "/usr/lib"; then \
		     echo "" > /dev/null; \
		   elif grep -q $(LIBDIR) /etc/ld.so.conf > /dev/null 2>&1; then \
		     echo "" > /dev/null; \
		   else \
		     echo "Unable to find $(LIBDIR) in ld.so.conf. In order to use "; \
		     echo "$(LIB_SHARED), you may need to add it or set LD_LIBRARY_PATH."; \
		   fi; \
		   echo "$(LDCONFIG)"; \
		   $(LDCONFIG); \
		 fi \
		)

install.inc:
		@( \
		 if [ -d $(AFTER_INC_DIR) ]; then \
		    echo "$(RM) $(AFTER_INC_DIR)/*"; \
		    $(RM) $(AFTER_INC_DIR)/*; \
		 else \
		    if [ -d $(INCLUDEDIR) ]; then \
		       echo "$(MKDIR) $(AFTER_INC_DIR)"; \
		       if $(MKDIR) $(AFTER_INC_DIR); then \
		          echo " ">/dev/null; \
		       else \
		    	  echo "failed to create include directory: $(AFTER_INC_DIR)"; \
		       fi; \
		    else \
		       echo "$(MKDIR) $(INCLUDEDIR)"; \
		       if $(MKDIR) $(INCLUDEDIR); then \
		          echo "$(MKDIR) $(AFTER_INC_DIR)"; \
		          if $(MKDIR) $(AFTER_INC_DIR) >/dev/null; then \
		             echo " ">/dev/null; \
		          else \
		             echo "failed to create include directory: $(AFTER_INC_DIR)"; \
			  fi; \
		       else \
		          echo "failed to create include directory: $(INCLUDEDIR)"; \
		       fi; \
		    fi; \
		 fi; \
		 if [ -d $(AFTER_INC_DIR) ]; then \
		    for file in $(LIB_INCS) ; do  \
			echo "$(INSTALL_HEADER) $$file $(AFTER_INC_DIR)"; \
			$(INSTALL_HEADER) $$file $(AFTER_INC_DIR); \
		    done; \
		 fi \
		)

install.apps: install.lib
		cd apps; make install; cd ..

uninstall.bin:

uninstall.lib:

uninstall.man:

uninstall.script:

clean:
		$(RMF) $(LIB_SHARED) $(LIB_STATIC) *.so.* *.so *.o *~ *% *.bak \#* core ; \
		for I in ${subdirs}; do $(RMF) $$I/*.o $$I/*.obj $$I/*.bak; done; \
		cd apps; make clean; cd ..

distclean:
		$(RMF) $(LIB_SHARED) $(LIB_STATIC)  *.o *.so.* *~ *% *.bak \#* *.orig core Makefile ; \
		cd apps; make distclean; cd ..

indent:
		@SRCS=`echo "$(AFTERIMAGE_OBJS) " | sed "s/.o /.c /g"`; \
		if test "x$$SRCS" == "x"; then exit; fi; \
		for i in $$SRCS; do \
		  if (indent -di14 -ts4 -i4 -l120 -lc80 -bad -nbbb -bli0 -c48 -cd48 -ce -cli1 -ncs -nbs -nbbo -hnl < $$i > /tmp/$$i); then \
		    echo "indent $$i"; \
		    mv /tmp/$$i $$i; \
		  fi; \
		done ; \
		cd apps; make indent; cd ..

deps:
		echo -n > .depend ; \
		buf="" ; \
		SRCS=`echo "$(AFTERIMAGE_OBJS) " | sed "s/.o /.c /g"`; \
		echo -n $(LIB_NAME)_OBJS = > .shared ; \
		for file in $$SRCS; do \
		  echo " \\" >> .shared ; \
		  echo -n '		$$('$(LIB_NAME)_PATH')'/$$file >> .shared ; \
		  if test "x$$buf" != "x"; then \
		      echo $$buf >>.depend ; \
		      echo >>.depend ; \
		  fi; \
		  buf="./"`echo "$$file " | sed "s/.c /.o /g"`: ; \
		  for d in `grep "#include \"" < $$file | awk -F \" '{print $$2}'`; do \
		      if test "x$$buf" != "x"; then \
		         echo $$buf \\ >>.depend ; \
		      fi; \
		      echo -n "		" >>.depend ; \
		      buf="$$d "; \
		  done; \
		done; \
		if test "x$$buf" != "x"; then \
		  echo $$buf >>.depend ; \
		fi; \
		echo "">> .shared ; \
		echo "">> .shared ; \
		echo -n $(LIB_NAME)_INCS = >> .shared ; \
		for f in $(LIB_INCS); do \
		  echo " \\" >> .shared ; \
		  echo -n '		$$('$(LIB_NAME)_PATH')'/$$f >> .shared ; \
		done; \
		echo "" >> .shared ; \
		cd apps; make deps; cd ..

apps: 	@LIBPROG@ $(LIB_OBJS) $(LIB_INCS) $(APPS_SRCS) $(APPS_INCS)
		cd apps; make ; cd ..


$(LIB_STATIC):	$(LIB_OBJS) $(LIB_INCS) config.h
		$(RMF) $(LIB_STATIC)
		$(AR) $(LIB_STATIC) $(LIB_OBJS)
		$(RANLIB) $(LIB_STATIC)

test_asstorage:
		$(CC) $(CCFLAGS) $(EXTRA_DEFINES) -DTEST_ASSTORAGE $(INCLUDES) $(EXTRA_INCLUDES) -c asstorage.c -o asstorage_test.o
		$(CC) asstorage_test.o $(LIBRARIES) $(EXTRA_LIBRARIES) -o asstorage_test



.c.o:
		$(CC) $(CCFLAGS) $(EXTRA_DEFINES) $(INCLUDES) $(EXTRA_INCLUDES) -c $*.c -o $@

.c.s:
		$(CC) $(CCFLAGS) $(EXTRA_DEFINES) $(INCLUDES) $(EXTRA_INCLUDES) -S $*.c

#
# include dependency files if they exist
#
# this is merely a precaution, but as it does not work with ansi make
# we took it out
#ifneq ($(wildcard .depend),)
include .depend
#endif

$(LIB_SHARED): $(LIB_SHARED).$(LIBVERMAJOR)
	ln -sf $(LIB_SHARED).$(LIBVERMAJOR) $(LIB_SHARED)

$(LIB_SHARED).$(LIBVERMAJOR): $(LIB_SHARED).$(LIBVER)
	ln -sf $(LIB_SHARED).$(LIBVER) $(LIB_SHARED).$(LIBVERMAJOR)

$(LIB_SHARED).$(LIBVER): $(LIB_OBJS) $(LIB_INCS) config.h
	$(CC) -shared -Wl,-soname,$(LIB_SHARED).$(LIBVERMAJOR) -o $(LIB_SHARED).$(LIBVER) \
	 $(LIB_OBJS)

install.man:
		@if [ -d doc/man ] ; then \
			cd doc/man; \
			for f in `ls *.man`; do \
				page_name=`echo $$f| awk -F . '{print $$1}'`; \
	  			echo $(INSTALL_DATA) $$page_name.man $(AFTER_MAN_DIR)/$$page_name.1x; \
	  			$(INSTALL_DATA) $$page_name.man $(AFTER_MAN_DIR)/$$page_name.1x; \
			done; cd ../..; \
		fi
			

install.doc:
		@if [ -d doc/html ] ; then \
			if [ ! -d $(AFTER_DOC_DIR)/html ] ; then \
				$(MKDIR) -p $(AFTER_DOC_DIR)/html; \
			fi; \
			cd doc/html; \
			for f in `ls *.html`; do \
		  		echo $(INSTALL_DATA) $$f $(AFTER_DOC_DIR)/html/$$f; \
		  		$(INSTALL_DATA) $$f $(AFTER_DOC_DIR)/html/$$f; \
			done; \
			cd ../..; \
		fi

install:	install.lib install.man install.doc install.apps


uninstall:	uninstall.lib

