.PHONY: clean install

prefix:=@prefix@
exec_prefix:=@exec_prefix@
libdir:=@libdir@
srcdir:=@srcdir@
top_builddir:=@top_builddir@
manpage:=@PACKAGE_NAME@

DOCDIR:=doc
MANDIR:=$(prefix)/share/man/man1
INCLUDEDIR:=$(prefix)/include
SUBDIRS:=src \
         po \
         tools

RM:=@RM@
INSTALL:=@INSTALL@
INSTALL_PROGRAM:=@INSTALL_PROGRAM@
INSTALL_DATA:=@INSTALL_DATA@
GZIP:=@GZIP@

@SET_MAKE@
THISFILE:=Makefile

all: build-lib

all: build-app

@PO_YES@all: build-po

build-lib:
	cd src && ${MAKE}

build-app:
	cd tools && ${MAKE}

build-po:
	cd po && ${MAKE}

@SHARED_YES@install: install-lib

install: install-app

install: install-doc

install: install-include

@PO_YES@install: install-po

install-app:
	cd tools && ${MAKE} install

install-include:
	cd include && { \
	for f in minidjvu/*.h minidjvu/*/*.h; do \
	  ${INSTALL} -d ${DESTDIR}${INCLUDEDIR}/$$(dirname $${f}) && \
	  ${INSTALL_DATA} $${f} ${DESTDIR}${INCLUDEDIR}/$$(dirname $${f})/ ; \
	done \
        }

install-lib:
	cd src && ${MAKE} install

install-po:
	cd po && ${MAKE} install

install-doc:
	${INSTALL} -d ${DESTDIR}$(MANDIR)
	${INSTALL_DATA} $(DOCDIR)/$(manpage).1 ${DESTDIR}$(MANDIR)
	${GZIP} -nf -9 ${DESTDIR}$(MANDIR)/$(manpage).1
	for dir in $(DOCDIR)/*/; do \
	  if [ -f $${dir}$(manpage).1 ]; then \
	    LOCMANDIR=${DESTDIR}$(prefix)/share/man$${dir#$(DOCDIR)}/man1 &&\
	    ${INSTALL} -d $${LOCMANDIR} &&\
	    ${INSTALL_DATA} $${dir}$(manpage).1 $${LOCMANDIR} && \
	    ${GZIP} -nf -9 $${LOCMANDIR}/$(manpage).1; \
	  fi; \
	done

clean:
	for d in ${SUBDIRS}; do ( cd $$d && ${MAKE} $@ ); done

distclean:
	for d in ${SUBDIRS}; do ( cd $$d && ${MAKE} $@ ); done
	-find . -name Makefile -exec ${RM} {} \;
	-${RM} config.h config.log config.status

uninstall:
	for d in ${SUBDIRS}; do ( cd $$d && ${MAKE} $@ ); done
	${RM} ${DESTDIR}$(MANDIR)/$(manpage).1.gz
	for dir in $(DOCDIR)/*/; do \
	  if [ -f $${dir}$(manpage).1 ]; then \
	    LOCMANDIR=${DESTDIR}$(prefix)/share/man$${dir#$(DOCDIR)}/man1 &&\
	    ${RM} $${LOCMANDIR}/$(manpage).1.gz; \
	  fi; \
	done

