#
# help - makefile for calc help files
#
# Copyright (c) 1994 David I. Bell and Landon Curt Noll
# Permission is granted to use, distribute, or modify this source,
# provided that this copyright notice remains intact.
#
# Arbitrary precision calculator.
#
# calculator by David I. Bell
# makefile by Landon Curt Noll

SHELL= /bin/sh

# Normally, the upper level makefile will set these values.  We provide
# a default here just in case you want to build from this directory.
#
TOPDIR= /usr/local/lib
#TOPDIR= /usr/lib
#TOPDIR= /usr/libdata

LIBDIR= ${TOPDIR}/calc
HELPDIR= ${LIBDIR}/help

# standard tools
#
NATIVE_CC= cc
NATIVE_CFLAGS=
SED= sed
SORT= sort
FMT= fmt
CMP= cmp

# Standard help files
#
# The obj.file is special and is not listed here.
#
STD_HELP_FILES1= intro overview help assoc command config \
	define environment expression file history interrupt list mat
STD_HELP_FILES2= operator statement types usage variable
STD_HELP_FILES3= todo credit
STD_HELP_FILES= ${STD_HELP_FILES1} ${STD_HELP_FILES2} ${STD_HELP_FILES3}
SYMBOL_HELP= assign mod quo

# These two lists are prodiced by the detaillist and missinglist rules
# when no WARNINGS are detected.
#
DETAIL_HELP= abs acos acosh acot acoth acsc acsch append appr arg asec asech \
	asin asinh atan atan2 atanh avg base bround btrunc ceil cfappr cfsim \
	char cmdbuf cmp comb conj cos cosh cot coth count cp csc csch delete \
	den det digit digits dp epsilon errno \
	exp fact fclose feof ferror fflush \
	fgetc fgetline fgets fib files floor fopen forall fputc fputs fseek \
	fsize ftell gcd getenv head highbit hypot ilog ilog10 ilog2 im insert \
	int iroot isassoc isatty isconfig iseven isfile ishash isident isint \
	islist ismat ismult isnull isnum isobj isodd isprime isqrt isrand \
	israndom isreal isrel isset issimple issq isstr istype join lcm ln \
	lowbit ltol makelist matdim matfill matmax matmin matsum mattrans max \
	meq min minv mne modify near norm num ord perm pi places pmod polar \
	poly pop power push putenv quomod \
	randperm re reverse root round rsearch \
	search sec sech segment sgn sin sinh size sort sqrt ssq str strcat \
	strlen strpos substr system tail tan tanh trunc xor
MISSING_HELP= eval factor fcnt fprintf frac frem gcdrem hash hmean \
	inverse jacobi lcmfact lfactor mmin nextcand nextprime null param \
	pfact pix prevcand prevprime printf prompt ptest rand randbit \
	rcin rcmul rcout rcpow rcsq remove runtime scale srand strprintf swap


# Help files that are constructed from other sources
#
# The obj.file is special and is not listed here.
#
BUILT_HELP_FILES= bindings altbind changes libcalc stdlib bugs

# These files are found (but not built) in the distribution
#
DISTLIST= ${STD_HELP_FILES} ${DETAIL_HELP} ${SYMBOL_HELP} Makefile \
	MISSING obj.file builtin.top builtin.end funclist.sed

all: ${STD_HELP_FILES} obj.file ${BUILT_HELP_FILES} full \
     ${DETAIL_HELP} ${MISSING_HELP} builtin .all

# used by the upper level Makefile to determine of we have done all
#
.all:
	rm -f .all
	touch .all

bindings: ../lib/bindings
	rm -f bindings
	cp ../lib/bindings bindings
	chmod 0444 bindings

altbind: ../lib/altbind
	rm -f altbind
	cp ../lib/altbind altbind
	chmod 0444 altbind

stdlib: ../lib/README
	rm -f stdlib
	cp ../lib/README stdlib
	chmod 0444 stdlib

changes: ../CHANGES
	rm -f changes
	cp ../CHANGES changes
	chmod 0444 changes

libcalc: ../LIBRARY
	rm -f libcalc
	${SED} -e 's:$${LIBDIR}:${LIBDIR}:g' < ../LIBRARY > libcalc
	chmod 0444 libcalc

bugs: ../BUGS
	rm -f bugs
	cp ../BUGS bugs
	chmod 0444 bugs

full: ${STD_HELP_FILES} ${BUILT_HELP_FILES} Makefile
	@echo "forming full"
	-@rm -f full
	-@for i in ${STD_HELP_FILES1} obj.file ${STD_HELP_FILES2} \
	    ${BUILT_HELP_FILES} ${STD_HELP_FILES3}; do \
	    if [ Xintro != X"$$i" ]; then \
		echo ""; \
	    fi; \
	    if [ Xobj.file = X"$$i" ]; then \
		j=obj; \
	    else \
		j=$$i; \
	    fi; \
	    echo "*************"; \
	    echo "* $$j"; \
	    echo "*************"; \
	    echo ""; \
	    cat $$i; \
	done > full
	@echo "full formed"

# Form the builtin file
#
# We ave a "chicken-and-egg" problem.  We want the builtn help file to
# accurately reflect the function list.  It would be nice if we could
# just execute calc show builtin, but calc may not have been built or
# buildable at this point.  The hack-a-round used is to convert ../func.c
# into a standalone program that generates a suitable function list
# that is standwiched between the top and bottom builtin help text.
#
# We form funclist.c by sedding out unwanted stuff from builtins table,
# converting NUMBER* and VALUE into harmless types and converting
# the showbuiltins() function into main().  Combined with the -DFUNCLIST
# we will avoid all of the complex calc types, macros and defines and
# be left with just main() and a mininal builtins table.
#
# Building funclist.o a portable fashion is ugly because some systems
# do not treat -I.. correctly!
#
builtin: builtin.top builtin.end ../func.c funclist.sed
	@echo "forming builtin help file"
	-@rm -f builtin
	@cat builtin.top > builtin
	-@rm -f funclist.c
	@${SED} -n -f funclist.sed ../func.c > funclist.c
	-@rm -f ../funclist.c ../funclist.o ../funclist funclist
	@cp funclist.c ..
	-@(cd ..; \
	 ${NATIVE_CC} ${NATIVE_CFLAGS} -DFUNCLIST funclist.c -o funclist; \
	 mv funclist help; \
	 rm -f funclist.c funclist.o funclist)
	@./funclist | \
	    ${SED} -e 's/^/	/' -e 's/[	 ][	 ]*$$//' >> builtin
	@cat builtin.end >> builtin
	@echo "builtin help file formed"

# create MISSING files
#
${MISSING_HELP}: MISSING
	-@if [ ! -f "$@" ]; then \
	    echo "rm -f $@"; \
	    rm -f $@; \
	    echo "cp MISSING $@"; \
	    cp MISSING $@; \
	fi

##
#
# File list generation.  You can ignore this section.
#
#
# We will form the names of source files as if they were in a
# sub-directory called calc/help.
#
##

distlist: ${DISTLIST}
	@for i in ${DISTLIST}; do \
		echo calc/help/$$i; \
	done | ${SORT}

# The bsdi distribution has generated files as well as distributed files.
#
bsdilist: ${DISTLIST} ${BUILT_HELP_FILES} ${MISSING_HELP}
	@for i in ${DISTLIST} ${BUILT_HELP_FILES} ${MISSING_HELP}; do \
		echo calc/help/$$i; \
	done | ${SORT}

# The BSDI cdrom makefile expects all help files to be pre-built.  This rule
# creats these fils so that the release can be shipped off to BSDI.  You can
# ignore this rule.
#
bsdi: all
	rm -f obj
	cp obj.file obj

# These next two rules help me form the ${DETAIL_HELP} and ${MISSING_HELP}
# makefile variables above
#
detaillist:
	@-(echo "xxxxx"; \
	   for i in ${DETAIL_HELP}; do \
		if ${CMP} -s $$i MISSING; then \
		    echo "WARNING: $$i is the same as MISSING" 1>&2; \
		else \
		    if [ ! -f SCCS/s.$$i ]; then \
			echo "WARNING: $$i not under SCCS control" 1>&2; \
		    else \
			echo $$i; \
		    fi; \
		fi; \
	done | ${SORT}) | ${FMT} -70 | \
	    ${SED} -e '1s/xxxxx/DETAIL_HELP=/' -e '2,$$s/^/	/' \
		   -e 's/$$/ \\/' -e '$$s/ \\$$//'

missinglist: ${MISSING_HELP}
	@-(echo "xxxxx"; \
	  for i in ${MISSING_HELP}; do \
		if ${CMP} -s $$i MISSING; then \
		    if [ -f SCCS/s.$$i ]; then \
			echo "WARNING: $$i under SCCS control" 1>&2; \
		    else \
			echo $$i; \
		    fi; \
		else \
		    echo "WARNING: $$i is not the same as MISSING" 1>&2; \
		fi; \
	done | ${SORT}) | ${FMT} -70 | \
	    ${SED} -e '1s/xxxxx/MISSING_HELP=/' -e '2,$$s/^/	/' \
		   -e 's/$$/ \\/' -e '$$s/ \\$$//'

clean:
	rm -f obj mkbuiltin funclist.c funclist.o funclist

clobber:
	rm -f ${BUILT_HELP_FILES} full ${MISSING_HELP} builtin .all
	rm -f obj mkbuiltin funclist.c funclist.o funclist

install: all
	-@if [ ! -d ${TOPDIR} ]; then \
		echo mkdir ${TOPDIR}; \
		mkdir ${TOPDIR}; \
	fi
	-@if [ ! -d ${LIBDIR} ]; then \
		echo mkdir ${LIBDIR}; \
		mkdir ${LIBDIR}; \
	fi
	-@if [ ! -d ${HELPDIR} ]; then \
		echo mkdir ${HELPDIR}; \
		mkdir ${HELPDIR}; \
	fi
	@for i in ${STD_HELP_FILES} ${BUILT_HELP_FILES} builtin \
		  full ${DETAIL_HELP} ${MISSING_HELP} ${SYMBOL_HELP}; do \
		echo rm -f ${HELPDIR}/$$i; \
		rm -f ${HELPDIR}/$$i; \
		echo cp $$i ${HELPDIR}; \
		cp $$i ${HELPDIR}; \
		echo chmod 0444 ${HELPDIR}/$$i; \
		chmod 0444 ${HELPDIR}/$$i; \
	done
	rm -f ${HELPDIR}/obj
	cp obj.file ${HELPDIR}/obj
	chmod 0444 ${HELPDIR}/obj
