SHELL = /bin/sh

CP = @CP@
GZIP = @GZIP@
INSTALL = @INSTALL@
RM = @RM@
STRIP = @STRIP@
TAR = @TAR@

prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = @bindir@
includedir = @includedir@
libdir = @libdir@
mandir = @mandir@
auxdir = ${prefix}/@auxdir@
version = @version@
distdir = xplanet-$(version)

CXX = @CXX@
CXXFLAGS += @OTHER_CXXFLAGS@ @IMLIB_CFLAGS@ @X_CFLAGS@ -I$(includedir)

X_LIBS = @IMLIB_LIBS@ @X_LIBS@ @X_PRE_LIBS@ @X_EXTRA_LIBS@ -lX11

OPENGLLIBS = -lglut -lXmu -lXi @OPENGLLIBS@ 

LIBS = $(OPENGLLIBS) $(X_LIBS) @LIBS@

SRCS = \
	xplanet.cc \
	glutfuncs.cc \
	image.cc \
	location.cc \
	moonpos.cc \
	options.cc \
	ortho.cc \
	planet.cc \
	sunpos.cc \
	text.cc \
	util.cc 

OBJS = $(SRCS:.cc=.o)
AUXDIR = $(notdir $(wildcard auxdir/*.@mapext@ auxdir/*@markerfile@))

.cc.o: 
	$(CXX) -c $(CXXFLAGS) $< 

all: xplanet xplanetbg

xplanet: $(OBJS)
	$(CXX) -o $@ $(OBJS) $(LIBS)

xplanetbg: xplanetbg.o orbit.o
	$(CXX) -o $@ $^ $(X_LIBS)

xplanet.o: xplanet.cc auxfiles.h image.h options.h planet.h text.h util.h

glutfuncs.o: glutfuncs.cc xplanet.h image.h

image.o: image.cc location.h options.h planet.h text.h util.h xplanet.h

location.o: location.cc location.h util.h

moonpos.o: moonpos.cc util.h

options.o: options.cc options.h checkfuncs.h util.h xplanet.h 

ortho.o: ortho.cc location.h options.h util.h

planet.o: planet.cc planet.h util.h

sunpos.o: sunpos.cc util.h

text.o: text.cc image.h location.h options.h planet.h text.h util.h xplanet.h

util.o: util.cc 

xplanetbg.o: xplanetbg.cc checkfuncs.h orbit.h 

orbit.o: orbit.cc orbit.h

install:
	$(INSTALL) -d $(bindir)
	$(INSTALL) xplanet $(bindir)
	$(INSTALL) xplanetbg $(bindir)
	$(INSTALL) -m 755 tkxplanet $(bindir)
	$(INSTALL) -d $(mandir)
	$(INSTALL) -d $(mandir)/man1
	$(INSTALL) -m 644 xplanet.1 $(mandir)/man1
	$(INSTALL) -m 644 xplanetbg.1 $(mandir)/man1
	$(INSTALL) -d $(auxdir)
ifneq ($(AUXDIR),)
	$(INSTALL) -m 644 $(addprefix auxdir/,$(AUXDIR)) $(auxdir)
endif

install-strip: install
	$(STRIP) $(bindir)/xplanet
	$(STRIP) $(bindir)/xplanetbg

uninstall:
	$(RM) $(bindir)/xplanet
	$(RM) $(bindir)/xplanetbg
	$(RM) $(bindir)/tkxplanet
	$(RM) $(mandir)/man1/xplanet.1
	$(RM) $(mandir)/man1/xplanetbg.1
ifneq ($(AUXDIR),)
	$(RM) $(addprefix $(auxdir)/, $(AUXDIR))
endif

dist: 	
	$(INSTALL) -d $(distdir)
	$(CP) BUGS COPYING CREDITS ChangeLog INSTALL Makefile* README *.cc \
	*.h *.h.in tkxplanet* aclocal.m4 configure* *.1 $(distdir)
	$(CP) -r autoconf $(distdir)
ifneq ($(AUXDIR),)
	$(INSTALL) -d $(distdir)/auxdir
	$(CP) -r auxdir/*.txt $(distdir)/auxdir	
endif
	$(TAR) cf $(distdir).tar $(distdir)
	$(GZIP) -9 $(distdir).tar
	$(RM) -fr $(distdir)

clean:
	$(RM) *.o 

distclean:
	$(RM) *.o auxfiles.h checkfuncs.h xplanet xplanetbg config.* \
	tkxplanet Makefile
