################################################################################
# Makefile for mdate++                                                         
# $Id: Makefile,v 1.15 2003/09/10 06:04:08 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

VERSION = mdate-1-4-0

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

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

## define nosnprintf=1 if you don't have snprintf, nogetopt=1 if you don't
## have getopt, noi18n if you don't want i18n. This affects what gets built!
 
nosnprintf=
nogetopt=
noi18n=
    
## autodetection of target, linux by default
ifneq ($(strip $(wildcard /usr/include/linux)),)
target = linux
endif
		 
OBJS=mdate.o cmdline.o main.o
		  
ifneq ($(nosnprintf),)
OBJS += snprintf.o 
endif

ifneq ($(nogetopt),)
OBJS += getopt.o getopt1.o
endif
			  
ifneq ($(noi18n),)
LIBS += -lintl
endif

## 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=-O3 -s -Wall -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

###################### END OF USER CONFIGURABLE SECTION ######################

## 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

install: installbin

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

installman: mdate
	[ -d $(MANDIR) ] || mkdir -p $(MANDIR); \
    install -m644 mdate.1 $(MANDIR); \
    gzip $(MANDIR)/mdate.1
		

installdoc: createdoc installman
	[ -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); \
	install -m644  FDL AUTHORS Translators $(DOCDIR); \
	[ -d $(HTMLDIR) ] || mkdir -p $(HTMLDIR); \
	install -m644  *.html $(HTMLDIR)

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

distclean: clean docclean pdfclean

## developers target only
updatecl:
	cvs2cl --gmt -S -b -P --no-wrap --header ChangeLog.header; \
	cvs commit -m "" ChangeLog; \
	cvs tag -F $(VERSION) ChangeLog

clean:
	-rm -f mdate $(OBJS) *.o

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 *~
