# Makefile for compiling, testing, and installing Mathomatic under any UNIX-like system.
# Remove the -DUNIX define in CFLAGS when not using Mathomatic for desktop UNIX/Linux.
# This makefile does not compile and install the Prime Number Tools in the "primes" directory.
# This makefile does create and install most of the Mathomatic documentation.

SHELL		= /bin/sh # from http://www.gnu.org/prep/standards/
CC		?= gcc # C compiler to use
INSTALL		?= install # installer to use
INSTALL_PROGRAM	?= $(INSTALL) # command to install program files
INSTALL_DATA	?= $(INSTALL) -m 0644 # command to install data files

VERSION		= `cat VERSION`
OPTFLAGS	?= -O -Wall -Wshadow -Wno-char-subscripts # default gcc specific flags, not required to compile
CFLAGS		+= $(OPTFLAGS)
CFLAGS		+= -DUNIX -DVERSION=\"$(VERSION)\"
LDLIBS		+= -lm # libraries to link with to create the Mathomatic executable

# "make READLINE=1" to include readline support:
CFLAGS		+= $(READLINE:1=-DREADLINE)
LDLIBS		+= $(READLINE:1=-lreadline -lncurses)

# Uncomment the following to force generation of 64bit x86-64 code:
#CFLAGS		+= -m64
#LDFLAGS	+= -m64

# Install directories follow; installs everything in /usr/local by default:
prefix		?= /usr/local
bindir		?= $(prefix)/bin
mandir		?= $(prefix)/share/man
docdir		?= $(prefix)/share/doc
mathdocdir	?= $(docdir)/mathomatic

# Mathomatic program names (can be changed):
AOUT		= mathomatic
M4SCRIPTNAME	= matho
M4SCRIPTPATH	= $(bindir)/$(M4SCRIPTNAME)

INCLUDES	= includes.h license.h am.h externs.h complex.h proto.h altproto.h
OBJECTS		= main.o globals.o am.o solve.o help.o parse.o cmds.o simplify.o \
		  factor.o super.o unfactor.o poly.o diff.o integrate.o \
		  complex.o complex_lib.o list.o gcd.o factor_int.o

PRIMES_MANHTML	= doc/matho-primes.1.html doc/matho-pascal.1.html doc/matho-sumsq.1.html \
		  doc/primorial.1.html doc/matho-mult.1.html doc/matho-sum.1.html
# HTML man pages to make:
MANHTML		= doc/mathomatic.1.html $(PRIMES_MANHTML)

# Flags to make HTML man pages with rman:
RMANOPTS	= -f HTML -r '%s.%s.html'

# man pages to install:
MAN1		= mathomatic.1
# Text files to install:
DOCS		= VERSION AUTHORS COPYING README.txt changes.txt

PDFDOC		= doc/manual.pdf

all: $(AOUT) html # make these by default

doc html: $(MANHTML)

# Run "make test" to see if the resulting mathomatic executable runs properly on your system.
# It does a diff between the output of the test and the expected output.
# If no differences are reported, "All tests passed" is displayed.
check test:
	@echo Testing ./$(AOUT)
	cd tests && time ../$(AOUT) -t all 0<&- >test.out && diff -u all.out test.out && rm test.out
	@echo All tests passed.

# "make baseline" generates the expected output file for "make test".
# Do not run this unless you are sure Mathomatic is working correctly
# and you need "make test" to succeed with no errors.
baseline tests/all.out:
	cd tests && ../$(AOUT) -t all 0<&- >all.out
	@rm -f tests/test.out
	@echo File tests/all.out updated with current test output.

proto.h:
	./update # shell script to update proto.h

$(OBJECTS): $(INCLUDES) VERSION

# Create the mathomatic executable:
$(AOUT): $(OBJECTS)
	$(CC) $(LDFLAGS) $(OBJECTS) $(LDLIBS) -o $(AOUT)
	@echo ./$(AOUT) created.

# To compile Mathomatic as a stand-alone executable
# that has no shared library dependencies, type "make clean static".
static: $(OBJECTS)
	$(CC) $(LDFLAGS) $(OBJECTS) -static $(LDLIBS) -o $(AOUT)
	@echo ./$(AOUT) created.

# Create the PDF version of the Mathomatic documentation, if desired.
# Requires the htmldoc program be installed.
pdf $(PDFDOC): doc/manual.html doc/am.html
	htmldoc --webpage -f $(PDFDOC) doc/manual.html doc/am.html
	@echo PDF documentation $(PDFDOC) generated.

# Here we convert the man pages to HTML docs with rman:
doc/mathomatic.1.html: mathomatic.1
	@rman --version # display version number and test for existence
	rman $(RMANOPTS) mathomatic.1 >doc/mathomatic.1.html

$(PRIMES_MANHTML): doc/%.1.html: primes/%.1
	@rman --version
	rman $(RMANOPTS) $< >$@

# The following creates a shell script allowing easy entry of trig functions.  Only works with GNU m4.
m4install: install
	echo '#!/bin/sh' >$(M4SCRIPTPATH)
	echo '# Shell script to run Mathomatic with the m4 preprocessor.' >>$(M4SCRIPTPATH)
	echo '# This allows entry of many standard math functions.' >>$(M4SCRIPTPATH)
	echo '#' >>$(M4SCRIPTPATH)
	echo '# Usage: $(M4SCRIPTNAME) [ input_files ]' >>$(M4SCRIPTPATH)
	echo >>$(M4SCRIPTPATH)
	echo 'm4 -eP -- $(mathdocdir)/m4/functions.m4 "$$@" - | mathomatic -ru' >>$(M4SCRIPTPATH)
	chmod 0755 $(M4SCRIPTPATH)
	$(INSTALL_PROGRAM) m4/rmath $(bindir)
	@echo
	@echo m4 Mathomatic install completed.
	@echo Type \"rmath\" to run m4 Mathomatic.

# Install the binaries and documentation.
install: bininstall docinstall
	@echo Mathomatic is installed.
	@echo Type \"$(AOUT)\" to run Mathomatic.
	@echo

install-strip:
	$(MAKE) INSTALL_PROGRAM='$(INSTALL_PROGRAM) -s' install

bininstall:
	$(INSTALL) -d $(bindir)
	$(INSTALL) -d $(prefix)/share/applications
	$(INSTALL) -d $(prefix)/share/pixmaps
	$(INSTALL_PROGRAM) $(AOUT) $(bindir)
	$(INSTALL_DATA) icons/mathomatic.desktop $(prefix)/share/applications
	$(INSTALL_DATA) icons/mathomatic.png $(prefix)/share/pixmaps

docinstall:
	$(INSTALL) -d $(mandir)/man1
	$(INSTALL) -d $(mathdocdir)
	$(INSTALL) -d $(mathdocdir)/html
	$(INSTALL) -d $(mathdocdir)/m4
	$(INSTALL) -d $(mathdocdir)/tests
	$(INSTALL) -d $(mathdocdir)/factorial
	$(INSTALL_DATA) $(MAN1) $(mandir)/man1
	$(INSTALL_DATA) $(DOCS) $(mathdocdir)
	$(INSTALL_DATA) doc/* $(mathdocdir)/html
	$(INSTALL_DATA) m4/* $(mathdocdir)/m4
	$(INSTALL_DATA) tests/* $(mathdocdir)/tests
	$(INSTALL_DATA) factorial/* $(mathdocdir)/factorial
	@echo
	@echo Documentation is installed in $(mathdocdir)/html

uninstall:
	rm -f $(bindir)/$(AOUT)
	cd $(mandir)/man1 && rm -f $(MAN1)
	rm -rf $(mathdocdir)
	rm -f $(prefix)/share/applications/mathomatic.desktop
	rm -f $(prefix)/share/pixmaps/mathomatic.png
	rm -f $(bindir)/rmath $(M4SCRIPTPATH)
	@echo
	@echo Mathomatic uninstalled.

clean:
	rm -f *.o *.a
	rm -f */*.o */*.a */*.pyc
	rm -f tests/test.out primes/test.out

distclean: clean
	rm -f $(AOUT) lib/$(AOUT)
	rm -f mathomatic_secure
	rm -f $(PDFDOC)
	cd lib && $(MAKE) distclean
	cd primes && $(MAKE) distclean

maintainer-clean flush: distclean
	rm -f $(MANHTML)
