################################################################################
# Makefile for mdate++                                                         
# $Id: Makefile,v 1.2 2002/08/16 03:40:00 ewe2 Exp $
#
# Usage:
#		make target=linux		# linux
#		make target=cygwin		# cygwin 1.3.x
#		make target=no-cygwin	# mingw32 for cygwin 1.3.x
#
# The default target is linux on x86 with a pentium-class processor.
#
# If the CFLAGS bother you, please dont change -ffloat-store which avoids
# architecture-related problems.
#
# If you want to add a needed target for your OS/distribution, go ahead but
# please email me (Sean Dwyer <ewe2@can.org.au>) and include a patch of your
# changes so everyone can share!
#
# You may also want to check the documentation targets if you don't need some
# formats: grep for createdoc.
################################################################################

## setup

.SUFFIXES:
.SUFFIXES: .c .cc .exe .o .obj

## install root directory, you need to change this!
DESTDIR = /usr/local

## autodetection of target
ifneq ($(strip $(wildcard /usr/include/linux)),)
target = linux
endif

OBJS=mdate.o cmdline.o main.o snprintf.o getopt.o getopt1.o

## Default compiler.
CXX=g++
CC=gcc
INC=-I/usr/include -I.
DEFS=-DHAVE_CONFIG_H
CFLAGS=-O2 -Wall -fomit-frame-pointer -ffloat-store $(DEFS)
CXXFLAGS=$(CFLAGS) -fno-rtti
LIBS=-lm

## linux Target.
ifeq ($(target),linux)
	CFLAGS += -march=i386 -mcpu=i686
endif

## cygwin 1.3.x target
ifeq ($(target),cygwin)
	CXXFLAGS += -march=i386 -mcpu=i586 $(INC) -I/usr/include/g++-3/
	CC += $(INC)
endif

## mingw32 under cygwin 1.3.x
## this one still crashes the stack. any ideas?
ifeq ($(target),no-cygwin)
	CXXFLAGS += -mno-cygwin -march=i386 -mcpu=i586 $(INC) -I/usr/include/g++-3/
	CC += $(INC)
endif

## main targets

all: mdate

.PHONY: clean docclean

mdate: $(OBJS) $(LIBS)
	$(CXX) $(CFLAGS) $(INC) -o mdate $(OBJS) $(LIBS)

## documentation targets

createdoc: mdate.pdf mdate.html mdate.tex mdate.dvi mdate.ps mdate.text

mdate.html:	mdate.sgml
	sgml2html -H header -F footer mdate.sgml

mdate.text:
	sgml2txt mdate.sgml
	mv mdate.txt mdate.text
	
mdate.tex: mdate.sgml
	sgml2latex -o tex $<
	grep -v "{t1enc}" $@ > $@.tmp
	mv $@.tmp $@

mdate.dvi: mdate.tex
	latex $<
	latex $<
	
mdate.ps: mdate.dvi
	dvips -o $@ $<

mdate.pdf: mdate.tex
	pdflatex $<
	pdflatex $<
	pdflatex $<

## install targets

BINDIR = $(DESTDIR)/bin
MANDIR = $(DESTDIR)/share/man/man1
DOCDIR = $(DESTDIR)/share/doc/mdate
HTMLDIR = $(DOCDIR)/html

install: installbin installdoc

installbin: mdate
	@[ -d $(BINDIR) ] || mkdir -p $(BINDIR); \
	install -m755 mdate $(BINDIR)/mdate

installdoc: createdoc installbin
	[ -d $(DOCCDIR) ] || mkdir -p $(DOCCDIR); \
	install -m644  mdate.dvi mdate.ps mdate.sgml $(DOCDIR); \
	gzip $(DOCDIR)/mdate.dvi $(DOCDIR)/mdate.ps $(DOCDIR)/mdate.sgml; \
	install -m644 mdate.text $(DOCDIR)/mdate.text; \
	gzip $(DOCDIR)/mdate.text ; \
	install -m644  README NEWS Changelog GPL API $(DOCDIR); \
	[ -d $(HTMLDIR) ] || mkdir -p $(HTMLDIR); \
	install -m644  *.html $(HTMLDIR); \
	[ -d $(MANDIR) ] || mkdir -p $(MANDIR); \
	install -m644 mdate.1 $(MANDIR); \
	gzip $(MANDIR)/mdate.1
	

## dependencies
mdate.o: mdate.cc config.h mdate.hh version.h
cmdline.o: cmdline.cc config.h mdate.hh cmdline.h getopt.h version.h
main.o: main.cc config.h mdate.hh cmdline.h getopt.h version.h
getopt.o: getopt.c config.h getopt.h version.h
getopt1.o: getopt1.c config.h getopt.h version.h
snprintf.o: snprintf.c config.h version.h

distclean: clean docclean

clean:
	-rm -f mdate $(OBJS)

docclean:
	-rm -f *.html *.ps *.aux *.log *.ps *.tex *.toc *.dvi *.pdf *.out *.bak \
	*.text *.txt *~
