################################################################################
# Makefile for mdate++                                                         
# $Id: Makefile,v 1.5 2003/08/26 01:35:25 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.
# I can't guarantee the other targets will work, I haven't used Cygwin for
# quite a while now, it's probably out of date. Fixes would be most welcome.
# The mingw32 does NOT work.
#
# 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: .c .cc .exe .o .obj

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

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

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

## Default compiler. On Debian, we're up to gcc-3.3, so check your cflags!
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.
## If you're not sure about your -march or -mcpu, try uname -a: it will tell
## you what cpu your kernel supports. Also see /proc/cpuinfo.
ifeq ($(target),linux)
# for athlons. A Pentium II or above is i686.
	CFLAGS += -march=athlon
# an example for my net box AMD K6-2
#	CFLAGS += -march=i386 -mcpu=i586
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 updatecl

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 pdfclean

updatecl:
	cvs2cl --gmt -S --branches --no-wrap --header ChangeLog.header

clean:
	-rm -f mdate $(OBJS)

pdfclean:
	-rm -f *.log *.out *.aux *.tex *.toc *.pdf

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