# 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 all documentation.

VERSION		= `cat VERSION`

CC		?= gcc
INSTALL		?= install

CFLAGS		+= -Wall -Wshadow -Wno-char-subscripts # gcc specific flags; can be removed
#CFLAGS		+= -fomit-frame-pointer # Uncomment this line to optimize further with gcc.
CFLAGS		+= -O -DUNIX -DVERSION=\"$(VERSION)\"
CFLAGS		+= -D__C99FEATURES__ # for a successful compile under Solaris
LDLIBS		+= -lm # libraries to link

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

# HTML man pages to make:
MANHTML		= doc/mathomatic.1.html doc/matho-primes.1.html doc/matho-pascal.1.html doc/matho-sumsq.1.html \
		  doc/primorial.1.html doc/matho-mult.1.html
# 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

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

doc: $(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 Mathomatic...
	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:
	cd tests && ../$(AOUT) -t all 0<&- >all.out
	@rm -f tests/test.out
	@echo File tests/all.out updated with current test output.

$(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.

# 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

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

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

doc/matho-mult.1.html: primes/matho-mult.1
	@rman --version
	rman $(RMANOPTS) primes/matho-mult.1 >doc/matho-mult.1.html

doc/matho-pascal.1.html: primes/matho-pascal.1
	@rman --version
	rman $(RMANOPTS) primes/matho-pascal.1 >doc/matho-pascal.1.html

doc/matho-sumsq.1.html: primes/matho-sumsq.1
	@rman --version
	rman $(RMANOPTS) primes/matho-sumsq.1 >doc/matho-sumsq.1.html

# 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) -m 0755 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 Type \"$(AOUT)\" to run Mathomatic.
	@echo

bininstall:
	$(INSTALL) -d $(bindir)
	$(INSTALL) -d $(prefix)/share/applications
	$(INSTALL) -d $(prefix)/share/pixmaps
	$(INSTALL) -m 0755 $(AOUT) $(bindir)
	$(INSTALL) -m 0644 icons/mathomatic.desktop $(prefix)/share/applications
	$(INSTALL) -m 0644 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) -m 0644 $(MAN1) $(mandir)/man1
	$(INSTALL) -m 0644 $(DOCS) $(mathdocdir)
	$(INSTALL) -m 0644 doc/* $(mathdocdir)/html
	$(INSTALL) -m 0644 m4/* $(mathdocdir)/m4
	$(INSTALL) -m 0644 tests/* $(mathdocdir)/tests
	$(INSTALL) -m 0644 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

flush: clean
	rm -f $(AOUT)
	rm -f mathomatic_secure
	rm -f $(MANHTML)
