################################################################################
# Herbert 1998:
# Makefile for building and installing elvis under OS/2,
# derived from Makefile created by configure for linux.
# Works with GNU make and nmake.
################################################################################

################################################################################
# This is the name of the compiler, along with any flags that should be used
# both for compiling and for linking.  Typical values are "cc -g" or "cc -O".
# If you're compiling with X-windows support, you may need to add a
# "-I/usr/X11/include" flag or something similar.
CC=icc -O -Q -DOS2 -I/work/ibmcpp/include -I/usr/include -I/tcpip/include

################################################################################
# This should be "unix" for all UNIX variants.  It causes the compiler to use
# files from the osunix subdirectory, and do a UNIX-style installation.
OS=os2

################################################################################
# This is the separator between a subdirectory name and a file name.  For UNIX
# this should always be a "/" character.
SLASH=\\\\

################################################################################
# This macro gives any arguments which will be needed during linking.
# Mostly, this means "-Llibdir" and "-llib" flags.  If you're compiling with
# X-windows support, then you'll have to add a "-lX11" and maybe a
# "-L/usr/X11/lib" flag or something similar.
LIBFILES= so32dll.lib tcp32dll.lib
DEFFILE= os$(OS)$(SLASH)elvis.def 
PMDEFFILE= os$(OS)$(SLASH)pmelvis.def

################################################################################
# This is a command for compiling files in a subdirectory, and placing the
# resultant object files in the current directory.  Most UNIX compilers behave
# this way naturally, so you probably won't need to modify this.
OSCC=$(CC) -I. $(CFLAGS) $(FLAGC)

################################################################################
# This is the file name extension for object files.  For UNIX, this is almost
# always ".o"
OBJ=.obj

################################################################################
# This is the file name extension for executable files.  For UNIX, this is
# always a null string because UNIX doesn't use file name extensions to mark
# executable files.
EXE=.exe

################################################################################
# This is the shell command which is used for deleting a file if it exists.
RM=cmd /c del 

################################################################################
# This is the shell command which is used for deleting a directory if it exists.
RD=cmd /c rd

################################################################################
# This is the shell command which is used for creating a directory 
MD=cmd /c md

################################################################################
# This is the shell command which is used for listing a file's content
CAT=cmd /c type

################################################################################
# This is the compiler flag which causes compilation but not linking.  For
# most UNIX compilers, this is "-c".
FLAGC=-c

################################################################################
# This is the compiler flag which specifies the output file name. For most
# UNIX compilers, this is "-o". Note that there should be a space after the
# "-o" like "-o "!
FLAGO=-fe

################################################################################
# This is the compiler flag for adding a directory to the list of directories
# to be searched for #include files.  For most UNIX compilers, this is "-I".
FLAGI=-I


################################################################################
# This is a list of programs to be created by "make all".  The possible list
# includes elvis$(EXE), ref$(EXE), ctags$(EXE), and fmt$(EXE).  The $(EXE) on
# the end is just in case this Makefile is reconfigured for an operating
# system which requires the EXE macro to be something like ".exe".
ALL=elvis$(EXE) vi$(EXE) ex$(EXE) ref$(EXE) ctags$(EXE) fmt$(EXE)

################################################################################
# The Elvis manual in INF format. This is done by a number of batch files
# in the manual subdirectory.
MANUAL=doc\\elvis.INF

################################################################################
# This is the directory where "make install" will place the executable programs
BINDIR=e:\usr\bin

################################################################################
# This is the directory where "make install" will place elvis' support files,
# such as the help text and the default initialization scripts.  If you change
# the value here, then you also need to change it in "config.h"
LIBDIR=e:\usr\elvis

################################################################################
# This is the shell command for copying one file to another
CP=cmd /c copy

###############################################################################
###############################################################################
###############################################################################
###########                                                         ###########
###########                         S T O P !                       ###########
###########                                                         ###########
###########  You shouldn't need to modify anything below this line  ###########
###########                                                         ###########
###############################################################################
###############################################################################
###############################################################################

###############################################################################
# Portable compilation rules

# Herbert 1997:
# Needed this for nmake compatibility...
.SUFFIXES:
.SUFFIXES: $(OBJ) $(EXE) .c
.c$(OBJ):
	$(OSCC) $<

MISC=	BUGS README.html COPYING INSTALL Makefile.in configure instman.sh\
	insticon.sh elvis.lsm makwin32.bat makmsdos.bat makos2.cmd e2
OSSRCS=	os$(OS)$(SLASH)osblock.c os$(OS)$(SLASH)osdir.c\
	os$(OS)$(SLASH)osprg.c os$(OS)$(SLASH)ostext.c\
	os$(OS)$(SLASH)osnet.c os$(OS)$(SLASH)tcaphelp.c\
	os$(OS)$(SLASH)debug.c
OSOBJS=	osblock$(OBJ) osdir$(OBJ) osprg$(OBJ) ostext$(OBJ) osnet$(OBJ) \
        debug$(OBJ)
OSOBJS2=tcaphelp$(OBJ)
OSHDRS=	os$(OS)$(SLASH)osdef.h
XBM=	guix11/elvis.xbm guix11/elvispin.xbm guix11/gray.xbm
X11SRCS=guix11/guix11.c guix11/xclip.c guix11/xevent.c guix11/xmisc.c\
	guix11/xscroll.c guix11/xstatus.c guix11/xtext.c guix11/xtool.c \
	guix11/xdialog.c
X11OBJS=guix11$(OBJ) xclip$(OBJ) xevent$(OBJ) xmisc$(OBJ) xscroll$(OBJ)\
	xstatus$(OBJ) xtext$(OBJ) xtool$(OBJ) xdialog$(OBJ)
GUISRCS=$(X11SRCS) guicurs.c guitcap.c guiopen.c os$(OS)$(SLASH)guivio.c
GUIOBJS=$(X11OBJS) guicurs$(OBJ) guitcap$(OBJ) guiopen$(OBJ) guivio$(OBJ)
LPSRCS=	lpescape.c lpovrtyp.c lpps.c
LPOBJS=	lpescape$(OBJ) lpovrtyp$(OBJ) lpps$(OBJ)
HDRS=	$(OSHDRS) buffer.h buffer2.h calc.h color.h cut.h descr.h digraph.h\
	display.h display2.h draw.h draw2.h elvis.h elvisio.h event.h ex.h\
	gui.h gui2.h input.h lowbuf.h lp.h map.h mark.h message.h message2.h\
	misc.h more.h move.h need.h operator.h opsys.h optglob.h options.h\
	regexp.h region.h safe.h scan.h session.h spell.h state.h state2.h\
	tag.h tagsrch.h tagelvis.h version.h vi.h vicmd.h window.h config.h
SRCS=	$(OSSRCS) $(GUISRCS) $(LPSRCS) buffer.c calc.c color.c cut.c descr.c\
	digraph.c display.c dmhex.c dmmarkup.c dmnormal.c dmsyntax.c draw.c\
	event.c ex.c exaction.c exconfig.c exedit.c exmake.c exsubst.c fold.c\
	ftp.c gui.c http.c input.c io.c lowbuf.c lp.c map.c mark.c message.c\
	misc.c more.c move.c need.c operator.c optglob.c options.c regexp.c\
	region.c regsub.c safe.c scan.c search.c session.c spell.c state.c\
	tag.c tagsrch.c tagelvis.c url.c vi.c vicmd.c window.c
OBJ1=	$(OSOBJS) optglob$(OBJ) options$(OBJ) safe$(OBJ) session$(OBJ)
OBJ2=	buffer$(OBJ) calc$(OBJ) color$(OBJ) descr$(OBJ) digraph$(OBJ)\
	display$(OBJ) gui$(OBJ) lowbuf$(OBJ) mark$(OBJ) misc$(OBJ) io$(OBJ)\
	dmhex$(OBJ) dmmarkup$(OBJ) dmnormal$(OBJ) dmsyntax$(OBJ) scan$(OBJ)
OBJ3=	$(OSOBJS2) cut$(OBJ) draw$(OBJ) event$(OBJ) ex$(OBJ) exaction$(OBJ)\
	exconfig$(OBJ) exedit$(OBJ) exmake$(OBJ) exsubst$(OBJ) fold$(OBJ)\
	ftp$(OBJ) http$(OBJ) input$(OBJ) lp$(OBJ) map$(OBJ) message$(OBJ)\
	move$(OBJ) more$(OBJ) need$(OBJ) operator$(OBJ) regexp$(OBJ)\
	region$(OBJ) regsub$(OBJ) search$(OBJ) spell$(OBJ) state$(OBJ)\
	tinytcap$(OBJ) tag$(OBJ) tagsrch$(OBJ) tagelvis$(OBJ) url$(OBJ)\
	vi$(OBJ) vicmd$(OBJ) window$(OBJ)
OBJS=	$(OBJ1) $(OBJ2) $(OBJ3)
REFOBJS=ref$(OBJ) tag$(OBJ) tagsrch$(OBJ) safe$(OBJ)
CTOBJS=	ctags$(OBJ) tag$(OBJ) safe$(OBJ)
CFLAGS=	$(FLAGI)os$(OS)
#LDFLAGS= -B"/DEBUG" 
# Herbert 1998: Targets calc and verify do not work yet!
EVERY=	$(ALL) elvdump$(EXE) calc$(EXE) verify$(EXE) 
# Herbert 1998: SHELL needs to be blanked out under OS/2 if using GNU make.
#SHELL=	/bin/sh
VERSION=2.2
DISTRIB=elvis-$(VERSION)
DOSEXE=exedos
WIN32EXE=exewin32
OS2EXE=exeos2

all: $(OS2EXE) $(ALL) 

every: $(OS2EXE) $(EVERY)

$(OS2EXE): ; mkdir $(OS2EXE)

# The manual converted from the HTML documentation to OS/2's INF...
$(MANUAL): 
	cd osos2\\manual && h2i-guide.cmd
	$(CP) osos2\\manual\\elvis.INF $(MANUAL)

# Elvis compiled for a.out format and linked against emx.dll...
emx: ; $(MAKE) elvisemx.exe -f osos2\\Makefile.os2 "OBJ=.o" "EXE=emx.exe" \
                    "CC=gcc -Wall -DOS2 -D__WITH_TERMCAP -O3 \
                    -I/emx/include -I/software/ibmcpp/include \
                    -I/software/ibmcpp/include/os2" \
                    "LIBFILES=-lsocket -ltermcap" "FLAGO=-o "

# Elvis compiled for a.out format and linked against emx.dll with x11...
debug: ; $(MAKE) -f osos2\\Makefile.os2 "OBJ=.o" "EXE=x11.exe" \
                    "CC=gcc -g -Wall -DOS2 -D__WITH_TERMCAP -O3 \
                    -Zmtd -D__D_ST_MT_ERRNO__ -D__WITH_X11"   \
                    "FLAGO=-o " \
                    "LIBFILES=-L/usr/lib/x11 -lsocket -ltermcap -lx11"

# Elvis compiled for a.out format and linked against emx.dll with x11...
x11: ; $(MAKE) elvisx11.exe -f osos2\\Makefile.os2 "OBJ=.o" "EXE=x11.exe" \
                    "CC=gcc -Wall -DOS2 -D__WITH_TERMCAP -O3 \
                    -Zmtd -D__D_ST_MT_ERRNO__ -D__WITH_X11   \
                    -I/usr/include -I/emx/include" "FLAGO=-o " \
                    "LIBFILES=-L/usr/lib/x11 -lsocket -ltermcap -lx11"

gcc: ; $(MAKE) -f osos2\\Makefile.os2 "EXE=gcc.exe" "FLAGO=-o " \
                    "CC=gcc -Wall -DOS2 -Zsys -Zomf -O3 -I/emx/include" \
                    "LIBFILES="
                    

elvis$(EXE): $(HDRS) main$(OBJ) $(OBJS) $(GUIOBJS) $(LPOBJS)
	$(CC) $(CFLAGS) $(LDFLAGS) main$(OBJ) $(OBJS) $(GUIOBJS) $(LPOBJS) $(LIBFILES) $(DEFFILE) $(FLAGO)elvis$(EXE)
	$(CP) elvis$(EXE) $(OS2EXE)
	$(RM) elvis$(EXE) 

pmelvis$(EXE): $(HDRS) main$(OBJ) $(OBJS) $(GUIOBJS) $(LPOBJS)
	$(CC) $(CFLAGS) $(LDFLAGS) main$(OBJ) $(OBJS) $(GUIOBJS) $(LPOBJS) $(LIBFILES) $(PMDEFFILE) $(FLAGO)pmelvis$(EXE)

elvis-no-tcp$(EXE): $(HDRS) main$(OBJ) $(OBJS) $(GUIOBJS) $(LPOBJS)
	$(CC) $(CFLAGS) $(LDFLAGS) main$(OBJ) $(OBJS) $(GUIOBJS) $(LPOBJS) $(DEFFILE) $(FLAGO)elvis-no-tcp$(EXE)
	$(CP) elvis-no-tcp$(EXE) $(OS2EXE)
	$(RM) elvis-no-tcp$(EXE) 

vi$(EXE): alias.c
	$(CC) $(CFLAGS) $(LDFLAGS) -DARGV0=VI alias.c $(FLAGO)vi$(EXE)
	$(CP) vi$(EXE) $(OS2EXE)
	$(RM) vi$(EXE) 

ex$(EXE): alias.c
	$(CC) $(CFLAGS) $(LDFLAGS) -DARGV0=EX alias.c $(FLAGO)ex$(EXE)
	$(CP) ex$(EXE) $(OS2EXE)
	$(RM) ex$(EXE) 

view$(EXE): alias.c
	$(CC) $(CFLAGS) $(LDFLAGS) -DARGV0=VIEW alias.c $(FLAGO)view$(EXE)

ctags$(EXE): $(CTOBJS)
	$(CC) $(CFLAGS) $(LDFLAGS) $(CTOBJS) $(FLAGO)ctags$(EXE)
	$(CP) ctags$(EXE) $(OS2EXE)
	$(RM) ctags$(EXE) 

ref$(EXE): $(REFOBJS)
	$(CC) $(CFLAGS) $(LDFLAGS) $(REFOBJS) $(FLAGO)ref$(EXE)
	$(CP) ref$(EXE) $(OS2EXE)
	$(RM) ref$(EXE) 

fmt$(EXE): fmt.c os$(OS)$(SLASH)osdir.c
	$(CC) $(CFLAGS) $(LDFLAGS) fmt.c $(FLAGO)fmt$(EXE)
	$(CP) fmt$(EXE) $(OS2EXE)
	$(RM) fmt$(EXE) 

verify$(EXE): $(HDRS) verify$(OBJ) $(OBJ1) $(OBJ2)
	$(CC) $(CFLAGS) $(LDFLAGS) verify$(OBJ) $(OBJS) $(LIBFILES) $(FLAGO)verify$(EXE)

elvdump$(EXE): $(HDRS) elvdump$(OBJ) $(OBJ1)
	$(OSCC) -DELVDUMP os$(OS)$(SLASH)osdir.c
	$(CC) $(CFLAGS) $(LDFLAGS) elvdump$(OBJ) $(OBJ1) $(LIBFILES) $(FLAGO)elvdump$(EXE)
	touch os$(OS)$(SLASH)osdir.c

calc$(EXE): $(HDRS) calc.c
	$(CC) $(CFLAGS) $(LDFLAGS) -DTRY os$(OS)$(SLASH)getopt.c calc.c $(FLAGO)calc$(EXE)

###############################################################################
# Rules for os-dependent files

osblock$(OBJ): os$(OS)$(SLASH)osblock.c
	$(OSCC) os$(OS)$(SLASH)osblock.c

osdir$(OBJ): os$(OS)$(SLASH)osdir.c
	$(OSCC) os$(OS)$(SLASH)osdir.c

osprg$(OBJ): os$(OS)$(SLASH)osprg.c
	$(OSCC) os$(OS)$(SLASH)osprg.c

ostext$(OBJ): os$(OS)$(SLASH)ostext.c
	$(OSCC) os$(OS)$(SLASH)ostext.c

osnet$(OBJ): os$(OS)$(SLASH)osnet.c
	$(OSCC) os$(OS)$(SLASH)osnet.c

tcaphelp$(OBJ): os$(OS)$(SLASH)tcaphelp.c
	$(OSCC) os$(OS)$(SLASH)tcaphelp.c

guivio$(OBJ): os$(OS)$(SLASH)guivio.c
	$(OSCC) os$(OS)$(SLASH)guivio.c

debug$(OBJ): os$(OS)$(SLASH)debug.c
	$(OSCC) os$(OS)$(SLASH)debug.c

###############################################################################
# Rules for GUI-dependent files.  Note that we can't really generalize here
# as well as we could for OS-dependent files, because different GUIs may be
# broken down differently.

guix11$(OBJ): guix11/guix11.c $(XBM)
	$(OSCC) guix11/guix11.c

xclip$(OBJ): guix11/xclip.c
	$(OSCC) guix11/xclip.c

xevent$(OBJ): guix11/xevent.c
	$(OSCC) guix11/xevent.c

xmisc$(OBJ): guix11/xmisc.c
	$(OSCC) guix11/xmisc.c

xscroll$(OBJ): guix11/xscroll.c
	$(OSCC) guix11/xscroll.c

xstatus$(OBJ): guix11/xstatus.c
	$(OSCC) guix11/xstatus.c

xtext$(OBJ): guix11/xtext.c
	$(OSCC) guix11/xtext.c

xtool$(OBJ): guix11/xtool.c
	$(OSCC) guix11/xtool.c

xdialog$(OBJ): guix11/xdialog.c
	$(OSCC) guix11/xdialog.c

###############################################################################
# Handy commands

clean: clean.$(OS)

clean.unix:
	$(RM) *$(OBJ)
	$(RM) core
	$(RM) errlist
	$(RM) verify.elv
	$(RM) $(DISTRIB).tar.gz

clean.msdos:
	$(RM) *$(OBJ)

clean.os2:
	$(RM) *$(OBJ)

clobber: clean
	$(RM) $(EVERY)
	$(RM) tags
	$(RM) detail
	$(RM) .elv*
	$(RM) foo
	$(RM) stderr

$(DISTRIB)-os2.tar.gz: $(OS2EXE)
	-$(RM) bin-os2\\data\\*
	-$(RD) bin-os2\\data
	-$(RM) bin-os2\\doc\\*
	-$(RD) bin-os2\\doc
	-$(RM) bin-os2\\*
	-$(RD) bin-os2
	-$(MD) bin-os2
	$(CP) $(OS2EXE)\\*.exe bin-os2
	$(CP) osos2\\*.ico bin-os2
	tar cvf - data doc | tar -C bin-os2 -xvf -
	$(CAT) README.html >bin-os2\\README.html
	$(CAT) BUGS >bin-os2\\BUGS
	$(CAT) COPYING >bin-os2\\COPYING
	$(CAT) doc\\printdoc.bat >bin-os2\\doc\\printdoc.bat
	(cd bin-os2 && tar cvf ..\\$(DISTRIB)-os2.tar * && gzip ..\\$(DISTRIB)-os2.tar)

doc$(SLASH)elvistrs.msg: $(SRCS)
	sed -n '/%[cds]/d; /[a-z]:/d; s/\[[a-zA-Z]*\]//; s/\\\\/\\/g; s/.*msg(MSG_[A-Z]*, "\([^ 	"][^"]*\)".*/\1/p' *.c os*$(SLASH)*.c | sort -u >doc$(SLASH)elvistrs.msg

