# INCLUDE VERSION NUMBER FROM FIRST NUMBER IN 'CHANGES.txt' FILE
SHELL=/bin/sh
VERSION=$(shell awk '/^[0-9]*\.[^ \t]/ {print $$1; exit(0);}' CHANGES.txt)

FLTK_DIR=/usr/local/src/fltk-1.3.x-svn

# FLTK BUILD 
CXX      = $(shell $(FLTK_DIR)/fltk-config --cxx)
CC       = $(shell $(FLTK_DIR)/fltk-config --cxx)
DEBUG    = -g
CXXFLAGS = $(shell $(FLTK_DIR)/fltk-config --cxxflags ) -I. -DVERSION=\"$(VERSION)\" -Wall
CFLAGS   = -Wall
LDLIBS   = $(shell $(FLTK_DIR)/fltk-config --ldstaticflags )
LINK     = $(CXX)
OS       = $(shell uname -s)

ifeq ($(shell uname -s ),Darwin)
    CXX += -arch i386 -arch ppc
    CC  += -arch i386 -arch ppc
    LDFLAGS  += -arch i386 -arch ppc
endif

# HOW TO BUILD
.SUFFIXES: .o .C

%.o: %.C %.H
	$(CXX) $(CXXFLAGS) $(DEBUG) -c $<

all: flruler docs

flruler: flruler.o
	$(LINK) flruler.o -o flruler $(LDLIBS) 
	strip flruler
ifeq ($(OS),Darwin)
	rm -rf flruler.app
	mkdir -p                   flruler.app/Contents/MacOS
	mkdir -p                   flruler.app/Contents/Resources
	cp misc/flruler-Info.plist flruler.app/Contents/Info.plist
	cp misc/flruler.icns       flruler.app/Contents/Resources/icon.icns
	echo APPLnone            > flruler.app/Contents/PkgInfo
	cp flruler                 flruler.app/Contents/MacOS
endif

# MAKE DOCUMENTATION
docs: FORCE
	( cd docs && \
          ( echo 'g/Version [0-9]*\.[0-9]*/s//Version '$(VERSION)'/g'; echo wq) | ex flruler.pod; \
	  pod2man --center="Erco's FLTK Tools" \
	          --name=flruler \
		  --section=1 \
		  --release=$(VERSION) \
		  flruler.pod > flruler.1 && \
	  echo "*** Built docs/flruler.1" && \
	  pod2html --title "flruler $(VERSION) Erco's FLTK Tools" \
	          flruler.pod > flruler.html && \
	  echo "*** Built docs/flruler.html" \
	)

# BUILD CLEAN UP
clean:
	-rm -f flruler
	-rm -f *.o
	-rm -f core
	-rm -f core.*
	-rm -f *.idb
	-rm -f *.pdb
	-rm -f *.obj
	-rm -f *.exe
	-rm -f *.rc
	-rm -f *.res
	-rm -f *.RES
	-rm -rf docs/pod2htm*
	-rm -rf docs/*.z
	-rm -rf docs/*.Z
	-rm -rf ._[A-z]*
	-rm -f flruler.app/Contents/MacOS/flruler   2> /dev/null
	-rm -f flruler.app/Contents/MacOS/._flruler 2> /dev/null

# DISTRO CLEANUP
distclean: clean
	-rm -rf bin.*
	-rm -rf reldir

###############################################################
### ERCO'S USE ONLY                                         ###
### These targets are ONLY used by the author for managing  ###
### the distro website.                                     ###
###############################################################

# BUILD SRC TAR FILE
tar: docs FORCE
	./distro-helper -tar $(VERSION)

# SEND SRC TAR
sendtar: tar docs FORCE
	./distro-helper -sendtar $(VERSION)

# SEND + BUILD DISTRO WEBPAGE
sendwww: docs FORCE
	./distro-helper -sendwww $(VERSION)

# CREATE BINARY DISTROS
bins: FORCE
	./distro-helper -makebins $(VERSION)

# SEND BINARY DISTROS
sendbins: bins FORCE
	./distro-helper -sendbins $(VERSION)

# SEND ALL
#    Rebuilds everything and updates website
#
sendall: distclean sendtar sendwww sendbins

# MAKE VERBOSITY
#.SILENT:

FORCE:
	
