#
# fv makefile for UNIX hosts
#
VERSION=220

#
# Feature options:   (add to OPTIONS line)
#	-DSTRICT_VI
#		Typematic mode at prompt starts in editor state when defined,
#		keys in search functions act more close to vi
#	-DZERO_START
#		'0' takes you to start of file rather than beginning of line
#	-DEMACS_FEEL
#		Give program more of Emacs feel, some compromise to vi feel
#	-DALIGN_VIEW
#		Always align the display address by current cursor size
#	-DSHOW_EXTASCII
#		Display ASCII codes 161-255 in viewer when defined
#		This mode is not appreciated by all terminal types
#	-DEDIT_MEMORY
#		Enable memory edit functions
#	-DCOLON_MODE
#		Enable colon mode prompt
#	-DHUGE_BUFFER
#		Make the search buffer very large for screenless edits
#	-DCOLOR_CURSES
#		Use curses colors for different display regions.  This is
#		not well-tested as color_xterm has curses emulation bugs.
#	-DCURSES_UPDATE_BUG
#		When displaying the screen, force buggy curses (Linux 2.0.29)
#		to redraw screen rather than attempting to just update it.
#
# Target options:   (add to OPTIONS line)
#	-DSolaris1           ** Should be automatically selected
#		If compiling for Solaris 1.x (SunOS 4.x)
#	-DSolaris2           ** Should be automatically selected
#		If compiling for Solaris 2.x (SunOS 5.x)
#	-DFreeBSD           ** Should be automatically selected
#		If compiling for FreeBSD (need different make)
#	-DLinux             ** Should be automatically selected
#		If compiling for Linux
#	-DAmiga             ** Should be automatically selected
#		If compiling for AmigaDOS (no signal support)
#		Amiga does not need -ltermcap with curses from S.J. Raybould
#       Additional options may be -m68040 -m68881
#	-DNetBSD
#		If compiling for NetBSD
#	-DHPUX
#		If compiling for HP/HPUX (need different make or use cmdline)
#	-DDynix
#		If compiling for Dynix/BSD (Sequent - no cbreak mode)
#	-DWin32
#		If compiling for Windows 95 / NT  (use makefile.dos)
#	-DOS9               (use makefile.os9)
#		If compiling for OS-9/OS-9000, must also define HOST_ARCH=OS9
#		Must also add -v=. -to=os9000 and -tp=<TARGET> to CFLAGS line
#		Must also swap $(OBJS) compile comment lines
#	-DLINT	Set if running lint on source (includes lint.h)
#
#
#
# To compile for extended curses key functions (like keypad) on SunOS 4.x:
#	Add -I/usr/5include to CFLAGS and -L/usr/5lib to LDFLAGS, does not work
#	currently due to other SYSV incompatibilities
#

#OPTIONS=-DSTRICT_VI -DEDIT_MEMORY -DCOLON_MODE -DAmiga -m68040 -m68881
OPTIONS=-DSTRICT_VI -DEDIT_MEMORY -DCOLON_MODE -DOS9
#OPTIONS=-DSTRICT_VI -DCOLON_MODE -DCOLOR_CURSES
OPTIONS=-DSTRICT_VI -DCOLON_MODE

# Only define for OS-9/OS-9000
#HOST_ARCH=OS9


#
# Possible CC=
#	CC=gcc
#	CC=cc
#	CC=acc
#	CC=lint
#	CC=xcc
#CC=cc
CC=gcc


# Regular compiler CFLAGS which may be used:
#	-ansi	Flag for ANSI language semantics (stricter checking)
#	-Wall	Gcc flags for strict type checking
#	-g  	Debugging
#	-O2 	Optimization


#  Optimized make
CFLAGS=$(OPTIONS) -v=. -to=OS9000 -tp=80386		# OS-9/OS-9000
CFLAGS=$(OPTIONS) -O
#CFLAGS=$(OPTIONS) -O2 -fomit-frame-pointer -fstrength-reduce -fexpensive-optimizations -frerun-cse-after-loop

#  Debug make
#CFLAGS=$(OPTIONS) -Wall
#CFLAGS=$(OPTIONS) -g


# Include flags
#INCLUDE=-I/usr/5include
INCLUDE=

# Linker flags
#LDFLAGS=-L/usr/5lib
LDFLAGS=

CCFLAGS = $(CFLAGS) $(INCLUDE)

ROOTDIR=/usr/local
BINDIR=$(ROOTDIR)/bin
MANDIR=$(ROOTDIR)/man
MANEXT=1

LIBS=-lcurses -ltermcap
AMIGALIBS=-lcurses
OS9LIBS=-l=curses.l -l=termlib.l -l=sys_clib.l -l=unix.l

PROG=fv
RM=rm -f
#RM=del
NROFF=groff -man
NROFF=nroff -man
PSROFF=psroff -t -man
#PSROFF=ptroff -t -man

# OS-9/OS-9000 definitions
O=r
TO_O=-eas=
TO_EXE=-fd=$(PROG)

# UNIX/Amiga definitions (these do not override previous with os9make)
O=o
TO_EXE=-o $(PROG)
TO_O=-c -o

OBJDIR=obj$(HOST_ARCH)
DOCDIR=doc
SRCDIR=.
MISC=Makefile makefile.dos makefile.os9 makefile.amiga \
	INSTALL TODO RELNOTES fv-$(VERSION).lsm fv-$(VERSION).readme \
	$(DOCDIR)/$(PROG).keymap $(DOCDIR)/$(PROG).man \
	$(DOCDIR)/$(PROG).catman $(DOCDIR)/$(PROG).catman.ps
INCLUDES=lint.h edit.h main.h screen.h pcurses.h curses.h io.h
SRCS=$(SRCDIR)/main.c $(SRCDIR)/edit.c $(SRCDIR)/screen.c $(SRCDIR)/io.c \
	 $(SRCDIR)/search.c $(SRCDIR)/prompt.c $(SRCDIR)/help.c
OBJS=$(OBJDIR)/main.$O $(OBJDIR)/edit.$O $(OBJDIR)/screen.$O $(OBJDIR)/io.$O \
	 $(OBJDIR)/search.$O $(OBJDIR)/prompt.$O $(OBJDIR)/help.$O
#OBJS=$(SRCS:$(SRCDIR)%.c=$(OBJDIR)/%.$(O))

all:
	@$(MAKE) -f Makefile host$(HOST_ARCH)

#\
-b

host-sparc host-sun4: $(PROG) $(DOCDIR)/$(PROG).catman

host amiga: $(OBJDIR) $(OBJS) $(DOCDIR)/$(PROG).catman
#	$(CC) -s $(CCFLAGS) $(TO_EXE) $(OBJS) $(LDFLAGS) $(AMIGALIBS)
	$(CC) $(CCFLAGS) $(TO_EXE) $(OBJS) $(LDFLAGS) $(AMIGALIBS)

hostOS9: $(OBJDIR) $(OBJS) $(DOCDIR)/$(PROG).catman
	$(CC) -s $(CCFLAGS) $(TO_EXE) $(OBJS) $(LDFLAGS) $(OS9LIBS)

hostWin32: $(OBJDIR) $(OBJS)
	$(CC) $(CFLAGS) $(TO_EXE) $(OBJS) $(LDFLAGS) $(WIN32LIBS)

#	@echo MAKEFLAGS=$(MAKEFLAGS)
#	@echo MAKE=$(MAKE)
#	@echo SHELL=$(SHELL)
#	@echo HOST_ARCH=$(HOST_ARCH)
#	@echo HOST_MACH=$(HOST_MACH)
#	@echo TARGET_MACH=$(TARGET_MACH)

$(PROG): $(OBJDIR) $(OBJS)
	$(CC) $(CCFLAGS) $(TO_EXE) $(OBJS) $(LDFLAGS) $(LIBS)

install: $(DOCDIR)/$(PROG).catman $(MANDIR) $(MANDIR)/man$(MANEXT) $(MANDIR)/cat$(MANEXT)
	cp $(PROG) $(BINDIR)/$(PROG)
	chmod 755 $(BINDIR)/$(PROG)
	cp $(DOCDIR)/$(PROG).man    $(MANDIR)/man$(MANEXT)/$(PROG).$(MANEXT)
	chmod 644 $(MANDIR)/man$(MANEXT)/$(PROG).$(MANEXT)
	cp $(DOCDIR)/$(PROG).catman $(MANDIR)/cat$(MANEXT)/$(PROG).$(MANEXT)
	chmod 644 $(MANDIR)/cat$(MANEXT)/$(PROG).$(MANEXT)

$(OBJDIR)/main.$(O):   $(SRCDIR)/main.c   main.h screen.h edit.h io.h
$(OBJDIR)/edit.$(O):   $(SRCDIR)/edit.c   main.h screen.h edit.h
$(OBJDIR)/screen.$(O): $(SRCDIR)/screen.c main.h screen.h pcurses.h io.h
$(OBJDIR)/search.$(O): $(SRCDIR)/search.c main.h screen.h io.h
$(OBJDIR)/prompt.$(O): $(SRCDIR)/prompt.c main.h screen.h io.h
$(OBJDIR)/io.$(O):     $(SRCDIR)/io.c     main.h io.h
$(OBJDIR)/help.$(O):   $(SRCDIR)/help.c   main.h screen.h

$(OBJS):
	$(CC) $(CCFLAGS) $(SRCDIR)/$(@:$(OBJDIR)/%.$(O)=%.c) $(TO_O) $@
# Use later line for OS9 and former for everything else
#	$(CC) $(CCFLAGS) $*.c $(TO_O)$(OBJDIR)

$(OBJDIR) $(MANDIR) $(MANDIR)/man$(MANEXT) $(MANDIR)/cat$(MANEXT):
	mkdir $@
	chmod 755 $@

man: $(DOCDIR)/$(PROG).catman
$(DOCDIR)/$(PROG).catman: $(DOCDIR)/$(PROG).man
	-$(NROFF) $? >$@
	-chmod 644 $@

ps: $(DOCDIR)/$(PROG).catman.ps
$(DOCDIR)/$(PROG).catman.ps: $(DOCDIR)/$(PROG).man
	-$(PSROFF) $? >$@

lint: clean
	$(MAKE) -e CC=lint CFLAGS="-bh -DLINT $(LIBS)" $(OBJS)

Wall: clean
	$(MAKE) -e CC=gcc CFLAGS="-Wall -DLINT" $(OBJS)

clean:
	$(RM) $(OBJS)

lha:
	$(RM) $(PROG)-$(VERSION).lha
	lha a $(PROG)-$(VERSION).lha $(SRCS) $(INCLUDES) $(MISC)

zip:
	$(RM) $(PROG)-$(VERSION).zip
	zip $(PROG)-$(VERSION).zip $(SRCS) $(INCLUDES) $(MISC)

tgz:
	$(RM) $(PROG)-$(VERSION).tgz
	tar cf - $(SRCS) $(INCLUDES) $(MISC) | gzip -9 >$(PROG)-$(VERSION).tgz
