# Scheme 9 from Empty Space
# Makefile (obviously)
# By Nils M Holm, 2007-2017
# In the public domain

# Change at least this line:

# Base version and Release
BASE=		20170124
RELEASE=	20170125

# Override default compiler and flags
# CC=	cc
CFLAGS=	-g -Wall -std=c99 -pedantic -O2

# Which OS are we using (unix or plan9)?
OSDEF=	-Dunix

# Uncomment these to include the Unix extensions
EXTRA_SCM+=	-l ext/sys-unix/unix.scm -l ext/sys-unix/unix-tools.scm
EXTRA_OBJS+=	unix.o
EXTRA_INIT+=	sys_init();

# Uncomment these to include the Curses extensions
EXTRA_SCM+=	-l ext/curses/curses.scm
EXTRA_OBJS+=	curses.o
EXTRA_INIT+=	curs_init();
EXTRA_LIBS+=	-lncurses

# Uncomment these to include the CSV extensions
EXTRA_SCM+=	-l ext/csv/csv.scm
EXTRA_OBJS+=	csv.o
EXTRA_INIT+=	csv_init();

# Options to be added to $(DEFS)
#	-DBITS_PER_WORD_64	# use 64-bit bignum arithmetics
#	-DLIBRARY_PATH="\"dir:...\""
#				# search path for LOCATE-FILE, etc
#	-DNETWORK		# include socket code in the Unix extension
#	-DCURSES_COLOR		# enable the CURS:SET-COLOR primitive
#	-DCURSES_RESET		# automatically run CURS:ENDWIN on the REPL
#				# (requires the Curses extension)

	-DLIBRARY_PATH="\".:~/s9fes:$(S9DIR)\"" \

# Where to install the stuff
S9DIR=	$(PREFIX)/share/s9fes
INCDIR=	$(PREFIX)/include
MANDIR=	$(PREFIX)/man/man1

# Set up environment to be used during the build process
BUILD_ENV=	env S9FES_LIBRARY_PATH=.:lib:ext/sys-unix:ext/curses:ext/csv:contrib

SETPREFIX=	sed -e "s|^\#! /usr/local|\#! $(PREFIX)|"

default:	s9 s9.image s9.1.gz s9.1.txt libs9core.a # s9core.pdf

all:	default

s9:	s9.o s9core.o $(EXTRA_OBJS)
	$(CC) -o s9 $(LDFLAGS) s9.o s9core.o $(EXTRA_OBJS) $(EXTRA_LIBS)

s9.o:	s9.c s9core.h s9import.h s9ext.h
	$(CC) -o s9.o $(CFLAGS) $(DEFS) -c s9.c

s9core.o:	s9core.c s9core.h
	$(CC) -o s9core.o $(CFLAGS) $(DEFS) -c s9core.c

s9.image:	s9 s9.scm ext/sys-unix/unix.scm ext/curses/curses.scm \
		ext/csv/csv.scm config.scm
	$(BUILD_ENV) ./s9 -i - $(EXTRA_SCM) -l config.scm -d s9.image

libs9core.a: s9core.o
	ar q libs9core.a s9core.o

s9.1.gz:	s9.1
	sed -e "s,@S9DIR@,$(S9DIR)," <s9.1 |gzip -9 >s9.1.gz

unix.o:	ext/sys-unix/unix.c s9core.h s9import.h s9ext.h
	$(CC) $(CFLAGS) $(DEFS) -I . -o unix.o -c ext/sys-unix/unix.c

curses.o:	ext/curses/curses.c s9core.h s9import.h s9ext.h
	$(CC) $(CFLAGS) $(DEFS) -I . -o curses.o -c ext/curses/curses.c

csv.o:	ext/csv/csv.c s9core.h s9import.h s9ext.h
	$(CC) $(CFLAGS) $(DEFS) -I . -o csv.o -c ext/csv/csv.c util/book
	groff -e -p -t -Tps -P-p9i,6i > 2>_meta
	sed -nf util/mktoc.sed <_meta | sed -e 's/  *	/	/' >
	sed -ne 's/^R;\(.*\)/\1/p' <_meta >
	sed -e 's/\.nr \(.*\) \(.*\)/\\f\[CB]\1\\fP \\n\[\1\]/' < \
		| sort -f >
	groff -e -p -t -Tps -P-p9i,6i > 2>/dev/null

	pdfwrite -p 432 648

	cc -g -Wall -ansi -pedantic -O3 s9.c s9core.c && rm a.out

test:	s9 test.image
	$(BUILD_ENV) ./s9 -i test -f util/test.scm

libtest:	s9 test.image
	$(BUILD_ENV) sh util/

systest:	s9 test.image s9.image
	$(BUILD_ENV) ./s9 -i test -f util/systest.scm

srtest:	s9 test.image
	$(BUILD_ENV) ./s9 -i test -f util/srtest.scm

realtest:	s9 test.image
	$(BUILD_ENV) ./s9 -i test -f util/realtest.scm

test.image:	s9 s9.scm
	$(BUILD_ENV) ./s9 -i - $(EXTRA_SCM) -d test.image

tests: test realtest srtest libtest systest

install:	install-s9 install-util

install-all:	install-s9 install-util install-progs

# old version of install(1) may need -c
install-s9:	s9 s9.scm s9.image s9.1.gz
	install -d -m 0755 $(S9DIR)
	install -d -m 0755 $(S9DIR)/help
	install -d -m 0755 $(S9DIR)/help/sys-unix
	install -d -m 0755 $(S9DIR)/help/curses
	install -d -m 0755 $(S9DIR)/help/csv
	install -d -m 0755 $(BINDIR)
	install -d -m 0755 $(LIBDIR)
	install -d -m 0755 $(INCDIR)
	install -d -m 0755 $(MANDIR)
	install $C -m 0755 s9 $(BINDIR)
	strip $(BINDIR)/s9
	install $C -m 0644 s9.scm $(S9DIR)
	install $C -m 0644 s9.image $(S9DIR)
	install $C -m 0644 lib/* $(S9DIR)
	install $C -m 0644 ext/sys-unix/*.scm $(S9DIR)
	install $C -m 0644 ext/curses/*.scm $(S9DIR)
	install $C -m 0644 ext/csv/*.scm $(S9DIR)
	install $C -m 0644 contrib/* $(S9DIR)
	install $C -m 0644 s9.1.gz $(MANDIR)
	(tar cf - help | tar xfC - $(S9DIR))
	install $C -m 0644 libs9core.a $(LIBDIR)
	install $C -m 0644 s9core.h $(INCDIR)
	install $C -m 0644 s9import.h $(INCDIR)
	install $C -m 0755 util/make-help-links $(S9DIR)

	$(SETPREFIX) <prog/s9help.scm >$(BINDIR)/s9help
	$(SETPREFIX) <prog/s9resolve.scm >$(BINDIR)/s9resolve
	$(SETPREFIX) <prog/scm2html1.scm >$(BINDIR)/scm2html
	$(SETPREFIX) <prog/scmpp.scm >$(BINDIR)/scmpp
	-chmod +x $(BINDIR)/s9help	\
		  $(BINDIR)/s9resolve	\
		  $(BINDIR)/scm2html	\

	$(SETPREFIX) <prog/advgen.scm >$(BINDIR)/advgen
	$(SETPREFIX) <prog/c2html1.scm >$(BINDIR)/c2html
	$(SETPREFIX) <prog/cols.scm >$(BINDIR)/cols
	$(SETPREFIX) <prog/dupes.scm >$(BINDIR)/dupes
	$(SETPREFIX) <prog/edoc.scm.edoc >$(BINDIR)/edoc
	$(SETPREFIX) <prog/htmlify.scm >$(BINDIR)/htmlify
	$(SETPREFIX) <prog/s9hts.scm >$(BINDIR)/s9hts
	$(SETPREFIX) <prog/soccat.scm >$(BINDIR)/soccat
	-chmod +x $(BINDIR)/advgen	\
		  $(BINDIR)/c2html	\
		  $(BINDIR)/cols	\
		  $(BINDIR)/dupes	\
		  $(BINDIR)/edoc	\
		  $(BINDIR)/htmlify	\
		  $(BINDIR)/s9hts	\

deinstall:	deinstall-s9 deinstall-util deinstall-progs

	rm -f $(S9DIR)/help/* && rmdir $(S9DIR)/help
	rm -f $(S9DIR)/* && rmdir $(S9DIR)
	rm -f $(BINDIR)/s9
	-rmdir $(BINDIR)
	-rmdir $(MANDIR)

	rm -f $(BINDIR)/s9help		\
	      $(BINDIR)/s9resolve	\
	      $(BINDIR)/scm2html	\

	rm -f $(BINDIR)/advgen		\
	      $(BINDIR)/c2html		\
	      $(BINDIR)/cols		\
	      $(BINDIR)/dupes		\
	      $(BINDIR)/edoc		\
	      $(BINDIR)/htmlify		\
	      $(BINDIR)/s9hts		\

	@find . -name \*.scm -exec grep -l "	" {} \;

	./s9 -f util/check-descr.scm

	rm -f s9 s9.image libs9core.a test.image s9.1.gz *.o *.core \
		CATEGORIES.html HACKING.html core s9fes-$(RELEASE).tgz \
		s9fes-$(BASE).tgz s9core-$(RELEASE).tgz __testfile__ \ s9core.pdf _meta

	vi Makefile s9.c CHANGES
	make s9.c

	vi util/make-docs
	vi util/make-help-links \
		util/descriptions \
	cd help && s9 -f ../util/procedures.scm >INDEX
	@echo "Now copy the new help pages from help-new to help"
	@echo "and run util/make-help-links."

s9.1.txt:	s9.1
	$(CC) -o rpp util/rpp.c
	nroff -c -mdoc s9.1 | ./rpp -a >s9.1.txt
	rm -f rpp

docs:	lib ext/sys-unix ext/sys-plan9 ext/curses ext/csv contrib
	mv -f help-new/sys-unix/* help/sys-unix
#	mv -f help-new/sys-plan9/* help/sys-plan9
	mv -f help-new/curses/* help/curses
#	mv -f help-new/csv/* help/csv
	rm help-new/sys-plan9/*
	rmdir help-new/sys-unix help-new/sys-plan9 help-new/curses help-new/csv
	mv -f help-new/* help
	rmdir help-new

	util/make-html -r $(RELEASE)

advdump:	prog/advgen.scm prog/adventure.adv prog/adventure.intro
	sed -e 's/@dir/quest/' -e 's/@file/index/g' <util/pagehead >pagehead
	prog/advgen.scm -rv \
		-P terminal:session \
		-p pagehead \
		-e util/pagetail \
		-i prog/adventure.intro \
		-t "The Quest for S9fES" \
		-y ../t3x.css \
	rm -f pagehead
	cp MASCOT.png advdump

	csum -u <_csums >
	mv _csums

mksums:	clean
	find . -type f | grep -v _csums | csum >_csums

dist:	clean s9.1.txt
	make s9core.pdf && mv s9core.pdf _s9core.pdf
	make clean
	mv -f _s9core.pdf s9core.pdf
	cd .. && \
		tar cf - s9 | gzip -9 > s9fes-$(RELEASE).tgz && \
		mv s9fes-$(RELEASE).tgz s9
	rm -f s9core.pdf
	ls -l s9fes-$(RELEASE).tgz | awk '{print int($$5/1024+.5)}'

	make s9core.pdf
	tar cf - s9core.[ch] s9core.pdf README.s9core \
		| gzip -9 > s9core-$(RELEASE).tgz 
	rm -f s9core.pdf

arc:	clean s9.1.txt
	cd .. && tar cf - s9 | gzip -9 > s9fes-$(BASE).tgz && \
		mv s9fes-$(BASE).tgz s9
	ls -l s9fes-$(BASE).tgz | awk '{print int($$5/1024+.5)}'